C language implementation of student course selection system code sharing

  • 2020-05-30 20:52:32
  • OfStack

I haven't played C for a long time. The last time was when I was a big 2... Without further discussion, here is a code of the student course selection system implemented by C language, which I would like to share with you. The details are as follows:


#include<stdio.h> 
#include<stdlib.h>
int N1,N2,kk1,kk2,kk3;
struct couse * head1;
struct student * head2;
struct couse// Course information structure  
{
	int num1;
	char name1[20];
	int score;
	int nelepeo;
	// Number of students selected for the course 
	int Melepeo;
	// Maximum course size 
	struct couse * next;
}
;
struct student// Student information structure  
{
	int num2;
	char name2[20];
	int nelenum[50];
	// Course number selected 
	int nelen;
	// Number of courses selected 
	struct student * next;
}
;
void Ms() 
{
	for (kk1=0;kk1<1100;kk1++)
	        for (kk2=0;kk2<1200;kk2++)
	          for (kk3=0;kk3<1200;kk3++);
}
void keyboardc()// Input the course subfunctions ( Keyboard entry ) 
{
	struct couse *p1,*p2;
	N1=0;
	p1=p2=(struct couse*)malloc(sizeof(struct couse));
	printf(" Course number \t Course name \t credits \t Maximum course size \n");
	scanf("%d%s%d%d",&p1->num1,p1->name1,&p1->score,&p1->Melepeo);
	p1->nelepeo=0;
	head1=NULL;
	while(p1->num1!=0) 
	{
		N1=N1+1;
		if(N1==1)head1=p1; else p2->next=p1;
		p2=p1;
		p1=(struct couse * )malloc(sizeof(struct couse));
		scanf("%d%s%d%d",&p1->num1,p1->name1,&p1->score,&p1->Melepeo);
		p1->nelepeo=0;
	}
	p2->next=NULL;
}
void filec()// Input keyboard subfunctions ( File entry ) 
{
	FILE * fp;
	char filepath[20];
	struct couse *p1,*p2;
	N1=0;
	printf(" Enter the file path to read in :");
	getchar();
	gets(filepath);
	if((fp=fopen(filepath,"r"))==NULL) 
	{
		printf(" Can't find %s file !\n",filepath);
		exit(0);
	}
	p1=p2=(struct couse*)malloc(sizeof(struct couse));
	fscanf(fp,"%d%s%d%d%d",&p1->num1,p1->name1,&p1->score,&p1->nelepeo,&p1->Melepeo);
	head1=NULL;
	while(!feof(fp)) 
	{
		N1=N1+1;
		if(N1==1)head1=p1; else p2->next=p1;
		p2=p1;
		p1=(struct couse * )malloc(sizeof(struct couse));
		fscanf(fp,"%d%s%d%d%d",&p1->num1,p1->name1,&p1->score,&p1->nelepeo,&p1->Melepeo);
	}
	p2->next=NULL;
}
void inputc()// Enter the main function of the course  
{
	int i;
	printf("\t\t\t Input course information \n");
	printf("\n1. Keyboard entry \n");
	printf("2. File entry \n");
	printf("3. Back to the main menu \n");
	printf(" Please select a (1~3):\n");
	scanf("%d",&i);
	switch(i) 
	{
		case(1):keyboardc();
		break;
		case(2):filec();
		break;
		case(3):break;
	}
}
void insertc(struct couse *incouse)// Course management subfunctions ( Increase course ) 
{
	struct couse *p0,*p1,*p2;
	p1=head1;
	p0=incouse;
	if(head1==NULL) 
	{
		head1=p0;
		p0->next=NULL;
	} else 
	{
		while((p0->num1 > p1->num1) && (p1->next!=NULL)) 
		{
			p2=p1;
			p1=p1->next;
		}
		if(p0->num1 <= p1->num1) 
		{
			if(head1==p1) head1=p0; else p2->next=p0;
			p0->next=p1;
		} else 
		{
			p1->next=p0;
			p0->next=NULL;
		}
	}
	N1=N1+1;
}
void delc(int num1)// Course management subfunctions ( Delete the course ) 
{
	struct couse *p1,*p2;
	if(head1==NULL) 
	{
		printf("\n No course , Unable to delete !\n");
		goto end;
	}
	p1=head1;
	while(num1!=p1->num1 && p1->next!=NULL) 
	{
		p2=p1;
		p1=p1->next;
	}
	if(num1==p1->num1) 
	{
		if(p1==head1) head1=p1->next; else p2->next=p1->next;
		printf(" The numbered course has been deleted !\n");
		N1=N1-1;
	} else printf(" A course without that number !\n");
	end:;
}
void managementc()// Course management main function  
{
	struct couse * incouse;
	int i,num1;
	printf("\t\t\t Course management \n");
	printf("1. The new curriculum \n");
	printf("2. Delete the course \n");
	printf("3. Back to the main menu \n");
	printf(" Please select a (1~3):\n");
	scanf("%d",&i);
	switch(i) 
	{
		case(1): 
		{
			incouse=(struct couse *)malloc(sizeof(struct couse));
			printf(" Course number \t Course name \t credits \t Maximum course size \n");
			scanf("%d%s%d%d",&incouse->num1,incouse->name1,&incouse->score,&incouse->Melepeo);
			incouse->nelepeo=0;
			insertc(incouse);
			break;
		}
		case(2): 
		{
			printf(" Please enter the course number to delete :\n");
			scanf("%d",&num1);
			delc(num1);
			break;
		}
		case(3):break;
	}
}
void keyboards()// Input student information subfunction (from the keyboard)  
{
	int i;
	struct student *p1,*p2;
	N2=0;
	p1=p2=(struct student *)malloc(sizeof(struct student));
	printf(" Student student id \t The student's name \n");
	scanf("%d%s",&p1->num2,p1->name2);
	p1->nelen=0;
	for (i=0;i<20;i++) p1->nelenum[i]=0;
	head2=NULL;
	while(p1->num2!=0) 
	{
		N2=N2+1;
		if(N2==1)head2=p1; else p2->next=p1;
		p2=p1;
		p1=(struct student * )malloc(sizeof(struct student));
		scanf("%d%s",&p1->num2,p1->name2);
		p1->nelen=0;
		for (i=0;i<20;i++) p1->nelenum[i]=0;
	}
	p2->next=NULL;
}
void files()// Input student information subfunction (from document input)  
{
	int i=0;
	FILE * fp;
	char filepath[20];
	struct student *p1,*p2;
	N2=0;
	printf(" Enter the file path to read in :");
	getchar();
	gets(filepath);
	if((fp=fopen(filepath,"r"))==NULL) 
	{
		printf(" Can't find %s file !\n",filepath);
		exit(0);
	}
	p1=p2=(struct student*)malloc(sizeof(struct student));
	fread(p1,sizeof(struct student),1,fp);
	head2=NULL;
	while(!feof(fp)) 
	{
		i=0;
		N2=N2+1;
		if(N2==1)head2=p1; else p2->next=p1;
		p2=p1;
		p1=(struct student * )malloc(sizeof(struct student));
		fread(p1,sizeof(struct student),1,fp);
	}
	p2->next=NULL;
}
void inputs()// Input the main function of student information  
{
	int i;
	printf("\t\t\t Input student information \n");
	printf("\n1. Keyboard entry \n");
	printf("2. File entry \n");
	printf("3. Back to the main menu \n");
	printf(" Please select a (1~3):\n");
	scanf("%d",&i);
	switch(i) 
	{
		case(1):keyboards();
		break;
		case(2):files();
		break;
		case(3):break;
	}
}
void inserts(struct student * incouse)// Student information management subfunction ( Fill in the student information ) 
{
	struct student *p0,*p1,*p2;
	p1=head2;
	p0=incouse;
	if(head2==NULL) 
	{
		head2=p0;
		p0->next=NULL;
	} else 
	{
		while((p0->num2 > p1->num2) && (p1->next!=NULL)) 
		{
			p2=p1;
			p1=p1->next;
		}
		if(p0->num2 <= p1->num2) 
		{
			if(head2==p1) head2=p0; else p2->next=p0;
			p0->next=p1;
		} else 
		{
			p1->next=p0;
			p0->next=NULL;
		}
	}
	N2=N2+1;
}
void dels(int num2)// Student information management subfunction (delete student information ) 
{
	struct student *p1,*p2;
	if(head2==NULL) 
	{
		printf("\n No information is available on the student , Unable to delete !\n");
		goto end;
	}
	p1=head2;
	while(num2!=p1->num2 && p1->next!=NULL) 
	{
		p2=p1;
		p1=p1->next;
	}
	if(num2==p1->num2) 
	{
		if(p1==head2) head2=p1->next; else p2->next=p1->next;
		printf(" The student information has been deleted !\n");
		N2=N2-1;
	} else printf(" Student without the student number !\n");
	end:;
}
void managements()// Student information management main function  
{
	struct student * incouse;
	int i,num2;
	printf("\t\t\t Student information management \n");
	printf("1. New student information \n");
	printf("2. Delete student information \n");
	printf("3. Back to the main menu \n");
	printf(" Please select a (1~3):\n");
	scanf("%d",&i);
	switch(i) 
	{
		case(1): 
		{
			incouse=(struct student *)malloc(sizeof(struct student));
			incouse->nelen=0;
			incouse->nelenum[0]=0;
			printf(" Student student id \t The student's name \n");
			scanf("%d%s",&incouse->num2,incouse->name2);
			inserts(incouse);
			break;
		}
		case(2): 
		{
			printf(" Please enter the student's student number to delete :\n");
			scanf("%d",&num2);
			dels(num2);
			break;
		}
		case(3):break;
	}
}
void elect(struct student * s)// Course selection  
{
	struct couse * p;
	int num1,i;
	printf(" Please enter your course number :\n");
	scanf("%d",&num1);
	for (i=0;s->nelenum[i]!=0;i++);
	s->nelenum[i]=num1;
	(s->nelen)++;
	p=head1;
	while(p->num1!=num1) p=p->next;
	(p->nelepeo)++;
}
void cheak()// The student selects the course subfunction ( Search for optional courses ) 
{
	char e;
	struct couse * c;
	struct student * s;
	int num2,i,j=0,t=0;
	printf(" Please enter your student number :");
	scanf("%d",&num2);
	s=head2;
	while(s->num2!=num2 && s->next!=NULL) s=s->next;
	if(s->num2!=num2) 
	{
		printf(" There is no information about you , Please enter your information in the main menu !\n");
		goto end;
	}
	c=head1;
	printf(" Your optional course number :\n");
	while(c!=NULL) 
	{
		for (t=0,i=0;s->nelenum[i]!=0;i++) 
		{
			if(c->num1==s->nelenum[i]) t=1;
		}
		if(t==0 && (c->nelepeo!=c->Melepeo)) 
		{
			printf("%d\n",c->num1);
			j++;
		}
		c=c->next;
	}
	if(j==0) 
	{
		printf(" You have taken all the courses and cannot take more !\n");
		goto end;
	}
	printf(" Course selection (y/n)?:\n");
	getchar();
	e=getchar();
	i=0;
	while(e=='y') 
	{
		elect(s);
		printf(" Continue to register (y/n)?:\n");
		getchar();
		e=getchar();
	}
	end:;
}
void back(struct student * p)// The drop  
{
	struct couse * p1;
	int num1,i,j;
	printf(" Please enter the course number you want to return :\n");
	scanf("%d",&num1);
	p1=head1;
	while(p1->num1!=num1) p1=p1->next;
	for (i=0;p->nelenum[i]!=num1;i++);
	for (j=i;p->nelenum[j]!=0;j++) p->nelenum[j]=p->nelenum[j+1];
	p->nelenum[--j]=0;
	(p1->nelepeo)--;
	printf(" Drop out successful !\n");
}
void hcheak()// Student course selection subfunction (queries for selected courses ) 
{
	char c;
	struct couse * p0;
	struct student * p;
	int num2,i,f=0;
	printf(" Please enter your student number :\n");
	scanf("%d",&num2);
	p=head2;
	while(p->num2!=num2 && p!=NULL) p=p->next;
	if(p==NULL) 
	{
		printf(" There is no information about you , Please go back to the main menu for information entry :\n");
		goto end;
	}
	printf(" Course number selected :\n");
	if(p->nelenum[0]==0) 
	{
		printf(" You haven't chosen a course yet !\n");
		goto end;
	}
	for (i=0;p->nelenum[i]!=0;i++) 
	{
		printf("%d\n",p->nelenum[i]);
		p0=head1;
		while(p0->num1!=p->nelenum[i]) p0=p0->next;
		f=f+p0->score;
	}
	printf(" Total credit :%d\n",f);
	printf(" Whether to withdraw or not (y/n)?");
	getchar();
	c=getchar();
	while(c=='y') 
	{
		back(p);
		printf(" Continue to drop (y/n)?");
		getchar();
		c=getchar();
		(p->nelen)--;
	}
	end:;
}
void elective()// The student selects the main function  
{
	int i;
	printf("\t\t\t Students' course selection \n");
	printf("1. Search for optional courses \n");
	printf("2. Check selected courses \n");
	printf("3. Back to the main menu \n");
	printf(" Please enter the (1~3):\n");
	scanf("%d",&i);
	switch(i) 
	{
		case(1):cheak();
		break;
		case(2):hcheak();
		break;
		case(3):break;
	}
}
void listc()// Output course information  
{
	struct couse * p;
	p=head1;
	printf(" Course number   Course name   credits   Number of students selected for the course   Maximum course size \n");
	while(p!=NULL) 
	{
		printf("%-8d%10s%6d%8d%12d\n",p->num1,p->name1,p->score,p->nelepeo,p->Melepeo);
		p=p->next;
	}
}
void lists()// Output student information  
{
	struct student * p;
	p=head2;
	printf(" Student student id   The student's name   Number of courses selected \n");
	while(p!=NULL) 
	{
		printf("%-4d %10s %6d\n",p->num2,p->name2,p->nelen);
		p=p->next;
	}
}
void intoc()// Storing course information  
{
	FILE * fp;
	struct couse * p;
	char filepath[30];
	printf(" Enter the file path to save course information :");
	getchar();
	gets(filepath);
	if((fp=fopen(filepath,"w"))==NULL) 
	{
		printf("\n Save failed !");
		exit(0);
	}
	p=head1;
	while(p!=NULL) 
	{
		fprintf(fp,"%d %s %d %d %d\n",p->num1,p->name1,p->score,p->nelepeo,p->Melepeo);
		p=p->next;
	}
	fclose(fp);
	printf(" The course information has been saved in %s In the !\n",filepath);
}
void intos()// Storing student information  
{
	FILE * fp;
	struct student * p;
	char filepath[30];
	printf(" Enter the file path to save the student information :");
	getchar();
	gets(filepath);
	if((fp=fopen(filepath,"w"))==NULL) 
	{
		printf("\n Save failed !");
		exit(0);
	}
	p=head2;
	while(p!=NULL) 
	{
		fwrite(p,sizeof(struct student),1,fp);
		p=p->next;
	}
	fclose(fp);
	printf(" Student information has been saved in %s In the !\n",filepath);
}
void into()// Store information  
{
	int i;
	printf("1. Storing course information \n");
	printf("2. Storing student information \n");
	printf("3. Back to the main menu \n");
	printf(" Please enter the (1~3)\n");
	scanf("%d",&i);
	switch(i) 
	{
		case(1):intoc();
		break;
		case(2):intos();
		break;
		case(3):break;
	}
}
void store()// Information main function  
{
	int i;
	printf("\t\t System information view and storage \n");
	printf("1. View course information \n");
	printf("2. View student information \n");
	printf("3. Store information \n");
	printf("4. Back to the main menu \n");
	printf(" Please enter the (1~4):\n");
	scanf("%d",&i);
	switch(i) 
	{
		case(1):listc();
		break;
		case(2):lists();
		break;
		case(3):into();
		break;
		case(4):break;
	}
}
int main()// The main function  
{
	int i;
	start:
	  printf("\n\t\t\t Welcome to the student course selection system !\n");
	printf(" The menu :\n");
	printf("1. Input course information \n");
	printf("2. Course management \n");
	printf("3. Input student information \n");
	printf("4. Student information management \n");
	printf("5. Students' course selection \n");
	printf("6. System information view and storage \n");
	printf("7. Log out \n");
	printf("\n Please enter menu options (1~7):\n");
	scanf("%d",&i);
	if(i<1 || i>7) 
	{
		printf(" Input error , Please retyping :\n");
		goto start;
	}
	switch(i) 
	{
		case(1): 
		{
			system("cls");
			inputc();
			goto start;
			break;
		}
		case(2): 
		{
			system("cls");
			managementc();
			goto start;
			break;
		}
		case(3): 
		{
			system("cls");
			inputs();
			goto start;
			break;
		}
		case(4): 
		{
			system("cls");
			managements();
			goto start;
			break;
		}
		case(5): 
		{
			system("cls");
			elective();
			goto start;
			break;
		}
		case(6): 
		{
			system("cls");
			store();
			goto start;
			break;
		}
		case(7): 
		{
			system("cls");
			printf(" Thanks for using this system !\n\n goodbye !\n");
		}
	}
	return(0);
}

conclusion

That is the C language in this article. Now the students select the course system code to share all the content, I hope to help you. Interested friends can continue to see this site: C/C++ compiler optimization introduction. If there are shortcomings, welcome to point out the message.


Related articles: