实现买菜功能(未完成代码)

<span style="font-family: Arial, Helvetica, sans-serif;">#include "stdafx.h"</span>

#include <windows.h>
#define PRICEOFARTICHOKES_PERPOUND 1.255
#define PRICEOFBEET_PERPOUND 0.65
#define PRICEOFCARROT_PERPOUND 0.89
float get_the_price_artichokes(float f)
{

	float price;
	price = 0;
	if (f<5)
	{
		price = 3.5 + f*PRICEOFARTICHOKES_PERPOUND;
	}
	else if (f<20&&f>=5)
	{
		price = 10 + f*PRICEOFARTICHOKES_PERPOUND;
	}
	else
	{
		price = 8 + f*PRICEOFARTICHOKES_PERPOUND+0.1*f;
	}
	if (price>100)
	{
		price = price*0.95;
	}
	return price;
}

float get_the_price_beets(float f)
{
	float price;
	price = 0;
	if (f<5)
	{
		price = 3.5 + f*PRICEOFBEET_PERPOUND;
	}
	else if (f<20 && f >= 5)
	{
		price = 10 + f*PRICEOFBEET_PERPOUND;
	}
	else
	{
		price = 8 + f*PRICEOFBEET_PERPOUND + 0.1*f;
	}
	if (price>100)
	{
		price = price*0.95;
	}
	return price;
}

float get_the_price_carrots(float f)
{
	float price;
	price = 0;
	if (f<5)
	{
		price = 3.5 + f*PRICEOFCARROT_PERPOUND;
	}
	else if (f<20 && f >= 5)
	{
		price = 10 + f*PRICEOFCARROT_PERPOUND;
	}
	else
	{
		price = 8 + f*PRICEOFCARROT_PERPOUND + 0.1*f;
	}
	if (price>100)
	{
		price = price*0.95;
	}
	return price;
}

int _tmain(int argc, _TCHAR* argv[])
{
	float a, b, c,p,f;
	char q;
	char ch;
	a = 0; 
	b = 0; 
	c = 0;
	p = 0;
	ch = ' ';
	while (ch!='q')
	{
		printf("a. input the pounds of artichokes \n");
		printf("b. input the pounds of beets \n");
		printf("c. input the pounds of carrots \n");
		printf("q. quit \n");
		scanf("%c", &ch);
		getchar();
		printf("input the pounds:\n");
		scanf("%f", &f);
		switch (ch)
		{
		case 'a': a =a+ get_the_price_artichokes(f); break; 
		case 'b': b =b+ get_the_price_beets(f); break;
		case 'c': c =c+ get_the_price_carrots(f); break;
		}
		p = p + a + b + c;

		printf("the price is %f \n", p);

	}

	system("pause");
	return 0;
}




初步构想

#define PRICEOFARTICHOKES_PERPOUND 1.255
#define PRICEOFBEET_PERPOUND 0.65
#define PRICEOFCARROT_PERPOUND 0.89
这三个功能表示每磅的价格,然后将其写入到对应的函数中

float get_the_price_artichokes(float f)
float get_the_price_beets(float f)
float get_the_price_carrots(float f)
也就是这三个函数,返回的值是根据折扣类型和售价得到的数,float型直接得到对应菜的总价,输入是菜的重量,定义一个float型

实际运行时发现循环有问题。


改错版本1

#include "stdafx.h"
#include <windows.h>
#define PRICEOFARTICHOKES_PERPOUND 1.255
#define PRICEOFBEET_PERPOUND 0.65
#define PRICEOFCARROT_PERPOUND 0.89
float get_the_price_artichokes(float f)
{

	float price;
	price = 0;
	if (f<5)
	{
		price = 3.5 + f*PRICEOFARTICHOKES_PERPOUND;
	}
	else if (f<20&&f>=5)
	{
		price = 10 + f*PRICEOFARTICHOKES_PERPOUND;
	}
	else
	{
		price = 8 + f*PRICEOFARTICHOKES_PERPOUND+0.1*f;
	}
	if (price>100)
	{
		price = price*0.95;
	}
	return price;
}

float get_the_price_beets(float f)
{
	float price;
	price = 0;
	if (f<5)
	{
		price = 3.5 + f*PRICEOFBEET_PERPOUND;
	}
	else if (f<20 && f >= 5)
	{
		price = 10 + f*PRICEOFBEET_PERPOUND;
	}
	else
	{
		price = 8 + f*PRICEOFBEET_PERPOUND + 0.1*f;
	}
	if (price>100)
	{
		price = price*0.95;
	}
	return price;
}

float get_the_price_carrots(float f)
{
	float price;
	price = 0;
	if (f<5)
	{
		price = 3.5 + f*PRICEOFCARROT_PERPOUND;
	}
	else if (f<20 && f >= 5)
	{
		price = 10 + f*PRICEOFCARROT_PERPOUND;
	}
	else
	{
		price = 8 + f*PRICEOFCARROT_PERPOUND + 0.1*f;
	}
	if (price>100)
	{
		price = price*0.95;
	}
	return price;
}

int _tmain(int argc, _TCHAR* argv[])
{
	float a, b, c,p,f;
	char q;
	char ch;
	a = 0; 
	b = 0; 
	c = 0;
	p = 0;
	ch = ' ';
	printf("a. input the pounds of artichokes \n");
	printf("b. input the pounds of beets \n");
	printf("c. input the pounds of carrots \n");
	printf("q. quit \n");
	scanf("%c", &ch);
	while (ch!='q')
	{

		switch (ch)
		{
		case 'a':
			printf("input the pounds:\n");
			scanf("%f", &f); 
			a = a + get_the_price_artichokes(f); ch = ' '; break;
		case 'b': 
			printf("input the pounds:\n");
			scanf("%f", &f);
			b = b + get_the_price_beets(f); ch = ' '; break;
		case 'c': 
			printf("input the pounds:\n");
			scanf("%f", &f); 
			c = c + get_the_price_carrots(f); ch = ' '; break;
		default: break;
		}
		printf("a. input the pounds of artichokes \n");
		printf("b. input the pounds of beets \n");
		printf("c. input the pounds of carrots \n");
		printf("q. quit \n");
		scanf("%c", &ch);
	}
	p = a + b + c;
	printf("the price is %f \n", p);
	system("pause");
	return 0;
}



实现买菜功能

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值