又来了
这个是学生的管理的 改改吧
#include
#include"string.h"
#include "process.h"
#include
struct Node
{
int age;
char* name;
Node* next;
};
struct newnode
{
char newname[20];
int newage;
};
void AddTohead(Node* node,char* name,int age)
{
if(!node)
return;
Node* newnode = new Node;
if(name)
{
int cd = strlen(name)+1;
newnode->name = new char [cd];
strcpy(newnode->name,name);
}
else newnode->name = NULL;
newnode->age = age;
newnode->next = node->next;
node->next = newnode;
}
void Show(Node* node)
{
Node* p = NULL;
p = node->next;
while(p!=NULL&&p->name!=NULL)//我的软肋 我没有注意指针的真正的指向 以后要注意 这是我看以前的程序中看出来的问题 while(p)这是我原先写 切记切记
{ //看完程序后我还是没写对 我写对了
cout<
p = p->next;
}
}
void Delete(Node* node)
{
Node* del;
while(node->next)
{
del = node->next;
if(del->next)
delete [] del->name;
node->next = del->next;
delete del;
}
}
void AddToList(Node* node,char* name,int age)
{
if(!node)
return;
Node* newnode = new Node;
while(node->next)
node = node->next;
if(name)
{
int cd = strlen(name)+1;
newnode->name = new char [cd];
strcpy(newnode->name,name);
}
else newnode->name = NULL;
newnode->age = age;
node->next = newnode;
newnode->next = NULL;
}
void FindAndAdd(Node* node,char* findname,char* addname,int addage,int canshu)
{
if(!node)
return;
Node* head = node;
Node* m = NULL;
m = node->next;
do
{
if (!strcmp(m->name,findname))
break;
m = m->next;
} while (m);
if(m==NULL)
{
cout<<"nobody"<
else
{
switch(canshu)
{
case 1:
{
Node* p = head;
while(p->next!= m )
{
p = p->next;
}
Node* newnode = new Node;
if(addname)
{
int cd = strlen(addname)+1;
newnode->name = new char [cd];
strcpy(newnode->name,addname);
}
else
{
newnode->name = new char;
strcpy(newnode->name,"somebody");
}
newnode->age = addage;
newnode->next = p->next;
p->next = newnode;
} break;
case 2:
{
Node* newnode = new Node;
if(addname)
{
int cd = strlen(addname)+1;
newnode->name = new char [cd];
strcpy(newnode->name,addname);
}
else
{
newnode->name = new char;
strcpy(newnode->name,"somebody");
}
newnode->age = addage;
newnode->next = m->next;
m->next = newnode;
} break;
default:break;
}
}
}
void DelSomeBody(Node* node,char* name)
{
if(!node)
return;
Node* head = node;
if(!name)
cout<<"the name please";
else
{
Node* p = node->next;
Node* m = node;
while(m)
{
if(!strcmp(p->name,name))
break;
m = m->next;
p = p->next;
}
if(p==NULL)
{
cout<<"no one";
}
else
{
while(head->next!= p)
head = head->next;
delete [] p->name;
head->next = p->next;
delete p;
}
}
}
void Change(Node* node,char* name,char* newname,int age)
{
if(!node)
return;
Node* head = node;
if(!name)
cout<<"the name please";
else
{
Node* p = node->next;
Node* m = node;
while(m)
{
if(!strcmp(p->name,name))
break;
m = m->next;
p = p->next;
}
if(p==NULL)
{
cout<<"no one";
}
else
{
int cd = strlen(newname)+1;
p->name = new char [cd];
strcpy(p->name,newname);
p->age = age;
}
}
}
void Find(Node* node,char* findname)
{
if(!node)
return;
Node* head = node;
Node* m = NULL;
m = node->next;
if(!m)
cout<<"链表为空的 不存在 请选择操作"<
do
{
if (!strcmp(m->name,findname))
break;
m = m->next;
} while (m);
if(m==NULL)
{
cout<<"nobody"<
else
{
cout<
}
}
void CleanTheWindow()
{
system("cls");
}
void Keep(Node* node,char* fname)
{
Node* p = node->next;
newnode newd;
FILE* ip = fopen(fname,"wb");
if(!ip)
{
cout<<"wrong"<
}
while(p)
{
strcpy(newd.newname,p->name);
newd.newage = p->age;
fwrite(&newd,sizeof(newnode),1,ip);
p = p->next;
}
fclose(ip);
}
void CreatList(Node* node,char* fname)
{
newnode newd;
FILE* ip =fopen(fname,"rb");
if(!ip)
{
cout<<"wrong"<
}
Delete(node);
while(!feof(ip))
{
int a =fread(&newd,sizeof(newnode),1,ip);
if(a==1)
AddTohead(node,newd.newname,newd.newage);
}
fclose(ip);
}
void main()
{
Node node;
node.age = NULL;
node.name = NULL;
node.next = NULL;
while(1)
{
cout<<"请选择您的操作1:添加到头部(输入姓名,年龄)"<
cin>>a;//如何对输入的数据进行判断,比如我输入adfd字符型的时候,我想提示用户输入错误
switch(a)
{
case 1:
{
cout<<"请输入要加在头部的姓名和年龄"<
int age;
cin>>name;
cin>>age;
AddTohead(&node,name,age);
cout<<"加入头部完成 请选择下一步操作"<
case 2:
{
cout<<"请输入要加在尾部的姓名和年龄"<
int age;
cin>>name;
cin>>age;
AddToList(&node,name,age);
cout<<"加入尾部完成 请选择下一步操作"<
case 3:
{
cout<<"请输入要查找人的姓名"<
cin>>name;
Find(&node,name);
} break;
case 4:
{
cout<<"请按上面的输入"<
char* name2 = new char;
int age;
int cs;
cin>>name;
cin>>name2;
cin>>age;
cin>>cs;
FindAndAdd(&node,name,name2,age,cs);
cout<<"4输入完成"<
} break;
case 5:
{
cout<<"输入要修改的信息和修改的信息"<
char* name2 = new char;
int age;
cin>>name;
cin>>name2;
cin>>age;
Change(&node,name,name2,age);
cout<<"修改完成"<
} break;
case 6:
{
cout<<"输入要删除的姓名"<
cin>>name;
DelSomeBody(&node,name);
cout<<"删除完成"<
case 7:
{
cout<<"确定要删除全部的链表吗?1 是 2否"<
cin>>npd;
if(npd==1)
{
Delete(&node);
}
else break;
} break;
case 8:
{
cout<<"当前的链表是"<
} break;
case 9:
{
CleanTheWindow();
} break;
case 10:
{
cout<<"请输入要保存的文件的名称"<
cin>>a;
Keep(&node,a);
}
break;
case 11:
{
cout<<"请输入保存的文件的名称"<
cin>>a;
CreatList(&node,a);
Show(&node);
}
break;
case 0:
{
goto stop;
} break;
default :cout<<"输入有误重新输入"<
}
}
stop: cout<<"exit"<
}
其实我也很菜的