鱼C论坛

 找回密码
 立即注册
查看: 3706|回复: 2

C++ 链表结构,文件操作

[复制链接]
发表于 2012-6-11 11:39:53 | 显示全部楼层 |阅读模式
50鱼币
本帖最后由 276691146 于 2012-6-12 20:17 编辑

   请问:  
            1. 使用类来实现链表和使用结构体来实现链表是一样的吗?
            2. 在读取文件的时候
            ifstream fin("tt.txt",ios::in);
           while(!fin.eof())
             { ........}
            貌似最后一个字符会读两遍,请问如何解决这个问题
            
             3.总之,所有问题就是因为写这个作业程序,很多地方很奇怪。
        如果哪位高人有时间帮忙的话, 最好的话可以加QQ一起讨论下:   qq:276691146







  1. // task.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream.h>
  5. #include "head.h"
  6. #include <Windows.h>
  7. #include <STRING.H>

  8. int main(int argc, char* argv[])
  9. {
  10.    
  11. while (true)
  12. {
  13.         Welcome();
  14.   cin>>n;
  15.         system("cls");
  16.      if (1 == n)
  17.      {
  18.    ListOperation();
  19.    n=0;
  20.      }

  21.      if (2 == n)
  22.      {
  23.    Introduction();
  24.    n=0;
  25.      }

  26.      if(3 == n){break;}
  27. }
  28. return 0;
  29. }

  30. ==================================================================
  31. 这上面是主函数,下面是头文件
  32. ==================================================================
  33. #include <IOSTREAM.H>
  34. #include <string.h>
  35. #include <Windows.h>
  36. #include <stdio.h>
  37. #include <FSTREAM.H>
  38. //*********************************************************************************************************
  39. void ListOperation();
  40. void Judgment();
  41. void Introduction();
  42. void Welcome();
  43. void Read();
  44. fstream file;
  45. int  flag=0;
  46. int  n;
  47. char Buffer[256];
  48. char BackupChar[1024];
  49. int  Backupint[1024];
  50. char FileBuffer[2048];
  51. char Path[256];
  52. HANDLE hFile;
  53. DWORD len;
  54. char haha;
  55. char Name[20],Hobby[20],Age[20],Sex[20],Number[20];
  56. int  sum=0;
  57. int  k;
  58. //*********************************************************************************************************
  59. void Introduction()
  60. {
  61. cout<<"  因为时间关系,个人精力关系,就简单的做了这么个程序。\n";
  62. cout<<"基本实现了一些简单的管理名单的功能。虽然和小组一起讨论了\n";
  63.     cout<<"另一些有趣的功能,但时间不够,没有能够一一实现。\n  就这样吧。"<<endl<<endl<<endl;
  64. cout<<"7秒后自动返回主界面!"<<endl;
  65. Sleep(7000);
  66.     system("cls");
  67. Welcome();

  68. }
  69. void Judgment()
  70. {
  71.    
  72. }
  73. void ErrorMessage()
  74. {
  75.       cout<<"输入数据错误!请根据提示要求重新输入相关数据!"<<endl;
  76. }
  77. void Welcome()
  78. {
  79.     cout<<endl;
  80. cout<<"┌─────────────朋友管理系统─────────────────┐"<<endl;
  81. cout<<"│  1:进行已录入名单操作                                                  │"<<endl;
  82. cout<<"│  2:关于本程序                                                          │"<<endl;
  83. cout<<"│  3:退出                                                                │"<<endl;
  84. cout<<"└────────────────────────────────────┘"<<endl;
  85. cout<<"                                                           By:"<<endl;
  86. cout<<"                                                       -----版本  2.00 -----"<<endl;
  87. cout<<endl<<endl;
  88. cout<<"请根据提示输入命令: ";
  89. }
  90. void CommandList()
  91. {
  92. cout<<endl;
  93. cout<<"┌─────────────名单操作命令─────────────────┐"<<endl;
  94. cout<<"│  1:查看成员                                                            │"<<endl;
  95. cout<<"│  2:添加成员                                                            │"<<endl;
  96. cout<<"│  3:修改成员                                                            │"<<endl;
  97. cout<<"│  4:删除成员                                                            │"<<endl;
  98. cout<<"│  5:返回                                                                │"<<endl;
  99. cout<<"└────────────────────────────────────┘"<<endl;
  100.     cout<<"                                           -----版本  2.00 -----"<<endl;
  101. cout<<endl<<endl;
  102. cout<<"请根据提示输入命令: ";
  103. cin>>n;
  104. }
  105. class num
  106. {
  107. public:
  108. char Name[20];
  109. char Age[20];
  110. char Hobby[256];
  111. char Sex[20];
  112. char Number[20];
  113. num  *pNext;
  114. num()
  115. {
  116.      int i;
  117.   for (i=0;i<20;i++)
  118.   {
  119.    Name[i]   = '\0';
  120.    Age[i]    = '\0';
  121.    Sex[i]    = '\0';
  122.    Number[i] = '\0';
  123.   }
  124.   for (i=0;i<256;i++)
  125.   {
  126.    Hobby[i] = '\0';
  127.   }
  128.   pNext = NULL;
  129. }
  130. };
  131. void insert(num *t,num *head);
  132. int  display(num *p);
  133. void DelNumber(num *first);
  134. void get(num *t);
  135. int AddNumber(num *nList,num *first);
  136. void Write(num *first);
  137. void Read(num *first)
  138. {
  139.     num *nList;
  140. nList = new num;
  141. nList = first;
  142. ifstream fin("NameList.txt",ios::nocreate);
  143. if (!fin)
  144. {
  145.   cout<<"名单文件不存在!"<<endl;
  146.   return;
  147. }

  148. while(!fin.eof())
  149. {
  150.       fin>>Name;
  151.    strcpy(nList->Name,Name);
  152.    fin>>Sex;
  153.    strcpy(nList->Sex,Sex);
  154.    fin>>Age;
  155.    strcpy(nList->Age,Age);
  156.    fin>>Hobby;
  157.    strcpy(nList->Hobby,Hobby);
  158.    fin>>Number;
  159.    strcpy(nList->Number,Number);
  160.    insert(nList,first);
  161.    nList->pNext = NULL;
  162.    nList = new num;
  163. }
  164.    
  165.    fin.close();
  166.    return ;
  167. }
  168. void ListOperation()
  169. {
  170. int k=0;
  171. num *nList;
  172. num *first;
  173. first = new num;
  174. nList = new num;
  175. Read(first);
  176. nList = first;
  177. while (nList->pNext)
  178. {
  179.   cout<<nList->Name<<endl;
  180.   nList = nList->pNext;
  181. }
  182. cout<<nList->Name<<endl;
  183. while (true)
  184. {
  185.   CommandList();
  186.   
  187.   if (1 == n)
  188.   {
  189.    Read(first);
  190.    display(first);
  191.    n=0;
  192.   }
  193.   
  194. else if (2 == n)
  195.   {
  196.    AddNumber(nList,first);
  197.    Write(first);
  198.    Read(first);
  199.    system("cls");
  200.    n=0;
  201.   }
  202.   
  203. else if (4 == n)
  204.   {
  205.    Read(first);
  206.    DelNumber(first);
  207.    Write(first);
  208.    n=0;
  209.   }
  210.   
  211.   
  212.   
  213.   
  214. else if (5 == n)
  215.   {
  216.    system("cls");
  217.             n = 0;
  218.    break;
  219.   }
  220. else cout<<"请按照菜单输入指令!"<<endl;
  221. }
  222. delete first;
  223. delete nList;
  224. }

  225. int  AddNumber(num *nList,num *first)
  226. {
  227. nList = new num;
  228. get(nList);
  229. insert(nList,first);
  230.     return 0;
  231. }
  232. void DelNumber(num *first)
  233. {
  234. num *p,*q;
  235. int flag = 0;
  236. display(first);
  237. cout<<"请输入要删除的成员的名字:";
  238. cin>>Name;
  239. cout<<"再输入要删除的成员的号码:";
  240. cin>>Number;
  241. p = first;
  242. q = first->pNext;
  243.     while (q)
  244.     {
  245.   if (strcmp(q->Name,Name) == 0 && strcmp(q->Number,Number) == 0)
  246.   {
  247.    p->pNext = q->pNext;
  248.    flag = 1;
  249.    break;
  250.   }
  251.   p = p->pNext;
  252.   q = q->pNext;
  253.     }
  254. if (!flag)
  255. {
  256.   cout<<"名单上没有此成员"<<endl;
  257.   return ;
  258. }
  259. }
  260. int display(num *first)
  261. {
  262. system("cls");
  263. num *nList;
  264.     nList = first;
  265. if (nList == NULL||nList->Name[0] == '\0')
  266. {
  267.   cout<<"现在没有任何成员的数据!"<<endl;
  268.   return 0;
  269. }
  270. else
  271. {
  272.         system("cls");
  273.   cout<<"姓名\t\t性别\t\t年龄\t\t爱好\t\t号码"<<endl;
  274.   while (nList->pNext)
  275.        {
  276.       cout<<nList->Name<<"\t\t";
  277.       
  278.        if (nList->Sex[0] == '1')
  279.     {
  280.      cout<<"男\t\t";
  281.     }
  282.       else
  283.     {
  284.                     cout<<"女\t\t";
  285.     }
  286.    
  287.     cout<<nList->Age<<"\t\t";   
  288.        cout<<nList->Hobby<<"\t\t";
  289.                 cout<<nList->Number<<endl;
  290.                
  291.     nList = nList->pNext;     
  292.        }
  293. }
  294. return 0;
  295. }
  296. void insert(num *t,num *head)
  297. {
  298. num *p;
  299. p = new num;
  300.     p = head;
  301.     while(p->pNext)
  302. {
  303.   p=p->pNext;
  304. }
  305.     p->pNext=t;
  306. }
  307. void Write(num *first)
  308. {
  309. num *nList;
  310. nList = new num;
  311. nList = first->pNext;
  312. if (nList == NULL)
  313. {
  314.   cout<<"现在没有人员信息,请先输入"<<endl;
  315.   return ;
  316. }
  317. ofstream fout("NameList.txt");
  318.     if (!fout)
  319.     {
  320.   cout<<"打开文件错误!"<<endl;
  321.   system("pause");
  322.   return;
  323.     }
  324. while (nList)
  325. {
  326.   fout<<nList->Name<<"\t";
  327.   fout<<nList->Sex<<"\t";
  328.   fout<<nList->Age<<"\t";
  329.   fout<<nList->Hobby<<"\t";
  330.   fout<<nList->Number<<endl;
  331.   nList = nList->pNext;
  332. }
  333.     fout.close();
  334. }
  335. void get(num *t)
  336. {
  337.   cout<<"请输入成员的姓名:"<<endl;
  338.   cin>>Name;
  339.   strcpy(t->Name,Name);
  340.   cout<<"请输入成员的电话号码:"<<endl;
  341.   cin>>Number;
  342.   strcpy(t->Number,Number);
  343.   cout<<"请输入成员的性别(1表示男,其他表示女):"<<endl;
  344.   cin>>Sex;
  345.   strcpy(t->Sex,Sex);
  346.   cout<<"请输入成员的年龄:"<<endl;
  347.   cin>>Age;
  348.   strcpy(t->Age,Age);
  349.   cout<<"请输入成员的爱好"<<endl;
  350.   cin>>Hobby;
  351.   strcpy(t->Hobby,Hobby);
  352.      t->pNext = NULL;
  353. }



