register.c

来自「一个作业题:链表的建立」· C语言 代码 · 共 333 行

C
333
字号
#include "stdio.h" 

typedef struct Lnode
{						/*链表类型*/
	long int num;        //班号
	char name[12];       //姓名
	char sex[7];         //性别
	char subject[20];    //科目
	char profession[20]; //职业
	struct Lnode *next;
}Linklist;

typedef struct Classnode
{						 /*链表类型*/
	long int num;        //班号
	int number;			 //人数
	struct Classnode *next;
}Classlist;

enum bool{false, true};

FILE	*fd;


int menu() /*主菜单函数*/
{
	int s=9;
	printf(" *************************************************************************\n");
	printf(" Welcome\n");
	printf(" ------------------------------------------------------------------04-6-13\n");
	printf(" 1. Register                             4. Search(num)\n");
	printf(" 2. list class no.and student number.    5. Clean the screen\n");
	printf(" 3. List the student information.        0. Exit\n");
	printf(" *************************************************************************\n");
	printf("Please make your choice (0-5):[ ]\b\b");
	scanf("%d",&s);
	while(s<0||s>5)
	{
		scanf("%d",&s);
		printf("Error! Please input a right number:[ ]\b\b");
	}

	return s;
}

Linklist *creat()
{
	Linklist *L;
	L=(Linklist *)malloc(sizeof(struct Lnode));/*建立带头结点的单链表*/
	L->next=NULL;
	if(L)
	{
		printf("Creating......success!\n");
	}
	else 
	{
		printf("Fail to create!\n");
	}
	
	return L;
}

Classlist *creat_class_list()
{
	Classlist *L;
	L=(Classlist *)malloc(sizeof(struct Classnode));/*建立带头结点的单链表*/
	L->next = NULL;
	if(L)
	{
		printf("Creating......success!\n");
	}
	else 
	{
		printf("Fail to create!\n");
	}
	
	return L;
}

save_in_class_file(Classlist *p)
{
	printf("%ld\t%-12d\n",p->num, p->number);
	
	fd = fopen("class.txt", "a+");
	if(fd == NULL)
	{
		printf("open file failed!\n");
		
	}
	fprintf(fd, "%ld\t%-12d\n",p->num,p->number);
	fclose(fd);

}

save_in_file(Linklist *p)
{
	printf("%ld\t%-12s%-7s%-20s%-20s\n",p->num,p->name,p->sex,p->subject,p->profession);
	fd = fopen("list.txt", "a+");
	if(fd == NULL)
	{
		printf("open file failed!\n");

	}
	fprintf(fd, "%ld\t%-12s%-7s%-20s%-20s\n",p->num,p->name,p->sex,p->subject,p->profession);
	fclose(fd);
}

int check_class_no(Classlist *p_class_list, long num)
{
	Classlist *p;
	p=p_class_list->next;
	
	if(!p_class_list->next)
	{
		printf("the first no. of class\n");
		return 0;
	}

	while(p&&p->num != num)
	{
		p = p->next; 
		
	} /*定位*/
	
	if(!p)//p到最后一个节点
	{
		return 0;
	}
	else
	{
		p->number = p->number + 1;
		return 1;
	}

}

Linklist *add(Linklist *L, Classlist *p_class_list)
{
	Linklist *p,*q;
	Classlist *p_class, *q_class;
	for(;;)
	{
		p=L;
		while(p->next)
		{
			p=p->next; /*定位*/
		}
		
		p_class = p_class_list;
		while(p_class->next)
		{
			p_class = p_class->next; /*定位*/
		}
		
		q=(Linklist *)malloc(sizeof(struct Lnode)); /*生成新结点*/
		printf("Please enter the class no.(enter 0 exit):");
		scanf("%ld",&q->num);
		if(q->num==0)
		{
			free(q);
			break;
		}

		//如果在班级链表上没有班级号为num的班级,插入班级号为num的班级
		if(check_class_no(p_class_list, q->num) == 0)
		{
			q_class = (Classlist *)malloc(sizeof(struct Classnode));/*生成新结点*/
			q_class->num = q->num;
			q_class->number = 1;
			//save_in_class_file(q_class);
			q_class->next = p_class->next;/*插入*/
			p_class->next = q_class;      
		}		
		
		printf("Please enter the name:");
		scanf("%s",q->name);
		printf("Please enter the sex:");
		scanf("%s",q->sex);
		printf("Please enter the subject:");
		scanf("%s",q->subject);
		printf("Please enter the profession:");
		scanf("%s",q->profession);
		
		save_in_file(q);

		q->next=p->next; /*插入*/
		p->next=q;
	}
	return L;
} 


print(Linklist *p)
{
	printf("%ld\t%-12s%-7s%-20s%-20s\n",p->num,p->name,p->sex,p->subject,p->profession);
}

putout(Linklist *L)
{
	Linklist *p;
	p=L->next;
	if(!L->next)
	{
		printf("Out of memory\n");
		return;
	}
	
	printf("class no. name      sex    subject            profession\n");
	while(p)
	{
		print(p);
		p=p->next;
	}
}


print_class(Classlist *p)
{
	printf("%ld\t\t%d\n",p->num, p->number);
}

putout_class(Classlist *L)
{
	Classlist *p;
	p=L->next;
	if(!L->next)
	{
		printf("Out of memory\n");
		return;
	}
	
	printf("class no. number\n");
	while(p)
	{
		print_class(p);
		p=p->next;
	}
}

find(Linklist *L, Classlist *Lclass)
{
	Linklist *p;
	Classlist *p_class;
	long int a; 
	int i = 0; 
	char ch;
	p = L->next;
	p_class = Lclass->next;
	if(!L->next)
	{
		printf("Out of memory\n");
		return;
	}
	if(!Lclass->next)
	{
		printf("Out of memory\n");
		return;
	}

	printf("Please enter the class no to search:");
	scanf("%ld",&a);
	
	while(p_class&&p_class->num!=a)
	{
		p_class = p_class->next;
	}

	if(!p_class)//p到最后一个节点
	{
		printf("No found!\nDo you want to add the class no?(y/n):");
		getchar();
		ch=getchar();
		if(ch=='y')
		{
			add(L, Lclass); /*没有找到,是否添加?*/
		}
	}
	else
	{
		printf("class no.  number\n");
		print_class(p_class);
		
		printf("the class student information is :\n");
		printf("class no.  name   sex    subject      profession\n");
		
		while(p)
		{
			if(p->num==a)
			{
				print(p);
				i++;
			}
			p=p->next; 
			
		} /*定位*/
		printf("The student number is %d\n",i);
	}

}

main()
{
	Linklist *L;
	Classlist *p_class_list;
	/*创建单链表*/
	L = creat();
	p_class_list = creat_class_list();

	for(;;)
	{
		switch(menu())
		{
			case 1:
				add(L, p_class_list); 
				break; 
			case 2:
				putout_class(p_class_list);
				break; /*录入信息*/
			case 3:
				putout(L);
				break; /*输出学生名单*/
			case 4:
				find(L, p_class_list);
				break; /*查找*/
			case 5: 
				//clrscr();
				break; /*清屏*/
			case 0:
				exit(0); /*退出*/
		}

	}
} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?