实验二

实验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;
printf("20490042%04d\n", number);
}
return 0;
}

运行结果

image

实验结论

问题1:这个程序的功能是什么?

答:随机生成5个20490042xxxx的学号

问题2:解释lin13代码的功能。

答:随机生成1~100的数字 本来前面是1~99 后来+1 变成了1~100

问题3:解释line14使用格式符%04d起到什么作用。

答:在不足四位整数的时候在前面补0

问题4:代码 srand(time(0)); 起到什么作用?(提示:去掉这行代码,多次运行程序,观察结果有什么特

点)

答:使每次随机输出的学号不同 如果去掉了 之后每一次生成的学号是一样的

实验2

源代码

点击查看代码
#include <stdio.h>
#include <windows.h> //因为编码问题加的代码
int main() {
system ("chcp 65001");
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:line47代码 total_price = 0; 如果去掉,对程序有什么影响?

答:如果去掉的话 前一次的商品的价钱会加到这次商品总价上面

问题2:while循环中,有两处使用 continue 语句。解释在循环中使用 continue 语句,语义是什么?

答:

第一个:如果输入的饮料编号不存在的话(就是不是1 2 3 4 0 的话)就输出重新输入 并且 重新给出菜单

第二个:如果输入的数量是整数的话 同上

实验3

源代码

点击查看代码
#include <stdio.h>
#include <windows.h>

int main(){
    system("chcp 65001");
    char color;

    printf("请输入红绿灯颜色:\n");


    while(scanf(" %c",&color) != EOF){

        switch(color){
            case 'r':
                printf("stop!\n");
                break;

            case 'g':
                printf("gogogo!\n");
                break;

            case 'y':
                printf("wait a minute!\n");
                break;

            default:
                printf("something must be wrong...\n");
                break;
        }
    }
return 0;
}

运行结果

image

实验结论

后面到底要不要加getchar();啊??
我感觉我没有加上也没有错

实验4

源代码

点击查看代码
#include <stdio.h>
#include <windows.h>

int main(){
    system("chcp 65001");
    float expense;
    float sigle_max = 0 ;
    float sigle_min = 20000 ;
    float total_expense = 0 ;

    printf("请输入开销:");
    
    while(1){
        
        scanf("%f",&expense);

        if (expense == -1){
            break;
        }

        if (expense <= 0 || expense > 20000){

            printf("输入无效\n");
            continue;
        }

        else{

            total_expense=total_expense + expense;

            if (expense > sigle_max){
                sigle_max = expense;
            }
            if (expense < sigle_min){
                sigle_min = expense;
            }

        }

    }

    printf("今日总消费:%.1f 元\n",total_expense);
    printf("最高开销:%.1f 元\n",sigle_max);
    printf("最低开销:%.1f 元\n",sigle_min);

return 0;
}

运行结果

image

实验结论

我去,%.1f表示保留一位小数 我一直打成了%1f 还不知道哪里错了

实验5

源代码

点击查看代码
#include <stdio.h>
#include <windows.h>

int main(){
    system("chcp 65001");
    int a,b,c;

    printf("请输入三边长:\n");

    while(scanf("%d %d %d",&a,&b,&c) != EOF){
        if (a + b > c && a + c >b && b +c > a){
            if (a ==b && b == c){
                printf("这是一个等边三角形");
            }

            else if (a == b ||a == c || b == c){
                printf("这是一个等腰三角形");
            
            }
            else if(a * a + b * b == c * c || a * a + b * b == c * c || b * b + c * c == a * a){
                printf("这是一个直角三角形");
            }

            else {
                printf("这是一个普通三角形");
            }
        }

        else{
            printf("这不是三角形");
        }
        }
    
return 0;
}
## 运行结果

image

实验结论

实验6

源代码

点击查看代码
#include <stdio.h>
#include <windows.h>

int main(){
    system("chcp 65001");
    int lucky_day, guess;
    int chances = 3;
    int guessed_correctly = 0;

    srand((unsigned int)time(NULL));
    lucky_day = rand() % 30 + 1;

    printf("猜测2026年4月哪一天是你的lucky day\n");
    printf("开始喽,你有3次机会,猜吧(1~30):");

    for (int i = 1; i <= chances; i++) {
        scanf("%d", &guess);

        if (guess == lucky_day) {
            printf("哇,猜中了:\n");
            guessed_correctly = 1;
            break;
        }
        else if (guess < lucky_day) {
            printf("你猜的日期早了,你的lucky day还没到呢\n");
        }
        else {
            printf("你猜的日期晚了,你的lucky day在前面哦\n");
        }

        if (i < chances) {
            printf("再猜(1~30):");
        }
    }

    if (!guessed_correctly) {
        printf("次数用光啦。4月你的lucky day是%d号\n", lucky_day);
    }

return 0;
}

运行结果

image

实验结论

posted @ 2026-04-05 13:04  hanxiuying  阅读(6)  评论(0)    收藏  举报