这个比上一个简单多了,做oj做到心态炸裂
描述
编写TableTennisPlayer类和RatedPlayer类(RatedPlayer类继承TableTennisPlayer类),其中TableTennisPlayer类的定义如下所示:
class TableTennisPlayer { private: string firstname; string lastname; bool hasTable; public: TableTennisPlayer(const string &, const string &, bool); string FirstName() const; string LastName() const; bool HasTable() const; };实现后,通过以下main函数的测试:
int main() { string firstname, lastname; bool hasTable; int rating; char flag; while (cin >> flag) { if (flag == 'T') { cin >> firstname >> lastname >> hasTable; TableTennisPlayer tp(firstname, lastname, hasTable); if (tp.HasTable()) cout << tp.FirstName() << " " << tp.LastName() << " has a table.\n"; else cout << tp.FirstName() << " " << tp.LastName() << " hasn't a table.\n"; } else if (flag == 'R') { cin >> firstname >> lastname >> hasTable >> rating; RatedPlayer rp(rating, firstname, lastname, hasTable); if (rp.HasTable()) cout << rp.FirstName() << " " << rp.LastName() << " has a table. The rating is " << rp.Rating() << ".\n"; else cout << rp.FirstName() << " " << rp.LastName() << " hasn't a table. The rating is " << rp.Rating() << ".\n"; } } return 0; }输入
输入多行,每一行以'T'或'R'开头,'T'表示本行接下来输入一个TableTennisPlayer对象的信息,包括firstname,lastname和hasTable(是否有乒乓球台);'R'表示本行接下来输入一个RatedPlayer对象的信息,包括firstname,lastname,hasTable和rating(选手的得分)。
输出
一行输入对应一行输出,输出详见main函数
输入样例 1
T Bill Gates 1输出样例 1
Bill Gates has a table.输入样例 2
R Jike Zhang 0 19000输出样例 2
Jike Zhang hasn't a table. The rating is 19000.提示
bool类型的输入:0表示false,1表示true
// // main.cpp // 1 // // Created by Joth Jiang on 2021/6/26. // #include <iostream> #include <cstring> using namespace std; class TableTennisPlayer { protected: string firstname; string lastname; bool hasTable; public: TableTennisPlayer(){} TableTennisPlayer(const string &a, const string &b, bool c){ firstname=a; lastname=b; hasTable=c; }; string FirstName() const{ return firstname; }; string LastName() const{ return lastname; }; bool HasTable() const{ return hasTable; }; }; class RatedPlayer:public TableTennisPlayer{ private: int rating; public: RatedPlayer(){} RatedPlayer(int,const string &,const string &,bool ); int Rating(){ return rating; }; }; int main() { string firstname, lastname; bool hasTable; int rating; char flag; while (cin >> flag) { if (flag == 'T') { cin >> firstname >> lastname >> hasTable; TableTennisPlayer tp(firstname, lastname, hasTable); if (tp.HasTable()) cout << tp.FirstName() << " " << tp.LastName() << " has a table.\n"; else cout << tp.FirstName() << " " << tp.LastName() << " hasn't a table.\n"; } else if (flag == 'R') { cin >> firstname >> lastname >> hasTable >> rating; RatedPlayer rp(rating, firstname, lastname, hasTable); if (rp.HasTable()) cout << rp.FirstName() << " " << rp.LastName() << " has a table. The rating is " << rp.Rating() << ".\n"; else cout << rp.FirstName() << " " << rp.LastName() << " hasn't a table. The rating is " << rp.Rating() << ".\n"; } } return 0; } RatedPlayer::RatedPlayer(int a,const string &b,const string &c,bool d ){ rating=a; firstname=b; lastname=c; hasTable=d; }
描述
实现一个Person类,再实现一个Student类,要求Student类继承Person类,通过以下测试:
int main() { Person * p; p = new Person; p->input(); p->display(); delete p; p = new Student; p->input(); p->display(); delete p; return 0; }输入
输入包含两行,第一行为一个姓名(不包含空格);第二行为一个学号和一个姓名(学号、姓名都不包含空格),学号和姓名之间用空格间隔。学号和姓名用string类型。
输出
输出为两行,第一行为一个姓名;第二行为学号和姓名,学号和姓名之间用空格间隔
输入样例 1
Mary 001 Mary输出样例 1
Mary 001 Mary提示
不能修改main函数,否则不得分
// // main.cpp // 2 // // Created by Joth Jiang on 2021/6/26. // using namespace std; class Person{ protected: string name; string totle; public: void input(){ cin>>name; cin>>totle>>name; }; void display(){ cout<<name<<endl<<totle<<" "<<name; }; }; class Student:public Person{ public: void input(); void display(); };//Person 实现于此
好家伙直接固定主函数了,居然有了代码默写题的即视感
描述
编写以下两个类,请实现并使得以下的main函数成立
class Item_base { //图书商品 public: Item_base(const string & book_ISBN = "", double sales_price = 0.0); string Get_ISBN() const; virtual double Net_price(int) const; //返回购买指定数量的图书的总价 virtual ~Item_base(); protected: string ISBN; //图书序列号 double price; //单价 }; class Bulk_Item : public Item_base { //根据购买数量打折 public: Bulk_Item(const string & book_ISBN = "", double sales_price = 0.0, int min_qty = 0, double discount = 0.0); double Net_price(int) const; //返回根据购买数量打折后的总价 private: int min_qty; // 买够这个数量可以打相应的折扣 double discount; //折扣 }; int main() { Item_base book("0-001-0001-1", 10.0); Bulk_Item bulk1("0-001-0001-1", 10.0, 5, 0.1); Bulk_Item bulk2("0-001-0001-1", 10.0, 10, 0.2); int num; while (cin >> num) { cout << bulk1.Get_ISBN() << "\t" << num << "\t"; Item_base * p; if (num >= 10) p = &bulk2; else if (num >= 5) p = &bulk1; else p = &book; cout << p->Net_price(num) << "\n"; } return 0; }输入
图书的数量。
输出
输出购买的图书的ISBN,它的数量以及总的价格。(用main函数中输出的形式即可)
输入样例 1
2 6 11输出样例 1
0-001-0001-1 2 20 0-001-0001-1 6 54 0-001-0001-1 11 88
// // main.cpp // 3 // // Created by Joth Jiang on 2021/6/26. // #include <iostream> #include<cstring> using namespace std; class Item_base { //图书商品 public: Item_base(const string & book_ISBN = "", double sales_price = 0.0); string Get_ISBN() const{ return ISBN; }; virtual double Net_price(int n) const{ return price*n; }; //返回购买指定数量的图书的总价 virtual ~Item_base(){ }; protected: string ISBN; //图书序列号 double price; //单价 }; class Bulk_Item : public Item_base { //根据购买数量打折 public: Bulk_Item(const string & book_ISBN = "", double sales_price = 0.0, int min_qty = 0, double discount = 0.0); double Net_price(int n) const{ return (1-discount)*n*price; }; //返回根据购买数量打折后的总价 private: int min_qty; // 买够这个数量可以打相应的折扣 double discount; //折扣 }; int main() { Item_base book("0-001-0001-1", 10.0); Bulk_Item bulk1("0-001-0001-1", 10.0, 5, 0.1); Bulk_Item bulk2("0-001-0001-1", 10.0, 10, 0.2); int num; while (cin >> num) { cout << bulk1.Get_ISBN() << "\t" << num << "\t"; Item_base * p; if (num >= 10) p = &bulk2; else if (num >= 5) p = &bulk1; else p = &book; cout << p->Net_price(num) << "\n"; } return 0; } Item_base::Item_base(const string & a,double b){ ISBN=a; price=b; } Bulk_Item::Bulk_Item(const string &a,double b,int c,double d){ ISBN=a; price=b; min_qty=c; discount=d; }
描述
设计一个抽象类Vehicle,由它派生出类Car和类Truck
类Car包含名称、颜色和载客数三个数据成员
类Truck包含名称、颜色和载重量三个数据成员
使用如下函数测试你的程序:
int main() { char type; while (cin >> type) { Vehicle *p; string name, color; cin >> name >> color; if (type == 'C') { int pas; cin >> pas; Car car(name, color, pas); p = &car; p->display(); } else if (type == 'T') { double cap; cin >> cap; Truck truck(name, color, cap); p = &truck; p->display(); } } return 0; }输入
多组输入。
每组输入的开头是'C'或者'T',代表此时我们要输入的车的信息分别为Car和Truck.
当输入的是Car的时候,我们要输入它的名称,颜色和载客数;
当输入的是Truck的时候,我们要输入它的名称,颜色和载重量。
然后用抽象类的Vehicle的指针来指向派生类对象,从而实现不同类中display()函数的多态性。
输出
根据不同车种类,输出不同信息,具体见样例输出。
输入样例 1
C Benz black 3 T Dongfeng white 8.5输出样例 1
Benz car, black, passenger volume: 3 Dongfeng truck, white, cargo capacity: 8.5(t)提示
Vehicle中可包含名称和颜色数据成员,并且有纯虚函数以提供接口完成信息的显示;
在派生类Car和Truck中根据需要实现纯虚函数以及添加成员。
仔细读输出样例的格式,输出的不同项目之间用一个空格隔开。
// // main.cpp // 4 // // Created by Joth Jiang on 2021/6/26. // //Vehicle Car Truck 实现于此 #include <iostream> #include<cstring> using namespace std; class Vehicle{ protected: string name; string color; public: virtual void display()const=0; }; class Car:public Vehicle{ private: int pas; public: Car(string &a ,string &b ,int c){ name=a; color=b; pas=c; }; virtual void display()const{ cout<<name<<" car, "<<color<<", passenger volume: "<<pas<<endl; }; }; class Truck:public Vehicle{ private: double cap; public: Truck(string & a,string & b,double c){ name=a; color=b; cap=c; }; virtual void display()const{ cout<<name<<" truck, "<<color<<", cargo capacity: "<<cap<<"(t)"<<endl; }; };
描述
编写程序计算长方体、圆柱体和球的表面积和体积。要求先定义一个抽象类Shape如下:
class Shape { public: Shape() {} virtual ~Shape() {} virtual double Area() = 0; virtual void Input() = 0; virtual double Volume() = 0; };使用Shape类派生出长方体类、圆柱体类、球类,在这些类里分别实现继承的纯虚函数。使用如下代码测试运行。
void work(Shape *s) { s->Input(); cout << s->Area() << " " << s->Volume() << endl; } int main() { char c; while (cin >> c) { switch (c) { case 'y': { Shape *s = new Cylinder(); work(s); delete s; break; } case 'c': { Shape *s = new Cuboid(); work(s); delete s; break; } case 'q': { Shape *s = new Ball(); work(s); delete s; break; } default: break; } } return 0; }输入
输入包含多行,每行首先是一个字符'c','y','q',分别表示输入长方体、圆柱体或球的信息,接下来是对应的输入。
输出
每行输入对应一行输出,表示该形状的表面积和体积,以空格分隔。
输入样例 1
c 3 4 5 y 3 5 q 5输出样例 1
94 60 150.796 141.372 314.159 523.599提示
pi的精度要足够,比如使用 const double pi = acos(-1);
// // main.cpp // 5 // // Created by Joth Jiang on 2021/6/26. // #include <iostream> #include <cstring> #include <cmath> double const pi=3.1415926; using namespace std; class Shape { public: Shape() {} virtual ~Shape() {} virtual double Area() = 0; virtual void Input() = 0; virtual double Volume() = 0; }; class Cuboid:public Shape{ private: int c; int k; int g; public: void Input(){ cin>>c>>k>>g; }; double Area(){ return (c*k+c*g+k*g)*2; }; double Volume(){ return c*k*g; }; }; class Cylinder:public Shape{ private: int r; int g; public: void Input(){ cin>>r>>g; }; double Area(){ return pi*pow(r,2)*2+2*pi*r*g; }; double Volume(){ return pi*pow(r,2)*g; }; }; #include <iostream> #include <cstring> #include <cmath> double const pi=3.1415926; using namespace std; class Shape { public: Shape() {} virtual ~Shape() {} virtual double Area() = 0; virtual void Input() = 0; virtual double Volume() = 0; }; class Cuboid:public Shape{ private: int c; int k; int g; public: void Input(){ cin>>c>>k>>g; }; double Area(){ return (c*k+c*g+k*g)*2; }; double Volume(){ return c*k*g; }; }; class Cylinder:public Shape{ private: int r; int g; public: void Input(){ cin>>r>>g; }; double Area(){ return pi*pow(r,2)*2+2*pi*r*g; }; double Volume(){ return pi*pow(r,2)*g; }; }; #include <iostream> #include <cstring> #include <cmath> double const pi=3.1415926; using namespace std; class Shape { public: Shape() {} virtual ~Shape() {} virtual double Area() = 0; virtual void Input() = 0; virtual double Volume() = 0; }; class Cuboid:public Shape{ private: int c; int k; int g; public: void Input(){ cin>>c>>k>>g; }; double Area(){ return (c*k+c*g+k*g)*2; }; double Volume(){ return c*k*g; }; }; class Cylinder:public Shape{ private: int r; int g; public: void Input(){ cin>>r>>g; }; double Area(){ return pi*pow(r,2)*2+2*pi*r*g; }; double Volume(){ return pi*pow(r,2)*g; }; }; class Ball:public Shape{ private: int r; public: void Input(){ cin>>r; }; double Area(){ return 4*pi*pow(r,2); }; double Volume(){ return 4*pi*pow(r,3)/3; }; }; void work(Shape *s) { s->Input(); cout << s->Area() << " " << s->Volume() << endl; } int main() { char c; while (cin >> c) { switch (c) { case 'y': { Shape *s = new Cylinder(); work(s); delete s; break; } case 'c': { Shape *s = new Cuboid(); work(s); delete s; break; } case 'q': { Shape *s = new Ball(); work(s); delete s; break; } default: break; } } return 0; }
天呐还有几个oj啊,不想每天早上起来都在做oj了呜呜呜呜