复制代码

小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-6-11 20:20:44 | 显示全部楼层
楼主这代码问题不止一点点吧 文件中保存了多个数据,也只能读出一条,但是显示两次,文件中只有一组数据,多次查看后,重复越来越多
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-6-12 04:24:26 | 显示全部楼层
//看了一眼,问题似乎不少的样子....
//      不过最主要的问题还是链表没搞好!
//        首先打开程序就应该打开文件,并显示一次全部内容;
        Num*pTemp;
        Num*pHead;
        pTemp=new Num,
        Num aNum:       //第一个Num,也即是链表头;
        pHead=aNum.pHead=aNum.pBack=&aNum;   //初始化
        aNum.pNext=pTemp;
        pTemp->pHead=pHead;
        pTemp->pBack=&aNum;
        pTemp->pNext=NULL;
//        插入一个新链表时:
        Num*pNew;
        Num*pTemp=aNum.pHead;
        while(pTemp->pNext)        //去到链表尾,想选择性插入自己研究代码;
        {
         pTemp=pTemp->pNext;
         }
         pNew=new Num;
         pNew->pHead=pTemp->pHead;
         pTemp->pNext=pNew;   
         pNew->pBack=pTemp;
         pNew->pNext=NULL;
//链表也就这么点绕人的东西了,其他都是很简单的,链表做好了其他的都不难了吧??:loveliness:
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-13 17:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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