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

实验作业2

实验1:

点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>#define N 5 
#define N1 80
#define N2 35int 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;
}
Q1:srand(time(NULL))的作用是随机生成,并且随着时间而改变。若去掉后,第一次的随机数据就会成为后面每一次的结果。 Q2:随机生成5个学号开头为20256343和20256136。 实验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.橙汁 - 3元/瓶 \n");printf("4.款泉水 - 3元/瓶 \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;elsetotal_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("感谢您的购买,欢迎下次光临");return 0; 
}
Q1:避免在第二次往后的与运行中total_price的初始值不为0。 Q2:触发continue语句时结束当前循环,不在运行下面还没开始运行的语句,且直接回到当前循环最前面。

实验3:

点击查看代码
#include <stdio.h>
int main ()
{char ans;while(scanf("%c",&ans)!= EOF){if(ans == 'y')printf("wait a minute\n");else if (ans == 'g')printf("go go go\n");else if(ans == 'r')printf("stop!\n");else if("ans == '\n'")continue; elseprintf("something must be wrong...\n");}return 0;
} 

屏幕截图 2025-10-15 191936
实验4:

点击查看代码
#include <stdio.h>
int main ()
{float cost,total,max,min;max = 0.0;min = 20000.0;total = 0.0;printf("输入今日开销,直到输入-1终止:\n"); while(1){scanf("%f",&cost);if(cost == -1)break;else {if(cost > max)max = cost;if(min > cost)min = cost;if(cost > 0 && cost <= 20000)total += cost;elsebreak;}		 	} printf("今日累计消费总额:%.1f,\n",total);printf("今日最高一笔开销:%.1f,\n",max);printf("今日最低一笔开销:%.1f,\n",min);return 0;
} 

屏幕截图 2025-10-15 195256
实验5:

点击查看代码
#include <stdio.h>
int main ()
{int a,b,c;while(scanf("%d%d%d",&a,&b,&c)!= EOF){if(a + b > c && b + c > a && a + c > b){if((a == b || b == c|| a == c ) && ( a != b|| a !=c|| b !=c ))printf("等腰三角形\n");else if(a == b && b == c && a == c)printf("等边三角形\n");else if((a*a == b*b +c*c) || (b*b == c*c + a*a) || (c*c == b*b + a*a))printf("直角三角形\n");elseprintf("普通三角形\n");}elseprintf("不能构成三角形\n"); }return 0;
} 

屏幕截图 2025-10-15 203201
实验6:

点击查看代码
#include <stdio.h>
#include <time.h> 
#include <stdlib.h> 
int main ()
{int i, ans;srand(time(NULL));int a = rand () %30 + 1 ;printf("猜猜2025年11月哪天是你的Lucky day\n");printf("开始喽,你有三次机会,猜吧(1~30):\n");for(i = 0;i < 3;i++) {scanf("%d",&ans);if(ans > a){printf("你猜的日期晚了,你的Lucky day还在前面哦\n");continue;}else if(ans < a){printf("你猜的日期早了,你的Lucky day还没到呢\n");continue;}else{printf("哇,猜中了\n");break;} }if(i == 3) printf("次数用光了。偷偷告诉你,11月你的Lucky day是%d\n",a);return 0;
} 

屏幕截图 2025-10-15 211611

http://www.hskmm.com/?act=detail&tid=32542

相关文章:

  • macOS 双开/多开微信WeChat完整教程(支持 4.X 及以上版本) - 实践
  • 快捷运用电脑的方式(不使用鼠标)
  • 2025.10.16总结 - A
  • 初识pytorch:更新网络参数的反向传播、损失函数和优化器
  • Composition API 与 React Hook 很像,区别是什么?
  • 题解:CF1483E Vabank
  • 20251016 正睿二十连测
  • [贝佐斯-六页纸]
  • cc
  • 感知节点@7@ ESP32+arduino+ 第五个程序FreeRTOS 上 增加一个新任务ADC任务
  • 2025年10月切削液厂家 TOP 企业品牌推荐排行榜,全合成切削液,半合成切削液,微乳切削液推荐这十家公司!
  • 普源精电RIGOL DS2202A示波器保存波形到CSV文件过慢解决方法:保存为WFM格式、通过LAN接口使用SCPI+PyVISA控制
  • 动手学深度学习——引言
  • CF1989E Distance to Different
  • AngularJS:构建更智能的Web应用框架
  • 给档案装上“智慧大脑”:文档抽取技术的四大赋能场景
  • P11816QOJ1250 Pionki 轮廓线DP
  • linux系统scatter/gather I/O技术
  • PostgreSQL 为什么不选择 B+ 树索引? - Lafite
  • Joeys shell
  • Redis 集群从部署到可视化管理全流程(超详细实战指南)
  • 什么是BPM流程自动化?从“财务报销”入手,一文读懂企业效率引擎
  • 软件工程学习日志2025.10.16
  • P1072 [NOIP 2009 提高组] Hankson 的趣味题
  • 25w41a快照测评:鹦鹉螺成精了?长矛教你戳穿末影人!
  • Day15-C:\Users\Lenovo\Desktop\note\code\JavaSE\Basic\src\com\classlei
  • Day14
  • window电脑开启hyperV虚拟化功能后导致本地服务端口被占用问题处理方案
  • RAG检索质量差?这5种分块策略帮你解决70%的问题
  • 初识pytorch:网络骨架中的填充之各种层