找回密码
 立即注册
首页 业界区 业界 实验2
百里宵月 前天 16:55
task1.c源代码
点击查看代码
  1. #include<stdlib.h>
  2. #include<time.h>
  3. #define N 5
  4. #define N1 80
  5. #define N2 35
  6. int main(){
  7.         int cnt;
  8.         int random_majior, random_no;
  9.     srand(time(NULL));
  10.        
  11.         cnt = 0;
  12.         while (cnt < N) {
  13.                 random_majior = rand() % 2;
  14.                 if (random_majior) {
  15.                         random_no = rand() % N1 + 1;
  16.                         printf("20256343%04d\n", random_no);
  17.                 }
  18.                 else {
  19.                         random_no = rand() % N2 + 1;
  20.                         printf("20256136%04d\n", random_no);
  21.                 }
  22.                 cnt++;
  23.         }
  24.         return 0;
  25. }
复制代码
运行结果截图
去掉前:
1.png

2.png

两次随机生成的数字不同
去掉后:
3.png

4.png

两次随机生成的数虽然随机但相同
回答问题
srand(time(NULL))的作用:设置随机数种子,使生成的数字完全随机
程序的作用:随机生成两个专业的学号中的5个学号
task2.c源代码
点击查看代码
  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. }
复制代码
运行结果截图
5.png

回答问题
若去掉line 47,会引发程序异常导致中止
continue的作用:继续循环中的下一步
task3.c源代码

点击查看代码
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. int main() {
  4.         char color;
  5.         while (1) {
  6.                 scanf("%c", &color);
  7.                 getchar();
  8.                 if (color == 'y')
  9.                 {
  10.                         printf("wait a minute\n");
  11.                 }
  12.                 else if (color == 'r')
  13.                 {
  14.                         printf("stop!\n");
  15.                 }
  16.                 else if (color == 'g')
  17.                 {
  18.                         printf("go go go\n");
  19.                 }
  20.                 else
  21.                 {
  22.                         printf("something must be wrong...\n");
  23.                 }
  24.         }
  25.         return 0;
  26. }
复制代码
运行结果截图
6.png

task4.c源代码
点击查看代码
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. int main() {
  4.         double sum = 0, max_num = 0, min_num = 20000, num;
  5.         printf("输入今日开销,直到输入-1时终止:\n");
  6.         while (1) {
  7.                 scanf("%lf", &num);
  8.                 if (num > max_num) {
  9.                         max_num = num;
  10.                 }
  11.                 if (num > 0 && num < min_num) {
  12.                         min_num = num;
  13.                 }
  14.                 sum += num;
  15.                 if (num == -1)
  16.                         break;
  17.         }
  18.         printf("今日累计消费总额:%.1lf\n", sum);
  19.         printf("今日最高一笔开销:%.1lf\n", max_num);
  20.         printf("今日最低一笔开销:%.1lf\n", min_num);
  21.         return 0;
  22. }
复制代码
运行结果截图
7.png

task5.c源代码
点击查看代码[code]#includeint main() {        int a, b, c;        while (1) {                scanf("%d %d %d", &a, &b, &c);                if(a + b

相关推荐

昨天 14:02

举报

yyds。多谢分享
您需要登录后才可以回帖 登录 | 立即注册