//函数种类
/* void dakai(int n,int j,char ch);
void save(int n,char ch);
void display(int n,char ch);//显示信息
void xiugai();
void dakai(int n,int j,char ch);//查找文件
void shuru(int n,char ch);//输入信息
void paixu(int n,char ch);
void xuanzhe(int n,char ch);//选择运行程序*/
//-------------------------------------------------------------------------------
struct student_type//定义结构体
{
char name[10];
int num;
int age;
char addr[15];
}stud[SIZE];
//-----------------------------------------------------------------------------------------
void save(int n,char ch)//保存输入信息
{
FILE *fp;
int i;
if((fp=fopen("f:\\stu_list.txt","wb+"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
for(i=0;i
printf("file write error\n");
fclose(fp);
}
//------------------------------------------------------------------------------------------
void display(int n,char ch)//显示信息
{
FILE *fp;
int i;
if((fp=fopen("f:\\stu_list.txt","rb"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
for(i=0;i
fread(&stud[i],sizeof(struct student_type),1,fp);
printf("姓名:%-10s学号:%4d年龄:%4d地址:%-15s\n",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
}
fclose(fp);
}
//--------------------------------------------------------------------------------------------
void xiugai(int n,char ch)//修改学生信息
{
int z;
FILE *pf;
pf=fopen("f:\\stu_list.txt","rb+");
if(pf==NULL)
{
printf("\n pf open Error,\n exit program.");
return;
}
else
{
//输入学生信息
cout<<"请输入你要修改的学号:"<
fseek(pf,z*sizeof(struct student_type),2);
scanf("%s%d%d%s",stud[z].name,&stud[z].num,&stud[z].age,stud[z].addr);
fseek(pf,sizeof(struct student_type),0);
fwrite(&stud,sizeof(struct student_type),1,pf);
}
fclose(pf);
//save(n,ch);
//display(n,ch);
}
//-----------------------------------------------------------------------------------------------
/* struct stud
{
int num;
struct studt;
} *head;
void printnode(stud
{
while(p->next!=NULL)
{
//输出p指向的数据
cout<
p=p->next;//指向下一个
}
cout<
//---------------------------------------------------------------------------------------------
void dakai2()
{
node *p=new node;
head=p;//设置头指针
p->num=90;
for(int k=1;k<10;k++)
{
//产生下一个结点1
p->next=p+k;
p=p->next;
p->num=k*100;
}
p->next=NULL;
printnode(head);
}*/
//------------------------------------------------------------------------------------------------
void dakai(int n,int j,char ch)//查找文件
{
//int i=2;
//char ch1,filename[20];
FILE *fp;
//int i=0;
//display(n,ch);
//cout<<"请输入所要查找的文件名,以'#'结束输入"<
cout<<"---------------------------------------------------------------------------"<
{
printf("cannot open this file\n");
exit(0);
}
for(int i=0;i<4;i+=1)
{
fseek(fp,i*sizeof(struct student_type),2);
fread(&stud[i],sizeof(struct student_type),1,fp);
if(stud[i].num==j)
//{
printf("姓名:%-10s学号:%4d年龄:%4d地址:%-15s\n",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
}
//i++;
//}
// }
/*fread(&stud[i-1],sizeof(struct student_type),i,fp);
cout<<"姓名";
printf("%-10s",stud[i-1].name);
cout<<"学号";
printf("%4d",stud[i-1].num);
cout<<"年龄";
printf("%4d",stud[i-1].age);
cout<<"地址";
printf("%-15s\n",stud[i-1].addr);
// }
/*
while((ch=getchar())!='#')
{
fputc(ch,fp);
putchar(ch);
}*/
fclose(fp);
}
//----------------------------------------------------------------------------------------------------------
void shuru(int n,char ch)//输入信息
{
int i;
cout<<"现在请输入学生个数:";
cin>>n;
cout<<"现在请输入"<
cout<<"请输入第"<scanf("%s%d%d%s",stud[i].name,&stud[i].num,&stud[i].age,stud[i].addr);
}
save(n,ch);
display(n,ch);
}
//----------------------------------------------------------------------------------------------------------
void paixu(int n,char ch)
{
fstream outfile,infile;
infile.open("f:\\stu_list.txt",ios::in);
if(!infile)
{
cerr<<"文件打开失败!"<
}
student_type textline[80];
// int i=0;
// while(!infile.eof())
// {
// i++;
// infile.getline(textline,sizeof(textline));
// if
//cout<}
infile.close();
}
//-------------------------------------------------------------------------------------------------------
/* void paixu2()
{
student_type ww;
for(int i=0;i
for(int j=i+1;j
if(stud[i].num>stu[j].num)
{
ww.num=stud[i].num;
stud[i].num=stud[j].num;
stud[j].num=ww.num;
ww.
*/
//-----------------------------------------------------------------------------------------------------------
void xuanzhe(int n,char ch)//选择运行程序
{
int i,j;
if(ch=='s')//输入
{
shuru(n,ch);
cout<<"---------------------------------------------------------------------"<
xuanzhe(n,ch);
}
else if(ch=='f')//查找
{
cout<<"请输入要查找的编号:"<
dakai(n,j,ch);
cout<<"---------------------------------------------------------------------"<
xuanzhe(n,ch);
}
else if(ch=='a')//保存
{
save(n,ch);
cout<<"---------------------------------------------------------------------"<
xuanzhe(n,ch);
}
else if(ch=='p')
{
paixu(n,ch);
cout<<"---------------------------------------------------------------------"<
xuanzhe(n,ch);
}
else if(ch=='x')//修改
{
xiugai(n,ch);
cout<<"------------------------------------------------------------------------"<
xuanzhe(n,ch);
}
else if(ch=='d')
{
display(n,ch);//显示信息
cout<<"------------------------------------------------------------------------"<
xuanzhe(n,ch);
}
else
{
cout<<"命令错误!"<
}
//--------------------------------------------------------------------------------------------------
void main()//主函数
{
int n;
char ch;
cout<<"---------------------------------------------------------------------"<
xuanzhe(n,ch);
}
#include
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
char name[20];
char sex[5];
long age;
long score;
struct student*next;
};
int n;
struct student*creat(void)
{
struct student*head;
struct student*p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);
scanf("%ld%s%s%ld%ld",&p1->num,&p1->name,&p1->sex,&p1->age,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);
scanf("%ld%s%s%ld%ld",&p1->num,&p1->name,&p1->sex,&p1->age,&p1->score);
}
p2->next=NULL;
return(head);
}
void print(struct student *head)
{
struct student *p;
printf("\nNow,these %d records are(结果数是%d,结果是):\n",n,n);
p=head;
if(head!=NULL)
do
{
printf("%ld %s %s %ld %ld\n",p->num,p->name,p->sex,p->age,p->score);
p=p->next;
}
while(p!=NULL);
}
void paixu(struct student *head)
{
struct student *p,*q,*small;
int temp;
for(p=head;p->next!=NULL;p=p->next)
{
small=p;
for(q=p;q;q=q->next)
if(q->num
small=q;
if(small!=p)
{
temp=p->num;
p->num=small->num;
small->num=temp;
}
}
print(head);
}
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1)head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
return(head);
}
struct student *search(struct student *head,long num)
{
struct student *p;
if(head==NULL)
{
printf("\nlist null! \n");
}
p=head;
while((num!=p->num)&&(p->next!=NULL))
{
p=p->next;
}
if(num==p->num)
printf("%ld %s %s %ld %ld",p->num,p->name,p->sex,p->age,p->score);
else
printf("num not been found(找不到)!\n");
return(head);
}
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL)
{
printf("\nlist null(无数据)! \n");
}
p1=head;
while((num!=p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("delete(删除):%ld\n",num);
n=n-1;
}
else
printf("num not been found(无效的数字)!\n");
return(head);
}
main()
{
struct student *head,*stu;
long del_num;
long search_num;
int x;
do
{
printf("***************\n1 create new chain(制作新链表)\n2 insert data(插入数据)\n3 query data(查询数据)\n4 output data(打印数据)\n5 delete data(删除数据)\n0 end(退出)\n***************\n");
printf("Please input number 0-5 to test the program(请输入0-5选择测试):\n");
scanf("%d",&x);
if(x==1)
{
printf("input records(Ex: num name sex age score)(输入数据如:学号 姓名 性别 年龄 分数):\n");
head=creat();
}
if(x==4)
{
paixu(head);
}
if(x==2)
{
printf("\ninput the inserted record(插入新数据):");
stu=(struct student*)malloc(LEN);
scanf("%ld%s%s%ld%ld",&stu->num,&stu->name,&stu->sex,&stu->age,&stu->score);
while(stu->num!=0)
{
head=insert(head,stu);
print(head);
printf("input the inserted record(插入新数据):");
stu=(struct student*)malloc(LEN);
scanf("%ld%s%s%ld%ld",&stu->num,&stu->name,&stu->sex,&stu->age,&stu->score);
}
}
if(x==3)
{
printf("\ninput the searched number(输入查询学号):");
scanf("%ld",&search_num);
while(search_num!=0)
{
head=search(head,search_num);
printf("\ninput the searched number(输入查询学号):");
scanf("%ld",&search_num);
}
}
if(x==5)
{
printf("\ninput the deleted number(输入删除学号):");
scanf("%ld",&del_num);
while(del_num!=0)
{
head=del(head,del_num);
print(head);
printf("input the deleted number(输入删除学号):");
scanf("%ld",&del_num);
}
}
}while(x!=0);
}