实验2 C语言分支与循环基础应用编程

一、实验目的
1. 能正确使用if语句实现分支结构
2. 能正确使用while语句、do...while语句实现循环结构
3. 能在具体问题场景中正确区分、使用continue和break
4. 能灵活、组合使用c语句编程解决简单应用问题
 
二、实验准备
实验前,请复习第3章以下内容:
1. 分支语句 if 和循环语句 while 、 do...while 的用法:语法规则及注意事项
2. 在循环语句中 continue 和 break 语句的功能和用法
3. 使用 while 语句实现多组输入的方法
4. 常见问题的算法
 
三、实验内容
1. 实验任务1
验证性实验。
在c开发环境下,输入如下程序。结合程序运行结果,理解代码、回答问题。
 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("20256136%04d\n", random_no);
   }
  cnt++;
  }
  return 0;
}

实验结论

image

image

多次重复运行发现每次产生的随机数序列都不同

问题一   删除srand(time(NULL))后每次运行结果产生相同的随机数序列

问题二 改函数以当前系统时间作为种子产生的时真随机数 ,删除代码种子固定产生的时伪随机数

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

实验结果

未将total_price=0 注释掉

image

 将total_price=0 注释

image

问题一 total_price=0将每次进行计算之后的总价归零 防止在下一次循环中出现两次循环的值总和相加的错误出现

问题二 第一处continue函数用于 如果输入的饮料编号超出范围 跳出本次进行下一次if语句进行循环

第二处continue函数用于判断如果购买数量不是正整数 则跳出循环重新进入输入新的购买数量

实验任务3

编写一个模拟红绿灯信息的程序task3.c。具体要求如下:
使用多分支if语句实现
要求用户从键盘输入字符,用来表示交通信号灯颜色。输入r表示red,输入g表示green, 输入y表示yellow
判断交通型号灯颜色,打印输出不同的提示信息:
如果输入的是r,打印输出字符串stop!
如果输入的是g,打印输出字符串go go go
如果输入的是y,打印输出字符串wait a minute
如果输入的是其它字符,打印输出字符串something must be wrong...
要求支持多组输入,直到用户按下CTRL+Z,回车,终止程序

task3.c

#include<stdio.h>
#include<string.h>
int main()
{
    char input[20]={0};
    printf("交通信号灯颜色。输入r表示red,输入g表示green, 输入y表示yellow\n");
    //当输入CtrlZ回车 
    while (scanf("%s",input)!=EOF) 
    {
    if(strcmp(input,"r")==0)
    {printf("stop\n");    }
    else if(strcmp(input,"g")==0)
    {printf("gogogo \n");}
    else if(strcmp(input,"y")==0)
    {printf("wait a minute\n");}
    else
    {printf("出现超出输入范围错误,请重新输入\n");}
}
return 0; 
 } 

实验结果

 

image

 

实验任务4

 编写一个模拟记账程序task4.c统计一天的开销。具体要求如下:
从键盘重复输入一天内的若干笔开销,输入-1时终止程序。
在屏幕上打印输出一天内最高一笔开销、最低一笔开销,及一天总开销。输出时保留到小数点后1位。
运行程序时,输入的测试数据限定:0元 < 每笔消费 <= 2万元

task4.c

#include <stdio.h>

int main() {
    double expense;
    double total = 0.0;
    double max_expense = 0.0;
    double min_expense = 20000.0;
    int valid_input;
    
    printf("请输入一天内的开销(输入-1结束):\n");
    
    while (1) {
        printf("请输入一笔开销:");
        valid_input = scanf("%lf", &expense);
        
        if (valid_input != 1) {
            printf("错误:请输入有效的数字!\n");
            while (getchar() != '\n');
            continue;
        }
        
        if (expense == -1) {
            break;
        }
        
        if (expense <= 0 || expense > 20000) {
            printf("错误:开销必须在0元到20000元之间,请重新输入!\n");
            continue;
        }
        
        total += expense;
        
        if (expense > max_expense) {
            max_expense = expense;
        }
        
        if (expense < min_expense) {
            min_expense = expense;
        }
    }
    
    if (total == 0.0) {
        printf("今天没有记录任何开销。\n");
    } else {
        printf("\n=== 今日开销统计 ===\n");
        printf("最高一笔开销:%.1f元\n", max_expense);
        printf("最低一笔开销:%.1f元\n", min_expense);
        printf("一天总开销:%.1f元\n", total);
    }
    
    return 0;
}

实验结论

image

 

实验任务5

编写程序task5.c,根据输入的三角形三边边长,判断三角形类型。具体要求如下:
从键盘输入三角形三边边长a, b, c。边长为整数。
输出要求:
如果a, b, c不能构成三角形,打印输出"不能构成三角形"
如果能构成三角形,根据边长编写条件式,判断三角形属于哪一类别:普通三角形、直角三角形、等边
三角形、等腰三角形
要求支持多组输入,直到用户按下CTRL+Z,敲回车,终止程序。

task5.c

#include <stdio.h>

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

 编写一个简单的猜日期程序task6.c。具体要求如下:
使用随机函数,生成[1, 30]之间一个随机整数作为11月的lucky day
用户有3次猜的机会。根据用户输入的日期,提示用户猜的日期是猜中了,或是,早或晚
如果用户已经猜中了,则无需打印输出lucky day
如果用户用光三次机会也没有猜中,则打印输出用户在11月的lucky day

task6.c

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

int main() {
    int lucky_day, guess, count = 0;
    
    srand(time(NULL));
    lucky_day = rand() % 30 + 1;
    
    printf("猜一猜11月的幸运日(1-30):\n");
    
    while (count < 3) {
        printf("第%d次猜测:", count + 1);
        scanf("%d", &guess);
        
        if (guess == lucky_day) {
            printf("恭喜你猜中了!\n");
            break;
        } else if (guess < lucky_day) {
            printf("早了\n");
        } else {
            printf("晚了\n");
        }
        
        count++;
    }
    
    if (count == 3 && guess != lucky_day) {
        printf("11月的幸运日是:%d\n", lucky_day);
    }
    
    return 0;
}

实验结论

image

 利用二分查找最快   第二次就猜对了(๑´ڡ`๑)

posted @ 2025-10-19 14:06  Linch114514  阅读(11)  评论(0)    收藏  举报