task2

三.实验内容

1.实验任务1

tsak1.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
#define N1 80
#define N2 35
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() % N1 + 1;
      printf("20256343%04d\n", random_no);
 }
    else {
      random_no = rand() % N2 + 1;
      printf("20256136%04d\n", random_no);
   }
    cnt++;
 }
  return 0;
}     

 捕获

捕获

问题1:代码 srand(time(NULL));起使每次运行结果根据时间不同而变化的作用

问题2:功能为随机生成学号

2.实验任务2

task2.c

#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. 橙汁 - 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;
   }
    if(choice == 1 || choice == 2)
      total_price += 3 * quantity;
    else if(choice == 3)
      total_price += 5 * quantity;
    else
      total_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("感谢您的购买,欢迎下次光临!\n");
  return 0;
}

捕获

问题1:去掉这行代码后,total_price变量不会在每次交易结束后再变回0

问题2:第一处是输入无效的饮料编号时,跳过本次循环。第二处是输入负数的购买数量时,跳过本次循环。

3.实验任务3

task3.c

 


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

 屏幕截图 2025-10-14 191024

4.实验任务4

task4.c

 1 #include <stdio.h>
 2 int main() {
 3     float expense;
 4     float total = 0.0f;
 5     float max = 0.0f;
 6     float min = 20000.0f;  
 7     printf("输入今日开销直到输入-1终止:\n");
 8     while (1) {
 9         scanf("%f", &expense);
10         if (expense == -1.0f) {
11             break;
12         }
13         if (expense <= 0.0f || expense > 20000.0f) {
14             printf("(0 < 每笔消费 <= 20000):\n");
15             continue;
16         }
17         total += expense;
18         if (expense > max) {
19             max = expense;
20         }
21         if (expense < min) {
22             min = expense;
23         }
24     }
25     printf("今日累计消费总额:%.1f\n", total);
26     printf("今日最高一笔开销:%.1f\n", max);
27     printf("今日最低一笔开销:%.1f\n", min);
28     return 0;
29 }

屏幕截图 2025-10-14 192614

5. 实验任务5

task5.c

 

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

屏幕截图 2025-10-14 194643

6. 实验任务6

task6.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
    srand(time(0));
    int lucky_day = rand() % 30 + 1;
    int guess;
    int count = 0;
    printf("猜猜2025年11月哪一天是你的lucky day\n");
    printf("开始喽,你有三次机会,猜吧(1~30):");
    while (count < 3) {
        scanf("%d", &guess);
        count++;
        if (guess == lucky_day) {
            printf("哇,猜中了:)\n");
            return 0;
        } else if (guess < lucky_day) {
            printf("你猜的日期早了,你的lucky day还没到呢\n");
        } else {
            printf("你猜的日期晚了,你的lucky day在前面哦\n");
        }
        if (count < 3) {
            printf("再猜(1~30):");
        }
    }
    printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号\n", lucky_day);
    return 0;
}

屏幕截图 2025-10-14 200404

屏幕截图 2025-10-14 200538

四、实验结论

1. 实验任务1

 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
#define N1 80
#define N2 35
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() % N1 + 1;
      printf("20256343%04d\n", random_no);
 }
    else {
      random_no = rand() % N2 + 1;
      printf("20256136%04d\n", random_no);
   }
    cnt++;
 }
  return 0;
}     

屏幕截图 2025-10-14 203808

问题1:代码 srand(time(NULL));起使每次运行结果根据时间不同而变化的作用

问题2:功能为随机生成学号

2. 实验任务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. 橙汁 - 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;
   }
    if(choice == 1 || choice == 2)
      total_price += 3 * quantity;
    else if(choice == 3)
      total_price += 5 * quantity;
    else
      total_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("感谢您的购买,欢迎下次光临!\n");
  return 0;
}

屏幕截图 2025-10-14 204107

问题1:去掉这行代码后,total_price变量不会在每次交易结束后再变回0

问题2:第一处是输入无效的饮料编号时,跳过本次循环。第二处是输入负数的购买数量时,跳过本次循环。

3. 实验任务3

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

屏幕截图 2025-10-14 191024

4. 实验任务4

 1 #include <stdio.h>
 2 int main() {
 3     float expense;
 4     float total = 0.0f;
 5     float max = 0.0f;
 6     float min = 20000.0f;  
 7     printf("输入今日开销直到输入-1终止:\n");
 8     while (1) {
 9         scanf("%f", &expense);
10         if (expense == -1.0f) {
11             break;
12         }
13         if (expense <= 0.0f || expense > 20000.0f) {
14             printf("(0 < 每笔消费 <= 20000):\n");
15             continue;
16         }
17         total += expense;
18         if (expense > max) {
19             max = expense;
20         }
21         if (expense < min) {
22             min = expense;
23         }
24     }
25     printf("今日累计消费总额:%.1f\n", total);
26     printf("今日最高一笔开销:%.1f\n", max);
27     printf("今日最低一笔开销:%.1f\n", min);
28     return 0;
29 }

屏幕截图 2025-10-14 192614

5. 实验任务5 

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

屏幕截图 2025-10-14 194643

6. 实验任务6

  

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
    srand(time(0));
    int lucky_day = rand() % 30 + 1;
    int guess;
    int count = 0;
    printf("猜猜2025年11月哪一天是你的lucky day\n");
    printf("开始喽,你有三次机会,猜吧(1~30):");
    while (count < 3) {
        scanf("%d", &guess);
        count++;
        if (guess == lucky_day) {
            printf("哇,猜中了:)\n");
            return 0;
        } else if (guess < lucky_day) {
            printf("你猜的日期早了,你的lucky day还没到呢\n");
        } else {
            printf("你猜的日期晚了,你的lucky day在前面哦\n");
        }
        if (count < 3) {
            printf("再猜(1~30):");
        }
    }
    printf("次数用光啦。偷偷告诉你,11月你的lucky day是%d号\n", lucky_day);
    return 0;
}

屏幕截图 2025-10-14 200404

屏幕截图 2025-10-14 200538

 

posted @ 2025-10-18 16:27  郎赛  阅读(6)  评论(0)    收藏  举报