实验二

实验任务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.srand函数是用于初始化rand函数的随机值,否则rand生成的结果将一模一样
2.随机生成两个不同班级共5人的学号

实验任务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;
     }

     return 0;
}

运行截图

QQ20251019-105441

实验结论

1.用于重置总额,去掉后将计算总额减去前几次输入的金额总和
2.跳过该次循环继续进行下一次循环直到达到循环终止条件

实验任务3

源代码

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

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

运行截图

QQ20251019-150428

实验任务4

源代码

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

int main(){
	double total=0,max=0,min=2e4,normal;
	printf("请输入今日开销,直到输入-1时终止:\n");
	
	while(1){
		scanf("%lf",&normal);
		if(normal == -1.0)
		  break;
		if (normal <= 0.0|| normal > 20000.0) {
            printf("输入不合法,请重新输入!\n");
            continue;    
        } 
		  
		if(normal>max)
		  max = normal;
		if(normal<min) 
		  min = normal; 
		
		
		total += normal;
		
		}
	printf("今日累计消费总额:%.1f\n",total);
	printf("今日最高一笔开销:%.1f\n",max);
	printf("今日最低一笔开销:%.1f\n",min);
	
	return 0;
}

运行截图

QQ20251019-155324

实验任务5

源代码

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

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");
		continue;}
	else
	{
		if(a == b&&b ==  c)
		   printf("等边三角形\n");
		else if(a==b||a==c||b==c)
		   printf("等腰三角形\n");
		else if(pow(a,2)+pow(b,2)==pow(c,2)||pow(b,2)+pow(c,2)==pow(a,2)||pow(c,2)+pow(a,2)==pow(b,2))    
	       printf("直角三角形\n");
		else
		   printf("普通三角形\n");    
	}	 
	
	}
    return 0;
}

运行截图

ex2task5

实验任务6

源代码

点击查看代码

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

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

运行截图

ex2task6

ex2task6(1)

posted @ 2025-10-19 17:31  lolikana  阅读(4)  评论(0)    收藏  举报