Problem D: 水果店

Problem D: 水果店

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 359   Solved: 184
[ Submit][ Status][ Web Board]

Description

小明经营着一个不大的水果店(似曾相识哦~),只销售苹果、香蕉和桔子。为了促销,小明制定了如下定价策略:

1. 苹果:按斤论价,每斤P元,买W斤,则需支付W*P元。

2. 香蕉:半价,每斤P元,买W斤,则需支付W/2*P元。

3.桔子:按斤论价,每斤P元,买W斤。如果W>10,则打半价,即需支付W*P/2元;否则如果W>5,则打八折,即需支付W*P*0.8元;其他情况不打折,即需支付W*P元。

请用C++来计算某个顾客采购的水果的总价。该程序至少应有:

1. Fruit类:是个抽象类,是Apple、Banana和Orange的父类。支持重载的加法运算。

2. Apple、Banana和Orange类:分别对应于苹果、香蕉和桔子三种水果,每种水果执行不同的定价策略。

Input

输入为多行,每行格式为:

C W P

其中C是水果类型(a、b、o分别代表苹果、香蕉和桔子),W和P分别是顾客购买的相应水果的重量和每斤水果的单价。

Output

输出顾客需支付的总金额。

Sample Input

a 1 1b 1 1o 1 1

Sample Output

2.5

HINT

注意包含vector容器的头文件。


需要用多态来实现。


Append Code


#include <bits/stdc++.h>

using namespace std;

class Fruit
{
public:
	double w;
	double p;
	Fruit(double w, double p) :w(w), p(p){}
	virtual double total() = 0;
	friend double operator +(Fruit &a, double b)
	{
		return a.total() + b;
	}
};
class Apple :public Fruit
{
public:
	Apple(double w, double p) :Fruit(w, p){}
	double total()
	{
		return p*w ;
	}
};
class Banana :public Fruit
{
public:
	Banana(double w, double p) :Fruit(w, p){}
	double total()
	{
		return p*w*0.5;
	}
};
class Orange :public Fruit
{
public:
	Orange(double w, double p) :Fruit(w, p){}
	double total()
	{
		if (w > 10)
		{
			return p *w*0.5;
		}
		if (w > 5)
			return p*w*0.8;
		else
			return p*w;
	}
};
int main()
{
	vector<Fruit*> fruits;
	vector<Fruit*>::iterator itr;
	char type;
	double weight, price, totalPrice;
	while (cin >> type >> weight >> price)
	{
		switch (type)
		{
		case 'a':
			fruits.push_back(new Apple(weight, price));
			break;
		case 'b':
			fruits.push_back(new Banana(weight, price));
			break;
		case 'o':
			fruits.push_back(new Orange(weight, price));
			break;
		}
	}
	totalPrice = 0;
	for (itr = fruits.begin(); itr != fruits.end(); itr++)
	{
		totalPrice = **itr + totalPrice;
	}
	cout << totalPrice << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Usher_Ou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值