实验2

#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("20236343%04d\n",random_no);
		}
		else{
			random_no=rand()%N2+1;
			printf("20256136%04d\n",random_no);
		}
		cnt++;
	}
	system("pause");
	return 0;
}

image

1.作用是生成随机数
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.进行多次购买时,总价一直在累加,支付时也是累加后的总价,导致无法进行多次购买
2.continue语句作用是结束本次循环,直接开始下一次循环。在本程序中,第一处continue作用是,当输入无效编号时,跳过后续步骤,直接返回菜单;第二处continue作用是,输入负数数量时,跳过后续步骤,直接返回菜单。

#include<stdio.h>

int main(){
	char color,r,g,y;
	
	printf("请输入交通灯颜色,按Ctrl+z结束:\n");
	while(scanf("%c",&color)!=EOF){
		getchar();
	if(color=='r'){
		printf("stop!\n");
	}
	else if(color=='g'){
		printf("go go go\n");
	}
	else if(color=='y'){
		printf("wait a minute\n");
	}
	else{
		printf("something must be wrong...\n");
	}
		printf("请输入下一个信号灯颜色,按Ctrl+z结束:\n");
	}
		return 0;
	}

image

#define  _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(){
	double total=0.0,expense,max_expense=0.0,min_expense=20000.0;
	printf("输入今日开销,直到输入-1终止:");
	while(1){
		scanf("%lf",&expense);
		getchar();
		if(expense==-1){
		break;
		}
		if(expense<0||expense>20000){
		continue;
		}
		total+=expense;

		if(expense<min_expense){
		min_expense=expense;
		}
		
		if(expense>max_expense){
		max_expense=expense;
		}
	}
		printf("\n今日累计消费总额;%.1f\n",total);
		printf("\n今日最高一笔开销;%.1f\n",max_expense);
		printf("\n今日最低一笔消费;%.1f\n",min_expense);		

		system("pause");
		return 0;
	}

image

#define  _CRT_SECURE_NO_WARNINGS

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

int main(){
	int  a,b,c;
	while(scanf("%d%d%d",&a,&b,&c)!=EOF){
		getchar();
		if(a+b>c&&a+c>b&&b+c>a){
			if(a==b||a==c||b==c){
				printf("等腰三角形\n");
			}
			else if(a*a+b*b==c*c||a*a==b*b+c*c||a*a+c*c==b*b){
				printf("直角三角形\n");
			}
			else{
				printf("普通三角形\n");
			}
	}
		else{
			printf("不能构成三角形\n");
	}	
		printf("按ctrl+z结束:\n");
	}
		system("pause");
		return 0;
	}

image

#define  _CRT_SECURE_NO_WARNINGS

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

int main(){
	srand(time(NULL));

	int lucky_day=rand()%30+1;
	int guess;
	int attempts=0;

	printf("猜猜2025年11月哪一天是你的lucky day\n");
	printf("开始喽,你有三次机会,猜吧(1~30):\n");
	while(attempts<3){
		scanf("%d",&guess);
		attempts++;
		if(guess==lucky_day){
			printf("哇,你猜中了!\n");
		
		}
		else if(guess<lucky_day){
			printf("你猜的日期早了,你的lucky day还没到呢\n");
			printf("再猜(1~30);");
		
		}
		else if(guess>lucky_day){
			printf("你猜的日期晚了,你的lucky day在前面哦\n");
			printf("再猜(1~30);");
		
		}
		if(attempts==3&&guess!=lucky_day){
			printf("次数用光了,偷偷告诉你,11月你的lucky day是%d号\n",lucky_day);
		}
	
	}

		system("pause");
		return 0;
	}

image

posted @ 2025-10-14 19:40  李佳颖  阅读(9)  评论(1)    收藏  举报