实验2
实验任务1
task1.c
代码
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 5 int main(){ int number; int i; srand(time(0)); for(i = 0;i<N; ++i) { number = rand() % 100 +1; printf("20490042%04d\n",number); } system("pause"); return 0; }
截图
问题
1.随机1~100的数字
2.在随机出来的数字前面加零直到有四个数字
3.随机挑选学号
实验任务2
task2.c
代码
#include <stdio.h> #include <stdlib.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; } switch (choice) { case 1: case 2: total_price += 3 * quantity; break; case 3: total_price += 5 * quantity; break; case 4: total_price += 2 * quantity; break; } printf("请投入金额: "); scanf("%f", &amount_paid); change = amount_paid - total_price; printf("本次购买总价: %.2f 元\n", total_price); printf("找零:%.2f 元\n",change); total_price = 0; } printf("感谢您的购买, 欢迎下次光临! \n"); system("pause"); return 0; }
截图
问题
1.清零总价。如果去掉第二次将会加上第一次的总价
2.break是跳出循环体,结束循环执行后面的程序;continue是跳过循环中剩余的语句,开始下一次循环
3.不需要。line20-22已经进行过choice的错误排查。
实验任务3
task3.c
代码
#include <stdio.h> #include <stdlib.h> int main() { char ans; while(scanf(" %c",&ans)!=EOF) { switch(ans) { case('y'):printf("wait a minute\n"); break; case('r'):printf("stop!\n"); break; case('g'):printf("go go go\n"); break; default:printf("something must be wrong...\n"); } } system("pause"); return 0; }
截图
实验任务4
task4.c
代码
#include <stdio.h> #include <stdlib.h> int main(){ double n,sum,max,min; scanf("%lf",&n); max=n; min=n; sum=0; while (n!=-1) { sum+=n; if (n>max) max=n; if (n<min) min=n; scanf("%lf",&n); } printf("今日累计消费总额: %.1f\n",sum); printf("今日最高一笔开销: %.1f\n",max); printf("今日最低一笔开销: %.1f\n",min); system("pause"); return 0; }
截图
实验任务5
task5.c
代码
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ int day,ans,i; srand(time(0)); day=rand()%30+1; printf("猜猜2025年4月哪一天是你的lucky day\n"); printf("开始了,你有三次机会,猜吧(1-30):\n"); for(i=1;i<=3;i++) { scanf("%d",&ans); if(ans>day) printf("你猜的日期晚了,你的lucky day在前面哦(1-30)\n"); if(ans<day) printf("你猜的日期早了,你的lucky day还没到呢(1-30)\n"); if(ans==day) { printf("哇,猜中了:-)\n"); break; } if(i==3) printf("次数用完了,偷偷告诉你,你的lucky day是: %d\n",day); } system("pause"); return 0; }
截图
实验任务6
task6.c
代码
#include <stdio.h> #include <stdlib.h> int main() { int n, i, j, k; printf("input n: "); scanf("%d", &n); for (i = n; i >= 1; i--) { for (j = 0; j < n - i; j++) { printf(" "); } for (k = 0; k < i; k++) { printf(" 0 "); } printf("\n"); for (j = 0; j < n - i; j++) { printf(" "); } for (k = 0; k < i; k++) { printf(" <H> "); } printf("\n"); for (j = 0; j < n - i; j++) { printf(" "); } for (k = 0; k < i; k++) { printf(" I I "); } if (i > 1) { printf("\n"); } } system("pause"); return 0; }
截图
实验总结:
实验6小人好像没有对齐,等老师上课讲。