《C++ Primer》改寫書店程序,不用連續(xù)輸入

《C++ Primer》的書店程序有一個缺點(diǎn)昼捍,就是相同isbn的數(shù)據(jù)必須連續(xù)輸入识虚。本程序?qū)ale_data類結(jié)合vector,實(shí)現(xiàn)了不用連續(xù)輸入妒茬。說明在注釋里担锤。

Sale_data.h

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace::std;

/*Sale_data類功能:
/1 使用get_isbn()獲取isbn
/2 使用read(或add(),功能相同乍钻,只是為了練習(xí)代碼)肛循,print來input铭腕,output一個Sale_data
/3 使用display合并相同數(shù)據(jù),并展示
/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)計算得到夹孔,第六個表示一個性能參數(shù)被盈,與用戶無關(guān),默認(rèn)為1(True)搭伤,當(dāng)賦值為0(False)時表示禁用只怎,在后面程序用到時詳細(xì)說明
    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){
    //設(shè)置able類成員變量,當(dāng)遍歷整個vector時闷畸,先以第一個Sale_data為基準(zhǔn)尝盼,找到所有isbn相同的Sale_data,累加佑菩。再將第一個Sale_data與其余Sale_data的able屬性設(shè)置為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)實(shí)現(xiàn)了只有able為1的Sale_data進(jìn)行運(yùn)算
                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;//進(jìn)行循環(huán)控制,選擇不同功能殿漠,由于一開始的想法是控制退出赴精,所以命名用的是if_quit
    //類的實(shí)例化中booklast用于push_back進(jìn)容器,不對其做函數(shù)操作
    //而其余(比如book)的則用于函數(shù)操作
    //沒有使用do-while循環(huán)绞幌,是因為while循環(huán)有分支,先輸入一個Sale_data便于后續(xù)操作(比如“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根據(jù)用戶輸入實(shí)現(xiàn)五種功能
    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')){
        //當(dāng)不進(jìn)行特殊操作時莲蜘,繼續(xù)輸入
        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;
}


運(yùn)行結(jié)果

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末谭确,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子票渠,更是在濱河造成了極大的恐慌逐哈,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,123評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件问顷,死亡現(xiàn)場離奇詭異昂秃,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)杜窄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,031評論 2 384
  • 文/潘曉璐 我一進(jìn)店門肠骆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人塞耕,你說我怎么就攤上這事蚀腿。” “怎么了扫外?”我有些...
    開封第一講書人閱讀 156,723評論 0 345
  • 文/不壞的土叔 我叫張陵唯咬,是天一觀的道長纱注。 經(jīng)常有香客問我,道長胆胰,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,357評論 1 283
  • 正文 為了忘掉前任刻获,我火速辦了婚禮蜀涨,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蝎毡。我一直安慰自己厚柳,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,412評論 5 384
  • 文/花漫 我一把揭開白布沐兵。 她就那樣靜靜地躺著别垮,像睡著了一般。 火紅的嫁衣襯著肌膚如雪扎谎。 梳的紋絲不亂的頭發(fā)上碳想,一...
    開封第一講書人閱讀 49,760評論 1 289
  • 那天,我揣著相機(jī)與錄音毁靶,去河邊找鬼胧奔。 笑死,一個胖子當(dāng)著我的面吹牛预吆,可吹牛的內(nèi)容都是我干的龙填。 我是一名探鬼主播,決...
    沈念sama閱讀 38,904評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼拐叉,長吁一口氣:“原來是場噩夢啊……” “哼岩遗!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起凤瘦,我...
    開封第一講書人閱讀 37,672評論 0 266
  • 序言:老撾萬榮一對情侶失蹤宿礁,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后廷粒,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體窘拯,經(jīng)...
    沈念sama閱讀 44,118評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,456評論 2 325
  • 正文 我和宋清朗相戀三年坝茎,在試婚紗的時候發(fā)現(xiàn)自己被綠了涤姊。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,599評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡嗤放,死狀恐怖思喊,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情次酌,我是刑警寧澤恨课,帶...
    沈念sama閱讀 34,264評論 4 328
  • 正文 年R本政府宣布舆乔,位于F島的核電站,受9級特大地震影響剂公,放射性物質(zhì)發(fā)生泄漏希俩。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,857評論 3 312
  • 文/蒙蒙 一纲辽、第九天 我趴在偏房一處隱蔽的房頂上張望颜武。 院中可真熱鬧,春花似錦拖吼、人聲如沸鳞上。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,731評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽篙议。三九已至,卻和暖如春怠硼,著一層夾襖步出監(jiān)牢的瞬間鬼贱,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,956評論 1 264
  • 我被黑心中介騙來泰國打工拒名, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留吩愧,地道東北人。 一個月前我還...
    沈念sama閱讀 46,286評論 2 360
  • 正文 我出身青樓增显,卻偏偏與公主長得像雁佳,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子同云,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,465評論 2 348

推薦閱讀更多精彩內(nèi)容