实验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;
	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++;
	}
	system("pause");
	return 0;
}

  

image

 

image

 

image

 

image

 问题1

起到根据程序运行的不同时间使该程序随机生成不同的数字

问题2

功能为每次运行程序时都随机生成形式为2025634300+两位序号或2025613600+两位序号的四个学号

任务2

#include <stdio.h>
#include<stdlib.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");
	system("pause");
	return 0;
}

  

image

 问题1

会使第二次再购买饮料使的总价钱为第一次和第二次总价相加之和,使其购买总价计算错误

问题2

不再执行continue后面的语句,重新进行一次循环

任务3

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

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

  

image

任务4

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

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

  

image

 任务5

#include<stdio.h>
#include<stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
int main(){
    int a,b,c;
    while(scanf("%d%d%d",&a,&b,&c)!=EOF){
        if(a+b<=c||a+c<=b||b+c<=a)
            printf("不能构成三角形\n");
        else
        {
            if(a==b||a==c||b==c)
                printf("等腰三角形\n");
            else if(a==b&&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");
        }
	}
	system("pause");
	return 0;
}

  

image

任务6

#include<stdio.h>
#include<stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
int main(){
	int random,guest,n=1;
	random=rand()%31;
	printf("猜猜2025年11月哪一天是你的lucky day\n");
	printf("\n开始喽,你有三次机会,猜吧(1~30):");
	while(n<=3){
		scanf("%d",&guest);
		n++;
		if(guest==random){
			printf("\n哇,猜中了:)");
			break;
		}
		if(guest!=random){
			if(guest<random){
				printf("\n你猜的日期早了,你的lucky day还没到呢\n");
				if(n<=3)
	    			printf("\n再猜(1~30):");
				else
					printf("\n次数用光啦。偷偷告诉你,11月你的lucky day是%d号",random);

				continue;
			}
			if(guest>random){
				printf("\n你猜的日期晚了,你的lucky day在前面哦\n");
				if(n<=3)
		    		printf("\n再猜(1~30):");
				else
					printf("\n次数用光啦。偷偷告诉你,11月你的lucky day是%d号",random);

				continue;
			}
		}
	}
	system("pause");
	return 0;
}

  

 

image

 

image

  

 

posted @ 2025-10-13 22:41  鸋y  阅读(15)  评论(0)    收藏  举报