Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

闫晋梁(宿舍管理系统) #15

Open
mythologicalwarrior opened this issue Nov 27, 2018 · 1 comment
Open

闫晋梁(宿舍管理系统) #15

mythologicalwarrior opened this issue Nov 27, 2018 · 1 comment
Labels
修改 代码出现问题,请修改后重新提交代码

Comments

@mythologicalwarrior
Copy link

mythologicalwarrior commented Nov 27, 2018

#include<stdio.h>
#include<stdlib.h>
struct student
{
	char name[20];
	int num;
	int age;
	char sex[20];
	int snum;
	int cnum;
	struct student *next;
};

struct student *creat()//创建链表
{
	struct student *head=NULL;
	struct student *cut,*end;
	end=cut=(struct student *)malloc(sizeof(struct student));
	scanf("%s %d %d %s %d %d",cut->name,&cut->num,&cut->age,cut->sex,&cut->snum,&cut->cnum);
	while(cut->num>0)
	{
		if(head==NULL)
			head=cut;
		else
		{
			end->next=cut;
			end=cut;
			cut=(struct student*)malloc(sizeof(struct student));
			scanf("%s %d %d %s %d %d",cut->name,&cut->num,&cut->age,cut->sex,&cut->snum,&cut->cnum);
		}
	}
	end->next=NULL;
	return(head);
}

void print(struct student*head)//输出链表
{
	struct student *a;
	a=head;
	if(a==NULL)
	{
		printf("这是空的");
		exit(1);
	}
	else
	   while(a!=0)
		{
			printf("%s %d %d %s %d %d\n",a->name,a->num,a->age,a->sex,a->snum,a->cnum);
			a=a->next;
		}
}
 
void chaxun(struct student*head,int num)//查询
{
	struct student *a;
	a=head;
	while(a!=NULL)
	{
		if(num==a->num)
			printf("%s %d %d %s %d %d",a->name,a->num,a->age,a->sex,a->snum,a->cnum);
		break;
	}
}

struct student *del(struct student*head,int num)//删除
{
	struct student *a,*cut,*end;
	a=head;
	while(num!=a->num&&a->next!=NULL)
	{
		end=a;
		a=a->next;
	}
	if(a->num!=num)
		printf("无该学生信息");
	else
	{
		if(a==head)
			head=a->next;
		else
		{
			end->next=a->next;
		}
	}
	return(head);
}

struct student *charu(struct student *head,struct student *jing,int num)//插入
{
	struct student *a,*cut,*end,*xin;
	a=head;
	xin=jing;
	while(num!=a->num&&a->next!=NULL)
	{
		cut=a;
		a=a->next;
	}
	if(num!=a->num)
		printf("无法插入到指定位置");
	else
		if(a==head)
		{
			head->next=xin;
		}
		else if(a->next=NULL)
		{
			a->next=xin;
			xin->next=NULL;
		}
		else
		{
			end=a->next;
			a->next=xin;
			xin->next=end;
		}
		return(head);
}
 void save(struct student*head)  //保存数据到文件
 {
	 struct student *a;
	 a=head;
	 FILE*fp;
	 if((fp=fopen("D:\student.dat","wb"))==NULL)
	 {
		 printf("cannot open the file");
		 exit(0);
	 }
	 while(a!=NULL)
	 {
		 fprintf(fp,"%s %d %d %s %d %d",a->name,a->num,a->age,a->sex,a->snum,a->cnum);
		 a=a->next;
	 }
 }
int main()
{
	struct student *head,*shan,*jin,*jing;
	int m,n,k;
	head=creat();
	printf("输出所有信息:\n");
	print(head);
    printf("请输入查询学生的学号:\n");
	scanf("%d",&m);
	printf("该学生的信息为:\n");
	chaxun(head,m);
	printf("请输入要删除信息的学生的学号:\n");
	scanf("%d",&n);
	shan=del(head,n);
	printf("输出剩余学生信息:\n");
	print(shan);
	printf("请输入新增学生的信息:\n");
	jing=(struct student *)malloc(sizeof(struct student));
	jing->next=NULL;
	scanf("%s %d %d %s %d %d",jing->name,&jing->num,&jing->age,jing->sex,&jing->snum,&jing->cnum);
	printf("请输入A学生的学号(新增学生的信息在A学生之后)(新增学生的位置):\n");
	scanf("%d",&k);
	jin=charu(head,jing,k);
	printf("输出新增学生后的所有学生的信息:\n");
	print(jin);
	system("pause");
	save(head);
	return 0;
}
@wanghao15536870732
Copy link
Member

优点

  • 每个功能都用函数独立了出来
  • 实现了添加、删除功能

缺点

  • 缺少必要的功能菜单提示
  • 查找功能有问题

gfk

  • 文件写入功能有问题

t1c viph27tx0r4hyef8 cv

  • 新增学生信息后,原来的信息丢失

123

  • 缺少从文件读取学生信息

@wanghao15536870732 wanghao15536870732 added the 修改 代码出现问题,请修改后重新提交代码 label Dec 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
修改 代码出现问题,请修改后重新提交代码
Projects
None yet
Development

No branches or pull requests

2 participants