您的位置:首页 > 理论基础 > 计算机网络

中小企业网络管理技术完全篇

2007-04-05 14:43 295 查看
#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; }


本文出自 “Boost” 博客,请务必保留此出处http://zhujifang.blog.51cto.com/8634872/1380221
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: