C++之客戶消費積分管理系統


之前數據結構課程設計要求做這么一個小程序,現在貼上源碼,來和大家進行交流學習,希望大家給出意見和建議

程序以鏈表為主要數據結構對客戶信息進行存儲,對身份證號碼判斷了位數及構成(前十七位為數字,最后一位是數字或X)

需求:

針對客戶的消費情況,進行客戶管理,根據客戶的消費積分對客戶實行不同程度的打折優惠。

  1. 采用一定的存儲結構進行客戶信息的存儲
  2. 對客戶的信息可以進行修改、刪除、添加
  3. 能夠根據消費情況進行客戶積分的累加
  4. 根據積分情況,對客戶實行不同程度的打折優惠
  1 #include<iostream>
  2 #include<iomanip>
  3 #include "stdlib.h"
  4 #include<string>
  5 using namespace std;
  6 
  7 bool isID(string &);
  8 typedef struct cnode
  9 {    
 10     char name[20];
 11     string ID;
 12     double consume;
 13     double integer;
 14     struct cnode *next;
 15 }cnode;
 16 
 17 void Initstack(cnode * &head)/*初始化鏈表*/
 18 {    
 19     head= new cnode();  //開辟節點空間
 20     head->next=NULL;
 21 }
 22 
 23 void Getelem (cnode *head);
 24 void Search(cnode *head,string &ID);
 25 void Amend(cnode *head,string &ID);
 26 void Delete(cnode *head,string &ID);
 27 void Showall(cnode *head);
 28 void count(cnode *head);
 29 double display_discount(double integer);
 30 
 31 int main()
 32 {
 33     cnode *head;
 34     int choice;
 35     string y;
 36     Initstack(head);
 37     do
 38     {
 39         cout<<endl;
 40         cout<<"       客戶消費 積分管理系統    "<<endl;
 41         cout<<"  ******************************"<<endl;
 42         cout<<"  *                            *"<<endl;
 43         cout<<"  *          主菜單            *"<<endl;
 44         cout<<"  *       1  添加客戶          *"<<endl;
 45         cout<<"  *       2  查找客戶          *"<<endl;
 46         cout<<"  *       3  修改客戶          *"<<endl;
 47         cout<<"  *       4  刪除客戶          *"<<endl;
 48         cout<<"  *       5  顯示客戶          *"<<endl;
 49         cout<<"  *       6  統計客戶          *"<<endl;
 50         cout<<"  *       7  退出              *"<<endl;
 51         cout<<"  *                            *"<<endl;
 52         cout<<"  ******************************"<<endl;
 53         cout<<"請輸入您的選擇(1,2,3,4,5,6):";
 54         cin>>choice;
 55         if(choice==1)
 56             Getelem(head);                                //添加
 57         else if(choice==2)
 58         {
 59             cout<<"請輸入您查找客戶的身份證號:";
 60             cin>>y;
 61             isID(y);
 62             Search(head,y);                           //查找
 63         }
 64         else if(choice==3)
 65         {
 66             cout<<"請輸入您想修改客戶的身份證號:";
 67             cin>>y;
 68             isID(y);
 69             Amend(head,y); 
 70         }                             //修改
 71         else if(choice==4)
 72         {
 73             cout<<"請輸入你想要刪除的客戶的身份證號:";
 74             cin>>y;
 75             isID(y);
 76             Delete(head,y);    
 77         }                          //刪除   
 78         else if(choice==5)
 79             Showall(head);                            //顯示   
 80         else if(choice==6)
 81             count(head);             //統計   
 82         else if(choice==7)
 83             exit(1);
 84     }
 85     while(choice<=7);
 86     system("pause");
 87     return 0;
 88 }
 89 void Getelem (cnode *head) 
 90 {      
 91     //添加客戶函數以頭節點為參數
 92     cnode *p;
 93     double y;
 94     p=new cnode;
 95     p->next=new cnode;/*申請空的節點空間*/
 96     p->ID=" ";
 97     cout<<"請輸入姓名:";
 98     cin>>p->name;
 99     cout<<"請輸入身份證號(18位):";
100     cin>>p->ID;
101     isID(p->ID);
102     cout<<"請輸入消費金額:";
103     cin>>p->consume;
104     p->integer=p->consume/100;
105     cout<<"積分:"<<p->integer<<endl;
106     y=display_discount(p->integer);                      //調用函數計算折扣
107     cout<<"折扣:"/*<<setprecision(1)*/<<y<<""<<endl;
108     p->next=head->next;
109     head->next=p;
110 }
111 void Search(cnode *head,string &ID)
112 {    
113     cnode *p=new cnode;
114     double y;
115     p=head;
116     if(p->next==NULL)
117         cout<<"沒有客戶!"<<endl;
118     else
119     {
120         while(p->next!=NULL)
121         {
122             p=p->next;
123             if(ID==p->ID)          //判斷身份證號是否相同
124             {                    
125                 cout<<"姓名:"<<p->name<<endl;
126                 cout<<"身份證號:"<<p->ID<<endl;
127                 cout<<"消費:"<</*setprecision(2)<<*/p->consume<<endl;
128                 cout<<"積分:"<<p->integer<<endl;
129                 y=display_discount(p->integer);
130                 cout<<"折扣"<</*setprecision(1)<<*/y<<""<<endl;
131                 return;
132             }
133         }
134         cout<<"不存在該客戶!"<<endl;
135     }
136 }
137 
138 /*
139 修改客戶函數
140 通過ID獲取信息
141 可以修改身份證號、姓名、消費金額
142 修改消費金額有覆蓋原有金額及續加兩種方式
143 */
144 void Amend(cnode *head,string &ID){                              
145     cnode *p;
146     double y,z;
147     int choose,x;
148     p=head;
149     if(p->next==NULL)
150         cout<<"沒有客戶!"<<endl;
151     else
152     {
153         while(p->next!=NULL)
154         {
155             p=p->next;
156             if(ID==p->ID)     //判斷身份證號是否相同
157             {                    
158                 cout<<"姓名:"<<p->name<<endl;
159                 cout<<"身份證號:"<<p->ID<<endl;
160                 cout<<"消費:"/*<<setprecision(2)*/<<p->consume<<endl;
161                 cout<<"積分:"<</*setprecision(1)<<*/p->integer<<endl;
162                 y=display_discount(p->integer);
163                 cout<<"折扣:"<</*setprecision(1)<<*/y<<""<<endl;
164                 cout<<"請選擇你要修改的1、姓名。2、身份證號。3、消費金額。";
165                 cin>>choose;
166                 if(choose==1)
167                 {
168                     cout<<"請輸入修改后姓名;";
169                     cin>>p->name;
170                 }
171                 if(choose==2)
172                 {
173                     cout<<"請輸入修改后的身份證號:";
174                     cin>>p->ID;
175                     isID(p->ID);
176                 }
177                 if(choose==3)
178                 {
179                     cout<<"1.覆蓋以前消費、2.續加上現在費用!請選擇:";
180                     cin>>x;
181                     if(x==1)
182                     {
183                         cout<<"請輸入修改后的消費:";
184                         cin>>p->consume;
185                     }
186                     else{
187                         printf("請輸入續加金額:");
188                         cin>>z;
189                         p->consume+=z;
190                     }
191                 }
192                 cout<<"姓名:"<<p->name<<endl;
193                 cout<<"身份證號:"<<p->ID<<endl;
194                 cout<<"消費:"<</*setprecision(2)<<*/p->consume<<endl;
195                 p->integer=p->consume/100.0;
196                 cout<<"積分:"<<p->integer<<endl;
197                 y=display_discount(p->integer);
198                 cout<<"折扣:"/*<<setprecision(1)*/<<y<<""<<endl;
199                 return;
200             }
201         }
202         cout<<"不存在該客戶!"<<endl;
203     }
204 }
205 void Delete(cnode *head,string &ID)
206 {
207     //刪除客戶函數
208     cnode *p;
209     int x;
210     double y;
211     p=head;
212     if(p->next==NULL)
213         cout<<"沒有客戶!"<<endl;
214     else
215     {
216         while(p->next!=NULL)
217         {
218             head=p;
219             p=p->next;
220             if(ID==p->ID) 
221             {                 //判斷身份證號是否相同                                   
222                 cout<<"姓名:"<<p->name<<endl;
223                 cout<<"身份證號:"<<p->ID<<endl;
224                 cout<<"消費:"/*<<setprecision(2)*/<<p->consume<<endl;
225                 cout<<"積分:"<<p->integer<<endl;
226                 y=display_discount(p->integer);
227                 cout<<"折扣:"<</*setprecision(1)<<*/y<<""<<endl;
228                 cout<<"你確認刪除?1、確定。2、取消。請選擇:";
229                 cin>>x;
230                 if(x==1)
231                 {
232                     head->next=p->next;
233                     cout<<("刪除成功!");
234                 }
235                 else
236                     cout<<"刪除失敗!";
237                 return ;
238             }
239         }    
240         cout<<"不存在該客戶!"<<endl;
241     }
242 }
243 void Showall(cnode *head) //顯示所有客戶函數
244 {
245     cnode *p;
246     double y;
247     p=head;
248     if(p->next==NULL)
249         cout<<"沒有客戶!"<<endl;
250     else
251         while(p->next!=NULL)
252         {
253             p=p->next;
254             cout<<"姓名:"<<p->name<<endl;
255             cout<<"身份證號:"<<p->ID<<endl;
256             cout<<"消費:"<</*setprecision(2)<<*/p->consume<<endl;
257             cout<<"積分:"<<p->integer<<endl;
258             y=display_discount(p->integer);
259             cout<<"折扣:"<</*setprecision(1)<<*/y<<""<<endl;
260         }
261 }
262 
263 void count(cnode *head) 
264 {
265     cnode *p;
266     int i=0;
267     p=head;
268     if(p->next==NULL)
269         cout<<"沒有客戶!"<<endl;
270     else
271         while(p->next!=NULL)
272         {
273             p=p->next;
274             i++;
275         }
276         cout<<"現有客戶數量為"<<i<<"位!"<<endl;
277 }
278 double display_discount(double points) 
279 {
280     //計算客戶折扣函數,接受一個double型的數作為參數,輸出對應的折扣
281     double discount;
282     if(points == 0)
283         discount = 0;
284     if(points > 0&&points <= 50)
285         discount = 9.8;
286     if(points > 50&&points <= 100)
287         discount = 9.5;
288     if(points > 100&&points <= 150)
289         discount = 9.2;
290     if(points > 150&&points <= 200)
291         discount = 9;
292     if(points > 200&&points <= 300)
293         discount = 8;
294     else if(points > 300)
295         discount = 7;
296     return discount;
297 }
298 
299 int cal(string a)
300 {
301     return (a[0] - '0') * 7 + (a[1] - '0') * 9 + (a[2] - '0') * 10 +  (a[3] - '0') * 5 + (a[4] - '0') * 8 + 
302         (a[5] - '0') *4  + (a[6] - '0') * 2 + (a[7] - '0') * 1 + (a[8] - '0') * 6 +(a[9] - '0') * 3 + 
303         (a[10] - '0') * 7 + (a[11] - '0') * 9 + (a[12] - '0') * 10 + (a[13] - '0') * 5 + (a[14] - '0') * 8 + 
304         (a[15] - '0') * 4 +(a[16] - '0') * 2;
305 }
306 
307 char s(string a)
308 {
309     int k = cal(a) % 11;
310     if (k == 0)
311         return '1';
312     else if (k == 1)
313         return '0';
314     else if (k == 2)
315         return 'X';
316     else
317         return '0'+12-k;
318 }
319 
320 bool isNumber(string str);
321 bool isID(string &number)
322 {
323     do
324     {
325             if(18==number.length()&&isNumber(number))
326             if (number[17] == s(number))
327                 return true;
328             else
329                 return false;
330         else
331             cout<<"輸入格式不正確,請重新輸入:"<<endl;
332     }while (cin >> number);
333 
334 }
335 
336 bool isNumber(string str)
337 {
338     for(int i=0;i<str.length()-1;i++)
339         if(!isdigit(str[i]))
340             return false;
341     if((isdigit(str[str.length()-1]))||str[str.length()-1]=='X')
342         return true;
343     else
344         return false;
345 }

 

作者:耑新新,發布於  博客園

轉載請注明出處,歡迎郵件交流:zhuanxinxin@aliyun.com


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM