日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C语言小知识点练习总结

發布時間:2025/3/21 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言小知识点练习总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近在準備C語言的上級考試,之前對C接觸不多,在練習過程中把一些小知識點記錄下來。

1.字符串的截取

利用strncpy函數,傳入三個參數,分別為目標字符串,起始位置,長度。

例如將日期字符串轉化為數字,如20120112

#include <stdio.h> #include <string.h> #include <stdlib.h>int main() {char date1[20],date2[20];scanf("%s",&date1);scanf("%s",&date2);char temp[4];int year1 = atoi(strncpy(temp,date1,4));int year2 = atoi(strncpy(temp,date2,4));printf("year1:%d\n",year1);printf("year2:%d\n",year2);char temp2[2];int month1 = atoi(strncpy(temp2,date1+4,2));int month2 = atoi(strncpy(temp2,date2+4,2));printf("month1:%d\n",month1);printf("month2:%d\n",month2);int day1 = atoi(strncpy(temp2,date1+6,2));int day2 = atoi(strncpy(temp2,date2+6,2));printf("day1:%d\n",day1);printf("day2:%d\n",day2);return 0;}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { ????char date1[20],date2[20]; ????scanf("%s",&date1); ????scanf("%s",&date2); ????char temp[4]; ????int year1 = atoi(strncpy(temp,date1,4)); ????int year2 = atoi(strncpy(temp,date2,4)); ????printf("year1:%d\n",year1); ????printf("year2:%d\n",year2); ????char temp2[2]; ????int month1 = atoi(strncpy(temp2,date1+4,2)); ????int month2 = atoi(strncpy(temp2,date2+4,2)); ????printf("month1:%d\n",month1); ????printf("month2:%d\n",month2); ????int day1 = atoi(strncpy(temp2,date1+6,2)); ????int day2 = atoi(strncpy(temp2,date2+6,2)); ????printf("day1:%d\n",day1); ????printf("day2:%d\n",day2); ????return 0; }

以上便實現了輸入一個日期然后對其進行分割的操作。














2.二維數組的動態聲明

利用malloc可以實現數組的動態聲明

int **a;a = (int **)malloc(2*sizeof(int *));int i,j;for (i = 0; i < 2; i ++) {a[i] = (int *)malloc(3*sizeof(int));for (j = 0; j < 3; j++) {scanf("%d",&a[i][j]);}}
1 2 3 4 5 6 7 8 9 int **a; ????a = (int **)malloc(2*sizeof(int *)); ????int i,j; ????for (i = 0; i < 2; i ++) { ????????a[i] = (int *)malloc(3*sizeof(int)); ????????for (j = 0; j < 3; j++) { ????????????scanf("%d",&a[i][j]); ????????} ????}

以上便實現了動態數組的分配,利用scanf為數組賦值







3.二維數組的聲明和初始化

頭文件

#include <memory.h>
1 #include <memory.h>

初始化和測試

int result[2][2];for (i = 0; i < 2; i ++) {for (j = 0; j < 2; j++) {printf("%d ",result[i][j]);}printf("\n");}memset(result,0,sizeof(int)*4);for (i = 0; i < 2; i ++) {for (j = 0; j < 2; j++) {printf("%d ",result[i][j]);}printf("\n");}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 int result[2][2]; ????for (i = 0; i < 2; i ++) { ????????for (j = 0; j < 2; j++) { ????????????printf("%d ",result[i][j]); ????????} ????????printf("\n"); ????} ????memset(result,0,sizeof(int)*4); ????for (i = 0; i < 2; i ++) { ????????for (j = 0; j < 2; j++) { ????????????printf("%d ",result[i][j]); ????????} ????????printf("\n"); ????}

結果

4196944 0 4195696 0 0 0 0 0
1 2 3 4 4196944 0 4195696 0 0 0 0 0

上述是數組的非動態聲明









4.快速排序

