实验 1 C语言输入输出和简单程序编写

实验任务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 }

 

 

 

 

运行结果

源代码

 

 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

源代码

 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 
20     system("pause");
21     return 0;
22 }

 

运行结果

实验任务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     // 思考这里为什么要加这一行。试着去掉这一行,看看对运行有没有影响。
11 
12     printf("\n动手敲代码实践了没? (输入y或Y表示敲了,输入n或N表示木有敲) :  ");
13     ans2 = getchar();
14 
15     if (ans1=='y'&&ans2=='Y') // 待补足,判断用户回答ans1和ans2都是小写y或大写Y
16         printf("\n罗马不是一天建成的, 继续保持哦:)\n");
17     else
18         printf("\n罗马不是一天毁灭的, 我们来建设吧\n");
19     system("pause");
20 
21     return 0;
22 }

运行结果

问题回答

如果没有这一行,会导致上一个输入字符中残留的/n直接被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);//漏放了存储地址
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     scanf("%lf,%lf", &x, &y);//输入为双精度数值类型时 要用“%lf”
15     printf("x = %f, y = %f\n",x, y);
16     system("pause");
17 
18     return 0;
19 }

 

运行结果

 

实验任务5

源程序

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     int year;
 7     year=(1000000000/ (365.25 * 24 * 60 * 60) + 0.5);
 8     printf("10亿秒约等于%d年\n",year);
 9     system("pause");
10 
11     return 0;
12 }

 

运行结果

 

实验任务6

源程序

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

 

运行结果

 

实验任务7

源程序

 

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

 

 

 

运行结果

 

 

实验任务8

 

源程序

 

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

 

 

 

 

运行结果

 

posted @ 2025-03-08 11:27  雾曦  阅读(39)  评论(0)    收藏  举报