Problem C: 农夫果园

Description

一果农,家里有苹果树、梨树,2016年收成不错,现在需要你来编程,求一下去年总共收获了多少斤的水果。

Input

第一行N表示之后有N行输入。

之后的N行输入,每行一个水果。包括一个字符和一个整数。

其中字符是‘a’或‘p’,分别表示是一个苹果或梨。整数是这个水果的重量。

Output

见样例。

Sample Input

5a 10p 20a 30p 40a 50

Sample Output

The 1th fruit is created.The 1th apple is created.The 2th fruit is created.The 1th peach is created.The 3th fruit is created.The 2th apple is created.The 4th fruit is created.The 2th peach is created.The 5th fruit is created.The 3th apple is created.There are 5 fruits, including 3 apples and 2 peaches. Their total weights is 150.

HINT

请使用C++的面向对象来实现,练习下如何编写类、使用类。

Append Code

#include <iostream>
using namespace std;

class sellFruit
{
private:
    static int numOfFruits, numOfApples,numOfPeach,numOfAll;
public:
    sellFruit()
    {

    }
    void setFruit(char f,int h)
    {
        if(f == 'a')
        {
            numOfApples++;
            numOfFruits++;
            cout << "The "<<numOfFruits<<"th fruit is created."<< endl;
            cout << "The "<<numOfApples<<"th apple is created." << endl;
        }
        else
        {
            numOfFruits++;
            numOfPeach++;
            cout << "The "<<numOfFruits<<"th fruit is created."<< endl;
            cout << "The "<<numOfPeach<<"th peach is created." << endl;
        }
        numOfAll += h;
    }
    static void getAll()
    {
        cout << "There are "<<numOfFruits<<" fruits, including "<<numOfApples<<" apples and "<<numOfPeach<<" peaches. Their total weights is "<<numOfAll<<".";
    }
};
int sellFruit::numOfAll = 0;
int sellFruit::numOfPeach = 0;
int sellFruit::numOfFruits = 0;
int sellFruit::numOfApples= 0;
int main()
{
    int num,h;
    char c;
    sellFruit *s;
    cin >> num;
    s = new sellFruit[num];
    for(int i = 0;i < num;i++)
    {
        cin >> c >> h;
        s[i].setFruit(c,h);
    }
    sellFruit::getAll();
    return 0;
}


设计题目:农夫果园 一个农场,专门种植销售各类水果,在这个系统中需要描述下列水果: 葡萄:Grape 草莓:Strawberry 苹果:Apple 水果与其他的植物有很大的不同,水果最终是可以采摘食用的。那么一个自然的做法就是建立一个各种水果都适用的接口,以便与农场里的其他植物区分开。水果接口规定出所有的水果都必须实现的接口,包括任何水果必须具备的方法:种植plant(),生长grow(),收获harvest()。 Apple类是水果中的一种,因此它实现了水果接口所声明的所有方法。另外,由于苹果是多年生植物,因此多出一个treeAge性质,描述苹果树的树龄。 Grape类是水果类的一种,也实现Fruit接口中所声明的所有方法。但由于葡萄分为有籽和无籽的两种,因此比通常的水果多出一个seedless性质。 Strawberry类也是水果的一种,也实现了Fruit接口。 农场的园丁也是系统的一部分,自然要由一个合适的类来代表。这个类就是FruitGardener,它会根据农场老板的要求,使用factory()方法创建出不同的水果对象,比如苹果(Apple),葡萄(Grape)或草莓(Strawberry)的实例。而如果接到不合法的要求,会提示错误。 农场的市场调查员也是系统的一部分,也需要一个类代表,这个类是MarketInquirer,它通过inquire()调查今年市场上哪一种水果热销。 农场的老板也是系统的一部分,仍需要一个类来代表,这个类是FruitBoss,他会根据市场调查员的反馈信息,通知农场的园丁今年种植哪种水果。 要求:请你根据上述系统需求,用Java语言设计这个农场系统,发挥你的想象力吧!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值