Problem C: 农夫果园

来源:互联网 发布:淘宝客链接怎么生成 编辑:程序博客网 时间:2024/03/29 13:27

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


原创粉丝点击