# Alvin's c_note [TOC] :::success >[name=AlvinXD] ::: ## ~~Alvin好帥~~ ## c 知識區 ---- ### '\0' \0為一字符串之結尾。 在字符數組中’\0’是佔一個位置的! 例如 定義char c[6]=“hello”,而在內存中字符數組 c 則是"hello\0"; 即’\0’在數組中佔有空間卻不被我們看到; 所以如果一個長度為10的字符串要放在字符數組裡的話就應該把字符數組的長度定義為11; ---------------------------- ### about file | 模式 | 描述 | 開檔失敗 | 指標位置 | 清空內容 | 讀取位置 | 寫入位置 | 注 | | ---- | ---- | -------- | -------- | -------- | -------- | --- | --- | | r | 只讀 | 開導失敗 | 開頭 | 否 | 任意讀 | 不可寫入 | - | | w | 只寫 | 新開檔 | 開頭 | 是 | 不可讀 | 任意位置寫入 | 會覆蓋原有位置 | | a | 只寫 | 新開檔 | 結尾 | 否 | 不可讀 | 只能寫尾部 | - | | r+ | 讀寫 | 開導失敗 | 開頭 | 否 | 任意讀 | 任意位置寫入 | 會覆蓋原有位置 | | w+ | 讀寫 | 新開檔 | 開頭 | 是 | 任意讀 | 任意位置寫入 | 會覆蓋原有位置 | | a+ | 讀寫 | 新開檔 | 結尾 | 否 | 任意讀 | 只能寫尾部 | - | --- ### 輸出格式 | 符號 | 輸出 | 符號 | 輸出 | | ---- | -------- | ---- | ---------------------------- | | %a | 浮點值 | %s | 讀入字符遇到空格制符號等停止 | | %c | 一個字符 | %f | 讀小數 | | %d | 讀dec | %p | 輸入指標 | | %o | 讀oct | %u | 無符號dec | | %x | 讀hex | %% | 輸出% | ---- ### %[^] 使用於sscanf,取%[^]以前的字符,[]裡可加字符或A-Z、a-z、A-z、0-9 諾有*表示捨棄該字符 ---- ### pointer a= 0x12345678 | Big-Endian | 內容 | Little-Endian | 內容 | | ---------- | --- | ------------- | --- | | 0x1000 | 12 | 0x1000 | 78 | | 0x1001 | 34 | 0x0fff | 56 | | 0x1002 | 56 | 0x0ffe | 34 | | 0x1003 | 78 | 0x0ffd | 12 | 動態記憶體分配(Dynamic memory allocation) malloc() 取得記憶體 free() 釋放記憶體 ```c= #define max 20 int *point; point=(int *)malloc(max * sizeof(int)); free(point); ``` ---- ## odd_or_even ```c= #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> #include <math.h> int main(){ int num; int new_num; printf("int a number:"); scanf("%d", &num); new_num = num %2; if (new_num == 0) printf("%d is even",num); else printf( "%d is odd",num); return 0; } ``` ## 1a1b ```c= #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> #include <string.h> int main(){ int a,b,c,d,a1,b1,c1,d1,num,aa=0,bb=0,new_input_num; char input_num[4]; num = 1234; a= (num/1000)%10; b= (num/100)%10; c= (num/10)%10; d= (num/1)%10; printf("int a num:"); scanf("%s",&input_num); new_input_num = atoi(input_num); a1= (new_input_num/1000)%10; b1= (new_input_num/100)%10; c1= (new_input_num/10)%10; d1= (new_input_num/1)%10; if (a==a1) aa=aa+1; else if ((a==c1) ||(a==d1) ||(a==b1) ) { bb=bb+1; } //// if (b==b1) aa=aa+1; else if ((b==a1)||(b==c1)||(a==d1)) { bb=bb+1; } /// if (c==c1) aa=aa+1; else if ((c==a1) ||(c==b1)||(c==d1)) { bb=bb+1; } ///// if (d==d1) aa=aa+1; else if ((d==a1)||(d==c1)|| (d==a1)) { bb=bb+1; } printf("%d A %d B",aa,bb); } ``` ## common_leap ```c= #include <stdio.h> int main(){ int year; printf("input a year:"); scanf("%d",&year); if((year%4==0) && (year%100!=0) ||(year%400==0)) printf ("%d is common\n",year); else printf ("%d is leap\n",year); } ``` ## common_leap ```c= #include<stdio.h> int main(){ FILE *fp; FILE *pf; int year; fp = fopen("./file.txt","r"); pf = fopen("./file.txt","r+"); if (fp == NULL){ printf("error"); } else{ while (feof(fp)==0){ fscanf(fp,"%d",&year); if(((year%4==0) && (year%100!=0) ||(year%400==0))){ printf ("%d是閏年\n",year); fprintf (pf,"%d是閏年\n",year);} else { printf ("%d是閏年\n",year); fprintf (pf,"%d是平年\n",year); } } } fclose(fp); return 0; } ``` >file >>73209 >>>>73209是平年 299 >>>>299是平年 2021 >>>>2021是平年 500 >>>>500是平年 ## sec to day, hour, min, sec ```c= #include <stdio.h> int main(void){ int sec, min, hour,day; printf("sec:"); scanf("%d", &sec); day = sec /86400; hour = (sec%86400) / 3600; min = (sec%3600) / 60; sec = sec % 60; printf("%d:%d:%d:%d\n", day, hour,min,sec); return 0; } ``` ## find the common on file ```c= #include <stdio.h> #include <stdlib.h> #define max 65535 int main(){ FILE *fp; int c[max]; int i,j,k; fp = fopen("./file.txt", "r"); if (fp == NULL){ printf("error"); } for(i=0;i<4;i++){ fscanf(fp,"%d",&c[i]); } for ( i = 0; c[i]; i++) { if (c[i]%2==0) { printf("%d\n",c[i]); } } fclose(fp); return 0; } ``` >file: >>768 993 753 10 ## check password ```c= #include <stdio.h> #include <string.h> int main(){ char psw[20]; char check_psw[] = "passwd"; printf("int a psw:"); scanf("%s", psw); !strcmp(psw,check_psw)? printf("in"): printf("error"); } ``` ## read then sum ```c= #include <stdio.h> int main(){ FILE *fp; int c[6]; int i,sum=0; fp = fopen("./file.txt", "r"); if (fp == NULL){ printf("error"); } for(i=0;i<6;i++){ fscanf(fp,"%d",&c[i]); } for ( i = 0; i < 6; i++) { sum=sum+c[i]; } printf("%d",sum); } ``` >file: >>546 886 1 95 864 55 ## read A file then write to B file ```c= #include <stdio.h> int main(){ int a=0,b=0,c=0,sum=0;char op1,op2; FILE *fp;FILE *pf; fp = fopen("./read.txt", "r"); pf = fopen("./write.txt", "a"); while (feof(fp)==0){ fscanf(fp,"%d%c%d%c%d",&a,&op1,&b,&op2,&c); switch (op1) { case '+': sum=a+b; break; case '-': sum=a-b; break; case '*': sum=a*b; break; case '/': sum=a/b; break; default: break; } switch (op2) { case '+': sum=sum+c; break; case '-': sum=sum-c; break; case '*': sum=sum*c; break; case '/': sum=sum/c; break; default: break; } printf("%d\t",sum); } fclose(fp); fprintf(pf,"%d",sum); } ``` >file: >>104+757-4 11+4-2 20+6/7 ## year to year ```c= #include <stdio.h> #include <stdlib.h> int main(){ int yy,mm,dd,ww,total=0,roc_y=0; printf("輸入年(民國)月日:"); scanf("%d,%d,%d",&roc_y,&mm,&dd); yy=roc_y +1911; for(int i=1911;i<yy;i++){ total=total+365; if((i%4==0) && (i%100!=0) ||(i%400==0)){ total=total+1; } } for (int m = 1; m < mm+1; m++) { switch (m) { case 1: total=total+0; break; case 2: total=total+31; break; case 3: if(((m%4)==0 && ((m %100)!=0)) || (m%400==0)){ total=total+29; break; } else{ total=total+28; break; } case 4: total=total+31; break; case 5: total=total+30; break; case 6: total=total+31; break; case 7: total=total+30; break; case 8: total=total+31; break; case 9: total=total+31; break; case 10: total=total+30; break; case 11: total=total+31; break; case 12: total=total+30; break; default: break; } } total=total+dd-1; ww=total%7; if (total==0){ ww=7; } printf("過了%d天,星期%d",total,ww); return 0; } ``` ## ASCII ```c= #include <stdio.h> #define MAX_STRING_LENGTH 65535 // max long int main(){ char s[MAX_STRING_LENGTH]; printf("any character:",MAX_STRING_LENGTH); scanf("%s",s); for(int i = 0; s[i]; i++){ printf("%c的ASCII:%d\t",s[i],s[i]); } } ``` ## find a number then stop in a file ```c= #include <stdio.h> #define max 99884 int main(){ FILE *fp; int c[max]; int num,mx=0,sm=0; int i=0,sum=0; fp = fopen("./flie.txt", "r"); if (fp == NULL){ printf("error"); } for(i=0;feof(fp)==0;i++){ fscanf(fp,"%d",&c[i]); if (c[i]==999){break;} } for (i=0;c[i];i++){ if (c[i]==999){break;} if (c[i]>mx){mx=c[i];} } for (i=0;c[i];i++){ if (c[i]==999){break;} if (c[i]<sm){sm=c[i];} } printf("max=%d,min=%d",mx,sm); } ``` >file: >>66 -1 77 9999 999 1 2 947 48 50 935 -45 ## Bubble_Sorting ```c= #include<stdio.h> #include<stdlib.h> void bubble_sorting(int array[],size){ int temp; int i,j; //size = sizeof(array)/sizeof(array[0]); //printf("%d\n",sizeof(array)); ///函式裡無法使用sizeof for (int i = 0;array[i]; i++) { printf("%d\t",array[i]); } for (i = 0;i < size-1; i++) { for (j = 0; j < size-1-i; j++) { if (array[j]>array[j+1]){ temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } } } for (int i = 0;array[i]; i++) { printf("%d\t",array[i]); } } int main(){ int size; int arr[]={-3,-1,9,9,4}; size = sizeof(arr)/sizeof(arr[0]); bubble_sorting(arr,size); return 0; } ``` ## 規律 ```c= #include <stdio.h> int main(){ int a,sum=0, i,b=0; scanf("%d",&a); for(i = 1; i <= a; i++){ if(sum%2==0){ sum+=3; } else{ sum+=5; } printf("%d+",sum); b+=sum ; } printf("\b=%d",b) ; return 0; } ``` >ex: >>n=7 >> 3 + 8 + 11 + 16 + 19 + 24 + 27 = 108 >>規律:奇數+5 偶數+3 ## funaction $\dfrac{b-2*\sqrt{(10a+4)^3}}{(c+1)^2}$ ```c= #include <stdio.h> #include <stdlib.h> int main() { int a,b,c; float sum; scanf("%d",&a); scanf("%d",&b); scanf("%d",&c); sum=b-(2*(sqrt(pow(10*a+4,3))))/(pow(c+1,2)); printf("%.2f",sum); //.2f 0.xzy y四捨五入 輸出至z return 0; } ``` ## read a file then check triangle ```c= #include <stdio.h> #include <stdlib.h> int main() { int a,b,c; char o1,o2; FILE *fp; fp = fopen("./exam.dat", "r"); while(feof(fp)==0){ fscanf(fp,"%d%c%d%c%d",&a,&o1,&b,&o2,&c); }if (a+b>c && b+c>a && a+c>a){ if ((a*a)+(b*b)==(c*c) ) { printf("成立,直角三角形"); } else if ((a*a)+(b*b)<(c*c) ) { printf("成立,鈍角三角形"); } else if ((a*a)+(b*b)>(c*c)) { printf("成立,銳角三角形"); } } else printf("不是三角形"); return 0; } ``` ## Enter the specified value and stop then find the max ```c= #include <stdio.h> #include <stdlib.h> int main() { int num=0,max=0; while(1){ scanf("%d",&num); if(num==(-6666)){break;} else if(num>max){max=num;} } printf("max = %d",max); } ``` ## Enter the specified value and sorting ```c= #include<stdio.h> #include<stdlib.h> #define max 99 void bubble_sorting(int array[],size){ int temp; int i,j; for (i = 0;i < size-1; i++){ for (j = 0; j < size-1-i; j++){ if (array[j]<array[j+1]){ temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } } } } int main(){ int nums[max]; int nnum; int size,temp,i,j,k=0; while (1) { printf("請輸入要排序的整數(輸入9999結束):"); scanf("%d",&nnum); if (nnum==9999){break;} nums[k]=nnum; k++; } bubble_sorting(nums,k); for (int i = 0;i<k; i++) { printf("%d\t",nums[i]); } return 0; } ``` ## n=6 1,2,3,4,5,6 write in a file ```c= #include <stdio.h> #include <stdlib.h> int main() { int i,j; FILE *fp; fp = fopen("./exam.out", "w"); scanf("%d",&j); for(i=1;i<j;i++){ fprintf(fp,"%d,",i); } fprintf(fp,"%d",j); return 0; } ``` ## factorial ```c= #include<stdio.h> int main(){ int i,j,sum=1; scanf("%d",&j); for ( i = j; j > 0; j--) { sum=sum*j; } printf("%d!=%d",i,sum); return 0; } ``` >ex: 2! =2*1 ## random ```c= #include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ srand( time(NULL) ); int min = 0; //range min int max = 1; //range max int x = rand() % (max - min + 1) + min; printf("\n%d\n",x); system("pause"); return 0; } ``` ## max,mid,min ```c= #include<stdio.h> int main(){ int a,b,c,m=0; scanf("%d,%d,%d",&a,&b,&c); if(a>b) {m=a;a=b;b=m;} if(a>c) {m=a;a=c;c=m;} if(b>c) {m=b;b=c;c=m;} printf("min=%d,mid=%d,max=%d",a,b,c); } ``` ## right_triangle,triangle,overturn_triangle,diamond ```c= #include<stdio.h> void right_triangle(){ int n; printf("n="); scanf("%d",&n); for(int i=0;i<n;i++){ for(int j=0;j<=i;j++){ printf("*");     } printf("\n"); } } void triangle(){ int n; printf("n="); scanf("%d",&n); for(int i=0;i<n;i++){ for (int j = n; j >=i; j--) { printf(" "); } for (int j = 0; j <=i; j++) { printf("*"); printf(" "); } printf("\n"); } } void overturn_triangle(){ int n; printf("n="); scanf("%d",&n); for(int i=0;i<n;i++){ for (int j = 0; j <=i; j++) { printf(" "); } for (int j = n; j >i; j--) { printf("*"); printf(" "); } printf("\n"); } } void diamond(){ int n; printf("n="); scanf("%d",&n); for(int i=0;i<n;i++){ for (int j = n; j >=i; j--) { printf(" "); } for (int j = 0; j <=i; j++) { printf("*"); printf(" "); } printf("\n"); } printf(" "); for(int i=0;i<=n;i++){ printf("*"); printf(" "); } printf("\n"); for(int i=0;i<n;i++){ for (int j = 0; j <=i; j++) { printf(" "); } printf(" "); for (int j = n; j >i; j--) { printf("*"); printf(" "); } printf("\n"); } } int main(){ int c; printf("[1]right_triangle\n[2]triangle\n[3]overturn_triangle\n[4]diamond\n:"); scanf("%d",&c); switch (c) { case 1: right_triangle(); break; case 2: triangle(); break; case 3: overturn_triangle(); break; case 4: diamond(); break; default: printf("error"); break; } } ``` ## Find length of string (without using strlen) ```c= #include<stdio.h> int main(){ char str[]="a string"; for(int i =0;str[i]!='\0';i++){ nnum+=1; } printf("%d\n",nnum-1); } } ///////////using strlen////////// #include<stdio.h> #include<string.h> int main(){ char str[]="string"; printf("len=%d\n",strlen(str)); } ``` ## 讀取data.txt中的字串,並能夠計算出每列有幾個字元 ```c= #include<stdio.h> #include<stdlib.h> #define max 99884 int main(){ char num[max]; char str[max+1]; int nnum=0; int line =0; FILE *fp; fp=fopen("./file.txt","r"); if (fp ==NULL)printf("error: file can't open"); while ((fgets(str,max,fp)!=NULL)) { line+=1; if (feof(fp)!=NULL)break; printf("第%d行有",line); for(int i =0;str[i]!='\0';i++){ nnum+=1; }printf("%d個字\n",nnum-1); nnum=0; } printf("第%d行有",line); for(int i =0;str[i]!='\0';i++){ nnum+=1; }printf("%d個字\n",nnum); } ``` ~~~ // If you using this code your file's line should +1 // 1 aaa // 2 bb // 3 ccc // #include<stdio.h> #include<stdlib.h> #define max 99884 int main(){ char num[max]; char str[max+1]; int nnum=0; int line =0; FILE *fp; fp=fopen("/file.txt","r"); if (fp ==NULL)printf("error: file can't open"); while (fgets(str,max,fp)!=NULL) { line+=1; printf("第%d行有",line); for(int i =0;str[i]!='\0';i++){ nnum+=1; }printf("%d個字\n",nnum-1); nnum=0; } } ~~~ >file: >aaa >bb >ccc ## use %[^] get terget ~~~c= #include<stdio.h> #include<stdlib.h> #define max 99 int main(){ char name[max]; int age; char tmp[]="name=john&&age=21"; sscanf(tmp,"%*[^=]=%[^&&] %*[^=]=%d",name,&age); printf("%s %d\n",name,age); } // name = john &&age = 21 // %*[^=] = %[^&&] %*[^=] = %d // 在sscanf裡使用%[^]來抓取值而不使用%S // %s 判斷終止條件是 '\0' \n 空白 ~~~ ## Big-Endian or Little-Endian ```c= main(){ int a = 0x123456; char *p; p = &a; printf("p=%#hhx\n",*p); printf("p=%#hhx\n",*(p+1)); printf("p=%#hhx\n",*(p+2)); } ``` >output: >p=0x56 >p=0x34 >p=0x12 ```c= int main(){ int a=100,b=200; int *p; p=&a; printf("%d,p point=%x,num=%d\n",a,p,*p); // ^ get memory p=&b; printf("%d,p point=%x,num=%d\n",b,p,*p); // ^ get memory } ``` >output: >100,p point=ef7df7c0,num=100 >200,p point=ef7df7bc,num=200 ## malloc ```c= #include<stdio.h> #include<stdlib.h> void bubble_sorting(int array[],int size){ int temp; int i,j; for (i = 0;i < size-1; i++) { for (j = 0; j < size-1-i; j++) { if (array[j]>array[j+1]){ temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } } } } int main(){ int a,i,k=0; int *pa; int data; FILE *fp; fp = fopen("./1214.txt","r"); if (fp==NULL) { printf("error"); } else { /// read file and get number for ( i = 0;feof(fp)==NULL; i++) { fscanf(fp,"%d",&data); k++; } pa=(int *)malloc(k * sizeof(int)); //get memory fclose(fp); fp = fopen("./1214.txt","r"); for ( i = 0;feof(fp)==0; i++) { fscanf(fp,"%d",&pa[i]); } bubble_sorting(pa,k); for ( i = 0;i<k; i++) { printf("%d\n",pa[i]); } } Free(pa); fclose(fp); } ``` >1214.txt >12 >23 >344 >54 >3 >-34 ## array reverse ```c= #include<stdio.h> #include<stdlib.h> void reverse(int *array,int size){ int i,j,temp; for( i=0;i < size/2; i++){ temp = array[i]; array[i]=array[size-i-1]; array[size-i-1] = temp; } } void display(int *array,int size){ int i; for ( i = 0; i < size; i++) { printf("%d\t",array[i]); } } int main(){ int i,j,n; int data[] = {2,10,7,4,99,5,7}; n=sizeof(data)/sizeof(data[0]); reverse(&data[0],n); display(&data[0],n); } ``` ## max to min or min to max ```c= #include<stdio.h> #include<stdlib.h> void bubble_sorting(int *array,int size,int order){ int i,j,temp; // or int size = 8; switch (order) { case 0: for (i = 0;i < size-1; i++){ for (j = 0; j < size-1-i; j++){ if (array[j]<array[j+1]){ temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } } } break; case 1: for (i = 0;i < size-1; i++){ for (j = 0; j < size-1-i; j++){ if (array[j]>array[j+1]){ temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } } } default: break; } } void display(int *array,int size){ int i; for ( i = 0; i < size; i++) { printf("%d\t",array[i]); } } int main(){ int order,size,i; int score[]={94,63,56,83,79,77,93,22}; size = sizeof(score)/sizeof(score[0]); printf("order([0] min to max\n [1] max to min):\n"); scanf("%d",&order); bubble_sorting(&score[0],size,order); display(&score[0],size); } ``` ## Squares of a Sorted Array ```c= #include<stdio.h> #include<stdlib.h> #include<math.h> void bubble_sorting(int *array,int size){ int i,j,temp; // or int size = 8; for (i = 0;i < size-1; i++){ for (j = 0; j < size-1-i; j++){ if (array[j]>array[j+1]){ temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } } } } void display(int *array,int size){ int i; for ( i = 0; i < size; i++) { printf("%d\t",array[i]); } } int main(){ int n1,size,i; int nums[] = {-4,-1,0,3,10}; int *arr; size = sizeof(nums)/sizeof(nums[0]); arr=(int *)malloc(size * sizeof(int)); for ( i = 0; i < size; i++) { n1 = pow(nums[i],2); arr[i]= n1; } bubble_sorting(&arr[0],size); display(&arr[0],size); } ``` ## sorting ```c= #include <stdio.h> #include <stdlib.h> #include <string.h> #define max 99 int main() { int i,j,g,tmp,n=0; int score[max]; char ttmp[max]; char name[max][9],id[max][10]; char chna[9],chid[10]; FILE *fp; fp = fopen("./score.txt","r"); if(fp==0) printf("read file erroe!\n"); else{ i=0; do{ fscanf(fp,"%s",ttmp); sscanf(ttmp,"%[^,],%[^,],%d",&name[i],&id[i],&score[i]); i++; n++; }while(feof(fp)==0); g=n; printf("name\t id\t score\n"); for ( i = 0; i < n-1; i++) { for ( j =0; j < n-i-1; j++) { if (score[j] < score[j+1]) { tmp=score[j] ; score[j]= score[j + 1]; score[j + 1] = tmp; strcpy(chna , name[j]); strcpy(name[j] , name[j + 1]); strcpy(name[j + 1] , chna ); strcpy(chid , id[j]); strcpy(id[j] , id[j + 1]); strcpy(id[j + 1] , chid ); } } } for (i = 0; i < g; i++) { printf("%s%10s%10d\n ",name[i],id[i],score[i]); } fclose(fp); return 0; } } ``` >file: >jack,aaa01,98 yora,aaa02,63 sa,aaa03,43 jona,aaa04,52 pawa,aaa05,21 >output ``` name id score jack aaa01 98 yora aaa02 63 jona aaa04 52 sa aaa03 43 pawa aaa05 21 ``` ## input then sorting ```c= #include <stdio.h> #include <stdlib.h> #include <string.h> #define max 99 #define num 5 int main() { int i,j,g,tmp,n=0; int score[max]; char ttmp[max],na[max],sc[max]; char name[max][9]; char chna[9]; i=0; for ( i = 0; i < num; i++) { printf("name:"); scanf("%s",&na); printf("score:"); scanf("%s",sc); strcat(na,sc); strcpy(ttmp,na); sscanf(ttmp,"%[^0-9]%d",&name[i],&score[i]); } for ( i = 0; i < num-1; i++) { for ( j =0; j < num-i-1; j++) { if (score[j] < score[j+1]) { tmp=score[j] ; score[j]= score[j + 1]; score[j + 1] = tmp; strcpy(chna , name[j]); strcpy(name[j] , name[j + 1]); strcpy(name[j + 1] , chna ); } } } printf(" "); for (i = 0; i < num; i++) { printf("%s\t%d\n ",name[i],score[i]); } return 0; } ``` >input: >>name:ja score:12 name:jb score:22 name:jc score:54 name:jd score:11 name:je score:99 >output: >>je 99 jc 54 jb 22 ja 12 jd 11