作业2

实验任务1:

源代码:

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

#define N 5
#define N1 80
#define N2 35

int main(){
    int cnt;
    int random_major,random_no;
    
    srand(time(NULL));
    
    cnt = 0;
    while(cnt < N){
        random_major = rand()%2;
        
        if(random_major){
            random_no = rand()%N1 + 1;
            printf("20256343%04d\n",random_no);
        }
        else{
            random_no = rand()%N2 + 1;
            printf("20256136%04d\n",random_no);
        }
        
        cnt++;
        
    }
    return 0;
}

 

代码运行结果:

image

 回答问题:1.以当前时间为种子,随机生成一串数字

2.随机生成学号(以“20256343”或者以“20256136”为开头)

实验任务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;
        }
        
        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时,返回循环开始的地方

实验任务3:

源代码:

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

int main() {
    char colour;
    
    while (1) {
        colour = getchar(); 
        
        if (colour == EOF)  
        break;
        
        if (colour == '\n') 
        continue; 
        
        if (colour == 'y') 
        printf("wait a minute\n");
        
        else if (colour == 'g') 
        printf("go go go\n");
        
        else if (colour == 'r') 
        printf("stop!\n");
        
        else 
        printf("something must be wrong...\n");
        
    }
    
    return 0;
}

代码运行结果:

image

 

实验任务4:

源代码:

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

int main(){
    float x,max,min,sum=0;
    printf("输入今日开销,直到输入-1终止:\n");
    scanf("%f",&x);
    max = x;
    min = x;
    while(x != -1)
    {
        sum = sum + x;
        if(x>max)
        max = x;
        if(x<min)
        min = x;
        scanf("%f",&x);
    }
    printf("今日累计消费总额:%.1lf\n,今日最高一笔开销:%.1lf\n,今日最低一笔开销:%.1lf\n",sum,max,min);
    return 0;
}

代码运行结果:

屏幕截图 2025-10-14 151020

实验任务5:

源代码:

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

int main(){
    int a,b,c,stop;
    
    while(1)
    {
        stop = scanf("%d%d%d",&a,&b,&c);
    
        if (stop == EOF)
        break;
        if (stop == '\n')
        continue;
    
        if (a+b<=c||a+c<=b||b+c<=a)
        printf("不能构成三角形\n");
        else if(a==b&&b==c)
        printf("等边三角形\n");
        else if(a==b||b==c||a==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"); 
    }
    
    return 0;
}

代码运行结果:

image

 实验任务6:

源代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
    int day,num = 1,guess;
    srand(time(NULL));
    day = rand()%30+1;
    printf("猜猜2025年11月哪一天是你的lucky day\n");
    printf("开始喽,你有三次机会,猜吧(1~30):");
    scanf("%d",&guess);
    while(num!=3){
        if(guess==day){
        printf("哇,猜中了:)");
        break;
    }
        else if(guess>day){
            printf("你猜的晚了,你的lucky day在前面哦\n再猜(0~30):");
            scanf("%d",&guess);
            num = num + 1; 
            continue;
        }    
        else{
            printf("你猜的早了,你的lucky day还没到呢\n再猜(0~30):");
            scanf("%d",&guess);
            num = num + 1; 
            continue;
        }
    }
    if(num==3){
        if(guess==day)
        printf("哇,猜中了:)");
        else if(guess>day){
            printf("你猜的晚了,你的lucky day在前面哦\n");
            printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号",day);         }
                 
            
        else{
            printf("你猜的早了,你的lucky day还没到呢\n");
            printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号",day);         }
            
    }
        return 0;
    }
    
    
        
        
    

代码运行结果:

屏幕截图 2025-10-14 230357

 

posted @ 2025-10-15 10:06  Lantesry  阅读(8)  评论(0)    收藏  举报