实验3 C语言函数应用编程
四.实验结论
任务1
源代码
1 #include <stdio.h> 2 #include<stdlib.h> 3 4 char score_to_grade(int score); 5 6 int main(){ 7 int score; 8 char grade; 9 10 while(scanf("%d", &score) != EOF) { 11 grade = score_to_grade(score); 12 printf("分数:%d, 等级:%c\n\n", score, grade); 13 } 14 system("pause"); 15 return 0; 16 } 17 18 19 char score_to_grade(int score) { 20 char ans; 21 22 switch(score/10) { 23 case 10: 24 case 9: ans = 'A'; break; 25 case 8: ans = 'B'; break; 26 case 7: ans = 'C'; break; 27 case 6: ans = 'D'; break; 28 default: ans = 'E'; 29 } 30 31 return ans; 32 }
运行截图

回答问题
问题1:其功能为将输入的分数转化为对应的等级;形参类型为 int ;返回值类型为: char 。
问题2:存在的问题有1.缺少break 语句,会导致ans最终都被赋为E;2.赋值类型不对,"A"是字符串常量,不能直接赋到ans中。
任务2
源代码
1 #include <stdio.h> 2 #include<stdlib.h> 3 4 int sum_digits(int n); 5 int main(){ 6 int n; 7 int ans; 8 9 while(printf("Enter n: "), scanf("%d", &n) != EOF) { 10 ans = sum_digits(n); 11 printf("n = %d, ans = %d\n\n", n, ans); 12 } 13 system("pause"); 14 return 0; 15 } 16 17 int sum_digits(int n) { 18 int ans = 0; 19 20 while(n != 0) { 21 ans += n % 10; 22 n /= 10; 23 } 24 25 return ans; 26 }
运行截图

回答问题
问题1:其功能是求一个整数所有位数上的数字的和。
问题2:能实现同样的输出;二者的区别是:循环是从底向上的剥离每个位上的数字,递归是从上而下的逐级分解问题再求和。
任务3
源代码
1 #include <stdio.h> 2 #include<stdlib.h> 3 4 int power (int x, int n); 5 int main(){ 6 int x, n; 7 int ans; 8 9 while(printf("Enter x and n: "), scanf("%d%d", &x, &n) != EOF) { 10 ans = power(x, n); 11 printf("n = %d, ans = %d\n\n", n, ans); 12 } 13 system("pause"); 14 return 0; 15 } 16 17 int power(int x, int n) { 18 int t; 19 20 if(n == 0) 21 return 1; 22 else if(n % 2) 23 return x * power(x, n-1); 24 else { 25 t = power(x, n/2); 26 return t * t; 27 } 28 }
运行截图

回答问题
问题1:其功能是计算x的n次幂。
问题2:这是递归函数。递归模式如下:

任务4
源代码
1 #include <stdio.h> 2 #include<stdlib.h> 3 4 int classify_triangle(int a, int b, int c); 5 int main(){ 6 int a, b, c, i; 7 while (scanf("%d%d%d", &a, &b, &c) != EOF){ 8 i = classify_triangle(a, b, c); 9 if (i==0) 10 printf("不能构成三角形\n"); 11 else if (i==1) 12 printf("普通三角形\n"); 13 else if (i==2) 14 printf("等边三角形\n"); 15 else if (i==3) 16 printf("等腰三角形\n"); 17 else if (i==4) 18 printf("直角三角形\n");} 19 system("pause"); 20 return 0; 21 } 22 23 24 int classify_triangle(int a, int b, int c){ 25 if ( !(a+b>c && a+c>b && b+c>a) ) 26 return 0; 27 28 if ( a==b && b==c) 29 return 2; 30 if (a==b || a==c || b==c) 31 return 3; 32 if ( a*a+b*b==c*c || a*a+c*c==b*b || b*b+c*c==a*a ) 33 return 4; 34 35 36 return 1; 37 }
运行截图
任务5
源代码
源代码1:
1 #include <stdio.h> 2 #include<stdlib.h> 3 4 int func(int n, int m); 5 int main(){ 6 int n, m; 7 int ans; 8 9 while( scanf("%d%d", &n, &m) != EOF) { 10 ans = func(n, m); 11 printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); 12 } 13 system("pause"); 14 return 0; 15 } 16 17 int func(int n, int m) { 18 int i, j, k, a=1, b=1, c=1, ans; 19 if(n<m) 20 return 0; 21 else if(n==m) 22 return 1; 23 else { 24 25 for(i=1; i<=m; ++i) 26 a = a * i; 27 for(j=1; j<=n; ++j) 28 b = b * j; 29 for(k=1; k<=(n-m); ++k) 30 c = c * k; 31 ans = b / (a * c); 32 return ans;} 33 }
源代码2:
1 #include <stdio.h> 2 #include<stdlib.h> 3 4 int func(int n, int m); 5 int main(){ 6 int n, m; 7 int ans; 8 9 while( scanf("%d%d", &n, &m) != EOF) { 10 ans = func(n, m); 11 printf("n = %d, m = %d, ans = %d\n\n", n, m, ans); 12 } 13 system("pause"); 14 return 0; 15 } 16 17 int func(int n, int m) { 18 if(n<m) 19 return 0; 20 if(m==0 || n==m) 21 return 1; 22 return func(n-1, m) + func(n-1, m-1); 23 24 }
运行截图
源代码1的运行截图:

源代码2的运行截图:
任务6
源代码
1 #include <stdio.h> 2 #include<stdlib.h> 3 4 int gcd(int a, int b, int c); 5 int main(){ 6 int a, b, c; 7 int ans; 8 9 while( scanf("%d%d%d", &a, &b, &c) != EOF) { 10 ans = gcd(a, b, c); 11 printf("最大公约数: %d\n\n", ans); 12 } 13 system("pause"); 14 return 0; 15 } 16 17 int gcd(int a, int b, int c) { 18 while(b != 0) { 19 int x = b; 20 b = a % b; 21 a = x;} 22 while(c != 0) { 23 int y = c; 24 c = a % c; 25 a = y;} 26 return a; 27 }
运行截图
任务7
源代码
1 #include <stdio.h> 2 #include<stdlib.h> 3 4 void print_charman(int n); 5 int main(){ 6 int n; 7 printf("Enter n: "); 8 scanf("%d", &n); 9 print_charman(n); 10 11 system("pause"); 12 return 0; 13 } 14 15 void print_charman(int n) { 16 int i, j; 17 for(i=1; i<=n; ++i) { 18 int x = 2 * n -2 * i + 1; 19 int y = i - 1; 20 for(j = 0; j < y; ++j) { 21 printf("\t");} 22 for(j = 0; j < x; ++j) { 23 printf(" O\t");} 24 printf("\n"); 25 for(j = 0; j < y; ++j) { 26 printf("\t");} 27 for(j = 0; j < x; ++j) { 28 printf("<H>\t");} 29 printf("\n"); 30 for(j = 0; j < y; ++j) { 31 printf("\t");} 32 for(j = 0; j < x; ++j) { 33 printf("I I\t");} 34 35 printf("\n"); 36 } 37 }
运行截图

五.实验总结
在编写函数时,我更喜欢用if else语句,这在递归函数的编写中会导致代码冗长,应在保证对的情况下适当简洁。




浙公网安备 33010602011771号