实验一
实验任务1
源代码task1_1.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 printf(" O \n"); 6 printf("<H>\n"); 7 printf("I I\n"); 8 9 system("pause"); 10 return 0; 11 }
运行结果截图

源代码task1_2.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 printf(" O O \n"); 6 printf("<H> <H>\n"); 7 printf("I I I I\n"); 8 9 system("pause"); 10 return 0; 11 }
运行结果截图

实验任务2
源代码task2.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 double a, b, c; 6 scanf("%lf%lf%lf", &a, &b, &c); 7 8 if(a + b > c && a + c > b && b + c > a ) 9 printf("能构成三角形\n"); 10 else 11 printf("不能构成三角形\n"); 12 system("pause"); 13 return 0; 14 }
运行结果截图



实验任务3
源代码task3.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 char ans1, ans2; 6 7 printf("每次课前认真预习、课后及时复习了没? (输入y或Y表示有,输入n或N表示没有) : "); 8 ans1 = getchar(); 9 10 getchar(); 11 12 printf("\n动手敲代码实践了没? (输入y或Y表示敲了,输入n或N表示木有敲) : "); 13 ans2 = getchar(); 14 15 if ((ans1 == 'y' || ans1 == 'Y') && (ans2 == 'y' || ans2 == 'Y')) 16 printf("\n罗马不是一天建成的, 继续保持哦:)\n"); 17 else 18 printf("\n罗马不是一天毁灭的, 我们来建设吧\n"); 19 system("pause"); 20 return 0; 21 }
运行结果截图




回答问题
结果:第二个问题的输入被跳过,ans2被赋值为换行符\n,程序直接执行后续判断。
原因:getchar()是为了清空输入缓冲区中残留的换行符,避免干扰下一次字符输入。
实验任务4
源代码task4.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 double x, y; 6 char c1, c2, c3; 7 int a1, a2, a3; 8 9 scanf("%d%d%d", &a1, &a2, &a3); // 在a之前加上& 10 printf("a1 = %d, a2 = %d, a3 = %d\n", a1, a2, a3); 11 12 scanf(" %c%c%c", &c1, &c2, &c3); // 在%c%c%c前加上空格 13 printf("c1 = %c, c2 = %c, c3 = %c\n", c1, c2, c3); 14 15 scanf("%lf%lf", &x, &y); //%f变成%lf,并去掉逗号 16 printf("x = %f, y = %lf\n",x, y); 17 system("pause"); 18 return 0; 19 }
运行结果截图

实验任务5
源代码task5.c
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main() 4 { 5 int year; 6 double seconds_per_year = 365.0 * 24 *3600; 7 year = (int)(1e9 / seconds_per_year + 0.5); 8 printf("10亿秒约等于%d年\n", year); 9 10 system("pause"); 11 return 0; 12 }
运行结果截图

实验任务6
源代码task6_1.c
1 #include <stdio.h> 2 #include <math.h> 3 #include<stdlib.h> 4 int main() 5 { 6 double x, ans; 7 8 scanf("%lf", &x); 9 ans = pow(x, 365); 10 printf("%.2f的365次方: %.2f\n", x, ans); 11 system("pause"); 12 return 0; 13 }
运行结果截图



源代码task6_2.c
1 #include <stdio.h> 2 #include <math.h> 3 #include<stdlib.h> 4 int main() 5 { 6 double x, ans; 7 while(scanf("%lf", &x) != EOF) 8 { 9 ans = pow(x, 365); 10 printf("%.2f的365次方: %.2f\n", x, ans); 11 printf("\n"); 12 } 13 system("pause"); 14 return 0; 15 }
运行结果截图



实验任务7
源代码task7.c
1 #include <stdio.h> 2 #include<stdlib.h> 3 int main() 4 { 5 double C,F; 6 printf("请输入摄氏温度C:\n"); 7 while(scanf("%lf", &C) != EOF) 8 { 9 F = 9.0 / 5 * C + 32; 10 printf("摄氏温度%.2f℃转化为华氏温度为: %.2f℉\n", C, F); 11 printf("请输入摄氏温度C; \n"); 12 } 13 system("pause"); 14 return 0; 15 }

实验任务8
源代码task8.c
1 #include <stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 int main() 5 { 6 double a,b,c,s,area; 7 printf("请输入三角形的三边:\n"); 8 while(scanf("%lf %lf %lf", &a, &b, &c) != EOF) 9 { 10 if (a == 0 && b == 0 && c == 0){ 11 break; 12 } 13 s = (a + b + c) / 2; 14 area = sqrt(s * (s - a) * (s - b) * (s - c)); 15 printf("a = %.0f, b = %.0f, c = %.0f, area = %.3f\n", a, b, c, area); 16 printf("请输入三角形的三边; \n"); 17 } 18 19 printf("程序结束\n"); 20 system("pause"); 21 return 0; 22 }
运行结果截图

浙公网安备 33010602011771号