假設要排序的數組是A[1]……A[N],首先任意選取一個數據(通常選用第一個數據)作為關鍵數據,然后將所有比它的數都放到它前面,所有比它大的數都放到它后面,這個過程稱為一趟快速排序。一趟快速排序的算法是:

1)設置兩個變量I、J,排序開始的時候 I=0,J=N-1;

2)以第一個數組元素作為關鍵數據,賦值給X,即X=A[0];

3)從J開始向前搜索,即由后開始向前搜索,找到第一個小于X的值,兩者交換;

4)從I開始向后搜索,即由前開始向后搜索,找到第一個大于X的值,兩者交換;

5)重復第3、4步,直到I=J;

例如:待排序的數組A的值分別是:(初始關鍵數據X:=49)

A[0] A[1] A[2] A[3] A[4] A[5] A[6]
49 38 65 97 76 13 27

進行第一次交換后: 27 38 65 97 76 13 49
( 按照算法的第三步從后面開始找 )

進行第二次交換后: 27 38 49 97 76 13 65
( 按照算法的第四步從前面開始找>X的值,65>49,兩者交換,此時I=3 )

進行第三次交換后: 27 38 13 97 76 49 65
( 按照算法的第五步將又一次執行算法的第三步從后開始找)

進行第四次交換后: 27 38 13 49 76 97 65
( 按照算法的第四步從前面開始找大于X的值,97>49,兩者交換,此時J=4 )

此時再執行第三不的時候就發現I=J,從而結束一躺快速排序,那么經過一躺快速排序之后:
27 38 13 49 76 97 65
即所有大于49的數全部在49的后面,所有小于49的數全部在49的前面。

#include <stdio.h>void quiksort(int a[],int low,int high) {int i = low;int j = high; int temp = a[i]; if( low < high){ while(i < j) {while((a[j] >= temp) && (i < j)){ j--; }a[i] = a[j];while((a[i] <= temp) && (i < j)){i++; } a[j]= a[i];}a[i] = temp;quiksort(a,low,i-1);quiksort(a,j+1,high);}else{return;} }int main() {int arr[5] = {23,1,21,4,19};quiksort(arr,0,4);int i;for(i=0;i<5;i++){printf("%d ",arr[i]);}printf("\n");return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include <stdio.h> void quiksort(int a[],int low,int high) { ????int i = low; ????int j = high;?? ????int temp = a[i]; ?? ????if( low < high) ????{?????????? ????????while(i < j) ????????{ ????????????while((a[j] >= temp) && (i < j)) ????????????{ ????????????????j--; ????????????} ????????????a[i] = a[j]; ????????????while((a[i] <= temp) && (i < j)) ????????????{ ????????????????i++; ????????????}?? ????????????a[j]= a[i]; ????????} ????????a[i] = temp; ????????quiksort(a,low,i-1); ????????quiksort(a,j+1,high); ????} ????else ????{ ????????return; ????} } int main() { ????int arr[5] = {23,1,21,4,19}; ????quiksort(arr,0,4); ????int i; ????for(i=0;i<5;i++) ????{ ????????printf("%d ",arr[i]); ????} ????printf("\n"); ????return 0; }

快速排序代碼如上






















5.字符串拷貝

小例子如下

#include <stdio.h> #include <string.h> #include <stdlib.h>int main(){char str[20] = "hello";char *a = "world";strcpy(str,a);printf("%s",str);printf("\n");system("pause");return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(){ ????char str[20] = "hello"; ????char *a = "world"; ????strcpy(str,a); ????printf("%s",str); ????printf("\n"); ????system("pause"); ????return 0; }

運行結果:world,即把后者完全覆蓋前者。

#include <stdio.h> #include <string.h> #include <stdlib.h>int main(){char *str = new char[6];char *a = "world";strcpy(str,a);printf("%s", str);printf("\n");system("pause");return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(){ ????char *str = new char[6]; ????char *a = "world"; ????strcpy(str,a); ????printf("%s", str); ????printf("\n"); ????system("pause"); ????return 0; }

