【C語言課程設計】學生學籍管理系統

xiaoyuyyun發表於2014-01-18
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>


//**********************************結構體*************************************
struct score
{
	char class_num[10];
	char person_num[10];
	char name[15];
	char sex;
	int  num;
}stu;



//******************************輸出*******************************************
void print1()                                               //輸出科目函式
{
printf("班級      學號      姓名              性別      分數\n");
}



//*****************************選單********************************************
int menu()                                                  //選單函式
{

	char i;
	printf("\n\n\n\n");
    puts("\t\t**********************主選單******************\t\n");
    puts("\t\t\t\t1.學生資訊輸入                               \n");
    puts("\t\t\t\t2.學生資訊瀏覽                               \n");
    puts("\t\t\t\t3.學生資訊檢索                               \n");
    puts("\t\t\t\t4.學生資訊修改                               \n");
    puts("\t\t\t\t5.學生資訊刪除                               \n");
    puts("\t\t\t\t6. 退 出 系 統                               \n");
    puts("\t\t**********************************************\t\n");
    printf("\t\t\t  請選擇(1-6):[ ]\b\b");
	i=getchar();
	system("cls");
	return i;
}







//**********************輸入函式*******************************************
void Enter()
{

	FILE *fp;
	char ch;
	if((fp=fopen("f:\\zimou.txt","a+"))==NULL)
	{
		printf("\nCannot open file!\n");
		getch();
		exit(1);
	}
	do
	{
		printf("請輸入班級:");scanf("%s",stu.class_num);getchar();
		printf("\n");
		printf("請輸入學號:");gets(stu.person_num);
		printf("\n");
		printf("請輸入姓名:");gets(stu.name);
		printf("\n");
		printf("性      別(m/f):");scanf("%c",&stu.sex);getchar();
		printf("\n");
		printf("分      數:");scanf("%d",&stu.num);getchar();
		printf("\n");
		fwrite(&stu,sizeof(stu),1,fp);
		printf("繼續(y/n)?[ ]\b\b");
		ch=getchar();  getchar();
	}while(ch=='y'||ch=='Y');
	fclose(fp);
	system("cls");
}



//*****************瀏覽函式**********************************************
void Browse()
{

FILE *fp;
int total=0;
if((fp=fopen("f:\\zimou.txt","a+"))==NULL)
{
   printf("\tCan not open the inform file!");
   getch();
   exit(1);
}
while(fread(&stu,sizeof(stu),1,fp)==1)
{
   total++;
   if(total==1)
	print1();
    printf("%-10s%-10s%-20s",stu.class_num,stu.person_num,stu.name);
    printf("%-8c",stu.sex);
    printf("%-4d   ",stu.num);
    printf("\n");
}
fclose(fp);
printf("\n\n\t共有 %d 條記錄!\n",total);
printf("\tpress any key to continue!");
getchar();
getchar();
system("cls");

}


//*******************查詢函式*********************************************
void Query()
{
	FILE * fp;
    char num1[10];
    char ch;
if((fp=fopen("f:\\zimou.txt","a+"))==NULL)
{
   printf("\tCan not open the inform file!");
   getch();
   exit(1);
}
do
{
	getchar();
    rewind(fp);
    printf("\n請輸入學號查詢:");
    gets(num1);
    while(fread(&stu,sizeof(stu),1,fp)==1)
     if(strcmp(num1,stu.person_num)==0)
     {
		 print1();
         printf("%-10s%-10s%-20s",stu.class_num,stu.person_num,stu.name);
         printf("%-8c",stu.sex);
         printf("%-4d   ",stu.num);
         printf("\n");
		 break;
     }
	 puts("\n");
   printf("繼續查詢?(Y/N):[ ]\b\b");
   ch=getchar();
}while(ch=='Y'||ch=='y');
fclose(fp);
system("cls");
}


//**********************學生成績修改函式***********************************

void Recopose()
{
     FILE * fp1,* fp2;
     char num1[10];
	 char ch;
	 getchar();
    do
	{
        if((fp1=fopen("f:\\zimou.txt","a+"))==NULL)
		{
        printf("\tCan not open the infom file!");
        getch();
        exit(1);
		}
        if((fp2=fopen("f:\\ziye.txt","a+"))==NULL)
		{
        printf("\tCan not creat the temp file!");
        getch();
        exit(1);
		}
        printf("請輸入想要修改的學號:");
        gets(num1);
while(fread(&stu, sizeof(stu),1,fp1)==1)
{
        if(strcmp(num1,stu.person_num)==0)
		{
         print1();
         printf("%-10s%-10s%-20s",stu.class_num,stu.person_num,stu.name);
         printf("%-8c",stu.sex);
         printf("%-4d   ",stu.num);
         printf("\n");
         printf("\n\nPlease input the new information:\n");
         printf("\t班級代號:");gets(stu.class_num);
         printf("\t學生學號:");gets(stu.person_num);
         printf("\t學生姓名:"); gets(stu.name);
         printf("\t學生性別:");gets(&stu.sex);
         printf("\t學生成績:");scanf("%d",&stu.num); getchar();
		}
    fwrite(&stu,sizeof(stu),1,fp2);
   }
   fclose(fp1);
   fclose(fp2);
   printf("修改成功!\n");
   remove("f:\\zimou.txt");
   rename("f:\\ziye.txt","f:\\zimou.txt");
   printf("繼續修改?(Y/N):[ ]\b\b");
   ch=getchar(); getchar();
}while(ch=='Y'||ch=='y');
	system("cls");
}


