找回密码
 立即注册
首页 业界区 安全 实验2作业

实验2作业

任俊慧 昨天 23:35
任务1
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. #define N 5
  5. #define N1 80
  6. #define N2 35
  7. int main(){
  8.         int cnt;
  9.         int random_major,random_no;
  10.         cnt=0;
  11.         while(cnt<N){
  12.                 random_major=rand()%2;
  13.                 if(random_major){
  14.                         random_no=rand()%N1+1;
  15.                         printf("20256343%04d\n",random_no);
  16.                 }
  17.                 else{
  18.                         random_no=rand()%N2+1;
  19.                         printf("20256136%04d\n",random_no);
  20.                 }
  21.                 cnt++;
  22.         }
  23.         system("pause");
  24.         return 0;
  25. }
复制代码
  
1.png

 问题1
会使第二次再购买饮料使的总价钱为第一次和第二次总价相加之和,使其购买总价计算错误
问题2
不再执行continue后面的语句,重新进行一次循环
任务3
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. int main() {
  4.         int choice, quantity;
  5.         float total_price = 0, amount_paid, change;
  6.         while (1) {
  7.             printf("\n自动饮料售卖机菜单:\n");
  8.             printf("1. 可乐 - 3 元/瓶\n");
  9.             printf("2. 雪碧 - 3 元/瓶\n");
  10.             printf("3. 橙汁 - 5 元/瓶\n");
  11.             printf("4. 矿泉水 - 2 元/瓶\n");
  12.             printf("0. 退出购买流程\n");
  13.             printf("请输入饮料编号: ");
  14.             scanf("%d", &choice);
  15.             if (choice == 0)
  16.                 break;
  17.             if (choice < 1 || choice > 4) {
  18.                     printf("无效的饮料编号,请重新输入。\n");
  19.                     continue;
  20.             }
  21.             printf("请输入购买的数量: ");
  22.             scanf("%d", &quantity);
  23.             if (quantity < 0) {
  24.                     printf("购买数量不能为负数,请重新输入。\n");
  25.                     continue;
  26.             }
  27.             if(choice == 1 || choice == 2)
  28.                     total_price += 3 * quantity;
  29.                 else if(choice == 3)
  30.                         total_price += 5 * quantity;
  31.                 else
  32.                         total_price += 2 * quantity;
  33.                 printf("请投入金额: ");
  34.                 scanf("%f", &amount_paid);
  35.                 change = amount_paid - total_price;
  36.                 printf("本次购买总价: %.2f 元\n", total_price);
  37.                 printf("找零: %.2f 元\n", change);
  38.                 total_price = 0;
  39.         }
  40.         printf("感谢您的购买,欢迎下次光临!\n");
  41.         system("pause");
  42.         return 0;
  43. }
复制代码
  
2.png

任务4

[code]#include#include#define _CRT_SECURE_NO_WARNINGSint main(){        float x,max=0,min=20000,ans=0;        printf("输入今日开销,直到输入-1终止:\n");        scanf("%f",&x);        while(x>0){                ans=ans+x;                if(x>max)                        max=x;                if(x

相关推荐

您需要登录后才可以回帖 登录 | 立即注册