实验2

实验任务1

源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 
 5 #define N 5
 6 #define N1 80
 7 #define N2 35
 8 
 9 int main (){
10     int  cnt;
11     int random_major,random_no;
12 
13 
14 
15     cnt = 0;
16     while(cnt<N){
17         random_major =rand() % 2;
18 
19     
20 
21             if (random_major){
22                 random_major =rand() % 2;
23                 printf("20236343%04d\n",random_no);
24             }
25             else {
26                 random_no = rand() % N2 +1;
27                 printf("20256136%04d\n",random_no);
28             }
29             cnt++;
30         }
31         return 0;
32     }
实验1

运行结果

image

 

image

 

image

 

image

 回答

有代码可以随机生成,而无代码只会生成一种

程序会随机的打出代码

 实验2

源代码

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

运行结果

屏幕截图 2025-10-19 151756

answer1:第一次的运行结果没变,但是后几次的支付金额会加上前几次

answer2再出现错误时可以保证重新循环;

实验3

源代码

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

运行结果

屏幕截图 2025-10-17 171040

实验5

源代码

 1 #include <stdio.h>
 2 
 3 int main() {
 4     float a, total = 0.0, max = 0.0, min = 20000.0;
 5     printf("输入今日开销,直到输入-1终止:\n");
 6     while (1) {
 7         scanf_s("%f", &a);
 8         if (a == -1) {
 9             break;
10         }
11         total += a;
12         if (a > max) {
13             max = a;
14         }
15         if (a < min) {
16             min =a;
17         }
18     }
19     printf("今日累计消费总额:%.1f\n", total);
20     printf("今日最高一笔开销:%.1f\n", max);
21     printf("今日最低一笔开销:%.1f\n", min);
22     return 0;
23 }

运行结果

屏幕截图 2025-10-18 175032

实验6

源代码

 1 #include<stdio.h>
 2 int main() {
 3     int a, b, c;
 4     
 5     while (1) {
 6         scanf_s("%d %d %d", &a, &b, &c);
 7         if ((a + b )> c && (b + c )&& a && (a + c) > b) {
 8             if ((a == b || b == c || c == a) && ((a * a + b * b) == (c * c) || (b * b + c * c) == a * a || (a * a + c * c == b * b))) {
 9                 printf("等腰直角三角形\n");
10                 continue;
11             }
12             else if (a == b && a == c) {
13                 printf("等边三角形\n");
14                 continue;
15             }
16             else if (((a * a + b * b) == (c * c)) || ((b * b + c * c) == a * a) || (a * a + c * c == b * b)) {
17                 printf("直角三角形\n");
18                 continue;
19             }
20             else if (a == b || b == c || c == a) {
21                 printf("等腰三角形\n");
22                 continue;
23             }
24             else { printf("普通三角形\n"); }
25         }
26         else {
27             printf("不能构成三角形\n"); }
28         }
29 
30 }

运行结果

屏幕截图 2025-10-18 185808

实验6

源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main() {
 4     int num, min=1, max=30, t;
 5     num = min + rand() % (max - min + 1);
 6     printf("猜猜2025年11月哪一天是你的luckly day\n");
 7     printf("开始喽,你有三次机会猜吧(1~30)\n");
 8 
 9 
10     
11         
12         for (int i = 1; i <= 3; i++) {
13             scanf_s("%d", &t);
14             if (t == num) {
15                 printf("哇,你猜对了");
16                 break;
17             }
18             else {
19                 if (t < num) {
20                     printf("你猜的日期早了,你的lucky day在后面哦\n再猜(1~30)");
21 
22                 }
23                 else
24                 {
25                     printf("你猜的日期晚了,你的lucky day在前面哦\n再猜(1~30)");
26 
27                 }
28 
29             }
30             if (i == 3) {
31                 printf("次数用光了。偷偷告诉你,11月你的lucky day是%d", num);
32             }
33         }
34         return 0;
35         
36 
37 }

运行结果

屏幕截图 2025-10-19 143832

 

posted @ 2025-10-19 15:23  徐维彬  阅读(6)  评论(0)    收藏  举报