//*********************刪除函式******************************************
void Delete()
{
   	 FILE * fp1,* fp2;
     int flag;
     char num1[10];
	 char ch;
	 getchar();
     do
	 {
        if((fp1=fopen("f:\\zimou.txt","a+"))==NULL)
		{
           printf("\tCan not open the inform file!");
           getch();
           exit(1);
		}
        if((fp2=fopen("f:\\ziye.txt","a+"))==NULL)
		{
           printf("\tCan not creat the temp file!");
           getch();
           exit(1);
		}
        printf("請輸入你要刪除的學生的學號:");
        gets(num1);
        flag=0;
        while(fread(&stu,sizeof(stu),1,fp1)==1)
		{
           if(strcmp(num1,stu.person_num)==0)
		   {
              print1();
              printf("%-10s%-10s%-20s",stu.class_num,stu.person_num,stu.name);
              printf("%-8c",stu.sex);
              printf("%-4d   ",stu.num);
              printf("\n");
              flag=1;
		   }
		   else
			   fwrite(&stu,sizeof(stu),1,fp2);
		}
       fclose(fp1);
       fclose(fp2);
       if(flag==1)
	   {
          remove("f:\\zimou.txt");
          rename("f:\\ziye.txt","f:\\zimou.txt");
	   }
       else
          printf("Can not find this record!\n");
        printf("繼續刪除?(Y/N):[ ]\b\b");
        ch=getchar(); getchar();
	 }while(ch=='Y'||ch=='y');
	 system("cls");
}


//***********************************退出**************************************
void Exit()
{
	puts("\n\t\t\t\t感謝使用本系統!!\n\n\t\t\t\t有任何問題請聯絡:1005483758@qq.com \n");
	exit(0);
}

//******************歡迎介面********************************** 
void Welcome()
{
	  printf("\n\n");
   printf("  \t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
   printf("  \t┃**********************************************************┃\n");
   printf("  \t┃***┏━━━━━━━━━━━━━━━━━━━━━━━━┓***┃\n");
   printf("  \t┃***┃************************************************┃***┃\n");
   printf("  \t┃***┃***                                         ****┃***┃\n");
   printf("  \t┃***┃***        歡迎使用學生成績管理系統         ****┃***┃\n");
   printf("  \t┃***┃***                                         ****┃***┃\n");
   printf("  \t┃***┃***                                         ****┃***┃\n");
   printf("  \t┃***┃***                    製作人:XXY          ****┃***┃\n");
   printf("  \t┃***┃***                                         ****┃***┃\n");
   printf("  \t┃***┃***                        2013.6.20        ****┃***┃\n");
   printf("  \t┃***┃***                                         ****┃***┃\n");
   printf("  \t┃***┃************************************************┃***┃\n");
   printf("  \t┃***┗━━━━━━━━━━━━━━━━━━━━━━━━┛***┃\n");
   printf("  \t┃**********************************************************┃\n");
   printf("  \t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
   puts("  \n\t                        ① 管理員登陸!                         \n");
   puts("  \n\t                        ② 遊客  登陸!                         \n");
   printf("\n請選擇登陸(1 or 2):[ ]\b\b");  / /左移一位“\b ” 
}











//*****************************主函式****************************************
int main()
{ 
		int i,k=0;
        char username[15],pwd[7],c;
		system("color 5e");//顏色
        Welcome(); 
	      c=getchar();getchar();
       if (c=='1')
	   {
            system("cls");//跳轉頁面。 
			puts("\n\n\n\n\n\n\n\t\t\t\t管 理 員 登 陸!\n\n\n\n\n\t\t\t\t   請 登 陸!"); 
			getchar();
	   		for(i=0;i<3;i++)
		        {
			   		 printf("\n請輸入使用者名稱:");
			        gets(username);
			        printf("\n請輸入6位密碼:");
			        gets(pwd);
					//判斷條件 
			        if((strcmp(username,"xiaxianyun")==0)&&(strcmp(pwd,"123456")==0))
			        {
		                printf("\n您已經成功登入\n");
		                k=1;
		                for(i=0;i<20;i++)
		                {
		                        printf(".");
		                        Sleep(100);
		                }
	                system("cls");
	 
						while(1)
						switch(menu())
						{
							case '1':Enter();break;
							case '2':Browse();break;
							case '3':Query();break;
							case '4':Recopose();break;
							case '5':Delete();break;
							case '6':Exit();break;
						}
		        		break;
	        		}
	        	else
	                printf("\n使用者名稱或密碼無效 請重新輸入:\n");
	        		continue;
	        	}
	        	 if(k==0)
                printf("\n連續輸入錯誤3次 將退出程式\n");
        Sleep(2000);
        exit(1);
    	}
     else if(c=='2')//遊客登入 
		{
			system("cls");//跳轉頁面。 
			puts("\n\n\n\n\n\n\n\t\t\t\t歡 迎 遊 客 進 入!\n\n\n\n\n\t\t\t\t   請 確 定!"); 
			getchar();
			system("cls");
			while(1)
				switch(menu())
				{
					case '1':Enter();break;
					case '2':Browse();break;
					case '3':Query();break;
					case '4':system("cls");getchar();
					puts("\n\n\t\t對不起你沒有許可權修改!\n\n");
					printf("\t\tpress any key to continue!");getchar();system("cls");break;
					case '5':system("cls");getchar();
					puts("\n\n\t\t對不起你沒有許可權刪除!\n\n");
					printf("\t\tpress any key to continue!");getchar();system("cls");break;
					case '6':Exit();break;	
				}
		} 	   
	return 0;
}

 

相關文章