实验2
实验2
实验结论
任务1
源代码:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 5 #define N 5 6 7 int main() 8 { 9 10 int number; 11 int i; 12 13 srand(time(0)); 14 for(i=0;i<N;++i) 15 { 16 17 number=rand()%100+1; 18 printf("20490042%04d\n",number); 19 20 } 21 system("pause"); 22 return 0; 23 }
运行截图:

问题回答:1.生成5个204900420001到204900420100之间的编号
2.随机生成一个1~100间的整数
3.使这个随机生成的整数占4个字符,并用0补空位
4.作用:使每次执行程序生成的5个数不同,如果删去,那每次运行它,都是固定的5个编号
任务2
源代码:
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int main() 5 { 6 7 int choice, quantity; 8 float total_price = 0, amount_paid, change; 9 10 while (1) 11 { 12 printf("\n自动饮料售卖机菜单:\n"); 13 printf("1.可乐—3元/瓶\n"); 14 printf("2.雪碧—3元/瓶\n"); 15 printf("3.橙汁—5元/瓶\n"); 16 printf("4.矿泉水—2元/瓶\n"); 17 printf("0.退出购买流程\n"); 18 printf("请输入饮料编号:"); 19 scanf_s("%d", &choice); 20 21 if (choice == 0) 22 break; 23 if (choice < 1 || choice>4) 24 { 25 printf("无效饮料编号,请重新输入。\n"); 26 continue; 27 } 28 printf("请输入购买数量:"); 29 scanf_s("%d", &quantity); 30 31 32 if (quantity < 0) 33 { 34 printf("购买数量不能为负数,请重新输入。"); 35 continue; 36 } 37 if (choice == 1 || choice == 2) 38 total_price += 3 * quantity; 39 else if (choice == 3) 40 total_price += 5 * quantity; 41 else 42 total_price += 2 * quantity; 43 44 printf("请投入金额:"); 45 scanf_s("%f", &amount_paid); 46 47 change = amount_paid - total_price; 48 printf("本次购买总价:%.2f元\n", total_price); 49 printf("找零:%.2f元\n", change); 50 51 total_price = 0; 52 53 } 54 55 printf("感谢您的购买,欢迎下次光临!\n"); 56 57 58 59 60 61 system("pause"); 62 return 0; 63 }
运行截图:

问题回答:1.重置上一次购买的数据,如果去掉,那储存价格的标识符初始值就不为0,这将导致算出的总金额为前面所有购买的总额
2.结束本次循环,并开始下一次循环
任务3
源代码:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<stdlib.h> 4 5 int main() 6 { 7 char ade; 8 printf("请输入一个字符:(‘r’→‘red’,‘g’→‘green’,‘y’→‘yellow’)\n"); 9 10 11 while (scanf(" %c", &ade) != EOF) 12 13 { 14 15 if (ade == 'r') 16 printf("stop!\n"); 17 else if (ade == 'g') 18 printf("go go go\n"); 19 else if (ade == 'y') 20 printf("wait a minute\n"); 21 else 22 printf("something must be wrong...\n");printf("请输入一个字符:(‘r’→‘red’,‘g’→‘green’,‘y’→‘yellow’)\n"); 23 } 24 system("pause"); 25 return 0; 26 }
运行截图:

任务4
源代码:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 int main() 4 { 5 double a=0, sum=0,max=0,min=20000; 6 printf("输入今日开销,直到输入-1为止\n"); 7 while (1) 8 { 9 scanf("%lf", &a); 10 11 12 if (a != -1) 13 { 14 if (a > max) 15 { 16 max = a; 17 } 18 if (a < min) 19 { 20 min = a; 21 } 22 sum += a; 23 } 24 else 25 break; 26 } 27 printf("今日累计消费总额:%.1lf\n",sum); 28 printf("今日最高一笔开销:%.1lf\n",max); 29 printf("今日最低一笔开销:%.1lf\n",min); 30 31 32 33 34 return 0; 35 }
运行截图:

任务5
源代码:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 int main() 4 { 5 int a=0, b=0, c=0; 6 printf("请输入三角形的三边长:\n"); 7 while (scanf("%d %d %d", &a, &b, &c) != EOF) 8 { 9 if (a + b > c && a + c > b && b + c > a) 10 { 11 if (a == b || a == c || b == c) 12 { 13 if (a == b && a == c) 14 printf("等边三角形\n"); 15 else if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a) 16 printf("等腰直角三角形\n"); 17 else 18 printf("等腰三角形\n"); 19 } 20 else if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a) 21 printf("直角三角形\n"); 22 else 23 printf("一般三角形\n"); 24 } 25 else 26 printf("不能构成三角形\n"); 27 28 } 29 return 0; 30 }
运行截图:

任务6
源代码:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<time.h> 5 int main() 6 { 7 int lucky_day,i,a; 8 srand(time(0)); 9 lucky_day = rand() % 30 + 1; 10 printf("猜猜2026年4月哪一天是你的luck day\n"); 11 12 for (i = 1; i <=3; i++) 13 { 14 scanf("%d", &a); 15 if (a == lucky_day) 16 printf("恭喜你,猜对了!\n"); 17 else if (a > lucky_day) 18 printf("你猜的日期晚了,你的luck day在前面哦~\n"); 19 else 20 printf("猜早了,你的luck day还没到呢~\n"); 21 } 22 23 printf("次数用光了,4月你的luck day是%d号", lucky_day); 24 return 0; 25 }
运行截图:

实验总结
Ⅰ.任务1中,line 5(#define N 5)不知道干啥的
Ⅱ.任务3中,scanf_s似乎和无限循环有冲突(一直不停地输出stop!),这是不理解的,还有,就如输入gg然后回车,会得到两次go go go和一次something must be wrong,后面那个通过在输入函数的%c前加个空格已得到解决
浙公网安备 33010602011771号