567.c

来自「(1)查找某门课程的学生名单; (2)查找某门课程的上课时间和地点; (3」· C语言 代码 · 共 429 行

C
429
字号
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void init();
void list_stu();
void list_grade();
void del_stu_cou();
void add_stu_cou();
void create_cou();
void del_cou();
void search_cou();
void change_cou();
void save_refresh();

typedef struct course
{
	char cno[20];
	char cname[20];
	char cadd[20];
	char ctime[20];
	struct course *next;
}cou;

typedef struct student
{
	char sno[15];
	char sname[20];
	char cname[20];
	int tag;
	int grade;
}stu;

cou *cour;
stu sc[40][10];
int counts;
int countc;

int main()
{
	int choice;
	init();
	do
	{
		system("cls");
		printf("\n\t***************************************************************\n\n");
		printf("\t       WELCOME TO USE THE COURSE MANAGE SYSTEM!ENJOY IT!\n\n");
		printf("\t You can choice the following items you need:\n\n");
		printf("\t\t1. Search the student list of one course.\n");
		printf("\t\t2. Show all students grade of one course.\n");
		printf("\t\t3. Delete a course of one student.\n");
		printf("\t\t4. Increase a course of one student.\n");
		printf("\t\t5. Delete a course.\n");
		printf("\t\t6. Create a course.\n");
		printf("\t\t7. Change the basic information of one course.\n");
		printf("\t\t8. Search the basic information of one course.\n");
		printf("\t\t9. Save your change and refresh the message.\n");
		printf("\t\t0. Quit.\n\n");
		printf("\t***************************************************************\n\n");
		printf("\t Please choice:\t");
		scanf("%d",&choice);
		printf("\n");
		switch(choice)
		{
		case 1:list_stu();break;
		case 2:list_grade();break;
		case 3:del_stu_cou();break;
		case 4:add_stu_cou();break;
		case 5:del_cou();break;
		case 6:create_cou();break;
		case 7:change_cou();break;
		case 8:search_cou();break;
		case 9:save_refresh();break;
		}
	}while(choice!=0);
	printf("\tThanks for your using!\n\tGoodbye!\n");
	return 0;
}

void init()
{
	int i,j;
	FILE *fp;
	cou *p,*q;
	cour=(cou*)malloc(sizeof(cou));
	p=cour;
	fp=fopen("c:\\coumsg.txt","r");
	fscanf(fp,"%d",&countc);
	for(i=0;i<countc;i++)
	{
		q=(cou*)malloc(sizeof(cou));
		fscanf(fp,"%s %s %s %s",q->cno,q->cname,q->cadd,q->ctime);
		p->next=q;
		p=p->next;
	}
	p->next=NULL;
	fclose(fp);
	fp=fopen("c:\\stumsg.txt","r");
	fscanf(fp,"%d",&counts);
	for(i=0;i<counts;i++)
	{
		j=0;
		fscanf(fp,"%s %s",sc[i][j].sno,sc[i][j].sname);
		for(j=0;j<countc;j++)
		{
			fscanf(fp,"%s %d",sc[i][j].cname,&sc[i][j].grade);
			if(sc[i][j].grade>=0 && sc[i][j].tag!=-1)sc[i][j].tag=1;
			else if(sc[i][j].tag!=-1)sc[i][j].tag=0;
			strcpy(sc[i][j].sno,sc[i][0].sno);
			strcpy(sc[i][j].sname,sc[i][0].sname);
		}
	}
	fclose(fp);
}

void list_stu()
{
	char ch;
	char name[20];
	int count=1,i,num=0;
	cou *p;
	printf("\tHere is the course list :\n");
	p=cour->next;
	while(p!=NULL)
	{
		printf("\t%s\n",p->cname);
		p=p->next;
	}
	printf("\tPlease enter the name of the course to list the students' name:\n\t");
	scanf("%s",name);
	printf("\t***************************************************************\n\t");
	p=cour->next;
	while(p!=NULL && strcmp(p->cname,name)!=0)
	{
		p=p->next;
		count++;
	}
	for(i=0;i<counts;i++)
		if(sc[i][count-1].tag==1)
		{
			if(num==0)printf("\n\tThe list of all students :\n");
			num++;
			printf(" \t%s\n",sc[i][count-1].sname);
		}
	if(count==(countc+1))printf("\n\tSorry!There is no this course!\n");
	if(count!=(countc+1) && num==0)printf("\n\tSorry!No student choice the course!\n");
	printf("\n\tYour choice have successfully been done.\n\tPress enter to continue.");
	ch=getchar();
	ch=getchar();
}

void list_grade()
{
	char ch;
	char name[20];
	int count=1,i,num=0;
	cou *p;
	printf("\tHere is the current course list :\n");
	p=cour->next;
	while(p!=NULL)
	{
		printf("\t%s\n",p->cname);
		p=p->next;
	}
	printf("\tPlease enter the name of the course to list the students' grade:\n\t");
	scanf("%s",name);
	printf("\t***************************************************************\n\t");
	p=cour->next;
	while(p!=NULL && strcmp(p->cname,name)!=0)
	{
		p=p->next;
		count++;
	}
	for(i=0;i<counts;i++)
		if(sc[i][count-1].tag==1)
		{
			if(num==0)printf("\n\tThe list of all students' grade showed as follow:\n");
			num++;
			printf(" \t%-16s%-16d\n",sc[i][count-1].sname,sc[i][count-1].grade);
		}
	if(count==(countc+1))printf("\n\tSorry!There is no this course!\n");
	if(count!=(countc+1) && num==0)printf("\n\tSorry!No student choice the course!\n");
	printf("\n\tYour choice have successfully been done.\n\tPress enter to continue.");
	ch=getchar();
	ch=getchar();
}

void del_stu_cou()
{
	int i,num=0;
	char ch;
	char sname[20],cname[20];
	printf("\tHere is the list of all student:\n");
	for(i=0;i<counts;i++)
		printf("\t%s\n",sc[i][0].sname);
	printf("\n\tPlease enter the name of student :\n\t");
	scanf("%s",sname);
	printf("\t***************************************************************\n");
	while(strcmp(sname,sc[num][0].sname)!=0 && num<counts)num++;
	if(num==counts && strcmp(sname,sc[num][0].sname)!=0)printf("\tSorry!There is no this student!\n");
	else
	{
		printf("\tHere is the list of course this student has choiced:\n");
		for(i=0;i<countc;i++)
			if(sc[num][i].tag==1)printf("\t%s\n",sc[num][i].cname);
		printf("\n\tPlease enter the name of the course you want to delete:\n\t");
		scanf("%s",cname);
		for(i=0;i<countc;i++)
			if(strcmp(sc[num][i].cname,cname)==0)
			{
				if(sc[num][i].tag==-1)
				{
					printf("Sorry!This course has been delete!\n");
					break;
				}
				sc[num][i].tag=0;
				sc[num][i].grade=-1;
				break;
			}
		if(i==countc)printf("\tSorry!There is no this course!\n");
	}
	printf("\n\tYour choice have successfully been done.\n\tPress enter to continue.");
	ch=getchar();
	ch=getchar();
}

void add_stu_cou()
{
	int i,num=0;
	char ch;
	char sname[20],cname[20];
	printf("\tHere is the list of all student:\n");
	for(i=0;i<counts;i++)
		printf("\t%s\n",sc[i][0].sname);
	printf("\n\tPlease enter the name of student :\n\t");
	scanf("%s",sname);
	printf("\t***************************************************************\n\t");
	while(strcmp(sname,sc[num][0].sname)!=0 && num<counts)num++;
	if(num==counts && strcmp(sname,sc[num][0].sname)!=0)printf("\tSorry!There is no this student!\n");
	else
	{
		printf("\tHere is the list of course this student has not choiced:\n");
		for(i=0;i<countc;i++)
			if(sc[num][i].tag==0)printf("\t%s\n",sc[num][i].cname);
		printf("\n\tPlease enter the name of the course you want to add:\n\t");
		scanf("%s",cname);
		for(i=0;i<countc;i++)
			if(strcmp(sc[num][i].cname,cname)==0)
			{
				sc[num][i].tag=1;
				sc[num][i].grade=0;
				break;
			}
		if(i==countc)printf("\tSorry!There is no this course!\n");
	}
	printf("\n\tYour choice have successfully been done.\n\tPress enter to continue.");
	ch=getchar();
	ch=getchar();
}

void create_cou()
{
	int i;
	char ch;
	char no[20],name[20],time[20],add[20];
	cou *p,*q;
	printf("\tThe messages of the course include:no. name address time.\n");
	printf("\tSample input:1001 maths 11308H 8:00-9:50am\n");
	printf("\tPlease enter the message of the new course:\n\t");
	scanf("%s %s %s %s",no,name,add,time);
	p=(cou*)malloc(sizeof(cou));
	strcpy(p->cadd,add);
	strcpy(p->cname,name);
	strcpy(p->cno,no);
	strcpy(p->ctime,time);
	p->next=NULL;
	q=cour;
	while(q->next!=NULL)q=q->next;
	q->next=p;
	countc++;
	for(i=0;i<counts;i++)
	{
		strcpy(sc[i][countc-1].cname,name);
		strcpy(sc[i][countc-1].sname,sc[i][0].sname);
		strcpy(sc[i][countc-1].sno,sc[i][0].sno);
		sc[i][countc-1].grade=-1;
		sc[i][countc-1].tag=0;
	}
	printf("\n\tYour choice have successfully been done.\n\tPress enter to continue.");
	ch=getchar();
	ch=getchar();
}

void del_cou()
{ 
	int i,count=1;
	char ch;
	char name[20];
	cou *p,*q;
	printf("\tThe current course list :\n");
	p=cour->next;
	while(p!=NULL)
	{
		printf("\t%s\n",p->cname);
		p=p->next;
	}
	printf("\n\tPlease enter the name of the course you want to delete:\n\t");
	scanf("%s",name);
	p=cour->next;
	q=cour;
	while(p!=NULL && strcmp(p->cname,name)!=0)
	{
		p=p->next;
		q=q->next;
		count++;
		if(p==NULL)break;
	}
	if((count-1)==countc && p==NULL)printf("\tSorry!There is no this course!\n");
	else
	{
		q->next=p->next;
		free(p);
		countc--;
		for(i=0;i<counts;i++)
			sc[i][count].tag=-1;
	}
	printf("\tYour choice have successfully been done.\n\tPress enter to continue.");
	ch=getchar();
	ch=getchar();
}

void search_cou()
{
	char ch;
	char name[20];
	cou *p;
	printf("\tThe current course list :\n");
	p=cour->next;
	while(p!=NULL)
	{
		printf("\t%s\n",p->cname);
		p=p->next;
	}
	printf("\tPlease enter the name of the course you want to search:\n\t");
	scanf("%s",name);
	p=cour->next;
	while(p!=NULL && strcmp(p->cname,name)!=0)
		p=p->next;
	if(p==NULL)printf("\tSorry!There is no this course!\n");
	else
	{
		printf("\t***************************************************************\n\n\t");
		printf("\n\tThe message of the course %s :\n",name);
		printf("\t\tname\t\taddress\t\ttime\n\t\t");
		printf("%-16s%-16s%-16s\n",p->cname,p->cadd,p->ctime);
	}
	printf("\tYour choice have successfully been done.\n\tPress enter to continue.");
	ch=getchar();
	ch=getchar();
}

void change_cou()
{
	char ch;
	char name[20];
	char add[20],time[20];
	cou *p;
	printf("\tThe current course list :\n");
	p=cour->next;
	while(p!=NULL)
	{
		printf("\t%s\n",p->cname);
		p=p->next;
	}
	printf("\tPlease enter the name of the course you want to change:\n\t");
	scanf("%s",name);
	p=cour->next;
	while(p!=NULL && strcmp(p->cname,name)!=0)
		p=p->next;
	if(p==NULL)printf("\tSorry!There is no this course!\n");
	else
	{
		printf("\t***************************************************************\n\t");
		printf("\n\tThe current message of the course %s :\n",name);
		printf("\t\tname\t\taddress\t\ttime\n\t\t");
		printf("%-16s%-16s%-16s\n\n",p->cname,p->cadd,p->ctime);
		printf("\tPlease enter the new message of the course %s :\n\t",name);
		printf("(Sample input:12101H 2:00-3:50pm)\n\t");
		scanf("%s %s",add,time);
		strcpy(p->cadd,add);
		strcpy(p->ctime,time);
		printf("\n\tNow the new message of the course %s :\n",name);
		printf("\t\tname\t\taddress\t\ttime\n\t\t");
		printf("%-16s%-16s%-16s\n\n",p->cname,p->cadd,p->ctime);
	}
	printf("\tYour choice have successfully been done.\n\tPress enter to continue.");
	ch=getchar();
	ch=getchar();
}

void save_refresh()
{
	int i,j;
	char ch;
	cou *p;
	FILE *fp;
	fp=fopen("c:\\stumsg.txt","w");	
	fprintf(fp,"%d\n",counts);
	for(i=0;i<counts;i++)
	{
		fprintf(fp,"%s %s\n",sc[i][0].sno,sc[i][0].sname);
		for(j=0;j<countc;j++)
			fprintf(fp,"%s %d\n",sc[i][j].cname,sc[i][j].grade);
	}
	fclose(fp);
	fp=fopen("c:\\coumsg.txt","w");
	fprintf(fp,"%d\n",countc);
	p=cour->next;
	for(i=0;i<countc;i++)
	{
		fprintf(fp,"%s %s %s %s\n",p->cno,p->cname,p->cadd,p->ctime);
		p=p->next;
	}
	fclose(fp);
	init();	
	printf("\n\tYour choice have successfully been done.\n\tPress enter to continue.");
	ch=getchar();
	ch=getchar();
}

⌨️ 快捷键说明

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