0

我需要帮助来编写这个程序我对 C 和 C++ 还很陌生。请帮助我开始。我不知道从哪里开始,但我正在尽力而为。我需要编写一个自动售货机程序。

  1. 在屏幕上显示饮品列表
  2. 允许用户退出或选择饮料
  3. 如果用户选择饮料,他或她将输入要插入机器的金额
#include<stdio.h>
#define system

void menu(void);
void chocolate(void);
void price(void);
void change(void);
void end(void);


int choc[10]={0,1,2,3,4,5,6,7,8,9};


int main()
{
 int intro;
 int option;
 int choice;
 int money;
 int payment;

e: menu();

 printf("Enter Your Option: ");
 scanf("%d",&intro);
 printf("\n");

 if(intro==1)
 {

w:  printf("The Chocolates\n\n");
  printf("1.Choose Chocolate\n");
  printf("2.View A Price To Be Paid\n");
  printf("3.Back To Main Menu\n\n");

  printf("Rules: Machine can only receive RM1 and RM5 only! \n\n\n");

  printf("Enter Your Option: ");
  scanf("%d",&option);
  printf("\n\n");

  if(option==1)
  {
   system("cls");
   printf("Name list of chocolate: ");
   printf("\n\n");
   printf("1. Cadburry Black Forest");          printf("\tRM3.00\n");
   printf("2. Kitkat");                         printf("\t\t\tRM2.00\n");
   printf("3. Crispy");                         printf("\t\t\tRM2.00\n");
   printf("4. Crunch");                         printf("\t\t\tRM2.00\n");
   printf("5. Mars");                           printf("\t\t\tRM2.00\n");
   printf("6. Kinder Bueno");                   printf("\t\tRM2.00\n");
   printf("7. White Kinder Bueno");             printf("\t\tRM2.00\n");
   printf("8. Milky Bar");                      printf("\t\t\tRM2.00\n");
   printf("9. M&M");                            printf("\t\t\t\tRM2.00\n");
   printf("10. Choki Choki");                    printf("\t\t\tRM2.00\n");
   printf("\n\n");

   printf("Choose Chocolate: ");
   scanf("%d",&choice);
   printf("\n\n");

   switch(choice)
   {
    case 0 :
     chocolate();
     printf("You choose Cadburry Black Forest\n\n");
     goto w;
    case 1 :
     chocolate();
     printf("You choose Kitkat\n\n");
     goto w;
    case 2 :
     chocolate();
     printf("You choose Crispy\n\n");
     goto w;
    case 3 :
     chocolate();
     printf("You choose Crunch\n\n");
     goto w;
    case 4 :
     chocolate();
     printf("You choose Mars\n\n");
     goto w;
    case 5 :
     chocolate();
     printf("You choose Kinder Bueno\n\n");
     goto w;
    case 6 :
     chocolate();
     printf("You choose White Kinder Bueno\n\n");
     goto w;
    case 7 :
     chocolate();
     printf("You choose Milky Bar\n\n");
     goto w;
    case 8 :
     chocolate();
     printf("You choose M&M\n\n");
     goto w;
    case 9 :
     chocolate();
     printf("You choose Choki Choki\n\n");
     goto w;

    default : goto e;
   }
  }
  else if(option==2)
  {
   printf("View A Price To Be Paid: ");
   scanf("%d",&choice);
   printf("\n\n");

   switch(choice)
   {
    case 0 :
     price();
     printf("RM3.00\n\n");
     goto w;
    case 1 :
     price();
     printf("RM2.00\n\n");
     goto w;
    case 2 :
     price();
     printf("RM2.00\n\n");
     goto w;
    case 3 :
     price();
     printf("RM2.00\n\n");
     goto w;
    case 4 :
     price();
     printf("RM2.00\n\n");
     goto w;
    case 5 :
     price();
     printf("RM2.00\n\n");
     goto w;
    case 6 :
     price();
     printf("RM2.00\n\n");
     goto w;
    case 7 :
     price();
     printf("RM2.00\n\n");
     goto w;
    case 8 :
     price();
     printf("RM2.00\n\n");
     goto w;
    case 9 :
     price();
     printf("RM2.00\n\n");
     goto w;

     default : goto e;
   }
  }
  else
   goto e;
 }

 else if(intro==2)
 {
  printf("Enter your money: ");
  scanf("%d",&money);
  printf("\n\n");

   if(choice==0)
   {
    printf("Your payment is RM3");
    printf("\n\n");
    choc[0]=payment;
    goto e;
   }
   if(choice==1)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[1]=payment;
    goto e;
   }
   if(choice==2)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[2]=payment;
    goto e;
   }
   if(choice==3)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[3]=payment;
    goto e;
   }
   if(choice==4)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[4]=payment;
    goto e;
   }
   if(choice==5)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[5]=payment;
    goto e;
   }
   if(choice==6)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[6]=payment;
    goto e;
   }
   if(choice==7)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[7]=payment;
    goto e;
   }
   if(choice==8)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[8]=payment;
    goto e;
   }
   if(choice==9)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[9]=payment;
    goto e;
   }
   else
   {
    printf("ERROR!\n\n");
    goto e;
   }
 }

 else if(intro==3)
 {
 change();
 goto e;
 }

 else if(intro==4)
 {
  printf("Enjoy it!");

   if(choice==0)
   {
    printf("Thank you and enjoy your Cadburry Black Forest!");
    printf("\n\n");
    choc[0]=0;
    goto e;
   }
   if(choice==1)
   {
    printf("Thank you and enjoy your Kitkat!");
    printf("\n\n");
    choc[1]=0;
    goto e;
   }
   if(choice==2)
   {
    printf("Thank you and enjoy your Crispy!");
    printf("\n\n");
    choc[2]=0;
    goto e;
   }
   if(choice==3)
   {
    printf("Thank you and enjoy your Crunch!");
    printf("\n\n");
    choc[3]=0;
    goto e;
   }
   if(choice==4)
   {
    printf("Thank you and enjoy your Mars!");
    printf("\n\n");
    choc[4]=0;
    goto e;
   }
   if(choice==5)
   {
    printf("Thank you and enjoy your Kinder Bueno!");
    printf("\n\n");
    choc[5]=0;
    goto e;
   }
   if(choice==6)
   {
    printf("Thank you and enjoy your White Kinder Bueno!");
    printf("\n\n");
    choc[6]=0;
    goto e;
   }
   if(choice==7)
   {
    printf("Thank you and enjoy your Milky Bar!");
    printf("\n\n");
    choc[7]=0;
    goto e;
   }
   if(choice==8)
   {
    printf("Thank you and enjoy your M&M!");
    printf("\n\n");
    choc[8]=0;
    goto e;
   }
   if(choice==9)
   {
    printf("Thank you and enjoy your Choki Choki!");
    printf("\n\n");
    choc[9]=0;
    goto e;
   }
   else
   {
    printf("Error! Try Again.\n\n");
    goto e;
   }
 }
  else
  end();

}

