当前位置: 首页 > news >正文

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

1.实验任务1

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #define N 5
 5 int main() {
 6 int number;
 7 int i;
 8 srand(time(0)); 
 9 for(i = 0; i < N; ++i) {
10 number = rand() % 100 + 1;
11 printf("20490042%04d\n", number);
12 }
13 return 0;
14 }

运行截图

捕获3

 

问题1 任意抽取5个学号

问题2 在1到100抽取一个随机数

问题3 占四个字符的位置,使格式更整齐

问题4 使每次生成的随机数都不一样

2.实验任务2

源代码

 1 #include <stdio.h>
 2 int main() {
 3 int choice, quantity;
 4 float total_price = 0, amount_paid, change;
 5 while (1) {
 6 printf("\n自动饮料售卖机菜单:\n");
 7 printf("1. 可乐 - 3 元/瓶\n");
 8 printf("2. 雪碧 - 3 元/瓶\n");
 9 printf("3. 橙汁 - 5 元/瓶\n");
10 printf("4. 矿泉水 - 2 元/瓶\n");
11 printf("0. 退出购买流程\n");
12 printf("请输入饮料编号: ");
13 scanf("%d", &choice);
14 if (choice == 0)
15 break;
16 if (choice < 1 || choice > 4) {
17 printf("无效的饮料编号,请重新输入。\n");
18 continue;
19 }
20 printf("请输入购买的数量: ");
21 scanf("%d", &quantity);
22 if (quantity < 0) {
23 printf("购买数量不能为负数,请重新输入。\n");
24 continue;
25 }
26 if(choice == 1 || choice == 2)
27 total_price += 3 * quantity;
28 else if(choice == 3)
29 total_price += 5 * quantity;
30 else
31 total_price += 2 * quantity;
32 printf("请投入金额: ");
33 scanf("%f", &amount_paid);
34 change = amount_paid - total_price;
35 printf("本次购买总价: %.2f 元\n", total_price);
36 printf("找零: %.2f 元\n", change);
37 total_price = 0;
38 }
39 printf("感谢您的购买,欢迎下次光临!\n");
40 return 0;
41 }

 

 运行截图

捕获2

问题1 如果去掉,第二次运行时totalprice的数值会在第一次的基础上累加

问题2 继续运行程序

3.实验任务3

源代码

 

 1 #include <stdio.h> 
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     char c;
 7     while((c=getchar())!=EOF){
 8         if (c=='\n')continue;
 9         switch(c){
10             case'r':
11                 printf("stop!\n");
12                 break;
13             case'g':
14                 printf("go go go\n");
15                 break;
16             case'y':
17                 printf("wait a minute\n");
18                 break;
19             default:
20                 printf("something must be wrong...\n");
21                 break;
22         }
23     }
24     return 0;
25 }

运行截图

123

实验任务4

源代码

 

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

运行截图

image

 实验任务5

源代码

 1 #include <stdio.h>
 2 
 3 int main() {
 4     int a, b, c;
 5     while (scanf("%d %d %d", &a, &b, &c) != EOF) {
 6         if (a + b <= c || a + c <= b || b + c <= a) {
 7             printf("不能构成三角形\n");
 8             continue; 
 9         }
10         if (a == b && b == c) {
11             printf("等边三角形\n");
12         } else if (a == b || a == c || b == c) {
13             printf("等腰三角形\n");
14         } else if (a*a + b*b == c*c || a*a + c*c == b*b || b*b + c*c == a*a) {
15             printf("直角三角形\n");
16         } else {
17             printf("普通三角形\n");
18         }
19     }
20     return 0;
21 }

运行截图

image

 实验任务6

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 int main() {
 6     int lucky_day, guess;
 7     int chance = 3;
 8 
 9         srand((unsigned int)time(NULL));
10         lucky_day = rand() % 30 + 1;
11 
12     printf("猜猜2026年4月哪一天是你的lucky day\n");
13     printf("开始喽,你有3次机会,猜吧(1~30):");
14 
15     while (chance > 0) {
16         scanf("%d", &guess);
17 
18         if (guess == lucky_day) {
19             printf("哇,猜中了:)\n");
20             return 0;
21         } else if (guess > lucky_day) {
22             printf("你猜的日期晚了,你的lucky day在前面哦\n");
23         } else {
24             printf("你猜的日期早了,你的lucky day还没到呢\n");
25         }
26 
27         chance--;
28         if (chance > 0) {
29             printf("再猜(1~30):");
30         }
31     }
32     printf("次数用光啦。4月你的lucky day是%d号\n", lucky_day);
33 
34     return 0;
35 }

运行截图

image

 

image

 实验总结

本次实验主要锻炼了循环语句,实验456较为困难,无法单独完成,在豆包的提示下才完成实验。

http://www.jsqmd.com/news/593586/

相关文章:

  • 2026年花洒产品推荐:花洒哪个品牌好?4款热门花洒排行榜
  • Linux下WRF-Chem Intel编译器实战:从环境配置到编译成功的避坑指南
  • 高效使用Ultimaker Cura:从入门到精通的3D打印切片工作流
  • 非华为电脑也能用上鸿蒙生态?手把手教你给Win10/Win11装上最新华为电脑管家(附移动应用引擎开启方法)
  • 告别printk:用Linux内核Tracepoint给你的驱动调试换个活法(附ext4实战代码)
  • AI元人文:自感痕迹论——工夫与功夫的再辩证
  • 04 月 05 日 AI 每日参考:谷歌 Gemma 4 开源 国产 AI 算力生态强势崛起
  • EC11编码器硬件设计避坑指南:上拉电阻选择与PCB布局要点
  • 基于Quartus平台的RISCV五级流水线CPU设计与验证
  • Gym - 100624D Non-boring sequences
  • MDIN380芯片高清视频处理方案:SDI转VGA与LVDS转换,专业PCB设计与源码集成
  • olonCode v0.0.20 发布 - 编程智能体(新增子代理和浏览器能力)
  • [T.2] 团队项目:选题和需求分析
  • Scratch二次开发实战:如何按需“阉割”菜单栏功能?从关闭语言切换、主题到隐藏教程按钮
  • 当openclaw安装报错时:如何用快马ai模型快速诊断与生成修复方案
  • 可伴臻选购物卡回收方式 - 京顺回收
  • AI时代程序员必看!揭秘Harness Engineerin
  • 对接亚马逊 SP-API(Amazon Selling Partner API) 第一章:AWS IAM 配置详解
  • 记录生活中的一件小事(佚名整理)
  • 无人船编队 无人车编队 MPC 模型预测控制 多智能体协同控制 一致性 MATLAB 无人车 USV
  • AI辅助开发新体验:打造智能链接内容分析与摘要生成工具
  • 从频谱仪读数到测试报告:深入理解dBμV/m、dBm这些单位在EMC辐射发射测试中的真实含义
  • OpenClaw家庭应用:Qwen3-32B管理智能家居设备控制脚本
  • 2026 最新全开源壁纸头像小程序源码:自带流量主,完美适配微信生态
  • 2025Reddit养号实战:3步打造高Karma账号矩阵
  • 解锁Intel GPU的CUDA能力:从零开始的跨硬件计算实践
  • 【FastAPI】 + SQLAlchemy 异步 ORM 实现完整 CRUD 操作
  • 华泰证券2027届校招启动|提前批+国际管培+金融科技,三个专场一次说清
  • 新手友好:用快马生成的代码学习谷歌注册表单开发基础
  • 夸克网盘自动化助手:彻底告别手动转存的智能管理方案