实验一_C语言输入输出和简单程序应用编程实验报告

实验任务一

task1_1

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main()
 4 {
 5     printf(" 0 \n");
 6     printf("<H>\n");
 7     printf("I I\n");
 8     printf(" 0 \n");
 9     printf("<H>\n");
10     printf("I I\n");
11 
12     system("pause");
13     return 0;
14 }
View Code

 

 

task1_2

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main()
 4 {
 5     printf(" 0     O \n");
 6     printf("<H>   <H>\n");
 7     printf("I I   I I\n");
 8 
 9     system("pause");
10     return 0;
11 }
View Code

 

实验任务二

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int main()
 5 {
 6     float a,b,c;
 7     scanf("%f%f%f",&a,&b,&c);
 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 }
View Code

 

实验任务三

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int main()
 5 {
 6     char ans1,ans2;
 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 
20     system("pause");
21     return 0;
22 }
View Code

 

 

 

 A:当把line9去掉以后,程序在第二次输入后结束; 上一行代码让ans1=getchar(),line9可以让程序等待下一个输入字符。

 

实验任务四

 

 1 int main()
 2 {
 3     double x,y;
 4     char c1,c2,c3;
 5     int a1,a2,a3;
 6 
 7     scanf("%d%d%d",&a1,&a2,&a3);//error
 8     printf("a1=%d,a2=%d,a3=%d\n",a1,a2,a3);
 9 
10     scanf("%c%c%c",&c1,&c2,&c3);
11     printf("c1=%c,c2=%c,c3=%c\n",c1,c2,c3);
12 
13     scanf("%lf%lf",&x,&y);//error
14     printf("x=%lf,y=%lf",x,y);
15 
16     system("pause");
17     return 0;
18 }
View Code

 

实验任务五

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int main()
 5 {
 6     int year;
 7     double sec,trueyear;
 8 
 9     sec=1000000000;
10     trueyear=sec/3600/24/365;
11     year=trueyear+0.5;
12 
13     printf("10亿秒约等于%d年\n", year);
14     system("pause");
15     return 0;
16 }
View Code

 

 

 

实验任务六

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<math.h>
 4 
 5 int main()
 6 {
 7     double ans,x;
 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     return 0;
17 }
View Code

 

实验任务七

 

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

 

 

实验任务八

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

 

 

 

实验总结
1.在进行数据的运算时要注意定义的格式以及算式中数字的形式,避免得数不够精确,四舍五入发生错误。
2.在输入数值时,尤其是char类型的几个数据间慎用空格,避免造成识别错误。

 

posted @ 2024-03-13 20:47  InitialJ  阅读(20)  评论(0编辑  收藏  举报