宿舍管理项目之完结版

时间:2021-01-12
本文章向大家介绍宿舍管理项目之完结版,主要包括宿舍管理项目之完结版使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在之前的基础上,我用对操作界面进行了小小的改动,同时完成对输入的数据写入文本的操作,虽然有地方明白需要完善,但是水平有限,无法把别人的代码有效的利用,包括对文本数据的修改与删除,这种对文本的直接操作仍然无法解决,这个学期就先写到这里吧

include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <windows.h>

struct Student
{
	int card;
	int classroom;
	int dormitory;
	int buildingnum;
	char name[20];
};

struct Student ID[620];

int dormitory_people[60]={0};
int dormitory_fee[60];
int building[5]={0};


void password()
{
   char user_name[20];
   

   int i,sum;
   char k;
   
   printf(" \n\n\n\t\t                   ----------------------------------\n");

   printf("    \t\t                   |                                |\n");
   printf("    \t\t                   |                                |\n");
   printf("    \t\t                   |                                |\n");
   printf("    \t\t                   |     欢迎进入宿舍管理系统       |\n");
   printf("    \t\t                   |                                |\n");
   printf("    \t\t                   |                                |\n");
   printf("    \t\t                   |                                |\n");
   printf("    \t\t                   ----------------------------------\n");

   
   for(i = 1; i < 4; i++)
   {
   		sum=0;
   		char password[20];
   		 
     	printf("\n\n\t\t\t\t     请输入您的用户名:");
        gets(user_name);
        
        printf("\t\t\t\t     请输入您的密码:");
        for(;;)//此处为输入密码不回显操作 
        {
            k=getch();
            if(k=='\r') //如果输入k为回车,则跳出循环 
            {
                break;
            }
            if(k=='\b')//如果输入k为删除键 
            {
                if(sum>0)//如果密码还没完全删除 
                {
                    printf("\b");
                    printf(" ");
                    printf("\b");
                    sum--;
                }
            }
            else  //如果输入的k既不是删除键,也不是回车键
            {
                password[sum]=k;
                printf("*");
                sum++;
            }
        }
        password[sum]='\0';//在字符串结尾作结束符标记

        if ((strcmp(user_name,"8208201411")==0) && (strcmp(password,"088977")==0))
        {
            printf("\n\t\t\t\t     登录成功!");
            Sleep(800);
            return;
        }
        else
        {
            if (i < 3)
            {
            	
                printf("\n\t\t\t\t      用户名或密码错误,请重新输入!(剩余输入次数:%d)",3-i);
            }
			   
            else
            {
                printf("\n\t\t\t\t您已连续3次将用户名或密码输错,系统退出!");
                exit(1); 
            }
        }
   }
}

 
int select_menu()
{
	char c;
	do
	{
		system("cls"); //清除屏幕            
		
        printf("\t\t\t\t---------------学生宿舍管理系统---------------\n");  
        printf("\t\t\t\t§ 1. 添加宿舍成员信息               				\n");
        printf("\t\t\t\t§ 2. 修改宿舍成员信息              				 \n");
        printf("\t\t\t\t§ 3. 删除宿舍成员信息         				\n");
        printf("\t\t\t\t§ 4. 查询宿舍成员信息             				\n");
        printf("\t\t\t\t§ 5. 显示楼栋人数    			\n");
        printf("\t\t\t\t§ 6. 显示宿舍信息        			  \n");
        printf("\t\t\t\t§ 7  宿舍信息的浏览           				  \n");
        printf("\t\t\t\t§ 8. 将已有信息读入              			  \n");
        printf("\t\t\t\t§ 9. 退出程序           				   \n");
        
        printf("\t\t\t\t----------------------------------------------\n");
        printf("\t\t\t\t请选择您要运行的选项按(1-9):");

            c=getchar();                  

    }while(c<'0'||c>'9');
		
	return(c-'0'); 
                         
}



int Input(Student IDNum[],int n)
{
	int i=0;
	char x=0;
	int fee;
	char y=0; 
	FILE *fp;
	fp=fopen("asd.txt","a+");
	while(x!='n'&&x!='N')
	{
		printf("\t输入学号:");
		scanf("%d",&IDNum[n+i+1].card); 
		printf("\t输入姓名:");
		scanf("%s",&IDNum[n+i+1].name);
		printf("\t输入班级:");
		scanf("%d",&IDNum[n+i+1].classroom);
		
		printf("\t输入宿舍号:");
		scanf("%d",&IDNum[n+i+1].dormitory);
		
		while((dormitory_people[IDNum[n+i+1].dormitory]+1)>4) 
		{
			printf("\t该宿舍已满,请选择其他宿舍:");
			scanf("%d",&IDNum[n+i+1].dormitory);
		}
		dormitory_people[IDNum[n+i+1].dormitory]++;
		
		printf("\t输入楼栋号: ");
		scanf("%d",&IDNum[n+i+1].buildingnum);
		printf("\t充值电费为:");
		scanf("%d",&fee); 
		//printf("%d\n",dormitory_fee[IDNum[n+i+1].dormitory]);
		dormitory_fee[IDNum[n+i+1].dormitory]+=fee;
		//printf("%d\n",dormitory_fee[IDNum[n+i+1].dormitory]);
		
		building[IDNum[n+i+1].buildingnum]++;
		
		getchar(); 
		
		printf("\t是否读入文件(Y/N)?");
		scanf("%c",&y);
		if(y!='n'&&y!='N')
		{
			fprintf(fp,"\n%s %d %d %d %d",IDNum[n+i+1].name,IDNum[n+i+1].card,IDNum[n+i+1].classroom,IDNum[n+i+1].dormitory,IDNum[n+i+1].buildingnum); 
			fclose(fp);
			getchar();
			printf("\t添加成功!");
		}
		getchar();
		printf("\t是否继续输入(Y/N)?");
		scanf("%c",&x);
		
		i++;
		
	}
	 
	return n+i;	
}

