实验1
任务1
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 5 6 int main() 7 { 8 printf(" O O\n"); 9 printf("<H> <H>\n"); 10 printf("I I I I\n"); 11 12 return 0; 13 }

1 #include <stdio.h> 2 #include <stdlib.h> 3 4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 5 6 int main() 7 { 8 printf(" O \n"); 9 printf("<H>\n"); 10 printf("I I\n"); 11 printf(" O \n"); 12 printf("<H>\n"); 13 printf("I I\n"); 14 return 0; 15 }

任务2
1 // 从键盘上输入三个数据作为三角形边长,判断其能否构成三角形 2 // 构成三角形的条件: 任意两边之和大于第三边 3 4 #include <stdio.h> 5 6 int main() 7 { 8 double a, b, c; 9 10 // 输入三边边长 11 scanf("%lf%lf%lf", &a, &b, &c); 12 13 14 if(a+b>c&&b+c>a&&a+c>b) 15 printf("能构成三角形\n"); 16 else 17 printf("不能构成三角形\n"); 18 19 return 0;
任务3
1 #include <stdio.h> 2 3 int main() 4 { 5 char ans1, ans2; 6 7 8 printf("每次课前认真预习,课后及时复习了没?(输入Y或y表示有,输入N或n表示没有)"); 9 ans1 = getchar(); 10 11 getchar(); 12 13 printf("\n动手敲代码实践了没?(输入Y或y表示敲了,N或n表示没有敲)"); 14 ans2 = getchar(); 15 16 17 if ((ans1 == 'Y' || ans1 == 'y') && (ans2 == 'Y' || ans2 == 'y')) 18 printf("罗马不是一天建成的,继续保持哦\n"); 19 else 20 printf("罗马不是一天毁灭的,我们来建设吧\n"); 21 22 23 return 0; 24 }

11行如果没有 getchar() 来消耗换行符,下一次读取字符时就会直接读到这个 \n,导致程序跳过输入。
任务4
#include <stdio.h>
int main()
{
double x, y;
char c1, c2, c3;
int a1, a2, a3;
//scanf没有取地址a1,a2,a3前加&
scanf("%d%d%d", &a1, &a2, &a3);
printf("a1=%d,a2=%d,a3=%d\n", a1, a2, a3);
scanf(" %c %c %c", &c1, &c2, &c3);
printf("c1=%c,c2=%c,c3=%c\n", c1, c2, c3);
// 双精度浮点数用lf
scanf("%lf,%lf", &x, &y);
printf("x=%lf,y=%lf\n", x, y);
return 0;
}
任务5
1 #include <stdio.h> 2 int main() { 3 int year = 1e9 / 60 / 60 / 24 / 365 + 0.5; 4 printf("%d年\n", year); 5 return 0; 6 }

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

任务7
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 5 6 int main() 7 { 8 double c; 9 while (scanf("%lf", &c) == 1) 10 printf("%.2lf\n", c * 9 / 5 + 32); 11 12 return 0; 13 }

任务8
1 #include <stdio.h> 2 #include <math.h> 3 4 int main() { 5 double a, b, c; 6 7 while (scanf_s("%lf %lf %lf", &a, &b, &c) == 3) { 8 double p = (a + b + c) / 2; 9 double area = sqrt(p * (p - a) * (p - b) * (p - c)); 10 printf("面积: %.3lf\n", area); 11 } 12 13 return 0; 14 }


浙公网安备 33010602011771号