首页 C语言程序设计参考答案

C语言程序设计参考答案

举报
开通vip

C语言程序设计参考答案3.3 x1=-1,177777,ffff,65535 x2=-3,17775,fffd,65533 y1=123.456703, 123.457,123.457,123.457 y2=123.449997,1.2350e+02,123.45 x1(%4d)= -1 3.4 (1) %c (2)%c (3)%f (4)%f (5)%lu (6)%hd (7)%d (8)%ld (9)%f (10)%Lf 3.5 (1) % 右边的值不能为0 (2)正确 (3)正确 (4)类型不匹配 x3=%Lf (5)类型不匹配 ...

C语言程序设计参考答案
3.3 x1=-1,177777,ffff,65535 x2=-3,17775,fffd,65533 y1=123.456703, 123.457,123.457,123.457 y2=123.449997,1.2350e+02,123.45 x1(%4d)= -1 3.4 (1) %c (2)%c (3)%f (4)%f (5)%lu (6)%hd (7)%d (8)%ld (9)%f (10)%Lf 3.5 (1) % 右边的值不能为0 (2)正确 (3)正确 (4)类型不匹配 x3=%Lf (5)类型不匹配 %Lf (6)类型不匹配 %u%lf (7)正确 (8) 不能修改const对象 (9) 类型不匹配 %u,%c (10)正确 3.6 -6.700000 -6 177601 123 -2 0 3.8 #include void main() { char c; c=getchar(); if((c>='0'&&c<='9')) printf("%d\n",c-'0'); else if((c>='A'&&c<='F')) printf("%d\n",c-'A'+10); else if((c>='a'&&c<='f')) printf("%d\n",c-'a'+10); else printf(“%d\n”, c); } 程序还可以简化 3.11 #include void main(void) { unsigned short x, m,n; scanf("%hu %hu %hu",&x,&m,&n); x = x >> ( m – n + 1); x = x << (16 - n); printf("%hu\n",x); } /*4.1 编写一个程序,输入A、B、C三个学生的考试分数,输出分数居中的那个学生名(A、B或C)及考试分数。*/ #include void main(void) { float a, b, c; printf("input the scores of student A, B, C:\n"); scanf("%f %f %f", &a, &b, &c); if(a>=b && a<=c || a<=b && a>=c) printf("The middle is A = %f", a); else if(b>=a && b<=c || b<=a && b>=c) printf("The middle is B = %f", b); else printf("The middle is C = %f", c); } /*4.3 编写一个程序,输入一个日期(年、月、日),计算并输出该日期是这一年的第几天。*/ #include void main(void) { int y, m, d, days = 0; int i; printf("input a date(year month day):\n"); scanf("%d %d %d", &y, &m, &d); for(i = 1; i < m; i++) switch(i){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: days += 31; break; case 2: days += 28; break; case 4: case 6: case 9: case 11: days += 30; break; } days += d; if(m > 2 && (!(y % 4) && (y % 100) || !(y % 400))) days++; printf("the day is the %dth of the year.\n", days); } /* 4.4 */ /*用if语句实现*/ #include void main(void) { float x, tax; printf("input the wage:\n"); scanf("%f", &x); if(x < 1000) tax = 0.0; else if(x < 2000) tax = x * 0.05; else if(x < 3000) tax = x * 0.1; else if(x < 4000) tax = x * 0.15; else if(x < 5000) tax = x * 0.2; else tax = x * 0.25; printf("the tax = %f.\n", tax); } /*用switch语句实现*/ #include void main(void) { float x, tax; printf("input the wage:\n"); scanf("%f", &x); switch((int)(x/1000)){ case 0: tax = 0.0; break; case 1: tax = x * 0.05; break; case 2: tax = x * 0.1; break; case 3: tax = x * 0.15; break; case 4: tax = x * 0.20; break; default: tax = x * 0.25; } printf("the tax = %f.\n", tax); } /* 4.7 */ #include void main(void) { char c; printf("input a string end with Enter:\n"); c = getchar(); while(c != '\n'){ putchar(c); if(c == 32) while((c = getchar()) == 32) ; else c = getchar(); } } /* 4.10 */ #include #include #define EPS 1.0E-5 void main(void) { long i = 1; int flag = 4; double pi = 0.0, item; do{ item = (double)flag / (2*i-1); pi += item; i++; flag *= -1; }while(fabs(item) > EPS); printf("PI = %f", pi); } /* 4.11 */ #include void main(void) { int a, b, c, d, r, gcd, lcm; do{ printf("input two integers: a and b (a<>0 and b<>0)\n"); scanf("%d %d", &a, &b); }while(!a||!b ); /*等价于while(a == 0 || b == 0)*/ if(a < 0) c = -a; else c = a; if(b < 0) d = -b; else d = b; while(r = c % d){ c = d; d = r; } gcd = d; lcm = a * b / gcd; printf("the greatest common divisor is %d, \nthe lease common multiple is %d.\n", gcd, lcm); } /* 4.13 */ #include #include int is_prime(int); void main(void) { int i,n; printf(" 4 = 2 + 2\n"); for(i=6;i<=100;i+=2) { for( n=3; n <= i/2; n+=2) if(is_prime(n)) if(is_prime(i-n)) { printf("%4d = %2d + %2d\n",i,n,i-n); break; } if(n > i/2) printf("error %d\n",i); } } int is_prime(int i) { int j; if(i <= 1) return 0; if(i == 2) return 1; if(!(i % 2)) return 0; for(j = 3;j <= sqrt(i);j += 2) if(!(i % j)) return 0; return 1; } /* 4.17 */ #include void main(void) { int c,i; for(i=1,c=32;c<=126;++i,++c) { printf("%3d%-5c",c,c); if(!(i%8)) printf("\n"); } } 5.3 函数原型的作用:函数原型告诉编译器函数返回值的数据类型,函数参数个数,每个参数的类型,参数的顺序和函数参数压栈方式。编译器使用函数原型校验函数的调用,从而避免因错误的函数调用所导致的致命运行错误或微妙而难以检测的逻辑错误。 什么情况下必须做函数原型说明:如果函数定义出现在函数调用后,或者被调用函数在其他文件中定义,则必须在函数调用之前给出其函数原型。 5.4 (1) (5) 为 标准 excel标准偏差excel标准偏差函数exl标准差函数国标检验抽样标准表免费下载红头文件格式标准下载 C语言的函数原型 5.5 extern和static存储类型的区别:static型外部变量和extern型外部变量的唯一区别是作用域的限制。静态外部变量只能作用于定义它的文件,其他文件中的函数不能使用。extern型外部变量的作用域可以扩大到整个程序的所有文件。 static和auto存储类型的区别:静态局部变量和自动变量有根本性的区别。由于静态局部变量在程序执行期间不会消失,因此,它的值有连续性。当退出块时,它的值能保存下来,以便再次进入块时使用,而自动变量的值在退出块时都丢失了。如果定义时静态局部变量有显示初始化,只在第一次进入时执行一次赋初值操作,而自动变量每次进入时都要执行赋初值操作。 5.7 第一次调用:6 第二次调用:12 5.10 #include long factorial(int); void main(void) { int i, n; double s; do{ printf("input n:(n>0)"); scanf("%d", &n); }while(n <= 0); for(i = 1, s = 0.0; i <= n; i++) s += 1.0 / factorial(i); printf("s = %f\n", s); } long factorial(int n) { long result = 1; while(n) result *= n--; return result; } 5.11 #include void hailstone(int); void main(void) { int n; do{ printf("input n:(n>0)"); scanf("%d", &n); }while(n <= 0); hailstone(n); } void hailstone(int n) { int i = 1; printf("Hailstone generated by %d:\n",n); printf("%d\t",n); while(n != 1){ if(n % 2) /*n为奇数*/ n = 3 * n + 1; else n = n / 2; /*n为偶数*/ printf("%d\t",n); if(++i % 6 == 0) printf("\n"); } printf("\nNumber of hailstone generated:%d\n", i); } 5.13 #include #include int digit(long, int); void main(void) { long n; int k; printf("Input n,k:(k>0)"); scanf("%ld %d", &n, &k); if(k <= 0){ printf("k must be greater than 0!\n"); return ; } printf("digit(%ld,%d)=%d\n", n, k, digit(n,k)); } int digit(long n, int k) { int i = 0, m; if(n < 0) n = -n; do{ m = n % 10; n /= 10; i++; }while(n && i< k); if(i == k) return m; else return -1; } 5.14 /*********************** primes.h ********************************/ #define BEGIN 10 #define END 20 extern int isprime(int n); /*********************** main.c ********************************/ #include #include "primes.h" void main(void) { int n, i=0; printf("GOLDBACH'S CONJECTURE:\n"); printf("every even number n>=4 is the sum of two primes.\n"); n = BEGIN % 2 == 0? BEGIN: BEGIN + 1; while(n <= END) { for(i = 2;i <= n/2; i++){ if(isprime(i) && isprime(n-i)){ printf("%d=%d+%d\n", n, i, n - i); break; } } n += 2; } } /************************ isprime.c **************************/ #include int isprime(int n) { int i, m = 1; for( i = 2;i <= (int)sqrt(n+1); i++) if(!(m = n % i)) break; return m; } 5.15 #include int gcd1(int, int); int gcd2(int, int); void main(void) { int a, b; do{ printf("Input two positive numbers:"); scanf("%d %d",&a,&b); }while(a <= 0 || b <= 0); printf("The greatest common divisor is %d\n", gcd1(a,b));/*将gcd1(a,b)换为gcd2(a,b)来测试迭代算法*/ } /*递归*/ int gcd1(int m, int n) { if(n == 0) return m; else return gcd1(n, m % n); } /*迭代*/ int gcd2(int m, int n) { int r; while(n){ r = m % n; m = n; n = r; } return m; } 5.17 #include void ni(void); void main(void) { printf("input a row of characters:\n"); ni(); } void ni(void) { char c; c=getchar(); if(c!='\n'){ ni(); putchar(c); } } 6.2 b+a 6.3 for(i=0;i #include #include #define g 9.8 void main(void) { int gdriver = DETECT, gmode, i, size; long ck, ck1, ck2; initgraph(&gdriver,&gmode,"d:\\tc"); /*初始化图形模式,第3个参数是图形驱动程序所在目录,视实际情况而定*/ setbkcolor(BLUE); /*设置屏幕背景色为蓝色*/ setcolor(RED); /*设置绘图前景色为红色*/ printf("Press Enter to star:\n"); /*提示,按回车键开始自由落体*/ setlinestyle(0,0,1); /*设置绘图所用线型和线宽:实线,一个像素宽*/ setfillstyle(1,4); /*设置填充方式和颜色:全部填充,红色*/ fillellipse(200,50,5,5); /*以200,50为圆心画半径为5的圆,同时以设定的填充方式和颜色填充该封闭区*/ getchar(); /*使程序停下来,等待用户键盘输入,按回车键继续执行*/ ck1 = clock(); /*计时,获取程序从开始运行到此条语句执行所用的时间,单位是毫秒*/ for(ck = 1; 55 + g * ck * ck / 2 / 10 <= 450; ck++){ /*当球的下端越过纵坐标450时,终止循环*/ while((ck2 = clock())==ck1); /*等待,确保从上次计时开始时间过去1毫秒*/ ck1 = ck2; /*更新计时器*/ setcolor(BLUE); /*设置绘图前景色为蓝色*/ setfillstyle(1,1); /*设置填充方式和颜色:全部填充,蓝色*/ fillellipse(200,50+g*(ck-1)*(ck-1)/2/10,5,5); /*用蓝色(背景色)在球的上一位置重画圆,并用蓝色(背景色)填充,相当于抹去了在上一位置所画的圆*/ setcolor(RED); /*设置绘图前景色为红色*/ setfillstyle(1,4); /*设置填充方式和颜色:全部填充,红色*/ fillellipse(200,50+g*ck*ck/2/10,5,5); /*用红色在球的初始位置下方g*ck*ck/2/10的位置重画圆,并用红色填充,*/ } getchar(); closegraph(); } 7.11 #include #include #include void Randomize(void); int RandomInteger(int,int); void main(void) { int i, j, k; Randomize(); for(i=0,j=0;j < 3; i++){ k = RandomInteger(1,2); if(k == 1){ j++; printf("heads\n"); } else{ j = 0; printf("tails\n"); } } printf("It took %d flips to get heads 3 consecutive times\n", i); } void Randomize(void) { srand(time(NULL)); } int RandomInteger(int low,int high) { int k; double d; d=(double)rand()/((double) RAND_MAX+1); k = (int)(d*(high-low+ 1)); return(low+k); } 7.16 /*caltools.h*/ #ifndef _caltools_h #define _caltools_h char *MonthName(int month); int MonthDays(int month,int year); int FirstDayOfMonth(int month,int year); int IsLeapYear(int year); #endif /*caltools.c*/ #include "caltools.h" char * MonthName(int month) { switch(month){ case 1: return("January"); case 2: return("February"); case 3: return("March"); case 4: return("April"); case 5: return("May"); case 6: return("June"); case 7: return("July"); case 8: return("August"); case 9: return("September"); case 10: return("October"); case 11: return("November"); case 12: return("December"); } } int MonthDays(int month,int year) { switch(month){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: return(31); case 4: case 6: case 9: case 11: return(30); case 2: return (IsLeapYear(year)?29:28); } } int FirstDayOfMonth(int month,int year) { int c, d, m, w, y; c = year / 100;/*年份的百位和千位*/ y = year % 100;/*年份的后两位*/ d = 1;/*该月的第一天*/ if (month < 3)/*如果月份为1、2月,则当作上年的13、14月来处理*/ m = 12 + month; else m = month; w = ((y + y/4 + c/4 - 2*c + 26*(m+1)/10 + d - 1) % 7 + 7) % 7;/*蔡勒(Zeller) 公式 小学单位换算公式大全免费下载公式下载行测公式大全下载excel公式下载逻辑回归公式下载 */ return w; } int IsLeapYear(int year) { return ( !(year%4)&&(year%100) || !(year%400) ); } /*calendar.c*/ #include #include "caltools.h" void main(void) { int month, year; do{ printf("Input the month and year(month>0 and month<13):\n"); scanf("%d %d" ,&month,&year); }while(month < 1 || month > 12); printf("The name of the month is %s.\n", MonthName(month)); printf("There are %d days in this month.\n", MonthDays(month,year)); printf("The first day of the month in this year is "); switch(FirstDayOfMonth(month,year)){ case 0: printf("SUNDAY.\n");break; case 1: printf("MONDAY.\n");break; case 2: printf("TUESDAY.\n");break; case 3: printf("WEDNESDAY.\n");break; case 4: printf("THURSDAY.\n");break; case 5: printf("FRIDAY.\n");break; case 6: printf("SATURDAY.\n");break; } printf("This year %s a leap year.\n", IsLeapYear(year)?"is":"isn't"); } 9.2 #include void ShowIntBit(int); char IntToChar(int); void main(void) { int x; printf("Please input an interger:"); scanf("%d", &x); ShowIntBit(x); getch(); } /* * 函数名: ShowIntBit * 功能 : 将整数从高字节到低字节,输出每四个bit中的数值 * 参数: 输入的整型数据 * 返回值: 无 */ void ShowIntBit(int x) { int i=0; char *p = (char*)&x; /*初始化p使指向变量x的第1个字节*/ p++; /*使p指向变量x的第2个字节*/ for(i=0;i<2;i++){ /*用循环依次输出x两个字节中的高4位和低4位*/ printf("%c ", IntToChar(*(p - i) >> 4 & 0x0F)); /*输出高4位对应的16进制码值*/ printf("%c ", IntToChar(*(p - i) & 0x0F)); /*输出低4位对应的16进制码值*/ } } /* * 函数名: IntToChar * 功能 : 将 0 ~15 转化成对应的十六进制数字字符’0’~’9’, ‘A’~’F’ * 参数: 输入的整型数据 (0~15) * 返回值: 返回对应的16进制数数字字符 */ char IntToChar(int i) { i &= 0x0f; /*取低4位*/ if( i>=0 && i<=9) return i + '0'; /*将1~9的数值转换为'0'~'9'的数字字符*/ else return i - 10 + 'A'; /*将10~15的数值转换为'A'~'F'的数字字符*/ } 9.5 #include #define N 9 void reverse(int *, int); /*对数组元素进行颠倒处理的函数原型声明*/ void printArray(int *, int); /*输出数组元素值的函数原型声明*/ void main() { int i, array[N]; printf("Please input %d integers:\n", N); for(i=0; i < N; i++) scanf("%d", array+i); reverse(array, N); printArray(array, N); } void reverse(int *a, int size) /*形参说明int *a等价于int a[]*/ { int *p, *q, temp; for(p=a, q=a+size-1; p < q; p++, q--){ /*使p、q分别指向数组的第1个和最后一个元素*/ temp = *p; /*交换p、q所指向元素的值*/ *p = *q; /*循环体执行一次后,使p指向后一个元素,并使q指向前一个元素*/ *q = temp; /*当p和q的指向重合或交错后,结束循环,完成了元素位置颠倒*/ } } void printArray(int *a, int size) { int i; for(i=0; i 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 达式*(a + i)等价于a[i] */ } 9.9 #include void transpose(int (*)[5], int (*)[5], int); /*转置函数原型声明*/ void main() { int a[5][5], b[5][5], i, j; /*二维数组a、b分别存放原矩阵和转置后矩阵*/ printf("please input 5*5 matrix:\n"); for(i=0; i < 5; i++) /*用二重循环来输入原矩阵数据值*/ for(j=0; j < 5; j++) scanf("%d", *(a+i)+j); /* 表达式*(a+i)+j等价于&a[i][j] */ transpose(a, b, 5); printf("after transposed:\n"); for(i=0; i < 5; i++){ for(j=0; j < 5; j++) printf("%6d", *(*(b+i)+j)); /* 表达式*(*(b+i)+j)等价于b[i][j] */ printf("\n"); } } void transpose(int (*x)[5], int (*y)[5], int n) /*形参说明int (*x)[5]等价于int x[][5],y类同*/ { int i, j; for(i=0; i < n; i++) for(j=0; j < 5; j++) *(*(y+i)+j) = *(*(x+j)+i); /*转置处理,将x[j][i]赋值给y[i][j]*/ } 9.15 #include #include void main(int argc, char *argv[]) { int i, x, max, min; long sum = 0; float avg; if(argc < 2) { printf("No digital string involved in command line.\n"); return ; } for(i=1; i < argc; i++){ /*用循环处理命令行中的argc-1个数字字符串*/ x = atoi(argv[i]); /*将第i个数字字符串转换为整数并赋值给x*/ sum += x; /* 累加求和 */ if( i == 1){ /* 假定转换的第一个数是最大的也是最小的,*/ max = min = x; continue; } if(x > max) /*用“打擂台”算法来求最大数和最小数*/ max = x; else min = x; } avg = sum/(argc - 1.0); /*求平均值*/ printf("sum = %ld\taverage = %f\tmax = %d\tmin = %d\n", sum, avg, max, min); } 9.10 /* 函数名:strcat 功 能:将一个字符串b连接到另一个字符串a的串尾 参 数:char *t:字符串a的首地址; char *s:字符串b的首地址 返回值:无 备 注:字符指针t所指向字符串的串尾必须有足够的可用空间以容纳字符指针s所指向的字符串 */ void strcat(char *t, char *s) { while(*t) t++; /*使t指向它所指字符串的串尾*/ while (*t++ = *s++); /*将s所指向的字符串复制到t所指向的串尾*/ } 9.13 b[i][j][k]的指针表示:*(*(*(b + i) + j) + k) u[i][j][k][l][m][n]的指针表示:*(*(*(*(*(*(u + i) + j) + k) + l) + m) + n) 9.18 (1) char *(*p[3])(void); (2) double (*p[9])(int); (3) char (*(*p)(void))[3]; (4) int (*(*p[3])(void))[4]; 9.20 区别在于: 用#define定义的STRING,STRING a, b; 等价于 char * a, b; ,该声明语句声明了两个变量a和b,类型分别为char *和char。 用typedef定义的STRING,STRING a, b; 等价于 char * a, *b; ,该声明语句同样声明了两个变量a和b,但a和b的类型相同,都是char *。 /*ex10_1.c */ #include #include struct point{ float x,y,z; }; void main(void) { struct point start, end; float px, py, pz, len; printf("input the coordinates of the start point:(x y z)\n"); scanf("%f %f %f", &px, &py, &pz); start.x = px; start.y = py; start.z = pz; printf("input the coordinates of the end point:(x y z)\n"); scanf("%f %f %f", &px, &py, &pz); end.x = px; end.y = py; end.z = pz; px = end.x - start.x; py = end.y - start.y; pz = end.z - start.z; len = sqrt(px*px + py*py + pz*pz); printf("length = %f\n", len); } /*ex10_2.c */ #include struct complex{ float a, b; }; struct complex add_complex(struct complex x, struct complex y) { struct complex result; result.a = x.a + y.a; result.b = x.b + y.b; return result; } struct complex sub_complex(struct complex x, struct complex y) { struct complex result; result.a = x.a - y.a; result.b = x.b - y.b; return result; } struct complex mul_complex(struct complex x, struct complex y) { struct complex result; result.a = x.a * y.a - x.b * y.b; result.b = x.a * y.b + x.b * y.a; return result; } struct complex div_complex(struct complex x, struct complex y) { struct complex result; result.a = (x.a * y.a + x.b * y.b) / (y.a * y.a + y.b * y.b); result.b = (x.b * y.a - x.a * y.b) / (y.a * y.a + y.b * y.b); return result; } /*ex10_3.c */ #include struct date{ int y, m, d; }; void main(void) { struct date day; int days = 0, i; printf("input a date:(year month day)\n"); scanf("%d %d %d", &day.y, &day.m, &day.d); for(i = 0;i < day.m; i++) switch(i){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: days += 31; break; case 4: case 6: case 9: case 11: days += 30; break; case 2: days += 28; break; } days += day.d; if(day.m > 2 && (!day.y % 4 && day.y % 100 || !day.y % 400)) days++; printf("the day is %dth of the year.\n", days); } /*ex10_4.c */ #include #include struct point{ float x,y,z; }; float len(struct point start, struct point end) { float px, py, pz; px = end.x - start.x; py = end.y - start.y; pz = end.z - start.z; return sqrt(px*px + py*py + pz*pz); } /*ex10_5.c */ #include #include struct point{ float x,y,z; }; float len(struct point *start, struct point *end) { float px, py, pz; px = end->x - start->x; py = end->y - start->y; pz = end->z - start->z; return sqrt(px*px + py*py + pz*pz); } /*ex10_7.c */ #include struct point{ float x,y,z; }; struct line{ struct point start, end; }; struct line move(struct line l, int x, int y) { l.start.x += x; l.start.y += y; l.end.x += x; l.end.y += y; return l; } 程序的改进 typedef struct tagPoint{     float x,y,z; }POINT,*PPOINT;   程序中可以直接使用如POINT start, end; 代替 struct point start,end; PPOINT pStart,pEnd 代替 struct point *pStart,*pEnd; 10.8:(1)0 (2)4 (3)3 10.9:(1)2 (2)1 (3)‘t’ (4)‘w’ (5)2 10.10 #include #include #include #define N 5 #define FLASE 0 #define TURE 1 struct student{ char num[10]; char name[15]; float score[3]; float avg_score; }; void Input(struct student stu[], int); void Output(struct student stu[], int, int); void Modefy(struct student stu[], int); void Average(struct student stu[], int); void Bubble_sort(struct student stu[], int); void main() { struct student stu[N]; Input(stu, N); Output(stu, N, 0); Modefy(stu, N); Average(stu, N); Bubble_sort(stu, N); Output(stu, N, 1); } void Input(struct student stu[], int n) { int i; float f1, f2, f3; for(i=0; i < n; i++){ printf("input the num, name, score1, score2, score3 of the %dth student:\n", i); scanf("%s %s %f %f %f", stu[i].num, stu[i].name, &f1, &f2, &f3); stu[i].score[0] = f1; stu[i].score[1] = f2; stu[i].score[2] = f3; } } void Output(struct student stu[], int n, int k) { int i; printf("num\tname\tscore1\tscore2\tscore3"); if(k) printf("\taverage\n"); else printf("\n"); for(i=0; i < n; i++){ printf("%s\t%s\t%.1f\t%.1f\t%.1f", stu[i].num, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2]); if(k) printf("\t%.2f\n", stu[i].avg_score); else printf("\n"); } } void Modefy(struct student stu[], int n) { int i, m; char ch, num[10]; float f; while(1){ printf("input the num of the student whose information you want to modify:\n"); scanf("%s", &num); for(i=0;i < n; i++) if(strcmp(stu[i].num, num) == 0) break; if(i == n) continue; Output(stu+i, 1, 0); printf("1. modify the num.\n"); printf("2. modify the name.\n"); printf("3. modify the score1.\n"); printf("4. modify the score2.\n"); printf("5. modify the score3.\n"); printf("input 1-5.\n"); scanf("%d", &m); switch(m){ case 1: printf("input the num:\n"); scanf("%s", stu[i].num); break; case 2: printf("input the name:\n"); scanf("%s", stu[i].name); break; case 3: printf("input the score1:\n"); scanf("%f", &f); stu[i].score[0] = f; break; case 4: printf("input the score2:\n"); scanf("%f", &f); stu[i].score[1] = f; break; case 5: printf("input the score3:\n"); scanf("%f", &f); stu[i].score[2] = f; break; } Output(stu+i, 1, 0); gets(num); printf("continue?(Y for yes, N for no):"); ch = getchar(); if(ch != 'Y' && ch != 'y') break; } } void Average(struct student stu[], int n) { int i; for(i=0; i < n; i++) stu[i].avg_score = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3; } void Bubble_sort(struct student stu[], int n) { int i,j; struct student temp; for(i=0;i < n - 1; i++) for(j=0; j < n - i - 1; j++) if(stu[j].avg_score < stu[j+1].avg_score){ temp = stu[j]; stu[j] = stu[j+1]; stu[j+1] = temp; } } 10.13 #include #include struct node { char a; struct node *next; }; void main() { struct node *head = NULL, *tail = NULL, *p; char c; printf("input a row of characters:\n"); if((c = getchar()) != '\n'){ head = (struct node*)malloc(sizeof(struct node)); head->a = c; tail = head; while((c = getchar()) != '\n'){ tail->next = (struct node*)malloc(sizeof(struct node)); tail = tail->next; tail->a = c; } tail->next = NULL; p = head; while(p != NULL){ printf("%c", p->a); p = p->next; } putchar('\n'); } } 10.14 #include #include struct node{ char a; struct node *next; }; void main() { struct node *head = NULL, *p; char c; printf("input a row of characters:\n"); while((c = getchar()) != '\n'){ p = (struct node*)malloc(sizeof(struct node)); p->a = c; p->next = head; h
本文档为【C语言程序设计参考答案】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_778894
暂无简介~
格式:doc
大小:140KB
软件:Word
页数:31
分类:工学
上传时间:2011-07-03
浏览量:156