• 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

  • 相关阅读:
    【原】 POJ 1012 Joseph 约瑟夫 解题报告
    【原】 POJ 1001 Exponentiation 大整数乘法 解题报告
    POJ——1517
    毕业了,校园里走走看看——华中科技大学
    毕业答辩后
    CV编程常用的获取鼠标圈定区域的方法
    送走了GB
    POJ——2546
    奥巴马在YY21#524
    POJ——3517
  • 原文地址:https://www.cnblogs.com/Arthurian/p/7103573.html
Copyright © 2020-2023  润新知