B.不菲的价格 | |||||
| |||||
Description | |||||
利用面向对象设计方法解决问题。 现有材料类Mate的完整定义,其中weight表示重量(double型),name表示名称(string型)。 现完成程序解决各种戒指信息的处理,戒指包括weight重量(double型),name表示材料名称(string型),price表示单价(int型),brand表示品牌(string型)等4部分信息。 每枚戒指的总价格=weight*price+100,输出数据保留小数2位。 | |||||
Input | |||||
有一组测试数据。一行输入一枚戒指的重量、材料名称、单价、品牌,数据之间以空格分隔。 | |||||
Output | |||||
针对输入数据,有一行输出。输出的内容分别是每枚戒指的品牌、材料、总价格。数据之间以空格分隔。 | |||||
Sample Input | |||||
3.57 Gold 240 Huamei | |||||
Sample Output | |||||
Huamei Gold 956.80 | |||||
Hint | |||||
#include<iostream> #include<stdio.h> #include<cmath> #include<iomanip> #include<cstdio> class Mate{ protected: double weight; string name; public: double getW(){return weight;} string getN(){return name;} void setW(double w){weight=w;} void setN(string n){name=n;} }; int main() { Ring r; double w; int p; string n,b; cin>>w>>n>>p>>b; r.setALL(w,n,p,b); cout<<r.getB()<<" "<<r.getN(); printf(" %.2lf\n",r.getTotal()); return 0; } |
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<iomanip>
#include<cstdio>
using namespace std;
class Mate {
protected:
double weight;
string name;
public:
double getW() { return weight; }
string getN() { return name; }
void setW(double w) { weight = w; }
void setN(string n) { name = n; }
};
class Ring
{
public:
string name, brand;
double weight;
int price;
void setALL(double w, string n, int p, string b)
{
name = n, weight = w, price = p, brand = b;
}
string getB()
{
return brand;
}
string getN()
{
return name;
}
double getTotal()
{
return weight * price + 100;
}
};
int main()
{
Ring r;
double w;
int p;
string n, b;
cin >> w >> n >> p >> b;
r.setALL(w, n, p, b);
cout << r.getB() << " " << r.getN();
printf(" %.2lf\n", r.getTotal());
return 0;
}