作业一

  1 //图书信息
  2 class book
  3 {
  4 public:
  5     void writet(const char *filename);                 //添加图书信息
  6     void readt(const char *filename);                  //读取图书信息
  7     void delt(const char *filename);                   //删除图书信息
  8     int search_post(const char *filename,int id);      //搜索图书信息
  9     void searcht(const char *filename);                //查看图书信息
 10     void editt(const char *filename);                  //修改图书信息
 11 };
 12 
 13 
 14 void book::writet(const char *filename)                 //添加图书信息
 15 {
 16     ofstream out;
 17     out.open("books.txt",ios::app);
 18     if(!out.is_open())
 19     {
 20         cout<<"open error"<<endl;
 21         return ;
 22     }
 23     else
 24     {
 25         int id;
 26         string name,author,publishing;
 27         double price;
 28         cout<<"请输入id、书名、作者、出版社、价格:"<<endl;
 29         cin>>id>>name>>author>>publishing>>price;
 30         out.fill(' ');
 31         out.setf(ios::left);
 32         out<<space<<id<<space<<name<<space<<author<<space<<publishing<<space<<price<<NEWLINE;
 33         out.close();
 34         cout<<"添加成功!!"<<endl;
 35     }
 36 }
 37 
 38 void book::readt(const char *filename)                   //读取图书信息
 39 {
 40     ifstream in;
 41     in.open("books.txt",ios::in);
 42     if(!in.is_open())
 43     {
 44         cout<<"file open error"<<endl;
 45         return ;
 46     }
 47     else
 48     {
 49         stringstream ss;   //创建存储str的副本的 stringstream 对象,其中str是 string 类型的对象
 50         char line[100];
 51         cout.setf(ios::left);
 52         cout.fill(' ');    //用空格填充
 53         cout<<space<<"id"<<space<<"name"<<space<<"author"<<space<<"publishing"<<space<<"price"<<endl;
 54         while(in.peek()!=EOF)
 55         {
 56             in.getline(line,100);
 57             ss<<line;
 58             int id;
 59             string name,author,publishing;
 60             double price;
 61             ss>>id>>name>>author>>publishing>>price;
 62             cout<<setw(20)<<id<<setw(20)<<name<<setw(20)<<author<<setw(20)<<publishing<<setw(20)<<price<<endl;
 63             ss.str("");                   //用""给ss赋值
 64             ss.clear();                   //清空字符串
 65         }
 66         in.close();
 67     }
 68 }
 69 
 70 
 71 void book::delt(const char *filename)        //删除图书信息    
 72 {
 73     int id;
 74     cout<<"请输入您要删除图书的编号"<<endl;
 75     cin>>id;
 76     ifstream in;
 77     in.open("books.txt",ios::in);
 78     if(!in.is_open())
 79     {
 80         cout<<"file open error"<<endl;
 81         return ;
 82     }
 83     else
 84     {
 85         string temp;
 86         stringstream ss;
 87         int curId;;
 88         while(in.peek()!=EOF)
 89         {
 90             string line;
 91             getline(in,line);
 92             ss<<line;
 93             ss>>curId;
 94             if(curId!=id)
 95             {
 96                 temp += line + NEWLINE;
 97             }
 98             ss.str("");
 99             ss.clear();
100         }
101         in.close();
102         ofstream out;
103         out.open("books.txt",ios::out);
104         if(!out.is_open())
105         {
106             cout<<"file open error"<<endl;
107             return ;
108         }
109         else
110         {
111             out<<temp;
112             out.close();
113             cout<<"删除成功!!"<<endl;
114         }
115     }
116 }
117 
118 
119 int book::search_post(const char *filename,int id)  //搜索图书信息
120 {
121     ifstream in;
122     in.open("books.txt",ios::in|ios::binary);
123     if(!in.is_open())
124     {
125         cout<<"file open error"<<endl;
126         return -1;
127     }
128     else
129     {
130         stringstream ss;
131         while(in.peek()!=EOF)
132         {
133             int start = in.tellg();
134             string line;
135             getline(in,line);
136             ss<<line;
137             int curID;
138             ss>>curID;
139             if(curID == id)
140             {
141                 in.close();
142                 return start;
143             }
144             ss.str("");
145         }
146         cout<<"对不起您查找的图书信息不存在!"<<endl;
147         in.close();
148     }
149     return -1;
150 }
151 
152 
153 void book::searcht(const char *filename)         //查看图书信息           
154 {
155     cout<<"请输入您要查找的图书id:"<<endl;
156     int id;
157     cin>>id;
158     int pos = search_post("books.txt",id);
159     string line;
160     fstream in;
161     in.open("books.txt",ios::in|ios::binary);
162     in.seekg(pos,ios::beg);
163     getline(in,line);
164     cout.setf(ios::left);
165     cout<<setw(13)<<"id"<<setw(20)<<"name"<<setw(15)<<"author"<<setw(15)<<"publishing"<<setw(15)<<"price"<<endl;
166     cout<<line<<endl;
167 }
168 
169 
170 void book::editt(const char *filename)      //修改图书信息         
171 {
172     int id;
173     string name,author,publishing;
174     double price;
175     cout<<"请输入您要修改的图书id"<<endl;
176     cin>>id;
177     cout<<"请输入该图书新的书名、作者、出版社、价格"<<endl;
178     cin>>name>>author>>publishing>>price;
179     stringstream infoTemp;
180     infoTemp.fill(' ');
181     infoTemp.setf(ios::left);
182     infoTemp<<space<<id<<space<<name<<space<<author<<publishing<<price;
183     string newInfo;
184     getline(infoTemp,newInfo);
185     fstream file;
186     file.open("books.txt",ios::in|ios::out|ios::binary);
187     if(!file.is_open())
188     {
189         cout<<"file open error"<<endl;
190         return ;
191     }
192     else
193     {
194         int pos = search_post("books.txt",id);
195         file.seekg(pos,ios::beg);
196         file<<newInfo;
197         cout<<"修改后信息为:"<<endl;
198         cout<<newInfo<<endl;
199         file.close();
200     }
201 }
202 
203 
204 
205 
206 
207 
208 用户
209 class buyer
210 {
211 public:
212     void ready(const char *filename);                   //读取用户信息
213     void dely(const char *filename);                    //删除用户信息
214     int search_posy(const char *filename,int id);       //搜索用户信息
215     void searchy(const char *filename);                 //查找用户信息
216     void readd(const char *filename);                   //查看订单信息
217 };
218 
219 
220 
221 void buyer::ready(const char *filename)                   //读取用户信息
222 {
223     ifstream in;
224     in.open("users.txt",ios::in);
225     if(!in.is_open())
226     {
227         cout<<"file open error"<<endl;
228         return ;
229     }
230     else
231     {
232         stringstream ss;   //创建存储str的副本的 stringstream 对象,其中str是 string 类型的对象
233         char line[100];
234         cout.setf(ios::left);
235         cout.fill(' ');    //用空格填充
236         cout<<space<<"id"<<space<<"name"<<space<<"address"<<space<<"会员等级"<<endl;
237         while(in.peek()!=EOF)
238         {
239             in.getline(line,100);
240             ss<<line;
241             int id,meg;
242             string name,address;
243             ss>>id>>name>>address>>meg;
244             cout<<space<<id<<space<<name<<space<<address<<space<<meg<<endl;
245             ss.str("");                   //用""给ss赋值
246             ss.clear();                   //清空字符串
247         }
248         in.close();
249     }
250 }
251 
252 
253 void buyer::dely(const char *filename)             //删除用户信息       
254 {
255     int id;
256     cout<<"请输入您要删除用户的编号"<<endl;
257     cin>>id;
258     ifstream in;
259     in.open("users.txt",ios::in);
260     if(!in.is_open())
261     {
262         cout<<"file open error"<<endl;
263         return ;
264     }
265     else
266     {
267         string temp;
268         stringstream ss;
269         int curId;;
270         while(in.peek()!=EOF)
271         {
272             string line;
273             getline(in,line);
274             ss<<line;
275             ss>>curId;
276             if(curId!=id)
277             {
278                 temp += line + NEWLINE;
279             }
280             ss.str("");
281             ss.clear();
282         }
283         in.close();
284         ofstream out;
285         out.open("users.txt",ios::out);
286         if(!out.is_open())
287         {
288             cout<<"file open error"<<endl;
289             return ;
290         }
291         else
292         {
293             out<<temp;
294             out.close();
295             cout<<"删除成功!!"<<endl;
296         }
297     }
298 
299 }
300 
301 
302 
303 int buyer::search_posy(const char *filename,int id)     //搜索用户信息
304 {
305     ifstream in;
306     in.open("users.txt",ios::in|ios::binary);
307     if(!in.is_open())
308     {
309         cout<<"file open error"<<endl;
310         return -1;
311     }
312     else
313     {
314         stringstream ss;
315         while(in.peek()!=EOF)
316         {
317             int start = in.tellg();
318             string line;
319             getline(in,line);
320             ss<<line;
321             int curID;
322             ss>>curID;
323             if(curID == id)
324             {
325                 in.close();
326                 return start;
327             }
328             ss.str("");
329         }
330         cout<<"对不起您查找的用户信息不存在!"<<endl;
331         in.close();
332     }
333     return -1;
334 }
335 
336 
337 void buyer::searchy(const char *filename)                 //查找用户信息           
338 {
339     cout<<"请输入您要查找的用户id:"<<endl;
340     int id;
341     cin>>id;
342     int pos = search_posy("users.txt",id);
343     string line;
344     fstream in;
345     in.open("users.txt",ios::in|ios::binary);
346     in.seekg(pos,ios::beg);
347     getline(in,line);
348     cout.setf(ios::left);
349     cout<<setw(12)<<"id"<<setw(14)<<"name"<<setw(14)<<"address"<<setw(12)<<"会员等级"<<endl;
350     cout<<line<<endl;
351 }
352 
353 
354 void buyer::readd(const char *filename)         //查看用户信息           
355 {
356     ifstream in;
357     in.open("orders.txt",ios::in);
358     if(!in.is_open())
359     {
360         cout<<"file open error"<<endl;
361         return ;
362     }
363     else
364     {
365         stringstream ss;   //创建存储str的副本的 stringstream 对象,其中str是 string 类型的对象
366         char line[100];
367         cout.setf(ios::left);
368         cout.fill(' ');    //用空格填充
369         while(in.peek()!=EOF)
370         {
371             in.getline(line,100);
372             ss<<line;
373             int id;
374             string name,author,publishing;
375             double price;
376             ss>>id>>name>>author>>publishing>>price;
377             cout<<space<<id<<space<<name<<space<<author<<space<<publishing<<space<<price<<endl;
378             ss.str("");                   //用""给ss赋值
379             ss.clear();                   //清空字符串
380         }
381         in.close();
382     }
383 }  
384 
385 
386 
387 
388 订单
389 class order
390 {
391 protected:
392     double pay;                         //购书金额
393 public:
394     order();
395     //购书功能
396     int find_user(const char *filename,int id);   //将用户信息输入到订单信息中
397     int find_book(const char *filename,int id);   //将图书信息输入到订单信息中
398     void options1();         //选购图书
399     int search_poso(const char *filename,int id);  //搜索订单信息
400     void searcho(const char *filename);            //查找订单信息
401 };
402 
403 order::order()
404 {
405     pay=0;
406 }
407 
408 int order::find_user(const char *filename,int id)
409 {
410     ofstream outfile;      
411     outfile.open("orders.txt", ios::app);
412     outfile<<endl;
413     ifstream in;
414     in.open("users.txt",ios::in|ios::binary);
415     if(!in.is_open())                          //将购书的用户信息输入到订单中
416     {
417         cout<<"file open error"<<endl;
418         return -1;
419     }
420     else
421     {
422         stringstream ss;
423         while(in.peek()!=EOF)
424         {
425             int start = in.tellg();
426             string line;
427             getline(in,line);
428             outfile<<line;
429             outfile.close();
430             ss<<line;
431             int curID;
432             ss>>curID;
433             if(curID == id)
434             {
435                 in.close();
436                 return start;
437             }
438             ss.str("");
439         }
440         cout<<"对不起您查找的图书信息不存在!"<<endl;   
441     }
442     return -1;
443 }
444 
445 
446 int order::find_book(const char *filename,int id)
447 {
448     ofstream outfile;      
449     outfile.open("orders.txt", ios::app);
450     ifstream in;
451     in.open("books.txt",ios::in|ios::binary);
452     if(!in.is_open())                          //将购买的图书信息输入到订单中
453     {
454         cout<<"file open error"<<endl;
455         return -1;
456     }
457     else
458     {
459         stringstream ss;
460         while(in.peek()!=EOF)
461         {
462             int start = in.tellg();
463             string line;
464             getline(in,line);
465             outfile << line;
466             outfile.close();
467             ss<<line;
468             int curID;
469             ss>>curID;
470             if(curID == id)
471             {
472                 in.close();
473                 return start;
474             }
475             ss.str("");
476         }
477         cout<<"对不起您查找的图书信息不存在!"<<endl;   
478     }
479     return -1;
480 }
481 
482 void order::options1()  //选购图书 
483 {
484     int p;
485     cout<<"请输入用户编号:"; 
486     cin>>p;
487     cout<<endl;
488     find_user("users.txt",p);
489     int q;
490     cout<<"请输入书编号:"; 
491     cin>>q;
492     cout<<endl;
493     find_book("books.txt",q);
494 //    options1();
495 }
496 
497 int order::search_poso(const char *filename,int id)  //搜索订单信息
498 {
499     ifstream in;
500     in.open("orders.txt",ios::in|ios::binary);
501     if(!in.is_open())
502     {
503         cout<<"file open error"<<endl;
504         return -1;
505     }
506     else
507     {
508         stringstream ss;
509         while(in.peek()!=EOF)
510         {
511             int start = in.tellg();
512             string line;
513             getline(in,line);
514             ss<<line;
515             int curID;
516             ss>>curID;
517             if(curID == id)
518             {
519                 in.close();
520                 return start;
521             }
522             ss.str("");
523         }
524         cout<<"对不起您查找的订单信息不存在!"<<endl;
525         in.close();
526     }
527     return -1;
528 }
529 
530 
531 void order::searcho(const char *filename)                      
532 {
533     cout<<"请输入您要查找的用户id:"<<endl;
534     int id;
535     cin>>id;
536     int pos = search_poso("orders.txt",id);
537     string line;
538     fstream in;
539     in.open("orders.txt",ios::in|ios::binary);
540     in.seekg(pos,ios::beg);
541     getline(in,line);
542     cout.setf(ios::left);
543     cout<<line<<endl;
544 }

我选的是 购书系统,在进行第一次测试时出现无法显示所购买的图书信息;如果所查找的图书不存在则无提示直接退出;无删除用户信息此功能。针对此问题他们进行了如上修改。结合课本上的知识,该程序多用存储继承完成。以后在编程中我们要有紧密的逻辑思路,对所做项目要考虑周全。

posted on 2019-03-03 16:34  neddyface  阅读(176)  评论(0)    收藏  举报

导航