班?


私信TA

用户名:uq_23406622795

访问量:630

签 名:

等  级
排  名 11813
经  验 1006
参赛次数 0
文章发表 6
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

TA的其他文章

解题思路:

注意事项:

参考代码:

#include <stdio.h>

#include <stdlib.h> 

#include <string.h>

struct student

{

int n;

char name[10];

char sex[10];

char born[10];

char phone[20];

char email[20];

int a[3];

int g;

struct student *next;

};

void change1(int *p1,int *p2)

{

int t;

t=*p1;*p1=*p2;*p2=t;

}

void change2(char *p1,char *p2)

{

char t[20];

strcpy(t,p1);

strcpy(p1,p2);

strcpy(p2,t);

}

void paixu(struct student *head,int n)

{

int i,j,k;struct student *p=head;

for(i=0;i<n-1;i++)

{

p=head;

for(j=0;j<n-1-i;j++,p=p->next)

{

if(p->g<(p->next)->g)

{change1(&p->n,&(p->next)->n);change2(p->name,(p->next)->name);change2(p->sex,(p->next)->sex);change2(p->born,(p->next)->born);change2(p->phone,(p->next)->phone);change2(p->email,(p->next)->email);

for(k=0;k<3;k++)

change1(&p->a[k],&(p->next)->a[k]);

change1(&p->g,&(p->next)->g);

}

}

}

}

void chaxun(struct student *head)

{

struct student *p=head;

int m,i,k;char c[10];

printf("输入查找方式:1.姓名查找。2学号查找\n"); 

scanf("%d",&k);

getchar();

if(k==1)

{

scanf("%s",c);

system("cls"); 

while(p!=NULL&&(strcmp(p->name,c)!=0))

p=p->next;

}

else 

{

system("cls");

printf("请输入学号\n"); 

scanf("%d",&m);

system("cls");

while(p->n!=m&&p!=NULL)

p=p->next;

}

if(p==NULL)

printf("未找到\n");

else

{

printf("该学生信息如下:\n"); 

printf("%d %s %s %s %s %s ",p->n,p->name,p->sex,p->born,p->phone,p->email);

for(i=0;i<3;i++)

printf("%d ",p->a[i]);

printf("%d\n",p->g);

}

}

void liulan(struct student *head)

{

struct student *p;int i;

p=head;

FILE *fp;

if((fp=fopen("studen.txt","w+"))==NULL)

{

printf("can not open\n");

exit(0);

}

while(p!=NULL)

{

fscanf(fp,"%d%s%s%s%s%s",&p->n,p->name,p->sex,p->born,p->phone,p->email);

p->g=0;

for(i=0;i<3;i++)

{

fscanf(fp,"%d",&p->a[i]);

p->g=p->g+p->a[i];

}

p=p->next;

}

p=head;

while(p!=NULL)

{

printf("%d %s %s %s %s %s ",p->n,p->name,p->sex,p->born,p->phone,p->email);

for(i=0;i<3;i++)

printf("%d ",p->a[i]);

printf("%d\n",p->g);

p=p->next;

}

if(fclose(fp))

{

printf("can not close\n");

exit (0);

}

}


void xiugai(struct student *head)

{

struct student *p=head;

int m,i,d;FILE *fp;

printf("输入要修改信息学生的学号\n"); 

scanf("%d",&m);

system("cls");

while(p->n!=m&&p!=NULL)

p=p->next;

if(p==NULL)

printf("未找到\n");

else

{

printf("输入要修改的项目\n");

scanf("%d",&d);

system("cls");

switch(d)

{

case 1:{scanf("%d",&p->n);break;}

case 2:{scanf("%s",p->name);break;}

case 3:{scanf("%s",p->sex);break;}

case 4:{scanf("%s",p->born);break;}

case 5:{scanf("%s",p->phone);break;}

case 6:{scanf("%s",p->email);break;}

case 7:{scanf("%d",&p->a[0]);break;}

case 8:{scanf("%d",&p->a[1]);break;}

case 9:{scanf("%d",&p->a[2]);break;}

case 10:{scanf("%d",&p->g);break;}

}

if((fp=fopen("stud.txt","w+"))==NULL)

{

printf("can not open\n");

exit(0);

}

p=head;

while(p!=NULL)

{

fprintf(fp,"%d %s %s %s %s %s ",p->n,p->name,p->sex,p->born,p->phone,p->email);

for(i=0;i<3;i++)

fprintf(fp,"%d ",p->a[i]);

fprintf(fp,"%d\n",p->g);

p=p->next;

}

if(fclose(fp))

{

printf("can not close\n");

exit (0);

}

}

}

