|
|
50鱼币
本帖最后由 276691146 于 2012-6-12 20:17 编辑
请问:
1. 使用类来实现链表和使用结构体来实现链表是一样的吗?
2. 在读取文件的时候
ifstream fin("tt.txt",ios::in);
while(!fin.eof())
{ ........}
貌似最后一个字符会读两遍,请问如何解决这个问题
3.总之,所有问题就是因为写这个作业程序,很多地方很奇怪。
如果哪位高人有时间帮忙的话, 最好的话可以加QQ一起讨论下: qq:276691146
- // task.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream.h>
- #include "head.h"
- #include <Windows.h>
- #include <STRING.H>
- int main(int argc, char* argv[])
- {
-
- while (true)
- {
- Welcome();
- cin>>n;
- system("cls");
- if (1 == n)
- {
- ListOperation();
- n=0;
- }
- if (2 == n)
- {
- Introduction();
- n=0;
- }
- if(3 == n){break;}
- }
- return 0;
- }
- ==================================================================
- 这上面是主函数,下面是头文件
- ==================================================================
- #include <IOSTREAM.H>
- #include <string.h>
- #include <Windows.h>
- #include <stdio.h>
- #include <FSTREAM.H>
- //*********************************************************************************************************
- void ListOperation();
- void Judgment();
- void Introduction();
- void Welcome();
- void Read();
- fstream file;
- int flag=0;
- int n;
- char Buffer[256];
- char BackupChar[1024];
- int Backupint[1024];
- char FileBuffer[2048];
- char Path[256];
- HANDLE hFile;
- DWORD len;
- char haha;
- char Name[20],Hobby[20],Age[20],Sex[20],Number[20];
- int sum=0;
- int k;
- //*********************************************************************************************************
- void Introduction()
- {
- cout<<" 因为时间关系,个人精力关系,就简单的做了这么个程序。\n";
- cout<<"基本实现了一些简单的管理名单的功能。虽然和小组一起讨论了\n";
- cout<<"另一些有趣的功能,但时间不够,没有能够一一实现。\n 就这样吧。"<<endl<<endl<<endl;
- cout<<"7秒后自动返回主界面!"<<endl;
- Sleep(7000);
- system("cls");
- Welcome();
- }
- void Judgment()
- {
-
- }
- void ErrorMessage()
- {
- cout<<"输入数据错误!请根据提示要求重新输入相关数据!"<<endl;
- }
- void Welcome()
- {
- cout<<endl;
- cout<<"┌─────────────朋友管理系统─────────────────┐"<<endl;
- cout<<"│ 1:进行已录入名单操作 │"<<endl;
- cout<<"│ 2:关于本程序 │"<<endl;
- cout<<"│ 3:退出 │"<<endl;
- cout<<"└────────────────────────────────────┘"<<endl;
- cout<<" By:"<<endl;
- cout<<" -----版本 2.00 -----"<<endl;
- cout<<endl<<endl;
- cout<<"请根据提示输入命令: ";
- }
- void CommandList()
- {
- cout<<endl;
- cout<<"┌─────────────名单操作命令─────────────────┐"<<endl;
- cout<<"│ 1:查看成员 │"<<endl;
- cout<<"│ 2:添加成员 │"<<endl;
- cout<<"│ 3:修改成员 │"<<endl;
- cout<<"│ 4:删除成员 │"<<endl;
- cout<<"│ 5:返回 │"<<endl;
- cout<<"└────────────────────────────────────┘"<<endl;
- cout<<" -----版本 2.00 -----"<<endl;
- cout<<endl<<endl;
- cout<<"请根据提示输入命令: ";
- cin>>n;
- }
- class num
- {
- public:
- char Name[20];
- char Age[20];
- char Hobby[256];
- char Sex[20];
- char Number[20];
- num *pNext;
- num()
- {
- int i;
- for (i=0;i<20;i++)
- {
- Name[i] = '\0';
- Age[i] = '\0';
- Sex[i] = '\0';
- Number[i] = '\0';
- }
- for (i=0;i<256;i++)
- {
- Hobby[i] = '\0';
- }
- pNext = NULL;
- }
- };
- void insert(num *t,num *head);
- int display(num *p);
- void DelNumber(num *first);
- void get(num *t);
- int AddNumber(num *nList,num *first);
- void Write(num *first);
- void Read(num *first)
- {
- num *nList;
- nList = new num;
- nList = first;
- ifstream fin("NameList.txt",ios::nocreate);
- if (!fin)
- {
- cout<<"名单文件不存在!"<<endl;
- return;
- }
- while(!fin.eof())
- {
- fin>>Name;
- strcpy(nList->Name,Name);
- fin>>Sex;
- strcpy(nList->Sex,Sex);
- fin>>Age;
- strcpy(nList->Age,Age);
- fin>>Hobby;
- strcpy(nList->Hobby,Hobby);
- fin>>Number;
- strcpy(nList->Number,Number);
- insert(nList,first);
- nList->pNext = NULL;
- nList = new num;
- }
-
- fin.close();
- return ;
- }
- void ListOperation()
- {
- int k=0;
- num *nList;
- num *first;
- first = new num;
- nList = new num;
- Read(first);
- nList = first;
- while (nList->pNext)
- {
- cout<<nList->Name<<endl;
- nList = nList->pNext;
- }
- cout<<nList->Name<<endl;
- while (true)
- {
- CommandList();
-
- if (1 == n)
- {
- Read(first);
- display(first);
- n=0;
- }
-
- else if (2 == n)
- {
- AddNumber(nList,first);
- Write(first);
- Read(first);
- system("cls");
- n=0;
- }
-
- else if (4 == n)
- {
- Read(first);
- DelNumber(first);
- Write(first);
- n=0;
- }
-
-
-
-
- else if (5 == n)
- {
- system("cls");
- n = 0;
- break;
- }
- else cout<<"请按照菜单输入指令!"<<endl;
- }
- delete first;
- delete nList;
- }
- int AddNumber(num *nList,num *first)
- {
- nList = new num;
- get(nList);
- insert(nList,first);
- return 0;
- }
- void DelNumber(num *first)
- {
- num *p,*q;
- int flag = 0;
- display(first);
- cout<<"请输入要删除的成员的名字:";
- cin>>Name;
- cout<<"再输入要删除的成员的号码:";
- cin>>Number;
- p = first;
- q = first->pNext;
- while (q)
- {
- if (strcmp(q->Name,Name) == 0 && strcmp(q->Number,Number) == 0)
- {
- p->pNext = q->pNext;
- flag = 1;
- break;
- }
- p = p->pNext;
- q = q->pNext;
- }
- if (!flag)
- {
- cout<<"名单上没有此成员"<<endl;
- return ;
- }
- }
- int display(num *first)
- {
- system("cls");
- num *nList;
- nList = first;
- if (nList == NULL||nList->Name[0] == '\0')
- {
- cout<<"现在没有任何成员的数据!"<<endl;
- return 0;
- }
- else
- {
- system("cls");
- cout<<"姓名\t\t性别\t\t年龄\t\t爱好\t\t号码"<<endl;
- while (nList->pNext)
- {
- cout<<nList->Name<<"\t\t";
-
- if (nList->Sex[0] == '1')
- {
- cout<<"男\t\t";
- }
- else
- {
- cout<<"女\t\t";
- }
-
- cout<<nList->Age<<"\t\t";
- cout<<nList->Hobby<<"\t\t";
- cout<<nList->Number<<endl;
-
- nList = nList->pNext;
- }
- }
- return 0;
- }
- void insert(num *t,num *head)
- {
- num *p;
- p = new num;
- p = head;
- while(p->pNext)
- {
- p=p->pNext;
- }
- p->pNext=t;
- }
- void Write(num *first)
- {
- num *nList;
- nList = new num;
- nList = first->pNext;
- if (nList == NULL)
- {
- cout<<"现在没有人员信息,请先输入"<<endl;
- return ;
- }
- ofstream fout("NameList.txt");
- if (!fout)
- {
- cout<<"打开文件错误!"<<endl;
- system("pause");
- return;
- }
- while (nList)
- {
- fout<<nList->Name<<"\t";
- fout<<nList->Sex<<"\t";
- fout<<nList->Age<<"\t";
- fout<<nList->Hobby<<"\t";
- fout<<nList->Number<<endl;
- nList = nList->pNext;
- }
- fout.close();
- }
- void get(num *t)
- {
- cout<<"请输入成员的姓名:"<<endl;
- cin>>Name;
- strcpy(t->Name,Name);
- cout<<"请输入成员的电话号码:"<<endl;
- cin>>Number;
- strcpy(t->Number,Number);
- cout<<"请输入成员的性别(1表示男,其他表示女):"<<endl;
- cin>>Sex;
- strcpy(t->Sex,Sex);
- cout<<"请输入成员的年龄:"<<endl;
- cin>>Age;
- strcpy(t->Age,Age);
- cout<<"请输入成员的爱好"<<endl;
- cin>>Hobby;
- strcpy(t->Hobby,Hobby);
- t->pNext = NULL;
- }
复制代码
|
|