实验1
任务1
1.源代码
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 system("pause"); 9 return 0; 10 }
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 system("pause"); 9 return 0; 10 }
2.运行结果


任务2
1.源代码
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 if("a+b>c &a-b<c" ) 8 printf("能构成三角形\n"); 9 else 10 printf("不能构成三角形\n"); 11 system("pause"); 12 return 0; 13 }
2.运行结果



任务3
1.源代码
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(); // 从键盘输入一个字符,赋值给ans1 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') // 待补足,判断用户回答ans1和ans2都是y(不区分大小写) 16 printf("\n罗马不是一天建成的, 继续保持哦:)\n"); 17 else 18 printf("\n罗马不是一天毁灭的, 我们来建设吧\n"); 19 system("pause"); 20 return 0; 21 }
2.运行结果




3.回答问题:罗马一定会毁灭,getchar吃掉了回车,去掉之后下面的答案就变成了回车,不等于Y或者y
任务4
1.源代码
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); 10 printf("a1 = %d, a2 = %d, a3 = %d\n", a1,a2,a3); 11 12 scanf("%c%c%c", &c1, &c2, &c3); 13 printf("c1 = %c, c2 = %c, c3 = %c\n", c1, c2, c3); 14 15 scanf("%lf%lf", &x, &y); 16 printf("x = %f, y = %lf\n",x, y); 17 system("pause"); 18 return 0; 19 }

任务5
1.源代码
1 // 计算10亿秒约等于多少年,并打印输出 2 #include <stdio.h> 3 #include<stdlib.h> 4 int main() 5 { 6 int year; 7 int second=1000000000; 8 double a; 9 int spy=365*24*60*60; 10 year=second/spy; 11 a=second%spy; 12 if(a>=0.5*spy) 13 { year++; 14 } 15 printf("10亿秒约等于%d年\n", year); 16 system("pause"); 17 return 0; 18 }

任务6
1.源代码
修改前
1 // 计算10亿秒约等于多少年,并打印输出 2 #include <stdio.h> 3 #include<math.h> 4 #include<stdlib.h> 5 int main() 6 { 7 double x,ans; 8 9 scanf("%lf",&x); 10 ans=pow(x,365); 11 printf("%.2f的365次方:%.2f\n",x,ans ); 12 system("pause"); 13 return 0; 14 15 }
修改后
1 #include <stdio.h> 2 #include<math.h> 3 int main() 4 { 5 double x,ans; 6 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 return 0; 14 15 }



任务7
1.源代码
1 #include <stdio.h> 2 #include<stdlib.h> 3 int main() 4 { 5 6 double C,F; 7 while(scanf("%lf",&C)!=EOF) 8 { 9 F=(9.0/5.0)*C+32; 10 printf("摄氏度C=%.2lf时,华氏度F=%.2lf\n",C,F); 11 } 12 system("pause"); 13 return 0; 14 15 16 }

任务8
1 #include <stdio.h> 2 #include<math.h> 3 #include<stdlib.h> 4 int main() 5 { 6 7 double a,b,c,s,area; 8 while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF) 9 { 10 s=(a+b+c)/2; 11 area=sqrt(s*(s-a)*(s-b)*(s-c)); 12 printf("a=%.1lf,b=%.1lf,c=%.1lf,area=%.3lf",a,b,c,area); 13 } 14 system("pause"); 15 return 0; 16 17 18 }

实验总结
突然冒出来个while,不清楚用法于是上网查,结果使用多出来了个“;”,程序一直没有报错,找不出来原因,还是问了豆包才明白

浙公网安备 33010602011771号