Problem G: Let's open a bookstore.Time limit:1 Sec Memory limit:128 MB
submit:537 solved:331
[Submit] [Status] [Web Board]
Description
A publishing house can publish books and tapes. The books are priced by the price per page multiplied by the number of pages, and the tape is priced according to the price per 10-minute times the minutes of tape recordings. Please define the Publicatioin, book, Tape, and bookstore four classes. which
1. Publication class:
1 The data member double price indicates the unit price (for books, the prices per page; for tapes, the price of recordings per 10 minutes).
2 data member int length indicates the length of the publication, the number of pages for the book, and the Minutes for the tape.
3 member function Gettotalprice () is used to return the pricing of a publication.
4 constructor Publication (double, int) is used to construct a publication.
5 member function double GetPrice () const and int getlength () are used to return the unit price and length of the publication.
6 destructor function.
2. The book class is a subclass of publication.
1 constructor Book (Double,int).
2 rewrite the Gettotalprice return pricing for the parent class, priced to the unit price multiplied by the length (that is, the number of pages).
3 destructor function.
3. Tape is a subclass of publication:
1 constructor tape (double,int).
2 rewrite the Gettotalprice return pricing for the parent class. Note: The price property is the unit cost of recording every 10 minutes, and the length of the tape is not necessarily an integral multiple of 10. Calculate the price, less than 10 minutes, according to 10 minutes calculation.
3 destructor function.
4.BookStore is a bookstore, with data member publications **pubs, is a bookstore owned list of publications; int NUM indicates the number of publications owned by the bookstore. The member function int getnumofbook () and int getnumoftape () calculates the number of books and tape that are owned in the bookstore. The class is already given in Appcode code.
Input
The input is divided into multiple lines.
The first line is an integer m>0, representing an M test case.
Each test is divided into three sections: the first part is the publication type (b indicates book,t tape), unit price, and quantity (pages or minutes).
Output
See examples.
Sample Input3 B 0.10 201 T 0.50 0.40 105Sample OutputCall publication ' s constructor!
Call book ' s constructor!
Call publication ' s constructor!
Call Tape ' s constructor!
Call publication ' s constructor!
Call Tape ' s constructor!
Call publication ' s constructor!
Call book ' s constructor!
Call publication ' s constructor!
Call Tape ' s constructor!
Call publication ' s constructor!
Call Tape ' s constructor!
There are 1 books and 2 tapes. Their total price is 29.50.
Call book ' s de-constructor!
Call publication ' s de-constructor!
Call Tape ' s de-constructor!
Call publication ' s de-constructor!
Call Tape ' s de-constructor!
Call publication ' s de-constructor!
Call book ' s de-constructor!
Call publication ' s de-constructor!
Call Tape ' s de-constructor!
Call publication ' s de-constructor!
Call Tape ' s de-constructor!
Call publication ' s de-constructor!
Call Bookstore ' s de-constructor!HINT
Use typeID to determine the type of actual object to which the object pointer points. Append Code
#include <iostream> #include <set> #include <iterator> #include <string> #include <set> # Include <typeinfo> #include <queue> #include <list> #include <algorithm> #include <cstdio > #include <cctype> #include <cstring> #include <map> #include <vector> #include <cstdlib
> #include <cmath> #include <stack> #include <sstream> #include <iomanip> using namespace std;
Class Publication {protected:double price;
int length; Public:virtual Double Gettotalprice () {} publication (double A,int b):p Rice (a), length (b) {cout<< "Call Publicat" Ion ' s constructor! '
<<endl;}
Double GetPrice () Const{return price;
int GetLength () const {return length;} Virtual~publication () {cout<< "Call publication ' s de-constructor!"
<<endl;}
}; Class Book:public Publication {Public:book (double s,int b):P ublication (s,b) {cout<< "call book ' s constructor!"
<<endl;} Double Gettotalprice () {return price*length;} ~book () {cout<< "call book ' de-constructor!"
<<endl;}
}; Class Tape:public Publication {Public:tape (double s,int b):P ublication (s,b) {cout<< "Call Tape ' s constructor!"
<<endl;}
Double Gettotalprice () {double count1=0;
if (length%10!=0) count1+=1+ (LENGTH/10);
else COUNT1=LENGTH/10;
return count1*1.0*price; } ~tape () {cout<< "Call Tape ' s de-constructor!"
<<endl;}
};
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)) {PU
Bs[i] = new book (P[i]->getprice (), p[i]->getlength ()); else {Pubs[i] = new Tape (P[i]->getprice (), p[i]->getlength());
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" <<setprecision (2) <<fixed<<total<< "."
<<endl;
for (int i = 0; i < cases i++) {delete Pub[i];
} delete[] Pub;
return 0;
}