ZJUPTA 7-1 Vending Machine

PTA 7-1 vending machine is
a simple vending machine as shown in the figure. There are 10 items on racks 1 and 2. They are numbered 1-10 in sequence, marked with prices and names, and each number corresponds to an operable button for selecting items. If the product on the shelf is bought by the user, the product will be automatically taken out of the locker and sent to the shelf to ensure that there will be a product on the shelf. The user can put in more coins at one time, and can choose a variety of products, and the vending machine can output the products and give change at one time.
insert image description here
The operation method for the user to purchase the product is:

(1) Put in coins from the "coin inlet", and put in multiple coins or banknotes in sequence. Coins can support 1 yuan (paper money, coins), 2 yuan (paper money), 5 yuan (paper money), 10 yuan (paper money). When putting in the money, the controller will first check the money to identify the currency value, and count the total value of the currency , displayed on the display screen of the controller, prompting the user to confirm that the coins have been put in;

(2) After confirming that the coins have been put in, the user can select the product by pressing the numbered button on the outside of the corresponding product with his finger. Every time a product is selected, the controller of the vending machine will judge whether there are enough coins to purchase. If there are enough coins, it will automatically count the items according to the number and calculate the required currency value, and prompt the balance. If the money is insufficient, the controller will prompt "Insufficient money". The user can cancel the purchase and all put-in coins will be returned to the user.

Input format:
first enter the currency value sequence, ending with -1, and then enter multiple purchased product numbers in sequence, ending with -1.

Output format:
output the total amount of coins and change, as well as the name and quantity of purchased goods.

Input sample:

1 1 2 2 5 5 10 10 -1
1 2 3 5 1 6 9 10 -1

Sample output:

Total:36yuan,change:19yuan
Table-water:2;Table-water:1;Table-water:1;Milk:1;Beer:1;Oolong-Tea:1;Green-Tea:1;

code:

#include <stdio.h>
#include<stdlib.h>
#include <math.h>
#include<string.h>
struct ITEM{
    
    
	char* name;
	int price;
};
int main()
{
    
    
	struct ITEM goods[11]=
	{
    
     
		{
    
    " ",0},{
    
    "Table-water",1},{
    
    "Table-water",1},{
    
    "Table-water",1},{
    
    "Coca-Cola",2},{
    
    "Milk",2},
		{
    
    "Beer",3},{
    
    "Orange-Juice",3},{
    
    "Sprite",3},{
    
    "Oolong-Tea",4},{
    
    "Green-Tea",4}
	};
	static int result[11];//储存购买结果
	int putinMoney = 0;//输入的总钱
	for (;;)
	{
    
    
		int temp = 0;
		scanf("%d", &temp);
		if (temp == -1)
		{
    
    
			break;
		}
		putinMoney += temp;
	}
	int printfMoney = putinMoney;
	while (1)
	{
    
    
		int index = 0;
		scanf("%d", &index);
		if (index == -1)
		{
    
    
			break;
		}
		if (putinMoney >= goods[index].price)
		{
    
    
			putinMoney -= goods[index].price;
			result[index]++;
		}
		else {
    
    
			printf("Insufficient money");
			return 0;
		}
	}
	printf("Total:%dyuan,change:%dyuan\n", printfMoney, putinMoney);
	for (int i = 1; i <= 10; i++)
	{
    
    
		if (result[i] != 0)
		{
    
    
			printf("%s:%d;", goods[i].name, result[i]);
		}
	}
	return 0;
}

おすすめ

転載: blog.csdn.net/qq_18571109/article/details/112385206