实验2

实验任务1

task1.c源代码:

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

测试截图:

问题1:生成一个1到100的随机整数

问题2:输出一个占4个字符宽度的整数,不足前面用0填充

问题3:随机生成并打印202400420001~202400420100的学号

实验任务2

task2.c源代码:

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

测试截图:

问题1:使下一次循环开始前金额清零,如果删去,下一次总金额会加上前一次

问题2:break直接跳出该循坏,运行循环外的程序;continue跳出该层循环直接执行下一层循环

问题3:不需要,前面的判断语句已经处理了错误输入的结果

实验任务3

task3.c源代码:

#include<stdio.h>

int main(){
    char ans;
    while(scanf("%c",&ans)!=EOF){switch(ans){
    case 'r':printf("stop!\n");break;
    case 'g':printf("go go go\n");break;
    case 'y':printf("wait a minute\n");break;
    default:printf("something must be wrong\n");
    }
    getchar();
    }
    return 0;
}

测试截图:

 

实验任务4

task4.c源代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main(){
 4     double x,max=0,min=20000,money=0;
 5     printf("输入今日开销,直到输入-1结束:\n");
 6     while(1){
 7         scanf("%lf",&x);
 8         if(x==-1)
 9             break;
10         if(x<0||x>20000){
11             printf("超出范围,请输入0到20000的金额");
12         continue;
13         }
14         if(x>max)
15             max=x;
16         if(x<min)
17             min=x;
18         money+=x;
19     }
20     printf("今日累计消费总额:%.1f\n",money);
21     printf("今日最高一笔开销:%.1f\n",max);
22     printf("今日最低一笔开销:%.1f\n",min);
23     system("pause");
24     return 0;
25 }

 

测试截图:

实验任务5

task5.c源代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 int main(){
 4     int n,i,x;
 5     n=rand()%30+1;
 6     printf("猜猜2025年4月那一天是你的lucky day\n");
 7     printf("开始喽,你有三次机会,猜吧(1~30)\n");
 8     for(i=1;i<=3;i++){
 9         scanf("%d",&x);
10         if(x==n){
11             printf("哇,猜中了:-)");
12             break;
13         }
14         if(x<n)
15             printf("你猜的日期早了,你的lucky day还没到呢\n");
16         if(x>n)
17             printf("你猜的日期晚了,你的lucky day在前面哦\n");
18         printf("再猜(1~30):\n");
19     }
20     if(i==4)
21         printf("次数用完了。偷偷告诉你,4月你的lucky day是:%d\n",n);
22     system("pause");
23     return 0;
24 }

 

测试截图:

 

实验任务6

task6.c源代码:

 1

    #include<stdio.h>
    #include<stdlib.h>
    int main(){

        int n,i,j;

 2     printf("input n:");
 3     scanf("%d",&n);
 4      for (i = 0; i < n; i++) {
 5         for (j = 0; j < i; j++) {
 6             printf("   \t");
 7         }
 8         for (j = 0; j < (n-i) * 2 - 1; j++) {
 9             printf(" O \t");
10         }
11         printf("\n");
12         for (j = 0; j < i; j++) {
13             printf("   \t");
14         }
15          for (j = 0; j < (n-i) * 2 - 1; j++) {
16             printf("<H>\t");
17         }
18         printf("\n");
19         for (j = 0; j < i; j++) {
20             printf("   \t");
21         }
22          for (j = 0; j < (n-i) * 2 - 1; j++) {
23             printf("I I\t");
24         }
25         printf("\n");
26     }
27 
28     system("pause");
29     return 0;
30 }

测试截图:

 

posted @ 2025-03-18 22:11  gyn1111  阅读(18)  评论(0)    收藏  举报