鱼C论坛

 找回密码
 立即注册
查看: 2729|回复: 0

[技术交流] C++第11讲的作业答案,求大神提意见优化

[复制链接]
发表于 2013-12-16 12:47:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<iostream>
  2. #include <string>
  3. #include<fstream>
  4. #include<windows.h>

  5. using namespace std;

  6. struct Xinxi
  7. {
  8.         long id;
  9.         string name;
  10.         char sex;
  11. };

  12. bool Start();
  13. bool Write();
  14. bool Luru();
  15. bool Owrite(Xinxi *ptr);

  16. int main()
  17. {       
  18.         cout << "首先,感谢小甲鱼老师提供思路." << endl;
  19.         char Panduan;
  20.         if(Start())
  21.         {
  22.                 cout << "初始化成功" << endl;
  23.         }

  24.         else
  25.         {
  26.                 cout << "初始化失败" << endl;
  27.         }
  28.         do
  29.         {
  30.                 cout << "\n1 打印数据\n" << "2 录入数据\n" << "3 退出程序\n" << endl;
  31.                 cin >> Panduan;
  32.                 switch (Panduan)
  33.                 {
  34.                 case '1':
  35.                         Write();
  36.                         break;
  37.                 case '2':
  38.                         Luru();
  39.                         break;
  40.                 case '3':
  41.                         return 0;
  42.        
  43.                 default:
  44.                         cout << "输入错误" << endl;
  45.                         break;
  46.                 }

  47.         } while (Panduan != '3');
  48.        
  49.         return 0;
  50. }

  51. bool Start()
  52. {       
  53.         char Xuanze;
  54.         fstream out;
  55.         out.open("数据.txt",ios::out|ios::in);
  56.         if (!out)
  57.         {       
  58.                 ofstream Create("数据.txt");//创建文件
  59.                 Create << "1\tAlan\tm\n";
  60.                 if (!Create)
  61.                 {
  62.                         Create.close();
  63.                 }
  64.                 Create.close();
  65.                 out.open("数据.txt", ios::out | ios::in);
  66.                 if (!out)
  67.                 {
  68.                         out.close();
  69.                         return false;
  70.                 }
  71.         }
  72.         bool Panduan = true;
  73.         char temp;
  74.         while (out.get(temp))
  75.         {
  76.                 if (temp == '2' || temp == '3'||temp == '4'||temp == '5'||temp == '6'||temp == '7'||temp == '8'||temp == '9')
  77.                 {
  78.                         cout << "检测到文件中有新数据,是否保留?(Y/N)" << endl;               
  79.                         while (Panduan)
  80.                         {
  81.                                 cin >> Xuanze;
  82.                                 if (Xuanze == 'y' || Xuanze == 'Y')
  83.                                 {
  84.                                         out.close();
  85.                                         return true;
  86.                                 }
  87.                                 else if (Xuanze == 'n' || Xuanze == 'N')
  88.                                 {
  89.                                         out.close();
  90.                                         out.open("数据.txt", ios::trunc | ios::out);
  91.                                         out << "01\tAlan\tm";
  92.                                         out.close();
  93.                                         return true;
  94.                                 }
  95.                                 else
  96.                                 {
  97.                                         cout << "输入有误,请重新输入" << endl;
  98.                                 }
  99.                         }

  100.                 }

  101.         }
  102.         out.close();
  103.         return true;
  104. }

  105. bool Write()
  106. {
  107.         ifstream out;
  108.         out.open("数据.txt");
  109.         if (!out)
  110.         {
  111.                 cout << "读取文件失败" << endl;
  112.                 out.close();
  113.                 return false;
  114.         }
  115.         else
  116.         {
  117.                 cout << "\n正在输出记录数据...... ";
  118.                 for (int i = 0; i <= 100; i++)      // 打印百分比
  119.                 {
  120.                         std::cout.width(3);
  121.                         std::cout << i << "%";
  122.                         Sleep(5);
  123.                         std::cout << "\b\b\b\b";
  124.                 }
  125.                 cout << "读取文件成功" << endl;
  126.                 cout << "id\t" << "name\t" << "sex" << endl;
  127.                 char temp;
  128.                 while (out.get(temp))
  129.                 {
  130.                         cout << temp ;
  131.                 }
  132.                 cout << endl;
  133.                 out.close();
  134.                 return true;
  135.         }
  136. }

  137. bool Luru()
  138. {
  139.         Xinxi Xdate, *pXdate = &Xdate;
  140.         cout << "录入初始化完成,当id输入-1时退出" << endl;
  141.         do
  142.         {
  143.                 cout << "请输入Id: ";
  144.                 cin >> Xdate.id;
  145.                 while (Xdate.id < -1)
  146.                 {
  147.                         cin.clear();
  148.                         cin.ignore(100, '\n');
  149.                         cout << "输入有误,请输入Id: ";
  150.                         cin >> Xdate.id;
  151.                 }
  152.                 if (Xdate.id == -1)
  153.                 {
  154.                         break;
  155.                 }
  156.                 cout << "请输入姓名:";
  157.                 cin >> Xdate.name;

  158.                 cout << "请输入性别:";
  159.                 cin >> Xdate.sex;
  160.                 while ((Xdate.sex != 'm') && (Xdate.sex != 'f') && (Xdate.sex != 'M') && (Xdate.sex != 'F'))
  161.                 {
  162.                         cout << "输入有误,请输入性别:";
  163.                         cin.clear();
  164.                         cin.ignore(100, '\n');
  165.                         cin >> Xdate.sex;
  166.                 }                
  167.                 if (Owrite(pXdate))
  168.                 {
  169.                         cout << "录入成功" << endl;
  170.                 }
  171.                 else
  172.                 {
  173.                         cout << "录入失败" << endl;
  174.                 }
  175.         }
  176.         while (1);
  177.                
  178.        
  179.         return true;
  180. }

  181. bool Owrite(Xinxi *ptr)
  182. {
  183.         fstream out("数据.txt", ios::app | ios::out);
  184.         if (!out)
  185.         {
  186.                 out.close();
  187.                 return false;
  188.         }
  189.         out << ptr->id << "\t";
  190.         out << ptr->name << "\t";
  191.         out << ptr->sex << "\n";
  192.         out.close();
  193.         return true;
  194. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-12 23:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表