C语言学生管理系统

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 60 

typedef struct         //int char float doouble
{
	char name[9];      //姓名 
	char no[13];       //学号 
	char gender[3];   //性别 
	char fromPlace[9];//生源地 
	char phone[12];//电话号码 
	
	
}Student;
Student student;

Student studentInformations[MAX];//存储学生信息的结构体数组 
void menu ();//UI界面
void menu1 ();// 统计学生信息UI页面 
void input_informations();//输入学生信息
void glanceStuData();  //加载并查看学生信息
void showGender();//统计学生男女生人数 
void showFromPlace ();//统计学生生源地信息 
void modify();//修改/删除学生信息 
int Totalcounts;//学生总数 
int a;//男生总数 
int b;//女生总数 


//-----------UI界面 -----------
void  menu ()
{
	printf("***************************************************\n");
	printf("*               学生信息管理系统                  *\n");
	printf("*********************主菜单************************\n");
	printf("*                 1输入学生信息                   *\n");
	printf("*                 2查看学生信息                   *\n");
	printf("*                 3统计学生信息                   *\n");
	printf("*                 4修改学生信息                   *\n");
	printf("*                   0退出系统                     *\n");
	printf("***************************************************\n");//系统界面 
	
	
	
	
} 


//----------次菜单UI界面----------
void  menu1 () 
{
	printf("***************************************************\n");
	printf("*********************次菜单************************\n");
	printf("*                 1统计学生性别信息                *\n");
	printf("*                 2统计学生生源地信息              *\n");
	printf("*                   3退出系统                      *\n");
	printf("***************************************************\n");//系统界面 
} 


 

	
//-----------输入学生信息---------	
   void input_informations()
    {
       FILE*fp;
       char yn='y'; 
	
		
			  if((fp=fopen("D:student.txt","a"))!=NULL) 
			      {
				   while(yn=='y')
			     	{ 
					printf("\n您已进入输入学生信息界面\n") ;
					printf("请输入学生信息\n");
					printf("输入学生姓名:");
					scanf("%s",student.name); 
					student.name[8]='\0';
					
					printf("输入学生学号:");
					scanf("%s",student.no); 
					student.no[12]='\0';
					
					printf("输入学生性别:");
					scanf("%s",student.gender); 
					student.gender[2]='\0';
					
					
					printf("输入学生生源地:");
					scanf("%s",student.fromPlace); 
					student.fromPlace[8]='\0';
					
					printf("输入学生手机号:");
					scanf("%s",student.phone); 
					student.phone[11]='\0';
					
					fflush(fp);//清空文件缓冲区
					fflush(stdin);//清空键盘缓冲区 
					
					
					printf("姓名:%s 学号:%s 性别: %s 生源地:%s 电话:%s\n",student.name,student.no,student.gender,student.fromPlace,student.phone);
					fprintf(fp,"%-8s,%-12s,%-2s,%-8s,%-11s\n",student.name,student.no,student.gender,student.fromPlace,student.phone);
					printf("是/否继续输入学生信息(y/n):"); 
					scanf("%c",&yn); 
				     }
			    	 fclose(fp) ;
					
				}else
		 printf("文件打开错误!"); 
	}
              
              
              
              
              
//--------------加载并查看学生信息  ---------------          
void glanceStuData()
{
    FILE *fp;
    int stuPos=0;
    fp=fopen("D:student.txt","r"); //用"r"只读形式打开文本文件 
    printf("%-8s,%-12s,%-2s,%-8s,%-11s\n","姓名","学号","性别","生源地","手机号");
    
    if(fp !=NULL)
    {
    	while(fread( & studentInformations[stuPos],sizeof(Student), 1 ,fp)  >= 1)// 按照一名一名学生读取一组数据块
    	{
    		if(studentInformations[stuPos].name[0]!='\0')
    		{
    			studentInformations[stuPos].name[8]='\0';
    			studentInformations[stuPos].no[12]='\0';
    			studentInformations[stuPos].gender[2]='\0';
    			studentInformations[stuPos].fromPlace[8]='\0';
    			studentInformations[stuPos].phone[11]='\0';//将学生信息依次换行打印出来  
    		printf("%-8s,%-12s,%-2s,%-8s,%-11s\n",studentInformations[stuPos].name,studentInformations[stuPos].no,studentInformations[stuPos].gender,studentInformations[stuPos].fromPlace,studentInformations[stuPos].phone);	
			stuPos++;
		    }
		    Totalcounts = stuPos+1;
		}
	}
	else
	   printf("文件打开错误!!");
	   fclose(fp); 
}





//------------统计学生性别信息 ----------------- 
void showGender()
{
	int i;
	int j;
    
	for(i=0;i<Totalcounts;i++)
    {
		    if(strcmp(studentInformations[i].gender,"男")==0)
		   {//对比数组里面的gender跟"男" 是否相同,相同则男生数量"a"加一 
			a++;
		   }
		   else if(strcmp(studentInformations[i].gender,"女")==0) 
		   {//对比数组里面的gender跟"女" 是否相同,相同则女生数量"b"加一 
			b++;
		   } 	
    } 
	
	 printf("一共有%d个男生\n",a);
     printf("一共有%d个女生\n",b);

} 





 //------------按生源地统计学生信息--------------*/ 
void showFromPlace() 
 {
 	struct PlaceCount 
 	{
 		char Place[9];//生源地 
 		int count;   // 该生源地的人数 
 		
	 }; 
	 struct PlaceCount PlaceCounts [Totalcounts];//创建结构体数组,每个元素包含一个char类型的place和int型的count 
	 
	      int i ; 
	      int marks=0;
	      int outer,inter;
	      struct PlaceCount tmp;//定义一个 结构体变量来交换 
	 
         	for(i=0;i<Totalcounts;i++) 
         	{     //初始化结构体数组 
	       PlaceCounts[i].Place[0] ='0';
	       PlaceCounts[i].count= 0;
	       
	        }
	 
	        for (i=0 ;i< Totalcounts ; i++)
		    {//将studentinformations结构体数组的每FromPlace拷贝到placecounts结构体数组中 
	        
	 	   strcpy(PlaceCounts[i].Place,studentInformations[i].fromPlace);
	        }
	 
	     
            for (outer=0 ;outer< Totalcounts-1; outer++)
            { //采用冒泡排序的方法将结构体数组接place大小进行排序 
	              for(inter=0;inter<Totalcounts-outer-1;inter++)//统计相同生源地的个数
        	       {
        	       	   if(strcmp(PlaceCounts[inter].Place,PlaceCounts[inter+1].Place)>0) 
        	       	    {
        	       	
        	 	        tmp =PlaceCounts[inter];//创建临时变量tmp,用于交换前后两个元素的位置 
        	 	         PlaceCounts[inter]=PlaceCounts[inter+1];
				         PlaceCounts[inter+1]=tmp;
				        }
				   }
			 }
		  
 
	          for(i=0;i<Totalcounts;i++)//统计相同生源地个数 
			  {
			  	
        	      if(strcmp(PlaceCounts[i].Place,PlaceCounts[marks].Place)==0)
         	      {
	          	/*
		       将已经排序好的结构体数组中的place元素进行比较,用marks=0作为起点
		       让它与后面的每一个生源地进行比较是否相同,相同则该生源地加,
		       i递增,marks不变,不同则将i赋值给marks,作为新起点与后面的生源地进行比较 
	         	*/ 
	                   PlaceCounts[marks].count++;
	              }  
	              else
			      {
	                  marks=i;
	                  PlaceCounts[marks].count++;
                  }
               }
    
              for(i=0; i < Totalcounts; i++)
              {
                  if(PlaceCounts[i].count > 0) //避免打印相同的生源地 
                {
                  printf("%-8s:\t",PlaceCounts[i].Place);//打印生源地名字		
                  printf("%d",PlaceCounts[i].count);//打印生源地个数	
			      printf("\n");
	            }
               }
   printf("  \n");
}




 //-----------修改/删除学生系统 ----------- 
