实验二
任务一
验证性实验
源码
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 5 #define N1 397 #define N2 476 #define N3 21 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() % (N2 - N1 + 1) + N1; printf("20248329%04d\n", random_no); } else { random_no = rand() % N3 + 1; printf("20248395%04d\n", random_no); } cnt++; } return 0; }
结果
好巧,第五个就是我()
回答
问题1:解释line21代码的功能
问题2:解释line25代码的功能
问题3:这个程序的功能是什么?
- 随机生成了一个397~476范围的数
- 随机生成了一个1~21范围的数
- 随机生成五个学号
任务二
验证性实验 根据一元二次方程求解公式,编程求解一元二次方程
源码
// 一元二次方程求解 #include <stdio.h> #include <math.h> int main() { double a, b, c; double delta, p1, p2; // 用于保存中间计算结果 while(scanf("%lf%lf%lf", &a, &b, &c) != EOF) { if(a == 0) { printf("a = 0, invalid input\n"); continue; } delta = b*b - 4*a*c; p1 = -b/2/a; p2 = sqrt(fabs(delta))/2/a; if(delta == 0) printf("x1 = x2 = %.2g\n", p1); else if(delta > 0) printf("x1 = %.2g, x2 = %.2g\n", p1+p2, p1-p2); else { printf("x1 = %.2g + %.2gi, ", p1, p2); printf("x2 = %.2g - %.2gi\n", p1, p2); } } return 0; }
另 之前写的
#include <stdio.h> #include <math.h> int main() { int a, b, c; double del, x1, x2; while (1) { printf("输入三个系数: "); scanf("%d %d %d", &a, &b, &c); if (a == 0) { if (b == 0 && c == 0) { break; } else { printf("不是一个有效的二次方程!\n"); continue; } } del = b * b - 4 * a * c; if (del > 0) { x1 = (-b + sqrt(del)) / (2 * a); x2 = (-b - sqrt(del)) / (2 * a); printf("方程有两个不等的实根。\n"); printf("x1 = %.2lf\n", x1); printf("x2 = %.2lf\n", x2); continue; } else if (del == 0) { x1 = (-b) / (2 * a); printf("方程有两个相等的实根。\n"); printf("x1 = x2 = %.2lf\n", x1); continue; } else if (del < 0) { double real, imag; real = -b / (2 * a); imag = sqrt(-del) / (2 * a); printf("方程有两个复数根。\n"); printf("x1 = %.2lf+%.2lfi\n", real, imag); printf("x2 = %.2lf-%.2lfi\n", real, imag); continue; } } return 0; }
结果
任务三
编写一个模拟红绿灯信息的程序
源码
#include <stdio.h> int main() { char in; while (scanf("%c", &in) != EOF) { if (in=='r') { getchar(); printf("stop!\n"); } else if (in=='g') { getchar(); printf("go go go\n"); } else if (in=='y') { getchar(); printf("wait a minute\n"); } else { getchar(); printf("something must be wrong\n"); } } return 0; }
结果
任务四
编写一个模拟记账程序统计一天的开销
源码
#include <stdio.h> int main() { float input; float sum = 0, max = 0, min = 20000; printf("Enter expense (-1 to stop):\n"); for (int i = 0; scanf("%f", &input) && input != -1; i++) { sum += input; if (input > max) max = input; if (input < min) min = input; } printf("Today's total expense:%.1f\nToday's biggest expense:%.1f\nToday's smallest expense:%.1f\n", sum, max, min); return 0; }
结果
任务五
编写程序 根据输入的三角形三边边长,判断三角形类型
源码
#include <stdio.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) { if (a == b || a == c || b == c) { if (a == b && b == c) { printf("等边三角形\n"); } else { printf("等腰三角形\n"); } } else if (a * a + b * b == c * c || a * a + c * c == b * b || c * c + b * b == a * a) { printf("直角三角形\n"); } else { printf("普通三角形\n"); } } else { printf("不能构成三角形\n"); } } return 0; }
结果
任务六
编写一个简单的猜日期程序
源码
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> int main() { srand(time(NULL)); int lknum = rand() % 30 + 1; int n = 3,input; printf("猜猜2024年11月哪一天会是你的lucky day\n开始咯,你有三次机会,猜吧(1~30):"); while (n--) { scanf("%d", &input); if (input == lknum) { printf("哇,猜中了:)"); break; } else if (input < lknum) { printf("你猜的日期早了,你的lucky day还没到呢\n"); if (n==0) { printf("你已经用完了三次机会啦,偷偷告诉你,lucky day是%d号哦\n",lknum); break; } else { printf("再猜(1~30):"); continue; } } else { printf("你猜的日期晚了,你的lucky day在前面哦\n"); if (n==0) { printf("你已经用完了三次机会啦,偷偷告诉你,lucky day是%d号哦\n",lknum); break; } else { printf("再猜(1~30):"); continue; } } } return 0; }
别骂了 理应把n==0放外面一层,但是我懒得改了
结果
没猜对
猜对了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端