实验2

实验任务1

task1.c源代码

#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("20256316%04d\n",random_no);
        }
        cnt++;
    }
    return 0;
} 

运行截图

联想截图_20251014193348

问题1:srand(time(NULL))用当前系统时间作为随机种子

问题2:这个程序能随机生成“20256343XXXX”或“20256316XXXX”的数列

实验任务2

task2.c源代码

#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;
}

运行截图

联想截图_20251014202328

问题1:如果去掉line47,则下一次循环中total_price的初始值为上一次循环结束时的值。

问题2:continue的作用为结束本次循环

 实验任务3

task3.c源代码

#include<stdio.h>
int main(){
    char n;
    
    while((n = getchar()) != EOF){
        if(n == 'r')
        printf("stop\n");
        else if(n == 'g')
        printf("go go go\n");
        else if(n == 'y')
        printf("wait a minute\n");
        else
        printf("something must be wrong\n");
        
        while(getchar() != '\n');
    }
    return 0;
}

运行截图

联想截图_20251014222517

 实验任务4

task4.c源代码

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

运行截图

联想截图_20251014224947

实验任务5

task5.c源代码

#include<stdio.h>
int main(){
    int a,b,c;
    while(scanf("%d%d%d",&a,&b,&c) != EOF){
        if(a + b > c && b + c > a && a + c > b){
            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 + c * c == b * b || b * b + c * c == a * a){
                printf("直角三角形");
            }
            else{
                printf("普通三角形");
            }
        }
        else{
            printf("不能构成三角形");
        }
        
    }
    return 0;
}

运行截图

联想截图_20251016190609

 实验任务6

task6.c

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

运行截图

联想截图_20251016195343

 

posted @ 2025-10-16 19:56  lidayu12  阅读(5)  评论(0)    收藏  举报