運行結果一致

某一長度的字符串截取

#include <stdio.h> #include <string.h> #include <stdlib.h>int main(){char *str = new char[6];char *a = "world";strncpy(str,a+1,5);printf("%s", str);printf("\n");system("pause");return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(){ ????char *str = new char[6]; ????char *a = "world"; ????strncpy(str,a+1,5); ????printf("%s", str); ????printf("\n"); ????system("pause"); ????return 0; }

運行結果:orld











6.字符串的拼接

小例子如下

#include <stdio.h> #include <string.h> #include <stdlib.h>int main(){char str[20] = "hello ";char *a = "world";char *x = strcat(str,a);printf("%s\n", x);printf("%s\n", x);printf("\n");system("pause");return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(){ ????char str[20] = "hello "; ????char *a = "world"; ????char *x = strcat(str,a); ????printf("%s\n", x); ????printf("%s\n", x); ????printf("\n"); ????system("pause"); ????return 0; }

運行結果:

hello world

hello world

函數既返回結果,又將目標字符串賦值








7.字符串查找匹配

例子如下

#include <stdio.h> #include <string.h> #include <stdlib.h>int main(){char str[20] = "hello ";char *x = strchr(str,'e');printf("%d\n", x - str);printf("%s\n",x);printf("\n");system("pause");return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(){ ????char str[20] = "hello "; ????char *x = strchr(str,'e'); ????printf("%d\n", x - str); ????printf("%s\n",x); ????printf("\n"); ????system("pause"); ????return 0; }

運行結果:

1

ello








8.字符串比較

例子如下

#include <stdio.h> #include <string.h> #include <stdlib.h>int main(){char str[20] = "hello ";char str2[20] = "hello2";int x = strcmp(str, str2);printf("%d\n",x);printf("\n");system("pause");return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(){ ????char str[20] = "hello "; ????char str2[20] = "hello2"; ????int x = strcmp(str, str2); ????printf("%d\n",x); ????printf("\n"); ????system("pause"); ????return 0; }

運行結果-1

忽略大小寫

#include <stdio.h> #include <string.h> #include <stdlib.h>int main(){char str[20] = "hello";char str2[20] = "Hello";int x = stricmp(str, str2);printf("%d\n",x);printf("\n");system("pause");return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> #include <string.h> #include <stdlib.h> int main(){ ????char str[20] = "hello"; ????char str2[20] = "Hello"; ????int x = stricmp(str, str2); ????printf("%d\n",x); ????printf("\n"); ????system("pause"); ????return 0; }

運行結果0








9.字符串分割

示例如下

#include <stdio.h> #include <string.h> #include <stdlib.h>int main() {char test1[] = "feng,ke,wei"; char x[3][30];char *p; p = strtok(test1, ",");int count = 0;while (p) { printf("%s\n", p); strcpy(x[count],p);count++;p = strtok(NULL, ","); } for (int i = 0; i<count; i ++) {printf("%s ", x[i]);}system("pause");return 0;}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { ??????char test1[] = "feng,ke,wei";?? ??????char x[3][30]; ??????char *p;?? ??????p = strtok(test1, ","); ??????int count = 0; ??????while (p) {?? ????????printf("%s\n", p);?? ????????strcpy(x[count],p); ????????count++; ????????p = strtok(NULL, ",");???? ??????}???? ??????for (int i = 0; i<count; i ++) { ????????printf("%s ", x[i]); ??????} ??????system("pause"); ??????return 0; }

運行結果

feng
ke
wei
feng ke wei











10.格式化輸出幾位小數

例如

printf("%.5f",18.223);
1 printf("%.5f",18.223);

則是輸出5位小數

又如

printf("%5.1f",1.2345);
1 printf("%5.1f",1.2345);

則是控制總位數為5,小數點后為1位,不夠的在前面補空格

轉載請注明:靜覓 ? C語言小知識點練習總結

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的C语言小知识点练习总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。