实验二

实验任务1

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 5
int main()
{
    int number;
    int i;
    
    srand(time(0));    
    for(i = 0;i < N; ++i)
    {
        number = rand() % 100 + 1;    //用于生成1~100间的随机数 
        printf("20490042%04d\n",number);   //%04d表示尾数为四位,且为上式生成的随机数 
    }
    return 0;
}                         //本程序的功能是在 204900420001~204900420100中随机生成学号 

 

 

实验任务2

#include <stdio.h>

int main() {
    int choice, quantity;
    float total_price = 0, amount_paid, change;
    while (1) 
    {
    printf("\n自动饮料售卖机菜单:\n");
    printf("1. 可乐 - 3 元/瓶\n");
    printf("2. 雪碧 - 3 元/瓶\n");
    printf("3. 橙汁 - 5 元/瓶\n");
    printf("4. 矿泉水 - 2 元/瓶\n");
    printf("0. 退出购买流程\n");
    printf("请输入饮料编号: ");
    scanf("%d", &choice);
    
    if (choice == 0)
        break;
    if (choice < 1 || choice > 4) 
    {
        printf("无效的饮料编号,请重新输入。\n");
        continue;
    }
    
    printf("请输入购买的数量: ");
    scanf("%d", &quantity);
    
    if (quantity < 0) 
    {
        printf("购买数量不能为负数,请重新输入。\n");
        continue;
    }
    switch (choice) 
    {
        case 1:
        case 2:
            total_price += 3 * quantity;
            break;
        case 3:
            total_price += 5 * quantity;
            break;
        case 4:
            total_price += 2 * quantity;
            break;
    }
    
    printf("请投入金额: ");
    scanf("%f", &amount_paid);
    
    change = amount_paid - total_price;
    printf("本次购买总价: %.2f 元\n", total_price);
    printf("找零: %.2f 元\n", change);
    
    total_price = 0;             //将总额归零。如果删去,则每次的总价都会加上上一次顾客已经完成消费的总价。 
    }
    printf("感谢您的购买,欢迎下次光临!\n");
    return 0;
}

        问题1:将总额归零。如果删去,则每次的总价都会加上上一次顾客已经完成消费的总价。 

     问题2:break表示结束整个循环,循环终止;而continue只是结束本次循环,不影响下次循环,即循环继续。

     问题3:我认为没有必要增加default语句,因为在此之前,有对choice进行判断是否符合规则即"if (choice < 1 || choice > 4)"

实验任务3

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char n;
    
    while(scanf("%c",&n) != EOF)
    {
        getchar();    //补足回车键的缓冲区 
        switch(n)
        {
        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");
        }
    }
    system ("pause");
    return 0;
}

 

 

 

实验任务4

#include <stdio.h>

int main()
{
    double cost,total;
    double max=0,min=20000;
    total = 0.0;
    while(1)
    {
        scanf("%lf",&cost);
        if(cost == -1)
          break;
        if(cost>20000||cost<0)
        {
            printf("输入无效,请重新输入\n");
            continue;
        }
        if(cost>max)
          max = cost;
        
        if(cost<min)
          min = cost;
        total = total + cost;
    }
    printf("今日累计消费总额: %.1f\n",total);
    printf("今日最高一边开销: %.1f\n",max);
    printf("今日最低一笔开销: %.1f\n",min); 
    return 0;
}

 

 实验任务5

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int n,lucky_day;
    int i=1;
    srand(time(0));
    lucky_day = rand() % 30 + 1;
    printf("开始喽,你有三次机会,猜吧(1~30): ");
    scanf("%d",&n);
    while(i<3)
    {
        if(n==lucky_day){
            printf("哇,猜中了:-)");
            break;
        }
        if(n<lucky_day)
            printf("你猜的日期早了,你的lucky day还没到呢~\n再猜(1~30): ");
        
        if(n>lucky_day)
            printf("你猜的日期晚了,你的lucky day在前面哦~\n再猜(1~30): ");
        scanf("%d",&n);
        
        i = i + 1; 
    }
    if(n!=lucky_day) 
        printf("次数用完啦,偷偷告诉你,4月你的lucky day 是: %d\n",lucky_day);
    else
        printf("哇,猜中了:-)");
    return 0;
}

 

实验任务6

 老师,实验6我不会了,搜了一下,还是看不太懂呢

posted @ 2025-03-19 17:22  LYHD  阅读(7)  评论(0)    收藏  举报