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

Task1_2程序
1 #include<stdlib.h> 2 #include<stdio.h> 3 int main() 4 { 5 printf(" O "); 6 printf(" O\n"); 7 printf("<H> "); 8 printf("<H>\n"); 9 printf("I I "); 10 printf("I I\n"); 11 12 system("pause"); 13 return 0; 14 }
运行结果

2.实验任务2
Task2程序
1 #include<stdlib.h> 2 #include<stdio.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 13 system("pause"); 14 return 0; 15 }
运行结果



3.实验任务3
Task3程序
1 #include<stdlib.h> 2 #include<stdio.h> 3 int main(){ 4 char ans1,ans2; 5 6 printf("每次课前认真预习、课后及时复习了没? (输入y或Y表示有,输入n或N表示没有) :"); 7 ans1=getchar(); 8 9 getchar(); 10 11 printf("\n动手敲代码实践了没? (输入y或Y表示敲了,输入n或N表示木有敲) : "); 12 ans2 = getchar(); 13 14 if ((ans1=='y' && ans2=='y')||(ans1=='Y' && ans2=='Y')) 15 printf("\n罗马不是一天建成的, 继续保持哦:)\n"); 16 else 17 printf("\n罗马不是一天毁灭的, 我们来建设吧\n"); 18 19 20 21 system("pause"); 22 return 0; 23 }
答:如果去掉第九行,ans2就会被赋值为换行符;第九行的作用就是处理换行符,保证后面的ans2 = getchar()可以顺利输入。
运行结果



4.实验任务4
Task4程序
1 #include<stdlib.h> 2 #include<stdio.h> 3 4 int main() 5 { 6 double x, y; 7 char c1, c2, c3; 8 int a1, a2, a3; 9 10 scanf("%d%d%d", &a1, &a2, &a3);//缺少"&" 11 printf("a1 = %d, a2 = %d, a3 = %d\n", a1, a2, a3); 12 scanf("%c%c%c", &c1, &c2, &c3); 13 printf("c1 = %c, c2 = %c, c3 = %c\n", c1, c2, c3); 14 scanf("%lf,%lf", &x, &y);//"f"改为"lf",输入的时候加逗号 15 printf("x = %f, y = %lf\n",x, y); 16 17 system("pause"); 18 19 return 0; 20 }
运行结果

5.实验任务5
Task5程序
1 #include<stdlib.h> 2 #include<stdio.h> 3 int main() 4 { 5 int year; 6 year=1000000000/60/60/24/365; 7 8 printf("10亿秒约等于%d年\n", year); 9 10 system("pause"); 11 return 0; 12 }
运行结果

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



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

7.实验任务7
Task7程序
1 #include<stdlib.h> 2 #include <stdio.h> 3 #include <math.h> 4 int main() 5 { 6 double c, f; 7 8 while(scanf("%lf", &c) != EOF) 9 { 10 f = c*9/5+32; 11 printf("当摄氏度c = %.2f时,华氏度f = %.2f\n",c,f); 12 printf("\n"); 13 } 14 15 system("pause"); 16 17 return 0; 18 }
运行结果

8.实验任务8
Task8程序
1 #include<stdlib.h> 2 #include <stdio.h> 3 #include <math.h> 4 int main() 5 { 6 double a,b,c,s,area; 7 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=%.0f,b=%.0f,c=%.0f,area=%.3f\n",a,b,c,area); 13 printf("\n"); 14 } 15 16 system("pause"); 17 18 return 0; 19 }
运行结果

浙公网安备 33010602011771号