实验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     printf(" o\n");
 9     printf("<H>\n");
10     printf("I I\n");
11     system("pause");
12     return 0;
13 }
View Code

运行结果截图

BC016A25B9C7949F800F2808DD52BEC1

代码

 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 }
View Code

运行结果截图

1

任务2

代码

 

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

 运行结果截图

21

22

23

任务3

代码

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main()
 4 {
 5     char ans1,ans2,ans3;
 6     printf("每次课前认真预习、课后认真复习了没?(输入y或Y表示有,输入n或N表示没有):");
 7     ans1=getchar();
 8     getchar();
 9     printf("\n动手敲代码了没?(输入y或Y表示敲了,输入n或N表示木有敲):");
10     ans2=getchar();
11     if((ans1=='y'||ans1=='Y')&&(ans2=='y'||ans2=='Y'))
12         printf("\n罗马不是一天建成的,继续保持努力吧\n");
13     else
14         printf("\n罗马不是一天毁灭的,我们来建设吧\n");
15     system("pause");
16     return 0;
17 }
View Code

 运行结果截图

31

 32

33

34

回答问题:去掉line9后,程序运行结果如下图37

程序不会等待输入第二次答案,直接执行else分支

原因:输入第一次字符后,按下回车键,回车键会复制给ans2,不满足(ans=='y

'||ans2=='Y')的条件,直接执行了else分支。

 

任务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);//缺少了取地址运算符
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);//错将lf写成f
16     printf("x = %f, y = %lf\n", x, y);
17     system ("pause");
18     return 0;
19 }
View Code

运行结果截图

4

任务5

代码

 

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

 

运行结果截图

 5

任务6

代码

 

 1 #include<stdio.h>
 2 #include<stdlib.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     system("pause");
15     return 0;
16 }
View Code

 运行结果截图

6

任务7

代码

 

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

 运行结果截图

7

任务8

代码

 

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

 

运行结果截图

8

 

 

 

 

 

 

 

 

posted @ 2026-03-24 20:47  极法魔君  阅读(4)  评论(0)    收藏  举报