2012年9月13日 星期四

C程式練習HW1-1

Homework1-1
Write a program to simulate a vending machine.

  1. List the items and price Your vending machine should have at least 10 items. Each student should choice different items to sell. 
  2. The customer first selects an item.
  3. The customer put coins into the machine.
  4. The machine discharges the selected item and give you the change if the coins are enough, otherwise indicates there is not enough money.



#include
#include

void main(){
int number=0;
int coins=0;
int good[10]={15,15,15,10,10,15,15,15,15,15};

do{
printf("List of Items of Vending Machine:\n");
printf("1: Water - 15 2: Coke - 15 3: 7 UP - 15 4: Gatorade - 10 5: Woolong Tea - 10\n");
printf("6: Sprite -15 7: Funta - 15 8: Coke - 15 9: Lemonade - 15 10: Root Beer - 15\n");
printf("Selection =>");scanf("%d",&number);
printf("Put coins:");scanf("%d",&coins);

while(coins{
int temp=0;
printf("Not enough. Put more coins.\n");
printf("Put coins:");scanf("%d",&temp);
coins+=temp;
}


switch(number)
{
case 1:
printf("You got a bottle of Water.\n"); break ;
case 2:
printf("You got a can of coke.\n"); break ;
case 3:
printf("You got a bottle of 7 UP.\n"); break ;
case 4:
printf("You got a bottle of Gatorade.\n"); break ;
case 5:
printf("You got a bottle of Woolong Tea.\n"); break ;
case 6:
printf("You got a bottle of Sprite.\n"); break ;
case 7:
printf("You got a bottle of Funta.\n"); break ;
case 8:
printf("You got a can of coke.\n"); break ;
case 9:
printf("You got a bottle of Lemonade.\n"); break ;
case 10:
printf("You got a bottle of Root Beer.\n"); break ;
default:
break ;
}

printf("Return: %d\n\n",coins-good[number-1]);

}while(1);
system ("pause");
}

沒有留言: