《C++ Primer》改写书店程序,不用连续输入

《C++ Primer》的书店程序有一个缺点,就是相同isbn的数据必须连续输入。本程序将Sale_data类结合vector,实现了不用连续输入。说明在注释里。

Sale_data.h

#include
#include
#include
#include
using namespace::std;

/*Sale_data类功能:
/1 使用get_isbn()获取isbn
/2 使用read(或add(),功能相同,只是为了练习代码),print来input,output一个Sale_data
/3 使用display合并相同数据,并展示
/4 使用assign赋值
/5 使用superposition(或combine(),功能相同)累加Sale_data
*/

class Sales_data{
    friend istream & read(istream &, Sales_data &);
    friend ostream & print(ostream &, const Sales_data &);

    string isbn;
    int book_num;
    double unit_price, discount, revenue;
    bool able;

public:
    Sales_data() = default;
    //Sale_data类有6个成员变量,前四个由用户输入,第五个(revenue)计算得到,第六个表示一个性能参数,与用户无关,默认为1(True),当赋值为0(False)时表示禁用,在后面程序用到时详细说明
    Sales_data(string a, int b, double c, double d):isbn(a),book_num(b),unit_price(c),discount(d),revenue(book_num*unit_price*discount),able(1){}
    ~Sales_data(){};


    void get_isbn();
    void add();
    void assign(Sales_data book);
    void superposition(Sales_data book);
    void display(vector &gather);

    Sales_data& combine(const Sales_data &book);
};

void Sales_data::get_isbn(){
    cout<>isbn>>book_num>>unit_price>>discount;
    revenue = book_num*unit_price*discount;
}
void Sales_data::assign(Sales_data book){
    isbn = book.isbn;
    book_num = book.book_num;
    unit_price = book.unit_price;
    discount = book.discount;
    revenue = book_num*unit_price*discount;

}

void Sales_data::superposition(Sales_data book){
    if(isbn==book.isbn){
        //累加相同Sale_data
        revenue = revenue + book.revenue;
        unit_price = (book_num*unit_price*discount+book.book_num*book.unit_price*book.discount)/(book_num+book.book_num);
        discount = discount*(((double)book_num/(book_num+book.book_num)))+book.discount*(((double)book.book_num/(book_num+book.book_num)));
        book_num += book.book_num;
    }
    else
        cout<<"Can't superposition"< &gather){
    //设置able类成员变量,当遍历整个vector时,先以第一个Sale_data为基准,找到所有isbn相同的Sale_data,累加。再将第一个Sale_data与其余Sale_data的able属性设置为0,即禁用这些Sale_data
    if(!gather.empty()){
    cout<<"display:"<::iterator it = gather.begin();it != gather.end();++it){
        //
        if(it->able==1){
                       for(auto it2 = gather.begin()+(it-itit+1);it2 != gather.end();++it2){
                //这两个if(itx->able==1)实现了只有able为1的Sale_data进行运算
                if(it2->able==1){
                    if((it->isbn)==(it2->isbn)){
                        (*it).superposition(*it2);
                        it2->able = 0;
                    }
                }
               }
        cout<<"\nisbn:"<isbn<<"\nbook_num:"<book_num<<"\nunit_price:"<unit_price<<"\ndiscount:"<discount<<"\nrevenue:"<revenue<>book.isbn>>book.book_num>>book.unit_price>>book.discount;
    book.revenue = book.book_num*book.unit_price*book.discount;
    return is;
}


bookstore.cpp

#include
#include
#include"Sales_item2.h"
#include
using namespace::std;

int main(){
    //定义一个vector储存多个Sale_data
    static vector gather;
    cout<<"Please input your data:"<>if_quit;
    while((if_quit != 'q')){
        //当不进行特殊操作时,继续输入
        if((if_quit!='n')&&(if_quit!='?')&&(if_quit!='+')){
            Sales_data booklast("299999", 999, 999, 999);
            booklast.add();
            gather.push_back(booklast);
            book.assign(booklast);
        }
        if(if_quit=='n'){
            Sales_data booklast("299999", 999, 999, 999);
            booklast.assign(book);
            gather.push_back(booklast);
        }
        if(if_quit=='?'){
            cout<<"<";
            book.get_isbn();
            cout<<">";
            print(cout, book)<>if_quit;
    }

    cout<<"The all data:"<


运行结果

Last login: Mon Jul 16 20:02:38 on console

Please input your data:
isbn:   book_num:   unit_price: discount:   
201701  5       100     1
Enter "q" to quit
Enter "n" to make a same data
Enter "?" to inqurey the last book
Enter "+" to make a combine and display them(one is the last data and one is you will input next)
Enter any another to continue
Your enter:a
201701  10      100     0.9
Enter "q" to quit
Enter "n" to make a same data
Enter "?" to inqurey the last book
Enter "+" to make a combine and display them(one is the last data and one is you will input next)
Enter any another to continue
Your enter:+
201701  20      100     0.85

isbn:201701
book_num:30
unit_price:86.6667
discount:0.866667
revenue:2600
Enter "q" to quit
Enter "n" to make a same data
Enter "?" to inqurey the last book
Enter "+" to make a combine and display them(one is the last data and one is you will input next)
Enter any another to continue
Your enter:?
<201701>
isbn:201701
book_num:20
unit_price:100
discount:0.85
revenue:1700
Enter "q" to quit
Enter "n" to make a same data
Enter "?" to inqurey the last book
Enter "+" to make a combine and display them(one is the last data and one is you will input next)
Enter any another to continue
Your enter:a
201702  1       50      1
Enter "q" to quit
Enter "n" to make a same data
Enter "?" to inqurey the last book
Enter "+" to make a combine and display them(one is the last data and one is you will input next)
Enter any another to continue
Your enter:n
Enter "q" to quit
Enter "n" to make a same data
Enter "?" to inqurey the last book
Enter "+" to make a combine and display them(one is the last data and one is you will input next)
Enter any another to continue
Your enter:q
The all data:
display:

isbn:201701
book_num:35
unit_price:85.9048
discount:0.885714
revenue:3100

isbn:201702
book_num:2
unit_price:50
discount:1
revenue:100

real    2m29.718s
user    0m0.003s
sys 0m0.003s

Press ENTER or type command to continue



附件下载:bookstore.cpp
        Sale_data.h

你可能感兴趣的:(《C++ Primer》改写书店程序,不用连续输入)