void Change(Student IDNum[],int n)
{
	int i=1;
	char s[20];
	printf("\t输入要修改的学生姓名:");
	scanf("%s",s);

    while(strcmp(IDNum[i].name,s)!=0&&i<=n) i++;          

    if(i==n+1)
    {
        printf("\t对不起没找到该学生!\n");  
		system("pause");            
    	return;
    }
	
	dormitory_people[IDNum[i].dormitory]--;
	printf("\t需要修改他/她的宿舍号是:");
	scanf("%d",&IDNum[i].dormitory);
	while((dormitory_people[IDNum[i].dormitory]+1)>4) 
	{
		printf("\t该宿舍已满,请选择其他宿舍:");
		scanf("%d",&IDNum[i].dormitory);
	}
	dormitory_people[IDNum[i].dormitory]++;
	
	printf("\t需要修改他/她的班级是:");
	scanf("%d",&IDNum[i].classroom);
	
	building[IDNum[i].buildingnum]--;
	printf("\t修改后他她的宿舍楼号是:");
	scanf("%d",&IDNum[i].buildingnum);
	building[IDNum[i].buildingnum]++;
	printf("\t修改成功!\n");
	system("pause");
	
}

int Delete(Student IDNum[],int n)
{
	char s[20];

    int i=1,j;

    printf("\t输入他(她)的姓名:");              
    scanf("%s",s);

    while(strcmp(IDNum[i].name,s)!=0&&i<=n) i++;          

    if(i==n+1)
    {
        printf("\t对不起没找到该学生!\n");              
    	return(n);
    }
    
	dormitory_people[IDNum[i].dormitory]--;
	building[IDNum[i].buildingnum]--;
	
    for(j=i;j<n;j++)                  
    {
        	
		IDNum[j].card=IDNum[j+1].card;
		IDNum[j].classroom=IDNum[j+1].classroom;
		IDNum[j].dormitory=IDNum[j+1].dormitory;
		IDNum[j].buildingnum=IDNum[j+1].buildingnum;
        strcpy(IDNum[j].name,IDNum[j+1].name);

    }
		
	
		Sleep(800); 
        return(n-1);
}

void Look_up(Student IDNum[],int n)
{

	char s[20];
    int i=1;

    printf("\t输入他(她)的姓名:");                  
    scanf("\t%s",&s);

    while(strcmp(IDNum[i].name,s)!=0&&i<=n) i++;          

    if(i==n+1)

    {
    	printf("\t对不起没找到该学生!\n");  
		system("pause");         
        return;
    }
	 
    printf("\t他/她的班级:%.2d班\n",IDNum[i].classroom);  
    printf("\t他/她的宿舍号:%.2d号\n",IDNum[i].dormitory);
    printf("\t他/她的楼号:%.2d栋\n",IDNum[i].buildingnum);
    getchar();
	system("pause");
}

void Present_floor()
{
	int num;
	printf("\t请输入要统计的楼号:");
	scanf("%d",&num);
	printf("\t该楼现在的总人数为:%d人\n",building[num]);
	system("pause");
}

void Present_dormitory(Student IDNum[],int n)
{
	int i=1,q;
	printf("\t输入要查找的宿舍号:");
	scanf("%d",&q);
	
	for(i;i<=n;i++)
	{
		if(IDNum[i].dormitory==q)
		{
			printf("\t姓名:%s  ",IDNum[i].name);
			printf("班级:%.2d  ",IDNum[i].classroom);  
    		printf("宿舍号:%.2d  ",IDNum[i].dormitory);
    		printf("楼号:%.2d  ",IDNum[i].buildingnum);
    		printf("\n");
		}
		
	}
	printf("\t电费余额:%d元\n",dormitory_fee[q]);
	system("pause");
}

