c++项目范例

  1 #include<iostream>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 using namespace std;
  5 class Card
  6 {
  7 protected:
  8      float remain;
  9 public:
 10     Card(){}
 11     Card(float _remain):remain(_remain){}
 12     float Getremain(){return remain;}
 13     float credit(float _price);
 14     float recharge(float moneny);
 15     virtual void showmoney()=0;
 16     virtual void isme(float a)=0;
 17     virtual void showinfo()=0;
 18     virtual const char* Getname()=0;
 19     //virtual bool operator >(Card *c) = 0;
 20 };
 21 
 22 class Stu : public Card
 23 {
 24 protected:
 25     int stunum;
 26     const char *name;
 27 public:
 28     Stu(){}
 29     Stu(int num,const char* _name):stunum(num),name(_name){}
 30     Stu(float _remain,int num,const char *_name):Card(_remain),stunum(num),name(_name){}
 31     void showmoney();
 32     void isme(float a);
 33     void showinfo();
 34     const char *Getname()    {return name;}
 35     bool operator >(Card *c);
 36 };
 37 bool Stu::operator >(Card *c)
 38 {
 39 if(remain>c->Getremain())
 40 return true;
 41 else
 42 return 
 43     false;
 44 }
 45 void Stu::showmoney()
 46 {}
 47 void Stu::isme(float a)
 48 {
 49     remain=remain - a;
 50 
 51 }
 52 void Stu::showinfo()
 53 {
 54 cout<<name<<"	"<<stunum<<"	"<<remain<<"	"<<endl;
 55 }
 56 
 57 class Gra:public Stu
 58 {
 59 private:
 60     float wage;
 61     
 62 public:
 63     Gra(float _remain,int num,const char *_name,float _wage):Stu(_remain,num,_name),wage(_wage){ }
 64     void showmoney();
 65     void isme(float a);
 66     void showinfo();
 67     const char *Getname(){return name;}
 68     bool operator >(Card *c);
 69 };
 70 bool Gra::operator >(Card *c)
 71 {
 72 if(remain>c->Getremain())
 73 return true;
 74 else
 75 return 
 76     false;
 77 }
 78 void Gra::showmoney()
 79 {}
 80 void Gra::isme(float a)
 81 {
 82     remain = remain-a;
 83 
 84 }
 85 void Gra::showinfo()
 86 {
 87     cout<<name<<"	"<<stunum<<"	"<<remain<<"	"<<wage<<"	"<<endl;
 88 }
 89 class Employees :public Card
 90 {
 91 private:
 92     int wonum;
 93     const char* ename;
 94 public:
 95     Employees( float _remain,int _wonum,const char* _ename):Card(_remain),wonum(_wonum),ename(_ename){}
 96     void showmoney();
 97     void isme(float a);
 98     void showinfo();
 99     const char *Getname(){return ename;}
100     bool operator >(Card *c);
101 };
102 bool Employees::operator >(Card *c)
103 {
104 if(remain>c->Getremain())
105 return true;
106 else
107 return 
108     false;
109 }
110 void Employees::showmoney()
111 {}
112 void Employees::isme(float a)
113 {
114     remain = remain-a;
115 
116 }
117 void Employees::showinfo()
118 {
119     cout<<ename<<"	"<<wonum<<"	"<<remain<<"	"<<endl;
120 }
121 
122 class Canteen
123 {
124 private:
125     const char* menu;
126     float price;
127 public:
128     Canteen(const char* _menu,float _price):menu(_menu),price(_price){}
129     void showmenu();
130     const char *Getmenu(){return menu;}
131     float getprice(){return price;}
132 };
133 void Canteen::showmenu()
134 {
135 cout<<menu<<"	"<<price<<"	"<<endl;
136 }
137 int main()
138 {
139 float tmp = 0;
140 float tmp1 = 160;
141 int i,flag1,flag2,flag3;
142 char pname[20];
143 char foodbuf[20];
144 Canteen c1("fish",10.0);
145 Canteen c2("leek",8.5);
146 Canteen c3("celery",7.2);
147 Canteen c4("cole",5.8);
148 Canteen c5("buds",6.9);
149 Canteen *p1[5]={&c1,&c2,&c3,&c4,&c5};
150 Stu s(150.0,1001,"timi");
151 Stu s1(200.0,1002,"tom");
152 Gra g(180.0,2001,"lucy",700.0);
153 Gra g1(200.0,2002,"jay",700.0);
154 Employees e(190.0,3001,"jimi");
155 Card *p[5] = {&s,&s1,&g,&g1,&e};
156 cout<<"people infomation:
"<<endl;
157 cout<<"NO.	"<<"name	"<<"num	"<<"remain	"<<"wage	"<<endl;
158 for(i=0;i<5;i++)
159 {
160 cout<<i<<"	";
161 p[i]->showinfo();
162 }
163 
164 cout<<"input name who is you:"<<endl;
165 char *cp;
166 cin>>pname; cout<<endl;
167 
168 for(i=0;i<5;i++)
169 {
170     if(!strncmp(pname,p[i]->Getname(),strlen(pname)-1))
171     {
172         cout<<"NO.	"<<"name	"<<"num	"<<"remain	"<<"wage	"<<endl;
173         cout<<i<<"	";
174         p[i]->showinfo();
175         flag1 = i;
176         break;
177     }
178 
179 }
180 
181 while(1)
182 {
183     cout<<"1: list		2: take your order	3:show max and min	0:quit	"<<endl;
184     cout<<"please input your select:"<<endl;
185     int select;
186     cin>>select;
187     switch(select)
188     {
189         case 0: exit(0);
190         case 1:{
191                     cout<<"NO.	"<<"food	"<<"price	"<<endl;
192                     for(i=0;i<5;i++)
193                     {
194                         cout<<i<<"	";
195                         p1[i]->showmenu();
196                         
197                     }
198                     break;
199             }
200         case 2:{
201                 cout<<"NO.	"<<"food	"<<"price	"<<endl;
202                     for(i=0;i<5;i++)
203                     {
204                         //cout<<i<<"	";
205                         p1[i]->showmenu();
206                         
207                     }
208                 cout<<"please input which do you like:"<<endl;
209                 cin>>foodbuf;
210                 for(i=0;i<5;i++)
211                 {//cout<<"hello"<<endl;
212                     if(!strncmp(foodbuf,p1[i]->Getmenu(),strlen(foodbuf)-1))
213                     {    
214                         
215                         //cout<<"hello"<<endl;
216                         p[flag1]->isme(p1[i]->getprice());
217                         cout<<"remaining:"<<p[flag1]->Getremain()<<endl;
218                     }
219                 
220                 }
221                     for(i=0;i<5;i++)
222                     {
223                         
224                         //cout<<"hello
";
225                         if(tmp < p[i]->Getremain())
226                         {
227                             tmp = p[i]->Getremain();
228                             flag2 = i;
229                         }
230                     }
231                     
232                     for(i=0;i<5;i++)
233                     {
234                                 if(tmp1>p[i]->Getremain())
235                                 {tmp1 = p[i]->Getremain();
236                                 flag3 = i;}
237                     }
238                     
239                     cout<<"the max is:"<<endl;
240                     p[flag2]->showinfo();
241                     cout<<"the min is:"<<endl;
242                     p[flag3]->showinfo();            
243                 break;
244                 }
245         case 3 :
246                 {    
247                 Card *maxp = p[0],*minp = p[0];
248                 for(i=0;i<4;i++)
249                 {
250                     if(p[i]>p[i+1])
251                     {
252                     minp = p[i-1];
253                     cout<<i<<endl;
254                     }
255                 }
256                 for(i=0;i<4;i++)
257                 {
258                 if(p[i+1]>p[i])
259                     {
260                     maxp = p[i];
261                     cout<<i-1<<endl;
262                     }
263                 }
264                 cout<<"the min remain is:"<<endl;
265                 minp->showinfo();
266                 cout<<"the max remain is:"<<endl;
267                 maxp->showinfo();
268                 
269                 break;
270                 }
271                     
272         default:{break;}
273     }
274     }
275 }
原文地址:https://www.cnblogs.com/defen/p/5459637.html