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

张世杰 宿舍管理系统(已改) #14

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

张世杰 宿舍管理系统(已改) #14

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

Comments

@ZSJ6
Copy link

ZSJ6 commented Nov 27, 2018

/*程序有bug,测试请见谅
1. 创建第一个节点必须学号为一 
2. 删除|插入时如果输入不存在的列会报错 
3. 删除时不能删除第一个输入的数据 
*/ 
#include<stdio.h>
#include<stdlib.h>
#include<windows.h> 
typedef struct Student_Info{//定义结构体 
	char name[20];
	int num;
	int age;
	char sex;
	int Dorm_Num;
	int Bed_Num;
	struct Student_Info *next;
}Stu;

int main(){
	system("title Student_Info_Manager");
	Stu *p_tail = (Stu *)malloc(sizeof(Stu));
	Stu *p_head = (Stu *)malloc(sizeof(Stu));
	Stu *create(Stu *,Stu *);
	void print(Stu *);
	int search(Stu *);
	Stu *insert(Stu *);
	int delect(Stu *p_head);
	void save(Stu *);
	void jundge(Stu *p_head);
	
	p_head = create(p_head,p_tail);
	print(p_head);
	search(p_head);
	insert(p_head);
	delect(p_head);
	save(p_head);
	jundge(p_head);
	return 1;
}

Stu *create(Stu *p_head,Stu *p_tail)
{
	int count = 0,i = 0;
	printf("输入创建的学生信息数:");
	scanf("%d",&count);
	for(i = 0;i < count;i ++)
	{
		Stu *p;
		p = (Stu *)malloc(sizeof(Stu));
		printf("请输入第%d个学生的信息(姓名 年龄 编号 性别 寝室号 床号)\n",i+1);
		scanf("%s %d %d %c %d %d",&p->name,&p->age,&p->num,&p->sex,&p->Dorm_Num,&p->Bed_Num);
		p->next = NULL;
		if(p->num == 1)
		{
			p_head = p;
			p_tail = p;
		}
		else
		{
			p_tail->next = p;
			p_tail = p;
		}
	}
	return p_head;
}

void print(Stu *p_head)
{	
	Stu *p = p_head;
	if(!p)
	{
		printf("数据为空");
		exit(0);
	}
	while(p)
	{
		printf("该学生姓名:%s 年龄:%d 学号:%d 性别:%c 宿舍号:%d 床号:%d\n",p->name,p->age,p->num,p->sex,p->Dorm_Num,p->Bed_Num);
		p = p->next;
	}
}

int search(Stu *p_head)
{
	int a;
	Stu *p = p_head;
	printf("请输入要查询的学号:");
	scanf("%d",&a);
	while(p)
	{
		if(p->num == a)
		{
			printf("该学生姓名:%s 年龄:%d 学号:%d 性别:%c 宿舍号:%d 床号:%d\n",p->name,p->age,p->num,p->sex,p->Dorm_Num,p->Bed_Num);
			return 0;
		}
		else p = p->next;
	}
	printf("查无此人!\n"); 
}

Stu *insert(Stu *p_head)
{
	Stu *p_new = (Stu *)malloc(sizeof(Stu));
	Stu *p = (Stu *)malloc(sizeof(Stu));
	int a = 0;
	printf("请输入插入位置:");
	scanf("%d",&a); 
	printf("请输入学生信息(姓名 年龄 编号 性别 寝室号 床号)\n");
	scanf("%s %d %d %c %d %d",&p_new->name,&p_new->age,&p_new->num,&p_new->sex,&p_new->Dorm_Num,&p_new->Bed_Num);
	p = p_head;
	while(p)
	{
		if(p->num == a)
		{
			p_new->next = p->next; 
			p->next = p_new;
			break;
		}
		else p = p->next;
	}
	print(p_head);
}

int delect(Stu *p_head)
{	
	int a;
	Stu *p = (Stu *)malloc(sizeof(Stu));
	Stu *p_del = (Stu *)malloc(sizeof(Stu));
	printf("请输入删除的学号:");
	scanf("%d",&a);
	p = p_head; 
	while(p)
	{
		if(p->next->num == a)
		{
			p_del = p->next;
			p->next = p_del->next;
			free(p_del);
			print(p_head);
			return 2;
		}
		else p = p->next;
	}
	printf("查无此人!\n"); 
}

void save(Stu *p_head)
{	
printf("现在开始向文件输入内容\n");
FILE *fp;
Stu *p = (Stu *)malloc(sizeof(Stu));
p = p_head;
fp=fopen("a.txt","w");
while(p!=NULL)
{
	if(fprintf(fp,"该学生姓名:%s 年龄:%d 学号:%d 性别:%c 宿舍号:%d 床号:%d\n",p->name,p->age,p->num,p->sex,p->Dorm_Num,p->Bed_Num)>=0)
	p = p->next;
	else printf("输入失败");
}
printf("输入完成!\n");
fclose(fp);
}

void jundge(Stu *p_head)
{
printf("现在开始从文件输出内容\n");
FILE *fp;
Stu *p = (Stu *)malloc(sizeof(Stu));
p = p_head;
fp=fopen("a.txt","r");
if(fp==NULL)
	{
	printf("Open the file fail!!!");
	exit(5);
	}
else
{
	printf("1");
	while(!feof(fp))
	{
		fscanf(fp,"该学生姓名:%s 年龄:%d 学号:%d 性别:%c 宿舍号:%d 床号:%d\n",p->name,&p->age,&p->num,&p->sex,&p->Dorm_Num,&p->Bed_Num);
		printf("该学生姓名:%s 年龄:%d 学号:%d 性别:%c 宿舍号:%d 床号:%d\n",p->name,p->age,p->num,p->sex,p->Dorm_Num,p->Bed_Num);
		p = p->next;
	}
}
fclose(fp);
print(p_head);
}
@wanghao15536870732
Copy link
Member

  • 编译不通过
  • 代码整体结构很乱
  • 缺少函数化,尽量把删除,添加,查找,独立成函数
  • 缺少与文件的交互

革命尚未成功:smile:

@wanghao15536870732 wanghao15536870732 added the 修改 代码出现问题,请修改后重新提交代码 label Dec 7, 2018
@ZSJ6 ZSJ6 closed this as completed Dec 14, 2018
@ZSJ6 ZSJ6 changed the title 张世杰 宿舍管理系统 张世杰 宿舍管理系统(已改) Feb 24, 2019
@ZSJ6 ZSJ6 reopened this Feb 24, 2019
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