实验2

task1

  • 源代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 100
#define CNT 5

int main() {
    int number;
    int i;

    srand(time(0));     
    for(i = 0; i < CNT; ++i) {
        number = rand() % N + 1;
        printf("20490042%04d\n", number);
    }

    return 0;
}
  • 运行结果
    image
  • 问题1
    该程序的功能是生成并打印5个特定的学号,这些学号的格式为“20490042”加上4位数字,后面四位数字通过随机函数生成,范围在0001到0100之间
  • 问题2
    line14的功能是生成一个1到100之间的随机整数并且赋值给变量number
  • 问题3
    line15中%04d的作用是格式化输出,表示输出一个宽度至少为4的十进制整数,如果数字不足4位,则在前面用0填充
  • 问题4
    设置随机数种子,保证每次运行程序时生成的随机数序列都不一样

task2

  • 源代码
#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;
        }

        if(choice == 1 || choice == 2)
            total_price += 3 * quantity;
        else if(choice == 3)
            total_price += 5 * quantity;
        else
            total_price += 2 * quantity;

        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;
}
  • 运行结果
    image
  • 问题1
    total_price = 0 用来吧上一次购买结算后的总价清零,准备下一轮选购,如果不清零下一轮金额会在上一轮总价的基础上叠加
  • 问题2
    continue作用是跳过本次循环中continue后面的语句,第一处作用是跳过后面输入数量,价格,找零的代码直接回到菜单重新输入;第二处的作用是跳过后续价格结算,回到循环开头重新选择

task3

  • 源代码
#include <stdio.h>
int main()
{
    char signal;
    int i;
    for(i = 1; i <= 4 ; ++i){
        signal = getchar();
        getchar();
        switch(signal){
        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");
    }
    }
    return 0;
}
  • 运行结果
    image

task4

  • 源代码
#include <stdio.h>
int main()
{
    double cost , max , min , total = 0;
    printf("请输入今日开销,知道输入-1终止: \n");
    scanf("%lf", &cost);
    if(cost == -1){
        return 0;
    }
    else{
        max = cost ; min = cost; total = cost;
        while(scanf("%lf",&cost) == 1 && cost != -1){
        if(cost > max){
            max = cost;
        }
        if(cost < min){
            min = cost;
        }
        total += cost;
    }
    printf("今日累计消费总额: %.1f\n" , total);
    printf("今日最高一笔开销: %.1f\n" , max);
    printf("今日最低一笔开销: %.1f\n" , min);
    }
    return 0;
}
  • 运行结果
    image

task5

  • 源代码
#include <stdio.h>
int main()
{
    int a , b , c , i;
    for(i = 1 ; i <= 5 ; ++i){
        scanf("%d%d%d",&a,&b,&c);
        if(a + b > c && a + c > b && b + c > a){
            if(a == b && b == c && a == c){
                printf("等边三角形 \n");
            }
            else if(a == b || a == c || b == c){
                printf("等腰三角形 \n");
            }
            else if(a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a){
                printf("直角三角形 \n");
            }
            else{
                printf("普通三角形 \n");
            }
        }
        else{
            printf("不能构成三角形 \n");
        }
    }
    return 0;  
}
  • 运行结果
    image

task6

  • 源代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
    int luck_day , i , ans;
    srand((unsigned)time(NULL));
    luck_day = rand() % 30 + 1;
    printf("猜猜2026年11月那一天是你的luck day \n");
    for(i = 1 ; i <= 3 ; ++i){
        if(i == 1){
            printf("开始喽,你有3次机会,猜吧(1 ~ 30) : ");
        }
        else{
            printf("再猜(1 ~30):");
        }
        scanf("%d", &ans);
        if (ans < luck_day){
            printf("你猜的日期早了,你的luck day还没到呢 \n");
            if(i == 3){
                printf("次数用光啦。11月你的luck day是%d号" , luck_day);
            }
        }
        else if(ans > luck_day){
            printf("你猜的日期完了,你的luck day在前面哦 \n");
            if(i == 3){
                printf("次数用光啦。11月你的luck day是%d号" , luck_day);
            }
        }
        else{
            printf("哇,猜中了!");
        }    
    }
    return 0;
}
  • 运行结果
    image
posted @ 2026-10-28 18:20  Katherina_0  阅读(4)  评论(0)    收藏  举报