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

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

Sale_data.h

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
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<Sales_data> &gather);

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

void Sales_data::get_isbn(){
    cout<<isbn;
}
void Sales_data::add(){
    cin>>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"<<endl;
}

void Sales_data::display(vector<Sales_data> &gather){
    //设置able类成员变量,当遍历整个vector时,先以第一个Sale_data为基准,找到所有isbn相同的Sale_data,累加。再将第一个Sale_data与其余Sale_data的able属性设置为0,即禁用这些Sale_data
    if(!gather.empty()){
    cout<<"display:"<<endl;
    auto itit = gather.begin();
    for(vector<Sales_data>::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:"<<it->isbn<<"\nbook_num:"<<it->book_num<<"\nunit_price:"<<it->unit_price<<"\ndiscount:"<<it->discount<<"\nrevenue:"<<it->revenue<<endl;
        }
    }
    }
    else
        cerr<<"No data?!"<<endl;
}

Sales_data& Sales_data::combine(const Sales_data &book){
    if(isbn==book.isbn){
        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"<<endl;

    return *this;
}

ostream &print(ostream &os, const Sales_data &book){
    os<<"\nisbn:"<<book.isbn<<"\nbook_num:"<<book.book_num<<"\nunit_price:"<<book.unit_price<<"\ndiscount:"<<book.discount<<"\nrevenue:"<<book.revenue;
    return os;
}

istream &read(istream &is, Sales_data &book){
    is>>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<iostream>
#include<vector>
#include"Sales_item2.h"
#include<string>
using namespace::std;

int main(){
    //定义一个vector储存多个Sale_data
    static vector<Sales_data> gather;
    cout<<"Please input your data:"<<endl;
    cout<<"isbn:\t"<<"book_num:\t"<<"unit_price:\t"<<"discount:\t"<<endl;
    char if_quit;//进行循环控制,选择不同功能,由于一开始的想法是控制退出,所以命名用的是if_quit
    //类的实例化中booklast用于push_back进容器,不对其做函数操作
    //而其余(比如book)的则用于函数操作
    //没有使用do-while循环,是因为while循环有分支,先输入一个Sale_data便于后续操作(比如“n”)
    Sales_data booklast("299999", 999, 999, 999);
    read(cin, booklast);
    gather.push_back(booklast);

    //将最后一个booklast暂存,以便于“?”操作,以下同此功能
    static Sales_data book("299999", 999, 999, 999);
    book.assign(booklast);

    //if_quit根据用户输入实现五种功能
    cout<<"Enter \"q\" to quit"<<endl;
    cout<<"Enter \"n\" to make a same data"<<endl;
    cout<<"Enter \"?\" to inqurey the last book"<<endl;
    cout<<"Enter \"+\" to make a combine and display them(one is the last data and one is you will input next)"<<endl;
    cout<<"Enter any another to continue"<<endl;
    cout<<"Your enter:";
    cin>>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)<<endl;
        }
        if(if_quit=='+'){
            Sales_data booklast("299999", 999, 999, 999);
            read(cin, booklast);
            gather.push_back(booklast);

            book.combine(booklast);
            print(cout, book)<<endl;
            book.assign(booklast);
        }
    cout<<"Enter \"q\" to quit"<<endl;
    cout<<"Enter \"n\" to make a same data"<<endl;
    cout<<"Enter \"?\" to inqurey the last book"<<endl;
    cout<<"Enter \"+\" to make a combine and display them(one is the last data and one is you will input next)"<<endl;
    cout<<"Enter any another to continue"<<endl;
    cout<<"Your enter:";

        cin>>if_quit;
    }

    cout<<"The all data:"<<endl;
    Sales_data bookx;
    bookx.display(gather);

    return 0;
}


运行结果

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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,907评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,987评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,298评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,586评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,633评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,488评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,275评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,176评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,619评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,819评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,932评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,655评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,265评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,871评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,994评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,095评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,884评论 2 354

推荐阅读更多精彩内容