实验二

试验任务一

 源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #define N 5
 5 int main() {
 6 int number;
 7 int i;
 8 srand(time(0));
 9 for(i = 0; i < N; ++i) {
10 number = rand() % 100 + 1;
11 printf("20490042%04d\n", number);
12 }
13 system("pause");
14 return 0;
15 }

运行结果截图

image

 问题

1.生成5个范围在1~100之间的随机整数,并按指定格式输出这些随机数

2.生成一个1~100的随机整数,并赋值给number

3.确保输出时,20490042后有四位数

4.使生成的数是随机的,而不是多次运行后结果一样

试验任务二

源代码

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 int main() {
 4 int choice, quantity;
 5 float total_price = 0, amount_paid, change;
 6 while (1) {
 7 printf("\n自动饮料售卖机菜单:\n");
 8 printf("1. 可乐 - 3 元/瓶\n");
 9 printf("2. 雪碧 - 3 元/瓶\n");
10 printf("3. 橙汁 - 5 元/瓶\n");
11 printf("4. 矿泉水 - 2 元/瓶\n");
12 printf("0. 退出购买流程\n");
13 printf("请输入饮料编号: ");
14 scanf("%d", &choice);
15 if (choice == 0)
16 break;
17 if (choice < 1 || choice > 4) {
18 printf("无效的饮料编号,请重新输入。\n");
19 continue;
20 }
21 printf("请输入购买的数量: ");
22 scanf("%d", &quantity);
23 if (quantity < 0) {
24 printf("购买数量不能为负数,请重新输入。\n");
25 continue;
26 }
27 if(choice == 1 || choice == 2)
28 total_price += 3 * quantity;
29 else if(choice == 3)
30 total_price += 5 * quantity;
31 else
32 total_price += 2 * quantity;
33 printf("请投入金额: ");
34 scanf("%f", &amount_paid);
35 change = amount_paid - total_price;
36 printf("本次购买总价: %.2f 元\n", total_price);
37 printf("找零: %.2f 元\n", change);
38 total_price = 0;
39 }
40 printf("感谢您的购买,欢迎下次光临!\n");
41 system("pause");
42 return 0;
43 }

 

运行结果截图

image

 

 问题

1.会记入上一次购买总价,导致这一次的输出结果有问题

2.跳过循环体内后续的代码,直到返回循环条件判断处,开始下一次循环

试验任务三

源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main(){
 4 char c;
 5 while(scanf("%c",&c) != EOF){
 6 if (c == '\n')
 7 continue;
 8 if (c == 'r'){
 9 printf("stop!\n");
10 }
11 else if (c == 'g'){
12 printf("go go go\n");
13 }
14 else if (c == 'y'){
15 printf("wait a mintue\n");
16 }
17 else{
18 printf("something must be wrong...\n");
19 }
20 }
21 system("pause");
22 return 0;
23 }

运行结果截图

image

 

试验任务四

源代码


#include <stdio.h>
#include<stdlib.h>
int main() {
double expense;
double total = 0.0;
double max_exp =0.0;
double min_exp =20000;
int count = 0;
printf("输入今日开销,直到输入-1终止:\n");
while (1) {
scanf("%lf", &expense);
if (expense == -1) {
break;
}
if (expense <= 0 || expense > 20000) {
printf("输入无效,请输入0~20000之间的金额,或-1终止\n");
continue;
}
total += expense;
count++;
if (expense > max_exp) {
max_exp = expense;
}
if (expense < min_exp) {
min_exp = expense;
}
}
if (count == 0) {
printf("无有效开销记录\n");
}else {
printf("今日累计消费总额: %.1f\n", total);
printf("今日最高一笔开销: %.1f\n", max_exp);
printf("今日最低一笔开销: %.1f\n", min_exp);
}
system("pause");
return 0;
}


 

运行结果截图

 

屏幕截图 2026-04-01 155239

 

试验任务五

源代码

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 int main()
 4 {
 5 int a, b, c;
 6 printf("请输入三角形三边边长(整数),输入结束按 CTRL+Z:\n");
 7 while (scanf("%d %d %d", &a, &b, &c) != EOF)
 8 {
 9 if (a + b <= c || a + c <= b || b + c <= a)
10 {
11 printf("不能构成三角形\n");
12 continue;
13 }
14 if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a)
15 {
16 printf("直角三角形\n");
17 continue;
18 }
19 if (a == b && b == c)
20 {
21 printf("等边三角形\n");
22 continue;
23 }
24 if (a == b || a == c || b == c)
25 {
26 printf("等腰三角形\n");
27 continue;
28 }
29 printf("普通三角形\n");
30 }
31 system("pause");
32 return 0;
33 }

 

运行结果截图

image

 

试验任务六

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 int main()
 5 {
 6 int luckyDay, guess;
 7 int attempt;
 8 srand((unsigned int)time(NULL));
 9 luckyDay = rand() % 30 + 1;
10 printf("猜猜2026年4月哪一天是你的lucky day\n");
11 printf("开始喽,你有3次机会,请猜(1-30):");
12 for (attempt = 1; attempt <= 3; attempt++)
13 {
14 scanf("%d", &guess);
15 if (guess == luckyDay)
16 {
17 printf("哇,猜中了!\n");
18 system("pause");
19 return 0;
20 }
21 else if (guess < luckyDay)
22 {
23 printf("你猜的日期早了,你的lucky day还没到呢\n");
24 }
25 else
26 {
27 printf("你猜的日期晚了,你的lucky day在前面哦\n");
28 }
29 if (attempt < 3)
30 {
31 printf("再猜(1-30):");
32 }
33 }
34 printf("次数用光啦。4月的lucky day是%d号\n", luckyDay);
35 system("pause");
36 return 0;
37 }

运行结果截图

屏幕截图 2026-04-01 162015

屏幕截图 2026-04-01 162404

 

posted @ 2026-04-01 16:25  kasdda  阅读(0)  评论(0)    收藏  举报