├── 141008 ├── ex1.c ├── ex2.c ├── ex3.c ├── ex4.c ├── ex5.c ├── ex6.c ├── ex7.c └── ex8.c ├── 141015 ├── Fahrenheit2Celsius.c ├── ex1.c ├── ex2.c ├── ex3.c ├── ex4.c └── ex5.c ├── 141021 ├── 9x9table.c ├── FindRoot.c ├── PIT.c └── calcularPI.c ├── 141029 ├── BinarySearch.c ├── BubbleSort#.c ├── BubbleSort.c ├── MatrixMul.c ├── SelectionSort.c ├── Transpose#.c └── Transpose.c ├── 141105 ├── Bisection.c ├── LCMandHCD.c ├── Newton.c ├── WordNum#.c └── WordNum.c ├── 141112 ├── Josephus.c ├── atof.c ├── pseudorandomness.c ├── strcompare#.c └── strcompare.c ├── 141118 ├── char_pointer.c ├── int_pointer.c ├── macros_with_arg.c ├── reverse_str.c └── score.c ├── 141126 ├── joseph.c ├── row_pointer.c ├── seek.c ├── sort.c └── transpose.c ├── 141203 ├── SelectSort.c ├── SortStr.c ├── fun_pointer.c ├── matrix_5x5.c └── struct_book.c ├── 141210 ├── createcpll1.c ├── createcpll2.c ├── deletell.c └── insertll.c ├── 141217 ├── copy3.txt ├── copy4.txt ├── file1.c ├── file2.c ├── filecopy3.c ├── filecopy4.c ├── input1.txt └── output1.txt ├── 141228 ├── exam1_1.c ├── exam1_2.c ├── exam4_1.c ├── exam4_2.c ├── exam4_3.c └── person_list.txt ├── .gitignore └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | a.out 2 | *~ 3 | *.out 4 | 1.c 5 | 2.c 6 | .c# -------------------------------------------------------------------------------- /141008/ex1.c: -------------------------------------------------------------------------------- 1 | /********************* 2 | * 教材84页习题1 3 | * 考察各种数据类型 4 | *********************/ 5 | 6 | #include 7 | main() 8 | { 9 | int a,b; 10 | char c1,c2; 11 | float d,e; 12 | double f,g; 13 | long m,n; 14 | unsigned int p,q; 15 | a=61; b=62; 16 | c1='a'; c2='b'; 17 | d=3.56; e=-6.78; 18 | f=3157.890121; g=0.123456789; 19 | m=50000; n=-60000; 20 | p=32768 ;q=4000; 21 | printf("sizeof:%d, a=%d, b=%d\n",sizeof(int),a,b); 22 | printf("sizeof:%d, c1=%c, c2=%c\n",sizeof(char),c1,c2); 23 | printf("sizeof:%d, d=%6.2f, e=%6.2f\n",sizeof(float),d,e); 24 | printf("sizeof:%d, f=%15.6f, g=%15.12f\n",sizeof(double),f,g); 25 | printf("sizeof:%d, m=%1d, n=%1d\n",sizeof(long),m,n); 26 | printf("sizeof:%d, p=%u, q=%u\n",sizeof(unsigned),p,q); 27 | } 28 | -------------------------------------------------------------------------------- /141008/ex2.c: -------------------------------------------------------------------------------- 1 | /********************* 2 | * 教材85页习题2 3 | *********************/ 4 | 5 | #include 6 | main() 7 | { 8 | int i,j,m,n; 9 | i=8; j=10; 10 | m=++i; n=j++; 11 | printf("%d,%d,%d,%d\n",i,j,m,n); 12 | } 13 | -------------------------------------------------------------------------------- /141008/ex3.c: -------------------------------------------------------------------------------- 1 | /********************* 2 | * 教材85页习题3(1) 3 | *********************/ 4 | #include 5 | main() 6 | { 7 | printf("%f\n",3.5+1/2+56%10); 8 | } 9 | -------------------------------------------------------------------------------- /141008/ex4.c: -------------------------------------------------------------------------------- 1 | /********************* 2 | * 教材85页习题3(2) 3 | *********************/ 4 | #include 5 | main() 6 | { 7 | int a=2; 8 | printf("%d\n",a++*1/3); 9 | } 10 | -------------------------------------------------------------------------------- /141008/ex5.c: -------------------------------------------------------------------------------- 1 | /********************* 2 | * 教材85页习题(3) 3 | *********************/ 4 | #include 5 | main() 6 | { 7 | float x=2.5,y=4.7; 8 | int a=7; 9 | printf("%f\n",x+a%3*(int)(x+y)%2/4); 10 | } 11 | -------------------------------------------------------------------------------- /141008/ex6.c: -------------------------------------------------------------------------------- 1 | /********************* 2 | * 教材85页习题3(4) 3 | *********************/ 4 | #include 5 | main() 6 | { 7 | int a=2,b=3; 8 | float x=3.5,y=2.5; 9 | printf("%f\n",(float)(a+b)/2+(int)x%(int)y); 10 | } 11 | -------------------------------------------------------------------------------- /141008/ex7.c: -------------------------------------------------------------------------------- 1 | /********************* 2 | * 教材85页习题3(5) 3 | *********************/ 4 | #include 5 | main() 6 | { 7 | int x=3,y=8; 8 | x=(x=++y,x+5,x/5); 9 | printf("%d\n",x); 10 | } 11 | -------------------------------------------------------------------------------- /141008/ex8.c: -------------------------------------------------------------------------------- 1 | /********************* 2 | * 教材85页习题5 3 | * 一些简单的表达式 4 | *********************/ 5 | #include 6 | main() 7 | { 8 | int a1,a2,a3,a4; 9 | a1=a2=a3=a4=12; 10 | a1+=a1; 11 | a2*=2+3; 12 | a3/=a3+a3; 13 | a4+=a4-=a4*=a4; 14 | printf("a1=%d,a2=%d,a3=%d,a4=%d\n",a1,a2,a3,a4); 15 | } 16 | -------------------------------------------------------------------------------- /141015/Fahrenheit2Celsius.c: -------------------------------------------------------------------------------- 1 | /********************************* 2 | * 教材85页习题9 3 | * 编程实现华氏温度到摄氏温度的转换 4 | *********************************/ 5 | #include 6 | main() 7 | { 8 | float f,c; 9 | printf("Please input the fahrenheit degree:"); 10 | scanf("%f",&f); 11 | if (f>-460.3) 12 | { 13 | c = 5*(f-32)/9; 14 | printf("The Celsius degree is: %f\n", c); 15 | } 16 | else 17 | printf("Your input have no meaning!\n"); 18 | } 19 | -------------------------------------------------------------------------------- /141015/ex1.c: -------------------------------------------------------------------------------- 1 | /********************* 2 | * 教材85页习题3(6) 3 | *********************/ 4 | 5 | #include 6 | main() 7 | { 8 | int x=4,y=3; 9 | printf("%d\n",(x>=y>=2)?1:0); 10 | } 11 | -------------------------------------------------------------------------------- /141015/ex2.c: -------------------------------------------------------------------------------- 1 | /************************ 2 | * 教材85页习题4 3 | * 学习各种逻辑表达式的值 4 | ************************/ 5 | 6 | #include 7 | main() 8 | { 9 | int a=3,b=4,c=5; 10 | int x,y; 11 | printf("%d\n",+b>c&&b==c); /*0*/ 12 | printf("%d\n",!(a+b)&&!c||1); /*1*/ 13 | printf("%d\n",!(x=a)&&(y=b)&&0); /*0*/ 14 | printf("%d\n",!(a+b)+c-1&&b+c/2); /*1*/ 15 | } 16 | -------------------------------------------------------------------------------- /141015/ex3.c: -------------------------------------------------------------------------------- 1 | /********************* 2 | * 教材85页习题6(1) 3 | *********************/ 4 | 5 | #include 6 | 7 | /*输入球的半径r,输出球的体积*/ 8 | main() 9 | { 10 | float r,v; 11 | double PI = 3.14159; 12 | printf("Please input the radius of a ball:"); 13 | scanf("%f",&r); 14 | if (r>0) 15 | { 16 | v = 4*PI/3*r*r*r; 17 | printf("The volume of this ball is: %f\n",v); 18 | } 19 | else 20 | printf("Your input is out of range!\nPlease input a right number.\n"); 21 | } 22 | -------------------------------------------------------------------------------- /141015/ex4.c: -------------------------------------------------------------------------------- 1 | /********************************* 2 | * 教材85页习题6(3) 3 | * 学习指数的表达, pow()函数的使用 4 | *********************************/ 5 | 6 | #include 7 | #include 8 | main() 9 | { 10 | double a, b, c, d; 11 | a = 3.31*pow(10, 18); 12 | b = 2.10*pow(10, -7); 13 | c = 7.16*pow(10, 5); 14 | d = 2.01*pow(10, 3); 15 | /* 16 | a = 3.31e18; 17 | b = 2.1e-7; 18 | c = 7.16e5; 19 | d = 2.01e3; 20 | */ 21 | double x = (a+b)/(c+d); 22 | printf("%lf\n%.11lf\n%lf\n%lf\n",a,b,c,d); 23 | /* 24 | 注意默认显示8位,因此如果b没用%.11f显示,那么出来的是0.000000 25 | 我应该花店时间弄懂输入和输出的问题 26 | */ 27 | printf("x = %f\n",x); 28 | } 29 | -------------------------------------------------------------------------------- /141015/ex5.c: -------------------------------------------------------------------------------- 1 | /********************* 2 | * 教材85页习题6(4) 3 | *********************/ 4 | 5 | #include 6 | /*输入5个数,计算一个分式的值*/ 7 | int main() 8 | { 9 | float a,b,c,d,e; 10 | printf("Input a,b,c,d,e,\n\ 11 | We will output the value of (a*b)/(c+d/e),\n\ 12 | so make sure the demoninator is not 0,\n\ 13 | Please input a,b,c,d,e in order:"); 14 | scanf("%f%f%f%f%f",&a,&b,&c,&d,&e); 15 | // test printf("a=%f b=%f c=%f d=%f e=%f\n",a,b,c,d,e); 16 | if (e != 0 && c+d/e != 0 ) 17 | { 18 | double y = (a*b)/(c+d/e); 19 | printf("y=%f\n",y); 20 | } 21 | else 22 | printf("Input Error!Please make sure the demoninator is not 0\n"); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /141021/9x9table.c: -------------------------------------------------------------------------------- 1 | /********************************* 2 | * Book P112 Exercise 5 3 | * print 9x9 multiplication table 4 | *********************************/ 5 | 6 | #include 7 | int main() 8 | { 9 | int i,j; 10 | printf(" 1 2 3 4 5 6 7 8 9\n"); 11 | for(i=1;i<=9;i++) 12 | { 13 | for(j=1;j<=i;j++) 14 | printf("%4d",i*j); 15 | printf("\n"); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /141021/FindRoot.c: -------------------------------------------------------------------------------- 1 | /************************************** 2 | * Calcular complex root of ax^2+bx+c = 0 3 | **************************************/ 4 | 5 | #include 6 | #include 7 | 8 | int main() 9 | { 10 | double a,b,c; 11 | double x1,x2,delta,t1,t2; 12 | printf("Find complex root of ax^2+bx+c=0.\n" 13 | "Please input a,b,c: "); 14 | scanf("%lf%lf%lf",&a,&b,&c); 15 | if(a==0 && b==0) 16 | printf(c==0?"All x is root!\n":"No root exist!\n"); 17 | else if(a==0 && b!=0) 18 | printf("x=%lf\n",(double)(-c/b)); 19 | else{ 20 | delta = b*b-4*a*c; 21 | t1 = -b/(2*a); 22 | if (delta >= 0){ 23 | t2 = sqrt(delta)/(2*a); 24 | printf("x1=%lf,x2=%lf\n",t1+t2,t1-t2); 25 | } 26 | else{ 27 | t2 = sqrt(-delta)/(2*a); 28 | printf("x1=%lf+%lfi,x2=%lf%+lfi\n",t1,t2,t1,-t2); 29 | } 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /141021/PIT.c: -------------------------------------------------------------------------------- 1 | /****************************************** 2 | * Book P112 Exercise 2 3 | * Learn how to use switch 4 | * calculate personal income tax 5 | ******************************************/ 6 | #include 7 | int main() 8 | { 9 | int salary; 10 | printf("Please input your salary number: "); 11 | scanf("%d",&salary); 12 | if (salary <= 3000) 13 | printf("No tax require!\n"); 14 | else 15 | { 16 | int num = (salary-3000-1)/500; 17 | float tax; 18 | switch(num) 19 | { 20 | case 0:tax=(salary-3000)*0.05;break; 21 | case 1: 22 | case 2:tax=(salary-3000)*0.10;break; 23 | case 3: 24 | case 4: 25 | case 5:tax=(salary-3000)*0.15;break; 26 | case 6: 27 | case 7: 28 | case 8: 29 | case 9:tax=(salary-3000)*0.20;break; 30 | default :tax=(salary-3000)*0.25; 31 | } 32 | printf("Personal income tax is %.1f yuan!\n",tax); 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /141021/calcularPI.c: -------------------------------------------------------------------------------- 1 | /****************************************** 2 | * 教材112页习题7 3 | * calculate the approximative value of pi 4 | ******************************************/ 5 | 6 | 7 | #include 8 | int main() 9 | { 10 | double i,j,PI=2; 11 | for (i=1;i<=1000;i++) 12 | { 13 | j = (2*i)*(2*i)/((2*i-1)*(2*i+1)); 14 | PI *= j; 15 | } 16 | printf("PI = %8.7lf\n",PI); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /141029/BinarySearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 4 3 | int main() 4 | { 5 | //Part#1 SelectionSort 6 | int a[N],i,j,temp,n; 7 | printf("Please input %d integer number: ",N); 8 | for (i=0; i find_num) 29 | high = k; 30 | else 31 | low = k; 32 | if (k == (low+high)/2){ //左边判断边界条件有错 33 | printf("The number is not found in this array.\n"); 34 | printf("%d\n",k); 35 | return 0; 36 | } 37 | k = (low+high)/2; 38 | } 39 | printf("Found! a[%d]=%d is the number you find.\n",k,find_num); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /141029/BubbleSort#.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define N 10 3 | int main() 4 | { 5 | int a[N],i,j,temp,status=1; 6 | printf("Please input %d integer number: ",N); 7 | 8 | for (i=0; ia[j+1]){ 16 | temp = a[j+1]; 17 | a[j+1] = a[j]; 18 | a[j] = temp; 19 | status = 1; 20 | } 21 | } 22 | } 23 | } 24 | 25 | for (i=0; i 2 | #define N 10 3 | int main() 4 | { 5 | int a[N],i,j,temp; 6 | printf("Please input %d integer number: ",N); 7 | for (i=0; ia[j+1]) { 12 | temp = a[j+1]; 13 | a[j+1] = a[j]; 14 | a[j] = temp; 15 | } 16 | } 17 | } 18 | for (i=0; i 2 | int main() 3 | { 4 | int i,j,k,m,p,n; 5 | printf("Matrix Mulplication! Calculate A x B .\n" 6 | "Matrix A = m x p, Matrix B = p x n.\n"); 7 | printf("Please input m,p,n: "); 8 | scanf("%d%d%d",&m,&p,&n); 9 | int a[m][p],b[p][n],c[m][n]; 10 | printf("Please input Matrix A: \n"); 11 | for (i=0; i 2 | #define N 10 3 | int main() 4 | { 5 | int a[N],i,j,temp,n; 6 | printf("Please input %d integer number: ",N); 7 | for (i=0; i 2 | int main() 3 | { 4 | int i,j,m,n; 5 | printf("Matrix Transpose! \n" 6 | "Please input the rows and columns of matrix A: \n"); 7 | scanf("%d%d",&m,&n); 8 | int a[m][n],b[m][n]; 9 | printf("Please input matrix A: \n"); 10 | for (i=0; i 2 | int main() 3 | { 4 | int i,j,m,n; 5 | printf("Matrix Transpose! \n" 6 | "Please input the rows and columns of matrix A: \n"); 7 | scanf("%d%d",&m,&n); 8 | int a[m][n]; 9 | printf("Please input matrix A: \n"); 10 | for (i=0; i 6 | #include 7 | int main() 8 | { 9 | double a=-10,b=10,x,y; 10 | x = (a+b)/2; 11 | y = 2*x*x*x-4*x*x+3*x-6; 12 | while (fabs(y)>1e-6) { 13 | y>0 ? (b=x) : (a=x) ; 14 | x = (a+b)/2; 15 | y = 2*x*x*x-4*x*x+3*x-6; 16 | } 17 | printf("x=%lf\n",x); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /141105/LCMandHCD.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int m,n; 5 | printf("Please input two natural number: "); 6 | scanf("%d%d",&m,&n); 7 | printf("lowest common multiple of m and n :%d\n" 8 | "highest common divisor of m and n is :%d\n", 9 | LCM(m,n),HCD(m,n)); 10 | return 0; 11 | } 12 | 13 | int LCM(int m,int n) 14 | { 15 | int x = (m>n ? m:n); 16 | while (x%m!=0 || x%n!=0) { 17 | x += m; 18 | } 19 | return x; 20 | } 21 | 22 | int HCD(int m,int n) 23 | //http://zh.wikipedia.org/wiki/%E8%BC%BE%E8%BD%89%E7%9B%B8%E9%99%A4%E6%B3%95 24 | { 25 | int temp; 26 | if (n > m){ 27 | temp = m; 28 | m = n; 29 | n = temp; 30 | } 31 | 32 | int r = m % n; 33 | while (r != 0) { 34 | m = n; 35 | n = r; 36 | r = m % n; 37 | } // mark! 38 | return n; 39 | } 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /141105/Newton.c: -------------------------------------------------------------------------------- 1 | /* Newton's method 2 | http://en.wikipedia.org/wiki/Newton%27s_method 3 | x[n+1] = x[n]-f(x[n])/f'(x[n]) */ 4 | 5 | #include 6 | #include 7 | 8 | int main() 9 | { 10 | double x=1.5,y1,y2; 11 | y1 = 2*x*x*x-4*x*x+3*x-6; 12 | y2 = 6*x*x-8*x+3; 13 | while (fabs(y1)>1e-6){ 14 | x = x - y1/y2; 15 | y1 = 2*x*x*x-4*x*x+3*x-6; 16 | y2 = 6*x*x-8*x+3; 17 | } 18 | printf("x = %f\n",x); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /141105/WordNum#.c: -------------------------------------------------------------------------------- 1 | /*输入语句,判断单词个数*/ 2 | 3 | #include 4 | int ischar(char now) { 5 | return (now >= 'a' && now <= 'z' 6 | || now >= 'A' && now <= 'Z' 7 | || now >= '0' && now <= '9'); 8 | } 9 | int main() 10 | { 11 | char last = 0, now = 0; 12 | int count = 0; 13 | while ((now = getchar()) != EOF) { 14 | if (ischar(now) && !ischar(last)) 15 | count++; 16 | last = now; 17 | } 18 | printf("\nWords Number : %d\n", count); 19 | } 20 | 21 | 22 | //with the help of boj 23 | //这个代码根本没有用字符数组,而且引入了ischar的函数! 24 | -------------------------------------------------------------------------------- /141105/WordNum.c: -------------------------------------------------------------------------------- 1 | /*输入语句,判断单词个数*/ 2 | #include 3 | #define N 1000 4 | 5 | int main() 6 | { 7 | char a[N]={0}; //** 8 | int i=1,nspece=0,nword; 9 | a[0]=' '; 10 | a[N-1]=' '; 11 | printf("Please input some sentencs.When stop input 'ctrl+D'.\n"); 12 | 13 | while ((a[i]=getchar()) != EOF) 14 | i++; 15 | a[i]=0; 16 | 17 | for (i=0; i'Z' && a[i]<'a') || a[i]>'z') 19 | a[i] = ' '; // fix me! 20 | 21 | printf("%s\n",a); 22 | 23 | for (i=0; i http://en.wikipedia.org/wiki/Josephus_problem 3 | wolfram > http://mathworld.wolfram.com/JosephusProblem.html */ 4 | 5 | #include 6 | 7 | #define MAX 1000 8 | 9 | int main() 10 | { 11 | int alive; // alive = number of people alive in last circle 12 | int i, j; 13 | int n, m, s; // s mean the people will be killed next! 14 | int last[MAX] = {0}; // last_circle 15 | int now[MAX] = {0}; // now_circle 16 | int death[MAX] = {0}; // death_order 17 | printf("n denote the number of people in the initial circle.\n" 18 | "m denotes the count for each step.\n" 19 | "Begin at sth people in the initial circle.\n"); 20 | printf("Please input n, m, s separated by 'space': "); 21 | scanf("%d%d%d",&n,&m,&s); 22 | 23 | /* initialize last circle */ 24 | for (i=0; i 1) 30 | { 31 | /* kill people! */ 32 | s = (s-1+m)%alive; 33 | if (s == 0) // if s == 0; s-1 == -1 and (s-1)%alive == -1 fail 34 | s = alive; 35 | death[n-alive] = last[s-1]; 36 | last[s-1] = 0; 37 | 38 | j = 0; 39 | /* last to now */ 40 | for (i=0; i 2 | #include 3 | // please gcc ~.c -lm 4 | 5 | #define MAX 32 6 | 7 | double atof(char s[]) 8 | { 9 | double f = 0; 10 | int i, j=0, l, t=1, k=0, n=0; 11 | double help1,help2,help0=0; 12 | for (i=0; s[i] != '\0'; i++) {} 13 | l = i; // length of number 14 | 15 | /* mark the position of '-' and 'e'*/ 16 | if (s[0] == '-') 17 | t = -1; 18 | for (i=0; i= (t==1?0:1) ; i--) { 24 | if (s[i] != '.') { 25 | f += (s[i]-48)*pow(10,j); 26 | j += 1; 27 | } 28 | else 29 | k = i + (t==1 ? 0:-1); 30 | } 31 | /* deal with e+1 part */ 32 | if (n!=0) { 33 | for (i=n+2; i 2 | #include 3 | #include 4 | 5 | /* this function generate a pseudo-random list, 6 | N : the number of list elements, 7 | MAX : the range of random numbers is between 0 and MAX */ 8 | 9 | int generator(int randlist[],int N,int MAX) 10 | { 11 | srand( (unsigned) time(NULL) ); 12 | int k; 13 | for (k=0; k 5 | 6 | char strcompare(char a[],char b[]) 7 | { 8 | int i; 9 | for (i=0; a[i]!='\0' || b[i]!='\0'; i++) 10 | { 11 | if (a[i]>b[i]) 12 | return '>'; 13 | if (a[i] 2 | 3 | char strcompare(char a[],char b[]) 4 | { 5 | int i; 6 | for (i=0; a[i]!='\0' || b[i]!='\0'; i++) 7 | { 8 | if (a[i]>b[i]) 9 | return '>'; 10 | if (a[i] 2 | 3 | #define M 5 4 | #define N 5 5 | 6 | int main() 7 | { 8 | int a[M][N]; 9 | int i,j; 10 | for (i = 0; i < M; i++) 11 | for (j = 0; j < N; j++) 12 | a[i][j] = 10*(i+1) + (j+1); 13 | 14 | int (*p)[N] = a; 15 | 16 | for (i = 0; i < M; i++) 17 | { 18 | for (j = 0; j < N; j++) 19 | { 20 | // four ways to represent the same element of array 21 | printf("%d ", a[i][j]); 22 | printf("%d ", p[i][j]); 23 | printf("%d ", *(*(p+i)+j)); 24 | printf("%d ", *(p[i]+j)); 25 | printf("%d ", (*(p+i))[j]); 26 | } 27 | printf("\n"); 28 | } 29 | 30 | 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /141118/macros_with_arg.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define swap(a,b) t = a; a = b; b = t; 4 | 5 | int main() 6 | { 7 | int m, n, t; 8 | printf("Please input two argument: "); 9 | scanf("%d%d",&m,&n); 10 | 11 | printf("m = %d\n",m); 12 | printf("n = %d\n",n); 13 | 14 | swap(m,n); 15 | 16 | printf("m = %d\n",m); 17 | printf("n = %d\n",n); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /141118/reverse_str.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX 50 4 | 5 | int reverse(char a[]) 6 | { 7 | char b[MAX]; 8 | int i, k; 9 | 10 | // b[] copy a[] 11 | for (i = 0; a[i] != '\0'; i++) 12 | { 13 | b[i] = a[i]; 14 | k = i; 15 | } 16 | 17 | // reverse a[] 18 | for (i = 0; i <= k; i++) 19 | a[i] = b[k-i]; 20 | return 0; 21 | } 22 | 23 | int main() 24 | { 25 | char str1[MAX]; 26 | printf("Please input a string: "); 27 | scanf("%s",str1); 28 | reverse(str1); 29 | puts(str1); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /141118/score.c: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * never thought this problem will cost so much time! 4 | * 二维数组作为函数参数的问题 5 | */ 6 | 7 | #include 8 | 9 | #define num_student 5 10 | #define num_course 3 11 | 12 | int score_input(float array[], int k) 13 | { 14 | int i; 15 | printf("Please input 3 scores of student%d: ",k); 16 | for (i = 0; i < num_course; i++) 17 | scanf("%f",&array[i]); 18 | return 0; 19 | } 20 | 21 | 22 | float stu_average(float array[]) 23 | { 24 | int i; 25 | float sum = 0; 26 | for (i = 0; i < num_course; i++) 27 | sum += array[i]; 28 | return sum/num_course; 29 | } 30 | 31 | 32 | // 二维数组需要告知列数 33 | float cou_average(float array[][num_course], int k) 34 | { 35 | int i; 36 | float sum = 0; 37 | for (i = 0; i < num_student; i++) 38 | sum += array[i][k]; 39 | return sum/num_student; 40 | } 41 | 42 | 43 | int find_max(float array[][num_course], int *student, int *course) 44 | { 45 | float temp = 0; 46 | int i,j; 47 | for (i = 0; i < num_student; i++) 48 | for (j = 0; j < num_course; j++) 49 | if (temp < array[i][j]) 50 | { 51 | temp = array[i][j]; 52 | *student = i+1; 53 | *course = j+1; 54 | } 55 | 56 | return 0; 57 | } 58 | 59 | int cal_variance(float array[], float *variance) 60 | { 61 | int i, j; 62 | float sigmaX = 0, sigmaX2 = 0; 63 | for (i = 0; i < num_student; i++) 64 | { 65 | sigmaX += array[i]; 66 | sigmaX2 += array[i]*array[i]; 67 | } 68 | *variance = sigmaX2/num_student - (sigmaX/num_student)*(sigmaX/num_student); 69 | return 0; 70 | } 71 | 72 | 73 | int main() 74 | { 75 | int x; 76 | int student, course; 77 | float score[num_student][num_course]={0}; 78 | float stu_ave[num_student]; 79 | float variance; 80 | 81 | for (x = 0; x < num_student; x++) 82 | score_input(score[x], x+1); 83 | 84 | for (x = 0; x < num_student; x++) 85 | printf("average score of student%d = %f\n", x+1, stu_ave[x]=stu_average(score[x])); 86 | 87 | for (x = 0; x < num_course; x++) 88 | printf("average score of course%d = %f\n", x+1, cou_average(score, x)); 89 | 90 | find_max(score, &student, &course); 91 | printf("student%d's course%d is highest!\n", student, course); 92 | 93 | cal_variance(stu_ave, &variance); 94 | printf("variance of all student's average score is %f !\n", variance); 95 | return 0; 96 | } 97 | 98 | 99 | 100 | // fix me! PLEASE add comments! 101 | -------------------------------------------------------------------------------- /141126/joseph.c: -------------------------------------------------------------------------------- 1 | #include 2 | /* Algorithm: 3 | * After the k-th person is killed, we're left with a circle with n-1 people and the starting point is ((k - 1) % n + 1) 4 | * f(n, k) = (f(n-1, k) + k - 1) % n + 1 5 | * f(1, k) = 1 6 | * 7 | * Simplify (number items from 0 to n-1) 8 | * g(n, k) = (g(n-1, k) + k) % n 9 | * g(1, k) = 0 10 | */ 11 | int joseph(int n, int k) { 12 | int a, m = 0; 13 | for (a = 1; a <= n; a++) 14 | m = (m + k) % a; 15 | return m + 1; 16 | } 17 | int main() { 18 | int n; 19 | printf("Please input the number of people: "); 20 | scanf("%d", &n); 21 | printf("%d\n", joseph(n, 3)); 22 | } 23 | -------------------------------------------------------------------------------- /141126/row_pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int *seek(int (*grade)[3], int j) { 4 | int i; 5 | for (i=0; i<3; i++) { 6 | if (grade[j][i] < 60) 7 | return grade[j]; 8 | } 9 | return NULL; 10 | } 11 | 12 | int main() { 13 | int i; 14 | int grade[3][3]={{65,65,75},{55,75,85},{75,80,90}}; 15 | int *fail; 16 | for (i=0; i<3; i++) { 17 | fail = seek(grade,i); 18 | if (fail != NULL) { 19 | printf("Grades of a failed student:\n"); 20 | for (i=0; i<3; i++) 21 | printf("%d ", fail[i]); 22 | printf("\n"); 23 | return 0; 24 | } 25 | } 26 | 27 | printf("No failed student!\n"); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /141126/seek.c: -------------------------------------------------------------------------------- 1 | /*************************************************************/ 2 | /*seek()函数:判断是否有不合格成绩 */ 3 | /*形参:指向由3个int型元素组成的1维数组的行指针变量 */ 4 | /*返回值:(1)有不合格成绩,则返回指向本行首列的一个(列)指针; */ 5 | /* (2)没有有不合格成绩,返回值为指向下一行的一个(列)指针 */ 6 | /*************************************************************/ 7 | #include 8 | 9 | int *seek(int (*pnt_row)[3]) 10 | { 11 | int i = 0, *pnt_col; //定义一个列指针变量 pnt_col 12 | pnt_col = *(pnt_row + 1); 13 | 14 | for(; i < 3; i++) 15 | if(*(*pnt_row + i) < 60) 16 | { 17 | pnt_col = *pnt_row; 18 | break; 19 | } 20 | return (pnt_col); 21 | } 22 | 23 | int main() 24 | { 25 | int grade[3][3] = {{55,65,75}, {65,75,85}, {75,80,90}}; 26 | int i, j, *pointer; 27 | for (i = 0; i < 3; i++) 28 | { 29 | pointer = seek(grade + i); 30 | if (pointer == *(grade + i)) 31 | { 32 | printf("No.%d grade list:",i+1); 33 | for (j = 0; j < 3; j++) 34 | printf("%d ",*(pointer + j)); 35 | printf("\n"); 36 | } 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /141126/sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void selection_sort(int n, char *pstr[]) 4 | { 5 | int max; 6 | char *tmp; 7 | int i,j; 8 | for (i=0; i 0) 12 | max = j; 13 | // swap (i, max) 14 | tmp = pstr[i]; 15 | pstr[i] = pstr[max]; 16 | pstr[max] = tmp; 17 | } 18 | } 19 | 20 | void bubble_sort(int n, char *pstr[]) 21 | { 22 | char *tmp; 23 | int i,j; 24 | for (i=0; i 0) { 27 | tmp = pstr[i]; 28 | pstr[i] = pstr[j]; 29 | pstr[j] = tmp; 30 | } 31 | } 32 | 33 | int main() { 34 | int i; 35 | char *pstr[5]; 36 | char *tosort[5]; 37 | 38 | pstr[1] = "1111"; 39 | pstr[0] = "1222"; 40 | pstr[3] = "1233"; 41 | pstr[4] = "44444"; 42 | pstr[2] = "55"; 43 | 44 | for (i=0; i<5; i++) 45 | tosort[i] = pstr[i]; 46 | selection_sort(5, tosort); 47 | printf("Selection sort result:\n"); 48 | for (i=0; i<5; i++) 49 | printf("%s\n", tosort[i]); 50 | 51 | for (i=0; i<5; i++) 52 | tosort[i] = pstr[i]; 53 | bubble_sort(5, tosort); 54 | printf("Bubble sort result:\n"); 55 | for (i=0; i<5; i++) 56 | printf("%s\n", tosort[i]); 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /141126/transpose.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void transpose(int n, int *mat) { 5 | int i, j; 6 | for (i=0; i 2 | #include 3 | 4 | #define MIN 64 5 | 6 | int main() 7 | { 8 | int i, j, min = 0; 9 | char str[5][MIN], *pstr[5], *tmp; 10 | printf("Please input 5 strings separated by enter:\n"); 11 | for (i = 0; i < 5; i++) 12 | scanf("%s", str[i]); 13 | 14 | for (i = 0; i < 5; i++) 15 | pstr[i] = str[i]; 16 | 17 | /* selection sort: 根据字符串大小,把指针数组进行排序 */ 18 | for (i = 0; i < 5; i++) { 19 | min = i; 20 | for (j = i; j < 5; j++) 21 | if (strcmp(pstr[j], pstr[min]) < 0) 22 | min = j; 23 | tmp = pstr[i]; 24 | pstr[i] = pstr[min]; 25 | pstr[min] = tmp; // 也就是把地址换来换去了 26 | } 27 | 28 | printf("After sort: \n"); 29 | 30 | for (i = 0; i < 5; i++) 31 | printf("%s\n", pstr[i]); 32 | 33 | return 0; 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /141203/fun_pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define N 10000 5 | 6 | double def_integral(double (*func)(double), double a, double b); 7 | 8 | int main() 9 | { 10 | int i; 11 | double y[3]; 12 | y[0] = def_integral(sin, 0, 1); 13 | y[1] = def_integral(cos, -1, 1); 14 | y[2] = def_integral(exp, 0, 2); 15 | 16 | for (i=0; i<3; i++) 17 | printf("y[%d] = %lf\n", i+1, y[i]); 18 | return 0; 19 | } 20 | 21 | double def_integral(double (*func)(double), double a, double b) 22 | { 23 | double sum = 0; 24 | double del, x = a; 25 | 26 | del = (b-a)/N; 27 | while (x < b) { 28 | x += del; 29 | sum += func(x)*del; 30 | } 31 | return sum; 32 | } 33 | 34 | 35 | -------------------------------------------------------------------------------- /141203/matrix_5x5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SelectSort.c" 3 | 4 | /* 这一题要怎么用指针啊!用指针函数呢还是指针数组呢? */ 5 | 6 | int change(int mat[]); 7 | 8 | int main() 9 | { 10 | int i, j; 11 | int matrix[25]; // 为什么用一维数组呢... 12 | srand( (unsigned) time(NULL) ); // 这一行其实我不懂.. 13 | for (i = 0; i < 25; i++) 14 | *(matrix + i) = rand() % 20; 15 | 16 | change(matrix); 17 | 18 | for (i = 0; i < 5; i++) { 19 | for (j = 0; j < 5; j++) 20 | printf("%4d",*(matrix+i*5+j)); 21 | printf("\n"); 22 | } 23 | 24 | return 0; 25 | } 26 | 27 | 28 | int change(int *mat) 29 | { 30 | int i = 0, j = 0; 31 | SelectSort(mat, 25); 32 | int help[5] = {mat[0], mat[1], mat[2], mat[3], mat[24]}; 33 | while (i < 25) { 34 | if (i != 12) { 35 | *(mat+i) = help[j%4]; 36 | j++; 37 | } 38 | i++; 39 | } 40 | mat[12] = help[4]; 41 | return 0; 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /141203/struct_book.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX 32 4 | #define N 5 5 | 6 | struct book { 7 | int id; 8 | char name[MAX]; 9 | char author[MAX]; 10 | char press[MAX]; 11 | double price; 12 | }; 13 | 14 | int main() 15 | { 16 | int i; 17 | double sum = 0; 18 | struct book bk[N]; 19 | for (i=0; i 4 | #include 5 | 6 | #define NAME_LEN 64 7 | 8 | struct couple *createCPLL(void); 9 | int printCPLL(struct couple *head); 10 | 11 | int main() 12 | { 13 | printCPLL(createCPLL()); 14 | return 0; 15 | } 16 | 17 | /* declare a struct */ 18 | struct couple 19 | { 20 | int No; 21 | char boy_name[NAME_LEN]; 22 | char girl_name[NAME_LEN]; 23 | struct couple *next; 24 | }; 25 | 26 | /* a function to create linked list: head-insert */ 27 | struct couple *createCPLL(void) 28 | { 29 | struct couple *head; 30 | struct couple *cp; 31 | int No; 32 | head = NULL; // initialize last node linking to NULL. 33 | printf("Please input Number of a couple, enter 0 to exit: "); 34 | scanf("%d", &No); 35 | 36 | while( No != 0) { 37 | cp = (struct couple *)malloc(sizeof(struct couple)); 38 | cp->next = head; 39 | cp->No = No; 40 | printf("Boy's name: "); 41 | scanf("%s", cp->boy_name); 42 | printf("Girl's name: "); 43 | scanf("%s", cp->girl_name); 44 | head = cp; 45 | printf("Please input Number of a couple, enter 0 to exit: "); 46 | scanf("%d", &No); 47 | } 48 | return head; 49 | } 50 | 51 | /* you need a function to printf your linked list to check */ 52 | int printCPLL(struct couple *head) 53 | { 54 | struct couple *cp = head; 55 | while (cp != NULL) { 56 | printf("No.%d couple is %s and %s\n", cp->No, cp->boy_name, cp->girl_name); 57 | cp = cp->next; 58 | } 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /141210/createcpll2.c: -------------------------------------------------------------------------------- 1 | /* create linked list: tail-insert */ 2 | 3 | #include 4 | #include 5 | 6 | #define NAME_LEN 64 7 | 8 | struct couple *createCPLL(void); 9 | int printCPLL(struct couple *head); 10 | 11 | int main() 12 | { 13 | printCPLL(createCPLL()); 14 | return 0; 15 | } 16 | 17 | /* declare a struct */ 18 | struct couple 19 | { 20 | int No; 21 | char boy_name[NAME_LEN]; 22 | char girl_name[NAME_LEN]; 23 | struct couple *next; 24 | }; 25 | 26 | /* a function to create linked list: tail-insert */ 27 | struct couple *createCPLL(void) 28 | { 29 | struct couple *head; 30 | struct couple *oldcp; 31 | struct couple *newcp; 32 | int No; 33 | int i = 0; 34 | head = NULL; // initialize last node linking to NULL. 35 | printf("Please input Number of a couple, enter 0 to exit: "); 36 | scanf("%d", &No); 37 | 38 | while( No != 0) { 39 | newcp = (struct couple *)malloc(sizeof(struct couple)); 40 | newcp->No = No; 41 | printf("Boy's name: "); 42 | scanf("%s", newcp->boy_name); 43 | printf("Girl's name: "); 44 | scanf("%s", newcp->girl_name); 45 | 46 | if (i++ == 0) { // at the beginning, it's a bit unique 47 | head = newcp; // mark the first note 48 | oldcp = newcp; 49 | } 50 | else { 51 | oldcp->next = newcp; // if a new couple's info was enrolled 52 | oldcp = newcp; // they become the old one 53 | } 54 | 55 | printf("Please input Number of a couple, enter 0 to exit: "); 56 | scanf("%d", &No); 57 | } 58 | return head; 59 | } 60 | 61 | /* you need a function to printf your linked list to check */ 62 | int printCPLL(struct couple *head) 63 | { 64 | struct couple *cp = head; 65 | while (cp != NULL) { 66 | printf("No.%d couple is %s and %s\n", cp->No, cp->boy_name, cp->girl_name); 67 | cp = cp->next; 68 | } 69 | return 0; 70 | } 71 | 72 | -------------------------------------------------------------------------------- /141210/deletell.c: -------------------------------------------------------------------------------- 1 | /* create linked list: tail-insert */ 2 | 3 | #include 4 | #include 5 | 6 | #define NAME_LEN 64 7 | 8 | struct couple *createCPLL(void); 9 | int printCPLL(struct couple *head); 10 | struct couple *deleteCPLL(struct couple *head, int No); 11 | 12 | 13 | int main() 14 | { 15 | int No; 16 | struct couple *CPLL = createCPLL(); 17 | printCPLL(CPLL); 18 | 19 | printf("\n**** DELETE ****\n"); 20 | printf("Please input a No you want to delete, enter 0 to quit: "); 21 | scanf("%d", &No); 22 | while (No != 0) { 23 | CPLL = deleteCPLL(CPLL, No); 24 | printCPLL(CPLL); 25 | printf("Please input a No you want to delete, enter 0 to quit: "); 26 | scanf("%d", &No); 27 | } 28 | printf("\n****DONE****\n"); 29 | return 0; 30 | } 31 | 32 | /* declare a struct */ 33 | struct couple 34 | { 35 | int No; 36 | char boy_name[NAME_LEN]; 37 | char girl_name[NAME_LEN]; 38 | struct couple *next; 39 | }; 40 | 41 | /* a function to create linked list: tail-insert */ 42 | struct couple *createCPLL(void) 43 | { 44 | struct couple *head; 45 | struct couple *oldcp; 46 | struct couple *newcp; 47 | int No; 48 | int i = 0; 49 | head = NULL; // initialize last node linking to NULL. 50 | printf("**** CREATE ****\n"); 51 | printf("Please input Number of a couple, enter 0 to exit: "); 52 | scanf("%d", &No); 53 | 54 | while( No != 0) { 55 | newcp = (struct couple *)malloc(sizeof(struct couple)); 56 | newcp->No = No; 57 | printf("Boy's name: "); 58 | scanf("%s", newcp->boy_name); 59 | printf("Girl's name: "); 60 | scanf("%s", newcp->girl_name); 61 | ; 62 | if (i++ == 0) { // at the beginning, it's a bit unique 63 | head = newcp; // mark the first note 64 | oldcp = newcp; 65 | } 66 | else { 67 | oldcp->next = newcp; // if a new couple's info was enrolled 68 | oldcp = newcp; // they become the old one 69 | } 70 | 71 | printf("Please input Number of a couple, enter 0 to exit: "); 72 | scanf("%d", &No); 73 | } 74 | return head; 75 | } 76 | 77 | /* you need a function to printf your linked list to check */ 78 | int printCPLL(struct couple *head) 79 | { 80 | struct couple *cp = head; 81 | while (cp != NULL) { 82 | printf("No.%d couple is %s and %s\n", cp->No, cp->boy_name, cp->girl_name); 83 | cp = cp->next; 84 | } 85 | return 0; 86 | } 87 | 88 | /* assume cp->No was sort from small to large */ 89 | struct couple *deleteCPLL(struct couple *head, int No) 90 | { 91 | struct couple *pnow = head; 92 | struct couple *pnext; 93 | char x; 94 | 95 | if (head == NULL) { 96 | printf("Warning: This is a empty linked list.\n" 97 | "You have nothing to delete!\n"); 98 | return head; 99 | } 100 | /* delete the first node */ 101 | if (head->No == No) { 102 | head = head->next; 103 | free(pnow); 104 | return head; 105 | } 106 | 107 | /* when didn't fine */ 108 | while (pnow->next != NULL && pnow->next->No != No) 109 | pnow = pnow->next; 110 | 111 | /* find the node and delete */ 112 | if (pnow->next != NULL) { 113 | pnext = pnow->next->next; 114 | free(pnow->next); 115 | pnow->next = pnext; 116 | } 117 | /* can't find */ 118 | else 119 | printf("Can not find the node you want to delete!\n"); 120 | return head; 121 | 122 | } 123 | -------------------------------------------------------------------------------- /141210/insertll.c: -------------------------------------------------------------------------------- 1 | /* create linked list: head-insert */ 2 | 3 | #include 4 | #include 5 | 6 | #define NAME_LEN 64 7 | 8 | struct couple *createCPLL(void); 9 | int printCPLL(struct couple *head); 10 | struct couple *insertCPLL(struct couple *head, int No); 11 | 12 | 13 | int main() 14 | { 15 | int No; 16 | struct couple *CPLL = createCPLL(); 17 | printCPLL(CPLL); 18 | printf("\n**** INSERT ****\n"); 19 | printf("Please input Number of a couple, enter 0 to exit: "); 20 | scanf("%d", &No); 21 | while (No != 0) { 22 | CPLL = insertCPLL(CPLL, No); 23 | printf("Please input Number of a couple, enter 0 to exit: "); 24 | scanf("%d", &No); 25 | } 26 | printCPLL(CPLL); 27 | return 0; 28 | } 29 | 30 | /* declare a struct */ 31 | struct couple 32 | { 33 | int No; 34 | char boy_name[NAME_LEN]; 35 | char girl_name[NAME_LEN]; 36 | struct couple *next; 37 | }; 38 | 39 | /* a function to create linked list: head-insert */ 40 | struct couple *createCPLL(void) 41 | { 42 | struct couple *head; 43 | struct couple *cp; 44 | int No; 45 | head = NULL; // initialize last node linking to NULL. 46 | printf("\nPlease input Number of a couple, enter 0 to exit: "); 47 | scanf("%d", &No); 48 | 49 | while( No != 0) { 50 | cp = (struct couple *)malloc(sizeof(struct couple)); 51 | cp->next = head; 52 | cp->No = No; 53 | printf("Boy's name: "); 54 | scanf("%s", cp->boy_name); 55 | printf("Girl's name: "); 56 | scanf("%s", cp->girl_name); 57 | head = cp; 58 | printf("Please input Number of a couple, enter 0 to exit: "); 59 | scanf("%d", &No); 60 | } 61 | return head; 62 | } 63 | 64 | /* you need a function to printf your linked list to check */ 65 | int printCPLL(struct couple *head) 66 | { 67 | struct couple *cp = head; 68 | while (cp != NULL) { 69 | printf("No.%d couple is %s and %s\n", cp->No, cp->boy_name, cp->girl_name); 70 | cp = cp->next; 71 | } 72 | return 0; 73 | } 74 | 75 | /* assume cp->No was sort from small to large */ 76 | struct couple *insertCPLL(struct couple *head, int No) 77 | { 78 | struct couple *cp = head; 79 | struct couple *pnew; 80 | pnew = (struct couple *)malloc(sizeof(struct couple)); 81 | // 第一次居然非常蠢的忘记 malloc 给 pnew 分配内存啦 82 | 83 | /* create a new node */ 84 | pnew->No = No; 85 | printf("Boy's name: "); 86 | scanf("%s", pnew->boy_name); 87 | printf("Girl's name: "); 88 | scanf("%s", pnew->girl_name); 89 | pnew->next = NULL; 90 | 91 | /* find the right position to insert */ 92 | 93 | // if CPLL is a NULL linked list 94 | if (head == NULL) 95 | return pnew; 96 | 97 | // if a new node should be inserted in front of head 98 | if (head->No > pnew->No) { 99 | pnew->next = head; 100 | return pnew; 101 | } 102 | 103 | while (cp->next != NULL) { 104 | if (pnew->No < cp->next->No) { 105 | pnew->next = cp->next; 106 | cp->next = pnew; 107 | return head; 108 | } 109 | else 110 | cp = cp->next; 111 | } 112 | 113 | // if a new node should be inserted at the end 114 | cp->next = pnew; 115 | return head; 116 | } 117 | -------------------------------------------------------------------------------- /141217/copy3.txt: -------------------------------------------------------------------------------- 1 | 1 A a 2 | 2 B b 3 | 3 C c 4 | 0 5 | -------------------------------------------------------------------------------- /141217/copy4.txt: -------------------------------------------------------------------------------- 1 | 1 A a 2 | 2 B b 3 | 3 C c 4 | 0 5 | -------------------------------------------------------------------------------- /141217/file1.c: -------------------------------------------------------------------------------- 1 | /* fscanf */ /* fprintf */ 2 | /* create linked list: head-insert */ 3 | 4 | #include 5 | #include 6 | 7 | #define NAME_LEN 64 8 | 9 | struct couple *createCPLL(FILE *fp); 10 | int print2file(struct couple *head, FILE *fp); 11 | 12 | int main() 13 | { 14 | FILE *fp; 15 | struct couple *head; 16 | 17 | if ((fp = fopen("input1.txt", "r")) == NULL) { 18 | printf("Can not open this file input1.txt\n"); 19 | return 0; 20 | } 21 | head = createCPLL(fp); 22 | fclose(fp); 23 | 24 | if ((fp = fopen("output1.txt", "w")) == NULL) { 25 | printf("Can not open this file output1.txt\n"); 26 | return 0; 27 | } 28 | print2file(head, fp); 29 | fclose(fp); 30 | 31 | return 0; 32 | } 33 | 34 | /* declare a struct */ 35 | struct couple 36 | { 37 | int No; 38 | char boy_name[NAME_LEN]; 39 | char girl_name[NAME_LEN]; 40 | struct couple *next; 41 | }; 42 | 43 | /* a function to create linked list: head-insert */ 44 | struct couple *createCPLL(FILE *fp) 45 | { 46 | struct couple *head; 47 | struct couple *cp; 48 | int No; 49 | head = NULL; // initialize last node linking to NULL. 50 | fscanf(fp, "%d", &No); 51 | 52 | while( No != 0) { 53 | cp = (struct couple *)malloc(sizeof(struct couple)); 54 | cp->next = head; 55 | cp->No = No; 56 | fscanf(fp, "%s", cp->boy_name); 57 | fscanf(fp, "%s", cp->girl_name); 58 | head = cp; 59 | fscanf(fp, "%d", &No); 60 | } 61 | return head; 62 | } 63 | 64 | /* you need a function to printf your linked list to check */ 65 | int print2file(struct couple *head, FILE *fp) 66 | { 67 | struct couple *cp = head; 68 | while (cp != NULL) { 69 | fprintf(fp, "No.%d couple is %s and %s\n", cp->No, cp->boy_name, cp->girl_name); 70 | cp = cp->next; 71 | } 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /141217/file2.c: -------------------------------------------------------------------------------- 1 | /* fwrite */ /* fread */ 2 | /* create linked list: tail-insert */ 3 | 4 | #include 5 | #include 6 | 7 | #define NAME_LEN 64 8 | 9 | struct couple *createCPLL(FILE *fp); 10 | int file2print(FILE *fp); 11 | 12 | int i; // waring!! 13 | 14 | int main() 15 | { 16 | FILE *fp; 17 | i = 0; 18 | 19 | if ((fp = fopen("couple2.out", "w")) == NULL) { 20 | printf("Can not open this file couple2.txt\n"); 21 | return 0; 22 | } 23 | createCPLL(fp); 24 | fclose(fp); 25 | 26 | if ((fp = fopen("couple2.out", "r")) == NULL) { 27 | printf("Can not open this file couple2.txt\n"); 28 | return 0; 29 | } 30 | file2print(fp); 31 | fclose(fp); 32 | 33 | return 0; 34 | } 35 | 36 | /* declare a struct */ 37 | struct couple 38 | { 39 | int No; 40 | char boy_name[NAME_LEN]; 41 | char girl_name[NAME_LEN]; 42 | struct couple *next; 43 | }; 44 | 45 | /* a function to create linked list: tail-insert */ 46 | struct couple *createCPLL(FILE *fp) 47 | { 48 | struct couple *head; 49 | struct couple *oldcp; 50 | struct couple *newcp; 51 | int No; 52 | head = NULL; // initialize last node linking to NULL. 53 | printf("Please input Number of a couple, enter 0 to exit: "); 54 | scanf("%d", &No); 55 | 56 | while( No != 0) { 57 | newcp = (struct couple *)malloc(sizeof(struct couple)); 58 | newcp->No = No; 59 | printf("Boy's name: "); 60 | scanf("%s", newcp->boy_name); 61 | printf("Girl's name: "); 62 | scanf("%s", newcp->girl_name); 63 | fwrite(newcp, sizeof(struct couple), 1, fp); 64 | 65 | if (i++ == 0) { // at the beginning, it's a bit unique 66 | head = newcp; // mark the first note 67 | oldcp = newcp; 68 | } 69 | else { 70 | oldcp->next = newcp; // if a new couple's info was enrolled 71 | oldcp = newcp; // they become the old one 72 | } 73 | 74 | printf("Please input Number of a couple, enter 0 to exit: "); 75 | scanf("%d", &No); 76 | } 77 | return head; 78 | } 79 | 80 | /* you need a function to printf your linked list to check */ 81 | int file2print(FILE *fp) 82 | { 83 | int j; 84 | struct couple cp[i]; 85 | for (j = 0; j < i; j++) { 86 | fread(&cp[j], sizeof(struct couple), 1, fp); 87 | printf("No.%d couple is %s and %s\n", cp[j].No, cp[j].boy_name, cp[j].girl_name); 88 | } 89 | return 0; 90 | } 91 | 92 | -------------------------------------------------------------------------------- /141217/filecopy3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void filecopy(FILE *fp1, FILE *fp2); 5 | 6 | int main(int argc, char *argv[]) 7 | { 8 | FILE *fp1, *fp2; 9 | if (argc > 1) { 10 | if((fp1 = fopen(*++argv, "r")) == NULL) { 11 | printf("can't fopen %s\n", *argv); 12 | return 0; 13 | } 14 | if((fp2 = fopen(*++argv, "w")) == NULL) { 15 | printf("can't fopen %s\n", *argv); 16 | return 0; 17 | } 18 | filecopy(fp1, fp2); 19 | fclose(fp1); 20 | fclose(fp2); 21 | } 22 | return 0; 23 | } 24 | 25 | 26 | void filecopy(FILE *fp1, FILE *fp2) 27 | { 28 | char c; 29 | while ((c = fgetc(fp1)) != EOF) 30 | fputc(c, fp2); 31 | } 32 | -------------------------------------------------------------------------------- /141217/filecopy4.c: -------------------------------------------------------------------------------- 1 | /* learn to use fgets and fputs */ 2 | #include 3 | #include 4 | 5 | #define SIZE 1024 6 | 7 | void filecopy(FILE *fp1, FILE *fp2); 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | FILE *fp1, *fp2; 12 | if (argc > 1) { 13 | if((fp1 = fopen(*++argv, "r")) == NULL) { 14 | printf("can't fopen %s\n", *argv); 15 | return 0; 16 | } 17 | if((fp2 = fopen(*++argv, "w")) == NULL) { 18 | printf("can't fopen %s\n", *argv); 19 | return 0; 20 | } 21 | filecopy(fp1, fp2); 22 | fclose(fp1); 23 | fclose(fp2); 24 | } 25 | return 0; 26 | } 27 | 28 | 29 | void filecopy(FILE *fp1, FILE *fp2) 30 | { 31 | char str[SIZE]; 32 | while (fgets(str, SIZE, fp1) != NULL) 33 | fputs(str, fp2); 34 | } 35 | -------------------------------------------------------------------------------- /141217/input1.txt: -------------------------------------------------------------------------------- 1 | 1 A a 2 | 2 B b 3 | 3 C c 4 | 0 5 | -------------------------------------------------------------------------------- /141217/output1.txt: -------------------------------------------------------------------------------- 1 | No.3 couple is C and c 2 | No.2 couple is B and b 3 | No.1 couple is A and a 4 | -------------------------------------------------------------------------------- /141228/exam1_1.c: -------------------------------------------------------------------------------- 1 | /****************************** 2 | * 测试卷一 第五题 1 3 | * 矩阵转置处理,二维数组和指针 4 | ******************************/ 5 | 6 | #include 7 | 8 | #define N 4 9 | 10 | void convert(int *p); 11 | int total(int a[][4], int k); 12 | 13 | int main() 14 | { 15 | static int a[N][N] = {{1,1,1,1},{2,2,2,2},{3,3,3,3},{4,4,4,4}}; 16 | int *p, i, sum; 17 | p = *a; 18 | 19 | sum = total(a, N); 20 | printf("%d\n", sum); 21 | 22 | convert(p); 23 | for (i = 0; i < N; i++) 24 | printf("%d %d %d %d\n",a[i][0], a[i][1], a[i][2], a[i][3]); 25 | return 0; 26 | } 27 | 28 | int total(int a[][4], int k) 29 | { 30 | int i, sum = 0; 31 | for (i = 0; i < N; i++) 32 | sum += a[i][i]; 33 | return sum; 34 | } 35 | 36 | /* 由于之前传入的实参是p,下面的函数不可以用数组实现 */ 37 | void convert(int *p) 38 | { 39 | int i, j, temp; 40 | for (i = 0; i < N; i++) 41 | for (j = i+1; j < N; j++) { 42 | temp = *(p+4*i+j); 43 | *(p+4*i+j) = *(p+4*j+i); 44 | *(p+4*j+i) = temp; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /141228/exam1_2.c: -------------------------------------------------------------------------------- 1 | /**************************** 2 | * 测试卷一 第五题 2 3 | * 结构体数组 4 | ****************************/ 5 | 6 | #include 7 | 8 | #define MAX_LEN 64 9 | #define N 3 10 | 11 | struct student { 12 | int id; 13 | char name[MAX_LEN]; 14 | int score[3]; 15 | int sum; 16 | }; 17 | 18 | void input_info(struct student stu[]); 19 | void select_sort(struct student *p[]); 20 | void output_info(struct student *p[]); 21 | 22 | int main() 23 | { 24 | int i; 25 | struct student stu[N], *pstu[N]; 26 | for (i = 0; i < N; i++) 27 | pstu[i] = &stu[i]; 28 | 29 | input_info(stu); 30 | select_sort(pstu); 31 | output_info(pstu); 32 | return 0; 33 | } 34 | 35 | void input_info(struct student stu[]) 36 | { 37 | int i, j, sum; 38 | for (i=0; i < N; i++) { 39 | printf("Please input student's id: "); 40 | scanf("%d", &stu[i].id); 41 | printf("Please input student's name: "); 42 | scanf("%s", stu[i].name); 43 | sum = 0; 44 | for (j = 0; j < 3; j++) { 45 | printf("Please input student's score%d: ", j+1); 46 | scanf("%d", &stu[i].score[j]); 47 | sum += stu[i].score[j]; 48 | } 49 | stu[i].sum = sum; 50 | } 51 | } 52 | 53 | void select_sort(struct student *p[]) 54 | { 55 | int i, j, max; 56 | struct student *temp; 57 | for (i = 0; i < N; i++) { 58 | max = i; 59 | for (j = i+1; j < N; j++) 60 | if(p[max]->sum < p[j]->sum) 61 | max = j; 62 | if (max != i) { 63 | temp = p[i]; 64 | p[i] = p[max]; 65 | p[max] = temp; 66 | } 67 | } 68 | } 69 | 70 | 71 | void output_info(struct student *p[]) 72 | { 73 | int i, j; 74 | for (i = 0; i < N; i++) { 75 | printf("id=%d, name=%s, ", p[i]->id, p[i]->name); 76 | for (j = 0; j < 3; j++) 77 | printf("score%d=%d, ", j, p[i]->score[j]); 78 | printf("total_score=%d\n", p[i]->sum); 79 | } 80 | } 81 | 82 | -------------------------------------------------------------------------------- /141228/exam4_1.c: -------------------------------------------------------------------------------- 1 | /************************ 2 | * 测试卷四 第四题 1 3 | * 算平均值,方差,均方差 4 | ************************/ 5 | 6 | #include 7 | #include 8 | 9 | #define N 10 10 | 11 | int udf_vft(double *v, double *f, double *t, double x[]) 12 | { 13 | double sum1 = 0, sum2 = 0; 14 | int i; 15 | 16 | for (i=0; i 7 | #include 8 | 9 | char *udf_gets(char *word); 10 | int udf_strlen(char *word); 11 | int udf_strcmp(char *s, char *t); 12 | 13 | 14 | int main() 15 | { 16 | int i = 0, sum = 0, length; 17 | char word[20]; // 这里并没有初始化,故后面的函数要注意word清零. 18 | char text[] = "We wish you merry Christmas,\nwe wish you merry Christmas,\nwe wish you merry Christmas,\nand happy new year!\n"; 19 | char temp[20] = {0}; 20 | printf("Please input the word which you want to count: \n"); 21 | udf_gets(word); 22 | length = udf_strlen(word); 23 | 24 | while(length != 0 && text[i] != '\0') { 25 | strncpy(temp, text+i, length); 26 | if (udf_strcmp(word, temp) == 0) { 27 | sum++; 28 | i += length; 29 | } 30 | i++; 31 | } 32 | printf("The word \"%s\" appears %d times in the text.\n", word, sum); 33 | return 0; 34 | } 35 | 36 | char *udf_gets(char *word) 37 | { 38 | char *word2 = word, ch; 39 | if ((ch = getchar()) == EOF) // EOF 40 | return NULL; 41 | do { 42 | *word2++ = ch; 43 | } while ((ch = getchar())!='\n'); 44 | *word2 = '\0'; 45 | return word; 46 | } 47 | 48 | 49 | int udf_strlen(char *word) 50 | { 51 | int count; 52 | if (word == NULL) // NULL 53 | return 0; 54 | 55 | for (count=0; word[count]!='\0'; count++); 56 | return count; 57 | } 58 | 59 | int udf_strcmp(char *s, char *t) 60 | { 61 | while( *s == *t) { 62 | if (*s == '\0') 63 | return 0; 64 | s++; 65 | t++; 66 | } 67 | return *s - *t; 68 | } 69 | -------------------------------------------------------------------------------- /141228/exam4_3.c: -------------------------------------------------------------------------------- 1 | /********************************** 2 | * 测试卷四 第四题 3 3 | * 尾插法建立链表, 把链表输出到文件中 4 | ***********************************/ 5 | 6 | #include 7 | #include 8 | 9 | struct person { 10 | int no; 11 | float salary; 12 | struct person *next; 13 | }; 14 | 15 | struct person *CreateListR(void); 16 | void writeListR(struct person *head); 17 | 18 | int main() 19 | { 20 | struct person *head; 21 | 22 | head = CreateListR(); 23 | writeListR(head); 24 | return 0; 25 | } 26 | 27 | struct person *CreateListR(void) 28 | { 29 | struct person *head, *p1, *p2; 30 | int no; 31 | head = NULL; 32 | printf("Please input a worker's number: "); 33 | scanf("%d", &no); 34 | 35 | while (no != 0) { 36 | p1 = (struct person *)malloc(sizeof(struct person)); 37 | p1->no = no; 38 | printf("Please input a worker's salary: "); 39 | scanf("%f", &p1->salary); 40 | p1->next = NULL; 41 | if (head == NULL) 42 | head = p2 = p1; 43 | else { 44 | p2->next = p1; 45 | p2 = p1; 46 | } 47 | printf("Please input a worker's number: "); 48 | scanf("%d", &no); 49 | } 50 | return head; 51 | } 52 | 53 | 54 | void writeListR(struct person *head) 55 | { 56 | struct person *p = head; 57 | FILE *fp; 58 | if ((fp = fopen("person_list.txt", "w")) == NULL) 59 | printf("Can't open this file!\n"); 60 | else 61 | while (p != NULL) { 62 | fprintf(fp, "no = %d, salary = %0.2f\n", p->no, p->salary); 63 | p = p->next; 64 | } 65 | fclose(fp); 66 | } 67 | -------------------------------------------------------------------------------- /141228/person_list.txt: -------------------------------------------------------------------------------- 1 | no = 1, salary = 1.00 2 | no = 2, salary = 2.00 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### 2014年秋 中科大C语言课 上机练习代码 2 | 3 | **课程信息** 4 | - 授课老师:郑重 5 | - 教材: 6 | - 计算机程序设计(C语言版) 贾伯琪 顾为兵 苏仕华 张四海 何克东@编著 (课本) 7 | - 计算机程序设计 学习指导与实践 贾伯琪@编著 (上机指导) 8 | - 开课时间:2014.9.9-2014.12.19 9 | - 上课形式:每周2次,4节C语言概念课,10.8开始每周一个晚上上机。 10 | - 作业考试:主要为上机作业,只有一次期末考是笔试(2014.12.28) 11 | 12 | 13 | **按照上机日期建立文件夹** 14 | 15 | 日期 | 题目 | 内容 16 | --------|---------------------------------|----------------------------------- 17 | 141008 | 课本84页的习题1,2,3,5 | 数据类型、算术表达式的值 18 | 141015 | 课本85页的习题4,6,9 | 逻辑表达式 19 | 141021 | 课本112页的习题2,5,7及其他 | 选择结构、循环结构,switch,for,if 20 | 141029 | 其他题目 | 冒泡排序(及改进),选择排序,矩阵相乘,矩阵转置 21 | 141105 | 上机指导109页7,8 | 字符串处理,二分法求根,最大公约数,最小公倍数,牛顿迭代法 22 | 141112 | 课本165页17,194页6 | 字符串处理,约瑟夫环,伪随机数,字符串比大小 23 | 141118 | 课本194页8,195页13,204页4 | 函数,指针,带参宏 24 | 141126 | 课本256页5,7 | 行指针,指针数组 25 | 141203 | 课本257页习题13,14,17,279页1 | 函数指针,结构体 26 | 141210 | 其他题目 | 链表的建立(头插法和尾插法),链表的插入和删除 27 | 141217 | 其他题目 | 文件的读入读出,文件的拷贝 28 | 141228 | 上机指导235 综合测试题一,四 | 字符串处理,函数,结构体数组,链表,文件 29 | 30 | 31 | **部分代码的题目说明** 32 | - 141228 33 | - **exam1_1.c** 求矩阵对角线元素的和,将矩阵转置 34 | - **exam1_2.c** 结构体数组数据的输入、输出和排序 35 | - **exam4_1.c** 求解数据的平均值、方差、均方差 36 | - **exam4_2.c** 统计给定文本中特定单词的个数 37 | - **exam4_3.c** 用尾插法建立链表,把链表的信息输出到文件中 38 | - 141217 39 | - **file1.c** 从input1.txt文件中读入数据,头插法建立链表,并输出到output1.txt文件中 40 | - **file2.c** 从键盘输入数据,尾插法建立链表,写入couple2.out,再从文件输出到显示屏 41 | - **filecopy3.c** 用带参main函数复制文件,用fgetc,fputc实现文本复制部分 42 | - **filecopy4.c** 用带参main函数复制文件,用fgets,fputs实现文本复制部分 43 | - 141210 44 | - **createcpll1.c** 头插法建立链表,从键盘读入数据,并链表的数据输出到显示屏 45 | - **createcpll2.c** 尾插法建立链表,从键盘读入数据,并链表的数据输出到显示屏 46 | - **deletell.c** 可连续删除链表中的元素,在createcpll2.c中加一个删除的函数。 47 | - **insertll.c** 向已排序好的链表中插入元素,在createcpll1.c中加入一个插入函数,并默认输入的是有序数据。 48 | - 141203 49 | - **matrix_5x5.c** 课本257/13,用指针将5x5矩阵元素按要求进行处理 50 | - **SelectSort.c** 选择排序,在matrix_5x5.c中作为 #include "SelectSort.c" 被引用 51 | - **SortStr.c** 课本257/15,用指针实现“输入5个字符串,按从小到大顺序输出” 52 | - **fun_pointer.c** 课本257/17,用指向函数的指针求几个函数的定积分 53 | - **struct_book.c** 课本279/1,声明一个图书相关的结构体,并输入输出信息 54 | - 141126 55 | - 略 56 | - 141118 57 | - **reverse_str.c** 课本194/8,函数实现字符串反序输出 58 | - **score.c** 课本195/13,不同的函数实现数据处理,如求平均数、方差 59 | - **macros_with_arguments.c** 课本204/4,定义一个带参宏,实现参数值互换 60 | - **char_pointer.c** 字符指针与行指针的学习 61 | - **int_pointer.c** 行指针 (*p)[N] 的学习 62 | - 141112 63 | - **Josephus.c** 课本165/17,约瑟夫问题:http://en.wikipedia.org/wiki/Josephus_problem 64 | - **atof.c** 课本194/6,函数实现“将数字字符串转换成等价的双精度浮点数” 65 | - **strcompare.c** 输入两个字符串,比大小。输出为"a > b"的格式 66 | - **strcompare#.c** 比较scanf,getchar,gets在输入字符串上面的不同,功能与strcompare.c相同 67 | - **pseudorandomness.c** 写一个随机数生成函数,并把生成的随机数组排序输出。 68 | - 141105 69 | - **Bisection.c** 用二分法求解方程 2x^3+4x^2+3x-6 = 0 的近似解 70 | - **Newton.c** 用牛顿法求解方程 2x^3+4x^2+3x-6 = 0 的近似解 71 | - **LCMandHCD.c** 输入两个自然数,求它们的最大公因数和最小公倍数 72 | - **WordNum.c** 输入语句,判断单词的个数:用了字符数组以及很麻烦的方法。 73 | - **WordNum#.c** 输入语句,判断单词的个数:引入ischar函数,巧妙的解法。 74 | - 141029 75 | - **BubbleSort.c** 冒泡排序 76 | - **BubbleSort#.c** 优化版的冒泡排序(指优化了一点点= =) 77 | - **SelectionSort.c** 选择排序 78 | - **BinarySearch.c** 选择排序后,用二分搜索 79 | - **MatrixMul.c** 输入两个矩阵,实现矩阵乘法,并输出结果 80 | - **Transpose.c** 输入矩阵,输出它的转置矩阵 81 | - **Transpose#.c** 输入矩阵,求出它的转置矩阵,并输出 82 | - 141021 83 | - **9x9talbe.c** 课本112/5,用for循环结构,输出九九乘法表 84 | - **calcularPI.c** 课本112/7,根据已知公式,求PI近似值 85 | - **PIT.c** 课本112/2,用switch语句,求不同情况下的个人所得税金额 86 | - **FindRoot.c** 计算一元二次方差在复数域上的根 87 | - 141015 88 | - **Fahrenheit2Celsius.c** 华氏温度到摄氏温度的转换小程序 89 | - 141008 90 | - 课本习题,略 91 | 92 | 93 | **友情链接 & 特别感谢** 94 | 课程评价和相册:http://home.ustc.edu.cn/~jenny42/c-programming.html 95 | 感谢助教赵聪:https://github.com/zhaocong89 96 | 我们可以到这里看~alkaid的代码:http://home.ustc.edu.cn/~alkaid/ 97 | 也可以看boj的代码:https://github.com/bojieli 98 | 还有cuihao的代码:http://cuihaopy.appspot.com/?p=105001 99 | 100 | 101 | **发现错误** 102 | 如果你发现任何错误,不管是代码错误、注释错误、信息错误,都欢迎告诉我 103 | 108 | --------------------------------------------------------------------------------