实验1 C语言输入输出和简单程序编写
实验任务1
源代码1
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main(){ 4 printf(" O \n"); 5 printf("<H>\n"); 6 printf("I I\n"); 7 8 printf(" O \n"); 9 printf("<H>\n"); 10 printf("I I\n"); 11 system("pause"); 12 return 0; 13 }
运行截图1

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

实验任务2
源代码
1 // 从键盘上输入三个数据作为三角形边长,判断其能否构成三角形 2 // 构成三角形的条件: 任意两边之和大于第三边 3 4 #include <stdio.h> 5 #include<stdlib.h> 6 int main() 7 { 8 double a, b, c; 9 10 // 输入三边边长 11 scanf("%lf%lf%lf", &a, &b, &c); 12 13 // 判断能否构成三角形 14 // 补足括号里的逻辑表达式 15 if((a+b>c)&&(a+c>b)&&(b+c>a)) 16 printf("能构成三角形\n"); 17 else 18 printf("不能构成三角形\n"); 19 system("pause"); 20 return 0; 21 }
运行截图



实验任务3
源代码
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 }
运行结果




回答问题
getchar()用来获取用户敲下的Enter键,去掉后,Enter被ans2获取,导致ans2不能获取正确的值
实验任务4
源代码
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);//使用scanf必须传递变量地址 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);//double类型用lf接收 16 printf("x = %f, y = %lf\n", x, y); 17 system("pause"); 18 return 0; 19 }
运行结果

实验任务5
源代码
1 // 计算10亿秒约等于多少年,并打印输出 2 #include <stdio.h> 3 #include<stdlib.h> 4 int main() 5 { 6 int year; 7 8 year=1000000000/(365*24*60*60); 9 10 printf("10亿秒约等于%d年\n", year); 11 system("pause"); 12 return 0; 13 }
运行结果

实验任务6
源代码
1 #include <stdio.h> 2 #include <math.h> 3 4 int main() { 5 double x, ans; 6 // 将 %1f 改为 %lf,正确读取 double 类型 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 }
运行结果

实验任务7
源代码
1 #include <stdio.h> 2 #include <math.h> 3 4 int main() { 5 double C, F; 6 7 while (scanf_s("%lf", &C) != EOF) 8 { 9 F = 9 * C / 5 + 32; 10 printf("摄氏度C=%.2f时,华氏度f=%.2f\n", C, F); 11 printf("\n"); 12 } 13 return 0; 14 }
运行结果

实验任务8
源代码
1 #include <stdio.h> 2 #include <math.h> 3 #include<stdlib.h> 4 int main() { 5 int a, b, c; 6 float s,area; 7 while(scanf("%d%d%d", &a, &b, &c)!=EOF) 8 { 9 s = (a + b + c) / 2; 10 area = sqrt(s * (s - a) * (s - b) * (s - c)); 11 printf("a=%d,b=%d,c=%d,area=%.3lf",a,b,c,area); 12 system("pause"); 13 } 14 return 0; 15 }
运行结果

实验总结
感受到细节在编程中的重要性

浙公网安备 33010602011771号