void modify()
{
int i=0,j=0;
       int choose;//功能的选择 
       int num;// 选择的学生序号
       int Pos;
       FILE *fp;
    
        //将学生信息依次换行全部打印出来 
  
    printf("%-8s,%-12s,%-2s,%-8s,%-11s\n","姓名","学号","性别","生源地","手机号");
    for(i=0;i<Totalcounts;i++)
    {
   	printf("%-2d,%-8s,%-12s,%-2s,%-8s,%-11s\n",i,studentInformations[i].name,studentInformations[i].no,studentInformations[i].gender,studentInformations[i].fromPlace,studentInformations[i].phone);	
    }
    printf("%s","请输入要修改的学生序号:") ;
	scanf("%d",&num) ;
	printf("%-8s,%-12s,%-2s,%-8s,%-11s\n",studentInformations[num].name,studentInformations[num].no,studentInformations[num].gender,studentInformations[num].fromPlace,studentInformations[num].phone) ;
	   
    printf("请选择要选择的功能\n");  
    printf("1.删除学生信息/2.修改学生信息\n");  
    scanf("%d",&choose);
    if(choose==1)//输入1选择删除学生信息 
    {
           
            for(Pos=num;Pos<Totalcounts-1;Pos++)
            { //当输入的序号跟数组下标相同时 ,将下一组studentInformations数据覆盖给上一组 
            studentInformations[Pos]=studentInformations[Pos+1];
			}
			Totalcounts--;
			//删除一条学生信息之后,学生总人数减一 
			printf("删除成功\n"); 
			fp=fopen("D:student.txt","w");//用"w"的形式打开文件 清空文件内容,便于新的学生信息录入 
	     	if(fp!=NULL)
		    {
	    	for(i=0;i<Totalcounts;i++)//删除一条学生消息,结构体数组发生改变 
			{                        //所以选择用for循环重新将所有学生信息重新录入文件 
			fprintf(fp,"%-8s,%-12s,%-2s,%-8s,%-11s\n",studentInformations[i].name,studentInformations[i].no,studentInformations[i].gender,studentInformations[i].fromPlace,studentInformations[i].phone);
		    }
			fclose(fp);
	   }
	   else
    	{
		printf("文件保存错误\n") ; 
	} 
		
}
else//修改学生信息 
{
	fp=fopen("D:student.txt","w");
	//用"w"的形式打开文件,清空缓存区,便于新的修改信息录入 
	//选择序号用新的学生信息覆盖原本错误的学生信息
	printf("请重新输入学生信息\n");
    printf("请输入姓名信息\n");
    scanf("%s",studentInformations[num].name); 
	student.name[8]='\0';
	//将新输入的学生信息覆盖掉原本错误的学生信息				
	printf("请输入学生学号:");
	scanf("%s",studentInformations[num].no); 
	student.no[12]='\0';
					
	printf("输入学生性别:");
	scanf("%s",studentInformations[num].gender); 
	student.gender[2]='\0';
					
					
	printf("输入学生生源地:");
	scanf("%s",studentInformations[num].fromPlace); 
	student.fromPlace[8]='\0';
					
	printf("输入学生手机号:");
	scanf("%s",studentInformations[num].phone); 
	student.phone[11]='\0';
	
	printf("修改学生信息成功\n") ;
	printf("成功修改的学生信息如下\n") ;
	printf("姓名:%-8s 学号:%-11s 性别:%-2s 生源地:%-8s 手机号:%-11s\n",studentInformations[num].name,studentInformations[num].no,studentInformations[num].gender,studentInformations[num].fromPlace,studentInformations[num].phone);

   //将修改后的所有学生信息重新录入文件中
	for(i=0;i<Totalcounts;i++) 
	{
		fprintf(fp,"%-8s,%-12s,%-2s,%-8s,%-11s\n",studentInformations[i].name,studentInformations[i].no,studentInformations[i].gender,studentInformations[i].fromPlace,studentInformations[i].phone);
	}
	fclose(fp);
}
}




//--------------主函数	------------		
    int main(int args,char*grvv[]) 
{
        menu ();       //调用界面函数 
	    int num;       //设置主页面系统输入变量   
		int num1;      //设置次页面系统输入变量
		printf("请重新输入要选择的功能:") ;
	    scanf("%d",&num);
		while(num!=0)
		{
	   	switch(num)
	   	 {
		
            case 1:
            printf("2.您已经进入了输入学生信息系统\n");	
			    input_informations();//调用输入学生信息的结构体数组	
			    break;
			     
         	case 2:
				printf("2.您已经进入了查看学生信息系统\n");
				glanceStuData();    //调用加载并打印学生信息的函数 
				break;
			case 3:
				  printf("3.您已经进入了统计学生信息系统\n"); 
				  menu1 (); 
		          printf("请输入次菜单要选择的功能(0-3):") ;
	              scanf("%d",&num1);
		        do
	           	{   
	   	              switch(num1)
	   	              {
                        case 1:
					    printf("2.您已经进入了统计学生性别信息系统\n");
					    showGender();        //调用统计学生性别信息函数 
				        break;
				
                 	    case 2:
                        printf("2.您已经进入了统计学生生源地信息系统\n");
                        showFromPlace();  //调用统计学生生源地信息函数 
                        break; 
                        
			    	    default:
					    printf("3.退出统计系统\n") ;
					    break;
			           }		
				        printf("请再次输入次菜单要选择的功能: (0-3)\n") ;
	                    scanf("%d",&num1);
	              }
	              while(num1!=3); 
				  break;
			case 4:
				printf("4.您已经进入了修改学生信息系统\n"); 
			    modify();               //调用修改学生信息函数 
			
				break;
			case 0:
				break;
        }
             printf("请输入要选择的功能:(0-4)\n");
	         scanf("%d",&num);
	    } 
  
    return 0;
}


 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值