HW 9
Problem A: 正方形、长方形、立方体
main函数:
int main() { int cases, l, w, h; cin >> cases; for(int i = 1; i <= cases; ++i) { cin >> l; Square squa(l); cout << "A Square length " << squa.length() << ", "; cout << "Perimeter " << squa.perimeter() << ", "; cout << "Area " << squa.area() << endl; } cout << "=========================" << endl; cin >> cases; for(int i = 1; i <= cases; ++i) { cin >> l >> w; Rectangle rect(l, w); cout << "A Rectangle length " << rect.length() << ", width " << rect.width() << ", "; cout << "Perimeter " << rect.perimeter() << ", "; cout << "Area " << rect.area() << endl; } cout << "=========================" << endl; cin >> cases; for(int i = 1; i <= cases; ++i) { cin >> l >> w >> h; Cuboid cubo(l, w, h); cout << "A Cuboid length " << cubo.length() << ", width " << cubo.width() << ", height " << cubo.height() << ", "; cout << "Perimeter " << cubo.perimeter() << ", "; cout << "Area " << cubo.area() << ", "; cout << "Volume " << cubo.volume() << endl; } }
类的继承,在派生类调用基类的重名函数,要用类域限定符。
AC代码:
#include <bits/stdc++.h> using namespace std; class Square { int side; public: Square(int _side):side(_side) { printf("Construct Square (%d)\n",side); } int length() { return side; } int perimeter() { return 4*side; } int area() { return side*side; } }; class Rectangle:public Square { int wide; public: Rectangle(int _len,int _wide):Square(_len),wide(_wide) { printf("Construct Rectangle (%d, %d)\n",Square::length(),wide); } int length() { return Square::length(); } int width() { return wide; } int perimeter() { return (Square::length()+wide)*2; } int area() { return Square::length()*wide; } }; class Cuboid:public Rectangle { int h; public: Cuboid(int len,int wide,int _h):Rectangle(len,wide),h(_h) { printf("Construct Cuboid (%d, %d, %d)\n",Rectangle::length(),Rectangle::width(),h); } int length() { return Rectangle::length(); } int width() { return Rectangle::width(); } int height() { return h; } int perimeter() { return (this->length()+this->width()+this->height())*4; } int area() { return 2*(this->length()*this->width()+this->length()*this->height()+this->width()*this->height()); } int volume() { return this->length()*this->width()*this->height(); } };
Problem B: 驾驶员与汽车
main函数:
void Driver::Drive(Automobile *automobile) { switch (type) { case 'A': cout<<"Driver "<<name<<" can drive "; automobile->run(); break; case 'B': if (typeid(*automobile) == typeid(Dayu) || typeid(*automobile) == typeid(Jianghuai)) cout<<"Driver "<<name<<" cannot drive large bus."<<endl; else { cout<<"Driver "<<name<<" can drive "; automobile->run(); } break; case 'C': if (typeid(*automobile) != typeid(Benz) && typeid(*automobile) != typeid(Buick)) cout<<"Driver "<<name<<" cannot drive bus."<<endl; else { cout<<"Driver "<<name<<" can drive "; automobile->run(); } break; } } int main() { string name; char type; double speed; char automobileType; int cases; Automobile *automobile; cin>>cases; for (int i = 0; i < cases; i++) { cin>>name>>type>>automobileType>>speed; Driver driver(name, type); switch (automobileType) { case 'a': automobile = new Benz(speed); break; case 'b': automobile = new Buick(speed); break; case 'c': automobile = new Zhongba(speed); break; case 'd': automobile = new Beiqi(speed); break; case 'e': automobile = new Dayu(speed); break; case 'f': automobile = new Jianghuai(speed); break; } driver.Drive(automobile); delete automobile; } return 0; }
纯虚函数,抽象基类
虚析构 以及控制两位小数(来自WA一发的惨痛教训。😭)
在c++中,typeid用于返回指针或引用所指对象的实际类型。
AC代码:
#include <bits/stdc++.h> using namespace std; class Automobile { double speed; public: Automobile(double _sp):speed(_sp){} virtual void run()const { printf("%.2fkm/h.\n",speed); } virtual~Automobile() { cout << "An automobile is erased!" << endl; } }; class Benz:public Automobile { public: Benz(double _sp):Automobile(_sp){} void run()const { printf("Benz at speed of "); Automobile::run(); } virtual~Benz() { cout << "A Benz is erased!" << endl; } }; class Buick:public Automobile { public: Buick(double _sp):Automobile(_sp){} void run()const { printf("Buick at speed of "); Automobile::run(); } virtual~Buick() { cout << "A Buick is erased!" << endl; } }; class Zhongba:public Automobile { public: Zhongba(double _sp):Automobile(_sp){} void run()const { printf("Zhongba at speed of "); Automobile::run(); } virtual~Zhongba() { cout << "A Zhongba is erased!" << endl; } }; class Beiqi:public Automobile { public: Beiqi(double _sp):Automobile(_sp){} void run()const { printf("Beiqi at speed of "); Automobile::run(); } virtual~Beiqi() { cout << "A Beiqi is erased!" << endl; } }; class Dayu:public Automobile { public: Dayu(double _sp):Automobile(_sp){} void run()const { printf("Dayu at speed of "); Automobile::run(); } virtual~Dayu() { cout << "A Dayu is erased!" << endl; } }; class Jianghuai:public Automobile { public: Jianghuai(double _sp):Automobile(_sp){} void run()const { printf("Jianghuai at speed of "); Automobile::run(); } virtual~Jianghuai() { cout << "A Jianghuai is erased!" << endl; } }; class Driver { string name ; char type; public: Driver(string _name,char _type):name(_name),type(_type){}; void Drive(Automobile *automobile); };
Problem C: 来开个书店吧
main函数
class BookStore { private: Publication **pubs; int num; public: BookStore(Publication **p, int n) { pubs = new Publication*[n]; num = n; for (int i = 0; i < n; i++) { if (typeid(*(p[i])) == typeid(Book)) { pubs[i] = new Book(p[i]->getPrice(), p[i]->getLength()); } else { pubs[i] = new Tape(p[i]->getPrice(), p[i]->getLength()); } } } int getNumOfBook() { int c = 0; for (int i = 0; i < num; i++) { if (typeid(*(pubs[i])) == typeid(Book)) c++; } return c; } int getNumOfTape() { int c = 0; for (int i = 0; i < num; i++) { if (typeid(*(pubs[i])) == typeid(Tape)) c++; } return c; } ~BookStore() { for (int i = 0; i < num; i++) { delete pubs[i]; } delete[] pubs; cout<<"Call BookStore's de-constructor!\n"; } }; int main() { int cases, date; char type; double total,price; Publication **pub; cin>>cases; pub = new Publication*[cases]; for (int i = 0; i < cases; i++) { cin>>type>>price>>date; switch(type) { case 'B': pub[i] = new Book(price,date); break; case 'T': pub[i] = new Tape(price,date); break; } } BookStore bookStore(pub, cases); cout<<"There are "<<bookStore.getNumOfBook()<<" books and "<<bookStore.getNumOfTape()<<" tapes."; total = 0; for (int i = 0; i < cases; i++) { total += pub[i] -> getTotalPrice(); } cout<<" Their total price is "<<setprecision(2)<<fixed<<total<<"."<<endl; for (int i = 0; i < cases; i++) { delete pub[i]; } delete[] pub; return 0; }
虚析构,虚函数算价格
AC代码:
#include <bits/stdc++.h> using namespace std; class Publication { double price; int length; public: Publication(double _p,int _len):price(_p),length(_len) { cout << "Call Publication's constructor!" << endl; } virtual double getTotalPrice()=0; double getPrice()const { return price; } int getLength() { return length; } virtual ~Publication() { cout << "Call Publication's de-constructor!" << endl; } }; class Book:public Publication { public: Book(double _p,int _l):Publication(_p,_l) { cout << "Call Book's constructor!" << endl; } double getTotalPrice() { return getPrice()*getLength(); } ~Book() { cout << "Call Book's de-constructor!" << endl; } }; class Tape:public Publication { public: Tape(double _p,int _l):Publication(_p,_l) { cout << "Call Tape's constructor!" << endl; } double getTotalPrice() { int d = getLength(); if(d%10!=0) d = d/10+1; else d = d/10; return getPrice()*d; } ~Tape() { cout << "Call Tape's de-constructor!" << endl; } };
Problem D: 不同交通工具的速度
main函数
int main() { int cases, n; char c; string name; Vechicle* vechicle; cout<<"In beginning, we have "<<Vechicle::getNumOfVechicles()<<" vechicles."<<endl; cin>>cases; for (int i = 0; i < cases; i++) { cin>>name>>c>>n; Person person(name); switch (c) { case 'B': vechicle = new Bike(n); break; case 'M': vechicle = new MotoBike(n); break; case 'C': vechicle = new Car(n); break; } person.drive(*vechicle); delete vechicle; } cout<<"At the end, we have "<<Vechicle::getNumOfVechicles()<<" vechicles."<<endl; return 0; }
多态,细节,虚析构,抽象基类
#include <bits/stdc++.h> using namespace std; class Vechicle { int speed; static int numOfVechicles; public: Vechicle(int _sp):speed(_sp){ numOfVechicles++;} static int getNumOfVechicles() { return numOfVechicles; } virtual ~ Vechicle() { // numOfVechicles--; cout << "A vechicle is deleted." << endl; } virtual void show()=0; int get() { return speed; } }; class Bike:public Vechicle { public: Bike(int sp):Vechicle(sp){} void show() { printf("A bike's speed is %dkm/h.\n", Vechicle::get()); } ~Bike() { printf("A bike is deleted.\n"); } }; class MotoBike:public Vechicle { public: MotoBike(int sp):Vechicle(sp){} void show() { printf("A motobike's speed is %dkm/h.\n", Vechicle::get()); } ~MotoBike() { printf("A motobike is deleted.\n"); } }; class Car:public Vechicle { public: Car(int sp):Vechicle(sp){} void show() { printf("A car's speed is %dkm/h.\n", Vechicle::get()); } ~Car() { printf("A car is deleted.\n"); } }; class Person { string name; public: Person(string _n):name(_n){}; void drive(Vechicle&a) { cout << name << " drives. "; a.show(); } }; int Vechicle::numOfVechicles = 0;
Problem E: 水果店
main函数:
int main() { vector<Fruit*> fruits; vector<Fruit*>::iterator itr; char type; double weight, price, totalPrice; while (cin>>type>>weight>>price) { switch(type) { case 'a': fruits.push_back(new Apple(weight, price)); break; case 'b': fruits.push_back(new Banana(weight,price)); break; case 'o': fruits.push_back(new Orange(weight, price)); break; } } totalPrice = 0; for (itr = fruits.begin(); itr != fruits.end(); itr++) { totalPrice = **itr + totalPrice; } cout<<totalPrice<<endl; return 0; }
运算符重载,多态。
#include <bits/stdc++.h> using namespace std; class Fruit { friend class Apple; friend class Banana; friend class Orange; double weight; double price; public: Fruit(double _w,double _p):weight(_w),price(_p){}; virtual double operator +(const double b) { } }; class Apple:public Fruit { public: Apple(double w,double p):Fruit(w,p){}; double operator +(const double b) { return weight*price+b; } }; class Banana:public Fruit { public: Banana(double w,double p):Fruit(w,p){}; double operator +(const double b) { return weight/2.0*price+b; } }; class Orange:public Fruit { public: Orange(double w,double p):Fruit(w,p){}; double operator +(const double b) { if(weight>10) return weight/2.0*price+b; else if(weight>5) return weight*price*0.8+b; else return weight*price+b; } };
Problem F: 图形计数与求面积
main函数:
int main() { int cases; char type; double data; Shape *shape; cin>>cases; cout<<"numOfShapes = "<<Shape::getNumOfShapes(); cout<<", numOfCircles = "<<Circle::getNumOfCircles(); cout<<", numOfSquares = "<<Square::getNumOfSquares()<<endl; for (int i = 0; i < cases; i++) { cin>>type>>data; switch(type) { case 'C': shape = new Circle(data); break; case 'S': shape = new Square(data); break; } cout<<"Area = "<<setprecision(2)<<fixed<<shape->getArea()<<endl; delete shape; } cout<<"numOfShapes = "<<Shape::getNumOfShapes(); cout<<", numOfCircles = "<<Circle::getNumOfCircles(); cout<<", numOfSquares = "<<Square::getNumOfSquares()<<endl; }
考点:类的继承,静态数据成员,静态函数,抽象基类,虚函数
#include <bits/stdc++.h> using namespace std; class Shape { static int NumOfShapes; public: Shape(){cout << "A shape is created!" << endl;NumOfShapes++;} virtual ~Shape(){cout << "A shape is erased!" << endl; } virtual double getArea(){} static int getNumOfShapes(){return NumOfShapes;} }; class Circle:public Shape { static int NumOfCircles; double r; public: Circle(double _r):Shape(),r(_r){NumOfCircles++;cout << "A circle is created!" << endl;} ~Circle(){cout << "A circle is erased!" << endl;} double getArea() { return 3.14*r*r; } static int getNumOfCircles() { return NumOfCircles; } }; class Square:public Shape { static int NumOfSquares; double side; public: Square(double _s):Shape(),side(_s){NumOfSquares++;cout << "A square is created!" << endl; } ~Square(){cout << "A square is erased!" << endl; } double getArea() { return side*side; } static int getNumOfSquares() { return NumOfSquares; } }; int Shape::NumOfShapes = 0; int Circle::NumOfCircles = 0; int Square::NumOfSquares = 0;
Problem G: 向量的运算
main函数:
int main() { Vector vec1, vec2, vec3; int cases, n; cin>>cases; cout<<"vect+vec2\tn*vec1\tvec1*vec2\n"; for (int i = 0; i < cases; i++) { cin>>vec1>>vec2>>n; vec3 = vec1 + vec2; cout<<vec3<<"\t"; vec3 = vec1 * n; cout<<vec3<<"\t"; vec3 = vec1 * vec2; cout<<vec3<<endl; } return 0; }
考点:运算符重载以及输出形式的控制(还有向量叉乘公式 😭QAQ)
AC代码:
#include <bits/stdc++.h> using namespace std; class Vector { int x; int y; int z; public: Vector():x(0),y(0),z(0){}; Vector(int _x,int _y,int _z):x(_x),y(_y),z(_z){}; Vector operator +(const Vector &b) { return Vector(x+b.x,y+b.y,z+b.z); } Vector operator -(const Vector &b) { return Vector(x-b.x,y-b.y,z-b.z); } Vector operator *(const Vector &b) { return Vector(y*b.z-b.y*z,z*b.x-b.z*x,x*b.y-b.x*y); } Vector operator *(const int b) { return Vector(x*b,y*b,z*b); } friend ostream& operator << (ostream &os,Vector &b) { bool f1 = true,f2 = true,f3 = true; if(b.x!=0) { os << b.x << "i"; f1 = false; } if(b.y!=0) { f2 = false; if(b.y>0&&!f1) os << '+' << b.y << "j"; else if(b.y>0&&f1) os << b.y << "j"; else os << b.y << "j"; } if(b.z!=0) { if(b.z>0&&(!f1||!f2)) os << '+' << b.z << "k"; else if(b.z>0&&f1&&f2) os << b.z << "k"; else os << b.z << "k"; f3 = false; } if(f1&&f2&&f3) os<< 0; return os; } friend istream& operator >> (istream &is,Vector&b) { is >> b.x >> b.y >> b.z; return is; } };
过往不恋 未来不迎 当下不负