void menu(void)
{
 printf("|WELCOME TO CHOCOLATES VENDING MACHINE|\n\n");

 printf("1.The Chocolates\n");
 printf("2.Login To Your Account\n");
 printf("3.View Change\n");
 printf("4.Exit Vending Machine\n");
 printf("5.Exit Program\n\n");
}

void chocolate(void)
{
 int u[30];
 char o[30];
 FILE *choclot;
 choclot=fopen("parking.txt","a");
 printf("Your Chocolate: ");
 scanf("%s",&o);
 printf("Total Price: ");
 scanf("%d",&u);
 fprintf(choclot,"Chocolate: %s Total Price: %d\n\n",o,u);
 fclose(choclot);
}

void change(void)
{
 int i;
 printf("Your Balance: \n\n");
 for(i=0;i<10;i++)
  printf("%d",choc[i]);
  printf("\n\n\n\n");
}

void end(void)
{
 system("cls");
 printf("\t\t\tTHANK YOU FOR USING THIS SERVICES\n");
  printf("\t\t\t\t  Visit Us At \n\n");
  printf("\t\t\t    vendingmachine.com.my\n\n\n");
  printf(" First Programmer : Adawiyah\n");
  printf(" First Programmer : Vijayah Santhi\n");
} 

我的程序正确吗?我的程序是否整洁有序?goto 语句有那么糟糕吗?对不起,我的英语不好。我正在尽我最大的努力在这几天内完成我的项目。我需要在下周向我的讲师介绍。

很抱歉给您添麻烦了。我不知道如何在这里复制和粘贴。

4

1 回答 1

0

在您的第一次尝试中,您的问题是您从他们输入的金额中减去他们选择的数字,而不是他们选择的饮料的成本。你可以简单地修改你的程序:

  1. float创建一个名为的新类型变量cost
  2. 在每个case之前break,设置cost为他们选择的饮料的成本。
  3. 而不是:

    change = money - choice;
    

    你会想要:

    change = money - cost;
    

您无法解决的另一个问题(如果钱多于所需,则显示更改;如果没有,则显示额外需要多少)相当简单。首先,你可以看看是否有足够的钱:

if(money >= cost) {
    // There's enough money.
}else{
    // There's not enough money.
}

然后你可以适当地打印:

if(money >= cost) {
    printf("Change: %.2f\n", money - cost);
}else{
    printf("You need RM%.2f more.\n", cost - money);
}

I have not had enough time to review your newest code, but your first try, while it could have some further improvements as suggested by other people in that thread, was not far off from where you needed it to be.

于 2013-03-30T05:24:31.947 回答