簡易客戶資料庫_鏈表實現_代碼及解析

來源:互聯網
上載者:User

#include <iostream>#include <iomanip>#include <cstdio>#include <windows.h>using namespace std;// 傳回值類型enum status{success,fail,underflow,overflow,range_error};// 客戶類struct Client{    string name;    string id;    double credit;    struct Client *next;};/* 可以儲存客戶的資料庫 鏈表實現*/class DataBase{private:    Client *headp;protected:    // 客戶數目    int count;public:    DataBase(void);    ~DataBase(void);    // 資料庫是否為空白    bool empty(void) const;    // 客戶數目    int length(void) const;    // 建立資料庫    status create(void);    // 清空資料庫    status clear(void);    // 新增加一個客戶    status insert(const Client &item);    // 刪除一個客戶    status remove(int position);    // 更新 position 位置的客戶資訊    status replace(int position,const Client &item);    // 遍曆資料庫    status traverse(void) const;    // 讀取一個客戶資訊    status retrieve(int position,Client &item) const;    // 客戶消費 -> 計算客戶的積分情況    // 根據客戶的積分 -> 計算客戶的優惠情況    //              第幾個客戶,消費金額    status consume(int position,double maney);};DataBase::DataBase(){    headp = new Client;    if (!headp)        exit(-1);    headp -> next = NULL;    count = 0;}DataBase::~DataBase(){    if (headp)    {        delete headp;        headp = NULL;    }    count = 0;}bool DataBase::empty(void) const{    return (count == 0);}int DataBase::length(void) const{    return count;}status DataBase::create(void){    // 如果原資料庫含有資料    // 則清空資料庫    if (count)        delete headp;    // 調用建構函式建立資料庫    DataBase();    Client *searchp = headp;    Client *newClient;    cout << "Please input the number of the clients: ";    int num;    cin >> num;    cout << "\nPlease input the information of the clients...\n(like that: name id credit)" << endl;    for (int i = 0; i < num; i++)    {        newClient = new Client;        if (!newClient)            exit(-1);        cin >> newClient -> name;        cin >> newClient -> id;        cin >> newClient -> credit;        searchp -> next = newClient;        newClient -> next = NULL;        searchp = searchp -> next;        ++ count;    }    searchp -> next = NULL;    return success;}status DataBase::clear(void){    Client *searchp = headp -> next;    Client *followp = headp;    while (searchp)    {        followp = searchp;        searchp = searchp -> next;        delete followp;    }    headp -> next = NULL;    count = 0;    return success;}// 倒序建立資料庫status DataBase::insert(const Client &item){    Client *newClient = new Client;    if (!newClient)        exit(-1);    newClient -> name = item.name;    newClient -> id = item.id;    newClient -> credit = item.credit;    //cout << newClient -> name << newClient -> id << newClient -> credit << endl;    newClient -> next = headp -> next;    headp -> next = newClient;    count ++;    return success;}status DataBase::remove(int position){    if (empty())        return underflow;    if (position < 1 || position > count)    {        cout << "Range_Error!" << endl;        return range_error;    }    Client *searchp = headp -> next;    Client *followp = headp;    int i = 1;    while (i < position && searchp)    {        followp = searchp;        searchp = searchp -> next;        i++;    }    followp -> next = searchp -> next;    delete searchp;    -- count;    return success;}status DataBase::replace(int position,const Client &item){    if (empty())    {        cout << "Data Bank Is Empty!" << endl;        return underflow;    }    if (position < 1 || position > count)    {        cout << "Range_Error!" << endl;        return range_error;    }    Client *searchp = headp -> next;    int i = 1;    while (i < position && searchp)    {        ++ i;        searchp = searchp -> next;    }    searchp -> name = item.name;    searchp -> id = item.id;    searchp -> credit = item.credit;    return success;}status DataBase::traverse(void) const{    if (empty())    {        cout << "Data Bank Is Empty!" << endl;        return underflow;    }    Client *searchp = headp -> next;    cout << "BEGIN:" << endl;    while (searchp)    {        cout << setw(6) << searchp -> name << ' ';        cout << setw(12) << searchp -> id << ' ';        cout << setw(6) << searchp -> credit << endl;        searchp = searchp -> next;    }    cout << "END!" << endl;    return success;}status DataBase::retrieve(int position,Client &item) const{    if (empty())    {        cout << "Data Bank Is Empty!" << endl;        return underflow;    }    if (position < 1 || position > count)    {        cout << "Range_Error!" << endl;        return range_error;    }    Client *searchp = headp -> next;    int i = 1;    while (i < position && searchp )    {        i++;        searchp = searchp -> next;    }    item.name = searchp -> name;    item.id = searchp -> id;    item.credit = searchp -> credit;    return success;}status DataBase::consume(int position,double maney){    if (empty())    {        cout << "Have no one person!" << endl;        return underflow;    }    if (position < 1 || position > length())    {        cout << "Have none this person!" << endl;        return overflow;    }    // 獲得這位顧客    Client item;    retrieve(position,item);    // 根據客戶積分計算優惠比率    double rate = item.credit;    while (rate > 100)        rate -= 100;    rate = rate / 100;    // 實際優惠計算需要交付的金額    double realManey = maney * (1 - rate);    cout << "Client: " << item.name << " ID: " << item.id;    cout << " Should pay: " << realManey << endl;    // 根據所交金額計算該客戶所得積分    if (realManey <= 10)    {        item.credit += realManey * 0.01;    }    else if (realManey <= 20)    {        item.credit += realManey * 0.02;    }    else if (realManey <= 30)    {        item.credit += realManey * 0.03;    }    else if (realManey <= 40)    {        item.credit += realManey * 0.04;    }    else if (realManey <= 50)    {        item.credit += realManey * 0.05;    }    else    {        item.credit += realManey * 0.1;    }    cout << "And now his credit is:" << item.credit << endl;    return success;}int main(){    DataBase D;    cout << "Let's Begin!" << endl;    cout << "\nDataBase is empty ? " << endl;    if (D.empty())        cout << "Yes";    else        cout << "No";    cout << "!" << endl;    cout << "\nFirst!" << endl;    freopen("input.txt","r",stdin);    D.create();    D.traverse();    cout << "\nSecond:" << endl;    cout << "After insert two clients:" << endl;    Client C;    cin >> C.name >> C.id >> C.credit;    D.insert(C);    cin >> C.name >> C.id >> C.credit;    D.insert(C);    D.traverse();    cout << "And we have " << D.length() << " clients!" << endl;    cout << "\nThird:" << endl;    cout << "After remove 3 clients:" << endl;    D.remove(1);    D.remove(1);    D.remove(1);    cout << "We have " << D.length() << " clients!" << endl;    cout << "And they are: " << endl;    D.traverse();    cout << "\nForth:" << endl;    cout << "we can replace a client:" << endl;    cin >> C.name >> C.id >> C.credit;    D.replace(1,C);    cout << "And they are: " << endl;    D.traverse();    cout << "\nFifth:" << endl;    cout << "We can get a certain client,please input his position: ";    int position;    cin >> position;    D.retrieve(position,C);    cout << "His name is " << C.name << " and his id: " << C.id << endl;    cout << "And his crident: " << C.credit << endl;    cout << "\nSixth:" << endl;    D.clear();    cout << "Ater clear the DataBase,we still have " << D.length()         << "'s client!" << endl;    cout << "And we read them agin!" << endl;    for (int i = 0; i < 5; i++)    {        cin >> C.name >> C.id >> C.credit;        D.insert(C);    }    D.traverse();    cout << "\nSeventh:" << endl;    cout << "If them goto shopping,and we can give them a discount ^_^" << endl;    cout << "Please input the number of the client,and how maney he will pay!" << endl;    double maney;    cin >> position;    while (position)    {        cin >> maney;        D.consume(position,maney);        cin >> position;    }    return 0;}


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.