void Present_all(Student IDNum[],int n)
{
	int floor;
	printf("\t输入浏览的楼号:");
	scanf("%d",&floor);
	
	for(int i=1;i<=n;i++)
	{
		if((IDNum[i].buildingnum)==floor)
		{
			printf("\t姓名:%s  ",IDNum[i].name);
			printf("学号:%.2d  ",IDNum[i].card); 
			printf("班级:%.2d  ",IDNum[i].classroom);  
	    	printf("宿舍号:%.2d  ",IDNum[i].dormitory);
	    	printf("楼号:%.2d\n",IDNum[i].buildingnum);
		}
		
	}
	system("pause");
}

int Addfromtxt(Student IDNum[],int n)              

{
        int num;
        FILE *fp;                                    
        char filename[20];
		                               
        printf("\t输入文件名:");
        scanf("%s",&filename);                    

        if((fp=fopen(filename,"r"))==NULL)            
        {
	        printf("\t无法打开该文件\n");      
	        printf("\t\t");
	    	system("pause");

        return(n);

        }

          //fscanf(fp,"%d",&num);                  

		int temp=n;
        while(!feof(fp))                

        {
		 	
      		fscanf(fp,"%s%d%d%d%d",IDNum[n+1].name,&IDNum[n+1].card,&IDNum[n+1].classroom,&IDNum[n+1].dormitory,&IDNum[n+1].buildingnum);
          	n++;
			
        }

        //printf("%d\n",n-1);
        fclose(fp);
		for(int j=temp+1;j<=n-1;j++)
		{
			dormitory_people[IDNum[j].dormitory]++;
			building[IDNum[j].buildingnum]++;
		}       
        printf("\t读入成功!\n");
        printf("\t\t");
    	system("pause");
     
        return(n-1);

}


int main()
{
	int n=0;
	memset(dormitory_fee,0,sizeof(dormitory_fee));
	system("color 3E");
	password();
	
	for(;;)
	{
		switch(select_menu())
		{
			case 1:
			printf("\t添加宿舍成员信息:\n");
			n=Input(ID,n);
			break;
			
			case 2:
			printf("\t修改宿舍成员信息:\n");
			Change(ID,n);
			break;
			
			case 3:
			printf("\t删除宿舍成员信息:\n");
			n=Delete(ID,n);
			break;
			
			case 4:
			printf("\t查询宿舍成员信息:\n");
			Look_up(ID,n);
			break;
			
			case 5:
			printf("\t显示楼栋人数:\n");
			Present_floor();
			break;
			
			case 6:
			printf("\t显示宿舍信息:\n");
			Present_dormitory(ID,n);
			break;
			
			case 7:
			printf("\t宿舍信息的浏览:\n");
			Present_all(ID,n);
			break;
			
			case 8:
			printf("\t将已有信息读入:\n");
			n=Addfromtxt(ID,n);
			break; 
			
			case 9:
			printf("\t谢谢使用\n");
			exit(0); 
			
		}
			
	}	
}
/*tzp 1 2 1 1
ttt 2 1 1 1
qwe 3 3 2 2
qyq 4 1 2 2
rte 5 2 2 2
yui 6 3 2 2
yis 7 4 3 3
jjy 8 4 4 4
ijh 9 1 5 1
kki 10 2 5 1
kij 11 2 5 1
fhb 12 5 6 2
kih 13 5 6 2
hjk 14 6 7 3
ksf 15 6 7 3*/
/*int flag=0;//flag是否找到删除学生的标志

	char c;
	do
	{	 
		FILE*fp1,*fp2;
		fp1=fopen("asd.txt","r+");//为了读和写,用r+打开stu文本文件
		fp2=fopen("linshi","w+");//用w+打开一个临时的文本文件
		if(fp1==NULL||fp2==NULL)
		{ 
		  printf("cannot open file!");
		  exit(0);
		}
		for(i=1;i<=n;i++)
		{
		  while(fread(&IDNum[i],sizeof(struct Student),1,fp1))//从文件中读取数据到File
		  {
		      if(feof(fp1))// 判断文件是否读到了最后
		          break;
		      if(strcmp(s,IDNum[i].name)==0)//判断要删除的学号和姓名是否与文件数据有相同,若相同则跳过这次循环
		      {
		          flag=1;
		          continue;
		      }
		      else
		      {
		          fwrite(&IDNum[i],sizeof(struct Student),1,fp2);//将File的信息写入文件
		      }
		  }
		}
		fclose(fp1);
		fclose(fp2);
		if(system("copy linshi asd.txt")==0)//将linshi文件的内容复制覆盖到stu文件,若成功则返回0
		{   
		  
		  system("DEL linshi");//删除linshi文件
		  
		}
		if(flag==1)//找到删除学生            
		{
		                 
		  printf("\t成功删除该学生,系统将自动帮您更新并保存档案\n");
		  Sleep(800);
		  return(n-1);
		                 
		}
		if(flag==0)//无法找到被删除学生
		{
		  
		printf("\t删除失败!学号或姓名输入错误");
		 exit(0);
		  
		}
	
	}while(1);

    */