Demo entry 6768066

Berton

   

Submitted by anonymous on Nov 01, 2018 at 07:15
Language: C. Code size: 2.3 kB.

#include <stdio.h>

int money_rest_0_5 = 2;
int money_rest_1 = 0;
int led_condition = 0;

//**************是否是5毛 或 一元
float money_just(float money_input)
{
	int start = 1;
	while (money_input == 0.5 || money_input == 1 || start)
	{
		start = 0;

		//投币,判断
		printf("请投币(输入金额数值;单位:元):");
		scanf_s("%f", &money_input);

		//*****不规范处理*********************
		if (money_input == 0.5)
		{
			printf("投入0.5元\n");
			money_rest_0_5 += 1;
			break;
		}
		else if (money_input == 1)
		{
			printf("投入1元\n");
			money_rest_1 += 1;
			break;
		}
		else
		{
			printf("投币错误;");
			printf("//***执行退币操作***//\n");
		}
	}

	if (money_input == 0.5)
	{
		return 0.5;
	}
	else if (money_input == 1)
	{
		return 1;
	}

}

//*************指示灯亮灭*****没钱时亮
int led_display()
{
	if (money_rest_0_5 == 0)
	{
		if (led_condition == 0)
		{
			printf("//***执行指示灯无零钱亮起操作***//\n");
			led_condition = 1;
		}
	
		return 0;
	}
	else if (money_rest_0_5 >= 1)
	{
		if (led_condition == 1)
		{
			printf("//***执行指示灯有零钱熄灭操作***//\n");
			led_condition = 0;
		}
		
		return 1;
	}
	else
	{
		printf("//***执行机器故障锁死操作***//\n");
		return -1;
	}
}




//*************
void main()
{
	float money_input = 0;
	int drink_num = 0;

	while (1)
	{
		if (led_display() != -1)		//状态正常 if1
		{
			money_input = money_just(money_input);

			if (money_input == 0.5)		//5毛,选饮料,指示灯不亮,送  if2.1
			{
				printf("/***执行选饮料操作***/\n");     //选饮料
				printf("输入选择饮料序号:(1~10)");
				scanf_s("%d", &drink_num);

				if (drink_num >= 1 && drink_num <= 10)
				{
					if (led_display() == 1)           //指示灯不亮
					{
						printf("/***执行送饮料操作***/\n");
					}
				}
			}//if2.1
			if (money_input == 1)		//1元,选饮料,指示灯亮,送/返钱 if2.2
			{
				printf("/***执行选饮料操作***/\n");     //选饮料
				printf("输入选择饮料序号(1~10):");
				scanf_s("%d", &drink_num);

				if (drink_num >= 1 && drink_num <= 10)
				{
					if (led_display() == 1)           //有零钱指示灯不亮
					{					
						printf("/***执行找零钱操作***/\n");
						printf("/***执行送饮料操作***/\n");
						money_rest_0_5 -= 1;
					}
					else if (led_display() == 0)           //无零钱指示灯亮
					{
						printf("/***执行退钱操作***/\n");
					}
				}
			}//if2.2
		}//if1

		printf("\n");
	}//while
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).