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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值