学生选修课程系统(C语言/C++实现)

2022/10/18 5:24:55

本文主要是介绍学生选修课程系统(C语言/C++实现),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

话不多说,先上代码

#include <stdio.h>
#include <string.h>
#include<malloc.h>
#include <stdlib.h>  //用于system语句的声明
void prin1();         //声明浏览学生所有选修课程函数
void choose();       //声明学生选课函数
typedef struct subjects        //定义结构体叫作SUB,在后面就可以直接使用
{
          
   
	 int num;                 //课程编号
	 char name[30];           //课程名称
	 char kind[20];           //课程性质
	 int stime;               //总学时
	 int ttime;               //授课学时
	 int etime;               //实验或上机学时
	 int score;               //学分
	 int term;                //开课学期
	 struct subjects *next;
}SUB;              

SUB *head=NULL;

SUB *create_form()          //创建链表 
{
          
   
	 SUB *head,*tail,*p;
	 int num,stime,ttime;
	 int etime,score,term;
	 char name[20],kind[10];
	 int size=sizeof(SUB);
	 head=tail=NULL;
	 printf("输入选修课程信息:
");
	 scanf("%d%s%s%d%d%d%d%d",&num,name,kind,&stime,&ttime,&etime,&score,&term);
	while(num!=0)
	{
          
   
		  p=(SUB *)malloc(size);
		  p->num=num;
		  strcpy(p->name,name);
		  strcpy(p->kind,kind);
		  p->stime=stime;
		  p->ttime=ttime;
		  p->etime=etime;
		  p->score=score;
		  p->term=term;
		 if(head==NULL)
			 head=p;
		 else
			 tail->next=p;
		 tail=p;
		 scanf("%d%s%s%d%d%d%d%d",&num,name,kind,&stime,&ttime,&etime,&score,&term);
	 }
	 tail->next=NULL;
	 return head;
}



void savefile()         //保存管理员文件 
{
          
   
	 SUB *p;
	 FILE *fp;
	 fp=fopen("2.txt","w");
	 if(fp==NULL)exit(0);
	 printf("课程编号  课程名称  课程性质  总学时  授课学时  实验或上机学时  学分  开课学期
");
	 for(p=head;p;p=p->next)
		fprintf(fp,"%5d%12s%9s%9d%9d%11d%11d%7d
",p->num,p->name,p->kind,p->stime,p->ttime,p->etime,p->score,p->term);
	 fclose(fp);
	 printf("创建后的信息已放入2.txt文件中
");
	 system("pause");
}

void savefile1()         //保存学生文件 
{
          
   
	SUB *p;
	FILE *fp;   //声明fp是指针,用来指向FILE类型的对象,fp是指向文件结构体的指针变
	fp=fopen("3.txt","w");
	 if(fp==NULL)exit(0);
	 for(p=head;p;p=p->next)
		fprintf(fp,"%5d%12s%9s%9d%9d%11d%11d%7d
",p->num,p->name,p->kind,p->stime,p->ttime,p->etime,p->score,p->term);
	 fclose(fp);
	 printf("创建后的信息已放入3.txt文件中
");
	 system("pause"); 
} 


void readfile()                    //阅读文件 
{
          
   
	void *myInsert(SUB*);
	SUB *newSub;     //新课程
    int num,stime,ttime,etime;
    int score,term;
    char c,name[20],kind[10],fname[20];
	FILE *fp;     //声明fp是指针,用来指向FILE类型的对象,fp是指向文件结构体的指针变
	fp=fopen("2.txt","r");
    while(!feof(fp))
    {
          
   
    	newSub=(SUB*)malloc(sizeof(SUB));
		fscanf(fp,"%d%s%s%d%d%d%d%d
",&newSub->num,newSub->name,newSub->kind,&newSub->stime,&newSub->ttime,&newSub->etime,&newSub->score,&newSub->term);
		myInsert(newSub);
    }
    fclose(fp);
}


void prin()   //浏览管理员所有课程
{
          
   
	SUB *ptr;
	head=NULL;
	readfile();
	if(head==NULL)
	{
          
   
		printf("

	*********NO RECORDS!************
");
		return;
	}
	printf("课程编号  课程名称  课程性质  总学时  授课学时  实践或上机学时  学分  开课学期
");
	for(ptr=head;ptr;ptr=ptr->next)
		{
          
   
			printf("%5d%12s%9s%9d%9d%11d%11d%7d
",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);
		}
	system("pause");
}

void prin1()   //浏览学生所有选修课程
{
          
   
	SUB *ptr;	
	FILE *fp;   //声明fp是指针,用来指向FILE类型的对象,fp是指向文件结构体的指针变量
	if((fp=fopen("3.txt","r"))==NULL)
	{
          
   
	printf("Cannot open file.
");
	choose();
	}
	printf("课程编号  课程名称  课程性质  总学时  授课学时  实践或上机学时  学分  开课学期
");
	while(!feof(fp))
	{
          
   
		ptr=(SUB*)malloc(sizeof(SUB));
		fscanf(fp,"%d%s%s%d%d%d%d%d
",&ptr->num,ptr->name,ptr->kind,&ptr->stime,&ptr->ttime,&ptr->etime,&ptr->score,&ptr->term);
		printf("%5d%12s%9s%9d%9d%11d%11d%7d
",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);
	}
	system("pause");
}

void *myInsert(SUB *subj)  //链表插入操作  
{
          
   
	 SUB *ptr,*ptr2;
	 ptr=subj;
 		if(head==NULL)
 		{
          
   
 			head=ptr;
 			head->next=NULL;
 		}
 		else
 		{
          
   
 			for(ptr2=head;ptr2;ptr2=ptr2->next)
				if(ptr2->next==NULL)
 				{
          
   
 					ptr2->next=subj;
 					subj->next=NULL;
 					break;
 				}
 		}
		return head;
}


void *insert()//插入课程信息 
{
          
   
	 SUB *ptr,*subj;     //定义结构体指针指向这个结构体SUB
	 int size=sizeof(SUB);
	 char ch,ch1;
	 while(ch!=0)
		{
          
   
			  subj=(SUB *)malloc(size); //(分配类型 *)malloc(分配元素个数 *sizeof(分配类型))如果成功,则返回该空间首地址,该空间没有初始化,如果失败,则返回0
			  ptr=subj;
			  printf("输入要插入的课程信息:
");
			  printf("
		请输入课程编号:");scanf("%d",&subj->num);   
			  printf("
		请输入课程名称:");scanf("%s",&subj->name);  
			  printf("
		请输入课程性质:");scanf("%s",&subj->kind);  
			  printf("
		请输入总学时:");scanf("%d",&subj->stime);
			  printf("
		请输入授课学时:");scanf("%d",&subj->ttime);
			  printf("
		请输入实践或上机学时:");scanf("%d",&subj->etime);
			  printf("
		请输入学分:");scanf("%d",&subj->score);
			  printf("
		请输入开课学期:");scanf("%d",&subj->term);
			  myInsert(subj);
			printf("
添加完毕,新信息存入文件中
");
			printf("
继续插入请按回车
");
			printf("
结束添加课程按 0: [ ]");
			ch1=getchar();                      //将回车键赋给CH1,否则subj->term输完后输入的回车键会赋给CH,因此用CH1填补。
			ch=getchar();
		}
	 return head;
}



void *del()  //删除课程
{
          
   
	SUB *p1,*p2;
	char ch,ch1;
	int num;
	while(ch!=0)
	{
          
   
		printf("输入想要删除的课程编号:[    ]");
		scanf("%d",&num);
		if(head->num==num)
		{
          
   
			p2=head;
			head=head->next;
			free(p2);
		}
		if(head==NULL)
			return NULL;
		p1=head;
		p2=head->next;
		while(p2)
		{
          
   
			if(p2->num==num)
			{
          
   
				p1->next=p2->next;
				free(p2);
			}
			else 
				p1=p2;
			p2=p1->next;
		}
		printf("
继续删除请按回车
");
		printf("
结束删除课程按 0: [ ]");
		ch1=getchar();                       //将回车键赋给CH1,否则num输完后再输入的回车键会赋给CH,因此用CH1填补。
		ch=getchar();
		printf("
删除完毕,新信息存入文件中
");
		system("pause"); 
	}
	return head;
	system("pause");
}


void choose()//选新课程--学生选课函数
{
          
   
	SUB *p,*q;
	int a[5];
	int num,total=0,i=0,j;  //total为总学分,i为num的数组单元
	printf("输入要选修的课程的编号,编号之间以空格分开
");
	scanf("%d",&num);
	printf("如果确认输入完要选修的课程的编号,请输入0: [ ]");
	while(num!=0)
	{
          
   
		for(p=head;p;p=p->next)
		if(p->num==num)
		{
          
   
			total=total+p->score;
			a[i]=num; //数组a存num
			i++;
		}
		scanf("%d",&num);
	}
	if(total<60)
	{
          
    
		printf("选修总学分为%d,未达到60,选修失败!
",total);
		system("pause");
	}
	else
	{
          
   
		
		FILE *fp;    //声明fp是指针,用来指向FILE类型的对象,fp是指向文件结构体的指针变量
		fp=fopen("3.txt","w"); 	  //fprintf(fp,"课程编号  课程名称  课程性质  总学时  授课学时  实践或上机学时  学分  开课学期
");
		for(j=0;j<i;j++)
		for(q=head;q;q=q->next)
			if(q->num==a[j])
			fprintf(fp,"%5d%12s%9s%9d%9d%11d%11d%7d
",q->num,q->name,q->kind,q->stime,q->ttime,q->etime,q->score,q->term);
		fclose(fp);  //关闭
		printf("		
*****选修成功!****
");
		printf("
您选修的课程总学分为%d,课程分别为:
",total);
		printf("
课程编号  课程名称  课程性质  总学时  授课学时  实践或上机学时  学分  开课学期
");
		for(j=0;j<i;j++)
		for(q=head;q;q=q->next)
			if(q->num==a[j])
				printf("%5d%12s%9s%9d%9d%11d%11d%7d
",q->num,q->name,q->kind,q->stime,q->ttime,q->etime,q->score,q->term);
		printf("
以上信息全部保存在3.txt中
");
	}
	system("pause");   //暂停,没这个语句点开生成的.exe文件
}


void search()          //课程信息查询
{
          
   
   int a,num;
   int t=1;
   char type[10],min[10];
   SUB *ptr;
    L1:system("cls");
   printf("

		**********请选择查询方式*************
");
   printf("
			1---按课程名称查找
");
   printf("
			2---按课程性质查找
");
   printf("
			3---按学分查找
");
   printf("
			4---退出查找
");
   printf("

		**************************************
");
   printf("

Chiose your number(1-4):[ ]");
   scanf("%d",&a);
   switch(a)
   {
          
   
		case 1:printf("请输入要查找的课程的名称:");
			scanf("%s",min);
			printf("课程编号  课程名称  课程性质  总学时  授课学时  实践或上机学时  学分  开课学期
");
			for(ptr=head;ptr;ptr=ptr->next)
				if(strcmp(min,ptr->name)==0)
				{
          
   
				 printf("%5d%12s%9s%9d%9d%11d%11d%7d
",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);
				 t=0;
				}
			if(t) 
				printf("	
未找到!
");
			t=1;
			system("pause");
			goto L1;
				
	   case 2:printf("请输入要查找的课程的性质:");
		   scanf("%s",type);
		   printf("课程编号  课程名称  课程性质  总学时  授课学时  实践或上机学时  学分  开课学期
");
		   for(ptr=head;ptr;ptr=ptr->next)
			   if(strcmp(type,ptr->kind)==0)   //strcmp为比较字符串
				{
          
   
					 printf("%5d%12s%9s%9d%9d%11d%11d%7d
",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);
					 t=0;
				}
			if(t) 
				printf("	
未找到!
");
			t=1;
			system("pause");
			goto L1;     //直接跳转到L1执行下面的语句
  
	  case 3:printf("输入要查找的课程的学分:");
		   scanf("%d",&num);
		   printf("课程编号  课程名称  课程性质  总学时  授课学时  实践或上机学时  学分  开课学期
");
		   for(ptr=head;ptr;ptr=ptr->next)
			   if(ptr->score==num)
				{
          
   
					 printf("%5d%12s%9s%9d%9d%11d%11d%7d
",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);
					 t=0;
				}
			if(t) 
				printf("
	未找到!
");
			t=1;
			system("pause");  //暂停
			goto L1;
			
		case 4:break;
	}
}

 
void Mangers()         //管理员界面(登录)
{
          
   
   int n,w=1,flag=0,i=3;
   char s[8];
   char password[7]="888";
   do
     {
          
   
        printf("

Enter password:");
        scanf("%s",s);
        if(!strcmp(s,password))              //进行密码匹配验证
         {
          
   
             flag=1;
           	break;
          }
       else
        {
          
   
          printf("

Error! You only have %d times! Enter again:
",i-1);
          i--;
        } 
     }
	while(i>0);
   if(!flag)
      {
          
   
      	printf("you have Enter 3 times!");       //输入密码超过了3次!!
        exit(0);                                //自动退出
      }
    do
      {
          
   
		system("cls");
    	puts("

		*********************管理员菜单***********************

");
        puts("				1---浏览课程
");
		puts("				2---查询课程
");
        puts("				3---添加课程
");
        puts("				4---删除课程
");
		puts("				5---返回主菜单");
		puts("

		******************************************************
");
        printf("Chiose your number(1-5):[ ]");
        scanf("%d",&n);
       switch(n)
        {
          
   
     	  case 1:prin();break;
		  case 2:search();break;
     	  case 3:insert();savefile();break;
     	  case 4:del();savefile();break;
		  case 5:return;
		  default:;
        } 
      }
      while(w==1);
}


void Students()    //学生菜单
{
          
   
	int n,w=1;
   do
    {
          
   
		system("cls");   //清屏;清除上一个回车的内容
    	puts("

		*********************学生菜单***********************

");
        puts("				1---浏览所有课程
");
		puts("				2---查询课程信息
");
        puts("				3---选择选修课程
");
        puts("				4---浏览我选修的课程
");
        puts("				5---删除错选课程
");
		puts("				6---返回主菜单");
        puts("

		****************************************************
");
        printf("Chiose your number(1-6):[ ]");
        scanf("%d",&n);
        switch(n)
        {
          
   
     	  case 1:prin();break;
		  case 2:search();break;
     	  case 3:choose();break;
    	  case 4:prin1();break;
     	  case 5:del();savefile1();break;
		  case 6:return;
		  default:;
        } 
      }
      while(w==1);
}
int  main()
{
          
   
	int n,w=1;
	do
    {
          
   
		system("cls");
		puts("

		*********************MENU***********************

");
        puts("				1.以管理员身份登录(password 1)
");
        puts("				2.以学生身份登录
");
		puts("				3.退出");
   puts("

		************************************************
");
        printf("Chiose your number(1-3):[ ]");
        scanf("%d",&n);
		switch(n)
		{
          
   
		case 1:Mangers() ;break;  //case为语句1
		case 2:Students();break;
		case 3:w=0;break;
		default:;   //语句n+1
		}
    }

    while(w==1);
    return 0;
}

//首先,进入管理员和学生界面: //然后,以管理员身份登录,登陆密码为888(这个可以在登陆程序中自己设置) 进入… //登录到管理员界面后,选‘3’可以先添加课程: //管理员添加课程: 选择‘1’可以浏览课程,选择‘4’可以在删除不想要的课程:

删除后浏览课程: //返回主菜单后,进入学生菜单,可以浏览课程,决定自己选啥,可以查询课程,通过课程性质,学分,编号之类的。 //进行选课,当学分达到要求后,选课成功,并且数据会保存到文件夹3.txt中,可以在内存中找见。 //选课完后,可以浏览自己选的课程,如果不想要就可以删除 好了,如果有什么疑问可以私我,互相探讨。。。



这篇关于学生选修课程系统(C语言/C++实现)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程