文件的读问题!!要交作业,很急的!
我有一外部文件a.txt,里面有一些信息:
编号 姓名 性别 年龄 工资
001 guo 男 21 3000
021 li 女 20 2500
。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。
怎样把它读到一链表中,想返回首指针h??
Workernode *read(){?????怎样写}//想返回首指针h(WorkerNode *h)
我在程序内定义啦一个类WorkerNode
class WorkerNode{
public:
int Num;
char Name[10];
char Sex[5];
int Age;
float S;
WorkerNode*next;
};
///我写的整个程序如下:
流这块学的不好,书上讲的也不是很细。
本人用的是VC++6.0 那位帮我下忙,尽量用流里的简单的代码写!!!谢啦!!!
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<fstream.h>
class WorkerNode{
public:
int Num;
char Name[10];
float S;
WorkerNode*next;
};
void Print ( WorkerNode *h)
{
WorkerNode *p;
p=h;
cout<<"output data;";
cout<<"编号"<<'\t'<<"姓名"<<'\t'<<"工资"<<'\n';
while(p!=0){
cout<<'\t'<<'\t'<<p->Num<<'\t'<<p->Name<<'\t'<<p->S<<'\n';
p=p->next;
}
cout<<'\n';
}
WorkerNode *Insert (WorkerNode *head=0,WorkerNode *p=0)
{
WorkerNode *p1,*p2;
if(head==0){
head=p;
p->next=0;
return head;
}
/*else{//无序链表
p1=head;
while(p1->next!=0) {p1=p1->next;}/////此处出错!!!!空链表不幸
p1->next=p;
p->next=0;
}*/
if(head->Num>=p->Num){
p->next=head;head=p;
return head;
}
p2=p1=head;
while(p2->next&&p2->Num<p->Num){
p1=p2;p2=p2->next;
}
if(p2->Num<p->Num){
p2->next=p;p->next=0;
}
else{
p->next=p2;p1->next=p;
}
return head;
}
WorkerNode * Delete_one_node(WorkerNode*head=0,int num=0)
{
WorkerNode *p1,*p2;
if(head==0){
cout<<"List is empty,no Node can delete \n";
return (0);
}
if(head->Num==num){
p1=head;
head=head->next;
delete p1;
cout<<"delete onenode\n";
}
else {
if(head->next==0){ cout<<"List has no the node deleted \n";}
else{
p1=head;
p2=head->next;
while(p2->Num!=num && p2->next!=0){
p1=p2;p2=p2->next;}
if(p2->Num==num){
p1->next=p2->next;
delete p2;
cout<<"delete one Node \n";
}
else{ cout<<"List has no the node deleted \n";}
}
}
return head;
}
void deletechain(WorkerNode*h)
{
WorkerNode *p1;
while(h){
p1=h;
h=h->next;
delete p1;
}
}
WorkerNode *Create_sort()//有序连表
{
WorkerNode *p1,*h=0;
int num;
cout<<"sort list !input Num,end with -1:\n";
cin>>num;
while(num!=-1){
p1=new WorkerNode;
p1->Num=num;
cout<<"input Name:";
cin>>p1->Name;
cout<<"input S:";
cin>>p1->S;
h=Insert(h,p1);
cin>>num;
}
return h;
}
WorkerNode *ModityName(WorkerNode*head=0,int num=0,char *na=0)
{
WorkerNode *p1,*p2;
if(head==0){
cout<<"List is empty,no Node can modity \n";
return (0);
}
if(head->Num==num){
strcpy(head->Name,na);
cout<<"modity one node\n";
}
else {
if(head->next==0) cout<<"List has no the node modityed \n";//*********//
else{
p1=head;
p2=head->next;
while(p2->Num!=num&&p2->next!=0){
p1=p2;p2=p2->next;}
if(p2->Num==num){
strcpy(p2->Name,na);
cout<<"modity one Node \n";
}
else cout<<"List has no the node modityed \n";
}
}
return head;
}
//static int w=0;
void write(WorkerNode *h)
{
WorkerNode *p;
p=h;
char filename[20];
cout<<"输入文件名:\n";
cin>>filename;
ofstream outfile(filename);
if(!outfile)
{
cout<<"不能建立该文件!";
exit(1);
}
outfile<<"编号"<<'\t'<<"姓名"<<'\t'<<"工资"<<'\n';
while(p!=0){
outfile<<p->Num<<'\t'<<p->Name<<'\t'<<p->S<<'\n';
p=p->next;//w++;
}
cout<<'\n';
}
//??????????????????/////////
Workernode *read()
{
}
//????????????????///////////////////////////
void main(void)
{ int num,A;
WorkerNode *head=0,*p3=0;
int n;
//char na[10];
//float s;
cout<<"***********************职工管理系统执行程序******************\n";
while(1){
cout<<"第一次输入职工基本信息请按1\n";
cout<<"删除某职工基本信息请按2\n";
cout<<"增加新职工请按3\n";
cout<<"输出全体职工基本信息请按4\n";
cout<<" \t修改信息请按5\n";
cout<<" \t读入文件请按6\n";
cout<<" \t写入文件请按7\n";
cout<<"\t退出程序请按8\n";
cin>>A;
switch(A){
case 1:
head=Create_sort();break;
case 2:
cout<<"input the num of node deleted :\n";
cin>>num;
head=Delete_one_node(head,num);break;
case 3:
p3=new WorkerNode;
cout<<"input Num:\n";
cin>>n;
p3->Num=n;
cout<<"input Name:\n";
cin>>p3->Name;
cout<<"input S:\n";
cin>>p3->S;
//strcpy(p3->Name,na);
//p3->S=s;
head=Insert(head,p3);break;
case 4:
Print(head);break;
case 8:exit(1);break;
case 5:
{char na[10];int num;//*na --->nas[10]
int f;
cout<<"修改姓名请按11:\n";
cout<<"修改工资请按22:\n";
cout<<" 退出请按33:\n";
cin>>f;
switch(f){
case 11:
cout<<" input Num:";
cin>>num;
cout<<"NAME:";
cin>>na;
ModityName(head,num, na);break;
case 33:exit(1);break;}
//default:cout<<"非法操作!";
}break;
case 6:read(head);break;
case 7:write(head);break;
}
}
}