OJ 2308 Problem D Grape

Description

Jackie开了一家水果店,店里昨日剩余m公斤葡萄(Grape),今天新进了n公斤,葡萄的属性是重量(weight)。
葡萄的每公斤单价是price。请你编写程序计算葡萄的总重量和总价格。
用C++编写Grape类来完成代码,调用格式见“Append Code”。
Grape::weight()葡萄的重量。
Grape::weight(double)修改葡萄的重量。
Grape::value()葡萄的价格。
Grape的构造根据题意设计。

Input

先输入葡萄的单价price,然后分别输入昨天剩余和今天新进的葡萄重量。

Output

输出葡萄的总重量和总价格。

Sample Input

22.5
11
22

Sample Output

33
742.5

HINT

Append Code

int main()
{
    double w, price;
    cin >> price;
    cin >> w;
    const Grape grap(price, w);
    Grape grape(price);
    cin >> w;
    grape.weight(w);
    cout << grape.weight() + grap.weight() << endl;
    cout << grape.value() + grap.value() << endl;
}

Acceped Code

#include <iostream>
#include <cstdio>
using namespace std;

class Cherry {
private:
    double m;
    double n, w;
public:
    Cherry(double wei = 0, double amo = 0) : n(wei), m(amo) { w = m * n; }
    double weight() {
        return w;
    }
    Cherry& weight(double wei) {
        w = wei;
        return *this;
    }
    Cherry& sold_weight(double sw) {
        w -= sw;
        if(w <= 0) w = 0;
        return *this;
    }
};
class Grape {
private:
    double price, w;
public:
    Grape(double p = 0, double ww = 0) : price(p), w(ww) {}
    double weight() const { return w; } //注意grap是const类型变量
    Grape& weight(double ww) {
        w = ww;
        return *this;
    }
    //注意grap是const类型变量
    double value() const { 
        return w * price;
    }
};
int main()
{
    double w, price;
    cin >> price;
    cin >> w;
    const Grape grap(price, w);
    Grape grape(price);
    cin >> w;
    grape.weight(w);
    cout << grape.weight() + grap.weight() << endl;
    cout << grape.value() + grap.value() << endl;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值