这是作业十中的三道题,据说是去年期末水果系列的题,做的不太顺,又犯了一些经典错误。
一、Problem A: Orange
Time Limit: 1 Sec Memory Limit: 2 MB
Description
Jackie开了一家水果店,店里新进了一些橙子(Orange)和橘子(Mandarin),
现在需要把它们装箱(FruitBox),每箱有6个水果(橙子或橘子)。
现在知道水果的单价(每公斤价格)和每个水果的重量(公斤数)。
请你编写程序计算这箱水果的价格
用C++编写Orange类、Mandarin类和FruitBox来完成代码,调用格式见“Append Code”。
FruitBox::input()输入一箱水果的重量。
FruitBox::value()一箱水果的价格。
Orange、Mandarin和FruitBox的构造根据题意设计。
Input
第一行输入橙子的单价,第二行是6个橙子的重量。
第三行输入橘子的单价,第四行是6个橘子的重量。
Output
第一行输出这箱橙子的价格,第二行输出这箱橘子的价格。
Sample Input
6.8
0.8 1.1 0.95 0.92 0.77 0.84
5.8
0.9 0.85 0.72 0.90 0.69 0.45
Sample Output
36.584
26.158
Append Code
int main()
{
double orange_price, mandarin_price;
cin >> orange_price;
FruitBox<Orange> orange(orange_price);
orange.input();
cout << orange.value() << endl;
cin >> mandarin_price;
FruitBox<Mandarin> mandarin(mandarin_price);
mandarin.input();
cout << mandarin.value() << endl;
}
这个题主要是又忘记给sum赋初值了,版本高一点的编译器能过,放OJ上就错了,可千万不要再犯了。
AC代码:
#include<iostream>
using namespace std;
class Orange
{
private:
double w;
public:
Orange(double a = 0):w(a){}
double getW()
{
return w;
}
void setW(double x)
{
w = x;
}
~Orange(){}
};
class Mandarin
{
private:
double w;
public:
Mandarin(double a = 0):w(a){}
double getW()
{
return w;
}
void setW(double x)
{
w = x;
}
~Mandarin(){}
};
template<typename T>
class FruitBox
{
private:
T *fruit;
double price;
public:
FruitBox(double pr = 0):price(pr){fruit = new T [6];}
void input()
{
for(int i = 0; i < 6; i++)
{
d