int tianjia(struct student *head,struct student *end,int n)

{

struct student *p,*e;FILE *fp;int i;

e=(struct student *) malloc(sizeof(struct student));

p=head;

end->next=e;

e->next=NULL; 

scanf("%d%s%s%s%s%s",&e->n,e->name,e->sex,e->born,e->phone,e->email);

e->g=0;

for(i=0;i<3;i++)

{

scanf("%d",&e->a[i]);

e->g=e->g+e->a[i];

}

n=n+1;

paixu(head,n);

if((fp=fopen("stud.txt","w+"))==NULL)

{

printf("can not open\n");

exit (0);

}

while(p!=NULL)

{

fprintf(fp,"%d %s %s %s %s %s ",p->n,p->name,p->sex,p->born,p->phone,p->email);

for(i=0;i<3;i++)

fprintf(fp,"%d ",p->a[i]);

fprintf(fp,"%d\n",p->g);

p=p->next;

}

if(fclose(fp))

{

printf("can not close\n");

}

return n;

}

struct student *shanchu(struct student *head,struct student *end)

int m,i;FILE *fp;

struct student *p;

if((fp=fopen("stud.txt","w+"))==NULL)

{

printf("can not open\n");

exit(0);

}

printf("输入删除学生的学号\n");

scanf("%d",&m);

system("cls");

p=head;

if(m==head->n)head=head->next;

else if(m==end->n)

{

while((p->next)->next!=0)

p=p->next;

p->next=NULL;

}

else

{

while((p->next)->n!=m&&p->next!=NULL)

p=p->next;

if(p->next==NULL)printf("未找到\n");

else {p->next=(p->next)->next;}

}

p=head;

while(p!=NULL)

{

fprintf(fp,"%d %s %s %s %s %s ",p->n,p->name,p->sex,p->born,p->phone,p->email);

for(i=0;i<3;i++)

fprintf(fp,"%d ",p->a[i]);

fprintf(fp,"%d\n",p->g);

p=p->next;

}

if(fclose(fp))

{

printf("can not close\n");

exit (0);

}

return head;

}

void menu()

{

printf("\t\t\t  ******************************************************\n\n");

printf("\t\t\t  *                学生信息管理系统                   * \n\n");

printf("\t\t\t  ******************************************************\n\n"); 

printf("\t\t\t*********************系统功能菜单*************************\n");

printf("\t\t\t     ----------------------   ----------------------  \n");

printf("\t\t\t     *********************************************  \n");

printf("\t\t\t     * 1.查询学生信息    * *  2.浏览学生信息   \n");

printf("\t\t\t     *********************************************  \n"); 

printf("\t\t\t     * 3.修改学生信息    * *  4.增加学生信息   *   \n");

printf("\t\t\t     *********************************************  \n");

printf("\t\t\t     * 5.按学号删除信息    * *  6.退出系统     *   \n");

printf("\t\t\t     *********************************************  \n");

printf("\t\t\t     **********************        \n");

printf("\t\t\t     ----------------------   ----------------------  \n");

}

int main()

{

int n=1,i;char k; struct student *head,*p,*end;

menu();

FILE *fp;

if((fp = fopen("stud.txt","r"))==NULL)

printf("can not open");

head=(struct student *)malloc(sizeof (struct student));

fscanf(fp,"%d %s %s %s %s %s %d %d %d %d",&head->n,head->name,head->sex,head->born,head->phone,head->email,&head->a[0],&head->a[1],&head->a[2],&head->g);

end=head;

p=head;

while(!feof(fp))

{

p=(struct student *)malloc(sizeof (struct student));

fscanf(fp,"%d %s %s %s %s %s %d %d %d %d",&p->n,p->name,p->sex,p->born,p->phone,p->email,&p->a[0],&p->a[1],&p->a[2],&p->g);

end->next=p;

end=p;

n++; 

}

end->next=NULL;

fclose(fp);

while(1)

{

k=getchar();

if(k=='6')

break;

switch(k)

{

case '1':{system("cls");chaxun(head);menu();break;}

case '2':{system("cls");liulan(head);menu();break;}

case '3':{system("cls");xiugai(head);system("cls");menu();break;}

case '4':{system("cls");n=tianjia(head,end,n);system("cls");menu();break;}

case '5':{system("cls");head=shanchu(head,end);menu();;break;}

}

}

return 0;

}


 

0.0分

0 人评分

  评论区

  • «
  • »