1. 程式人生 > 程式設計 >C語言實現班檔案管理系統課程設計

C語言實現班檔案管理系統課程設計

本文例項為大家分享了C語言班檔案管理系統的具體程式碼,供大家參考,具體內容如下

做的挺長時間的課程設計,當作參考吧

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 20
struct student
{
 long num;
 char name[20];
 char sex[10];
 int age;
 char bz[40];
 struct student *next;
};
int i,j,n,num2,num3,age3,k,m;
char name3[20],sex3[20],bz3[20],ch;
FILE *fp;
int login() //登陸函式
{
 char key[20];
 printf("\t  ********************請輸入系統密碼********************\n");
 do
 {
 scanf("%s",key);
 if((strcmp("a",key))==0)
 {
 printf("\t        password correct,welcome !\n");
 return 1; //當密碼正確時,返回1,進入系統
 }
 printf("\t        password incorrect,please input again!\n");
 }while(key!=1);//當返回值不為1時,重新輸入密碼,直到輸入真確為止
 system("cls");
}
int menu() //選單
{
 int c;
 printf("\t\t**********歡迎進入通訊客戶端!************\n\n");
 printf("\t\t|—————1.錄入學生的基本資訊—————|\n");
 printf("\t\t|----------2.顯示學生的基本資訊----------|\n");
 printf("\t\t|----------3.儲存學生的基本資訊----------|\n");
 printf("\t\t|----------4.刪除學生的基本資訊----------|\n");
 printf("\t\t|----------5.修改學生的基本資訊----------|\n");
 printf("\t\t|----------6.查詢學生的基本資訊----------|\n");
 printf("\t\t|—————7.退出系統——————————|\n");
 printf("\t\t請選擇您要進行的功能(0~7) ");
 scanf("%d",&c);
 return c;
}
struct student *creat() //錄入資訊函式
{
 struct student *head,*p1,*p2;
 n=0;
 p1=p2=(struct student *)malloc(sizeof(struct student));
 head=NULL;
 printf("請輸入學生資訊學號,姓名,性別,年齡,備註(鍵入學生學號為0時結束)\n");
 while(1) //為1表真,p2->next不為0;
 {
 scanf("%d",&p1->num);
 if(p1->num==0) //判斷學生的學號是否為0,如果為0則停止輸入資料;
 {
  break;
 }
 scanf("%s%s%d%s",p1->name,p1->sex,&p1->age,p1->bz);
 n=n+1;
 if(n==1)
 {
  head=p1;
 }
 else
 {
  p2->next=p1;
 }
 p2=p1;
 p1=(struct student *)malloc(sizeof(struct student));
 }
 p2->next=NULL;
 system("cls");
 return(head);
}
void print(struct student *head) //輸出資訊函式
{
 struct student *p;
 printf("\t\t這裡有 %d 個學生的資料資訊\n",n);
 p=head;
 if(head!=NULL)
 {
 do
 {
 printf("\t\t學號:%d\t姓名:%s\t性別:%s\t年齡:%d\t備註:%s\n",p->num,p->name,p->sex,p->age,p->bz);
 p=p->next;
 }while(p!=NULL);
 }
 else
 {
 return 0;
 }
 printf("\n");
}
int save(struct student *p) //儲存資訊函式
{
 FILE *fp;
 if((fp=fopen("keshe.txt","wb"))==NULL)
 {
 printf("open file fail\n");
 }
 fp=fopen("stud","wb");
 do
 {
 fwrite(p,sizeof(struct student),1,fp);
 p=p->next;
 }while(p!=NULL);
 printf("\t\t\t儲存成功!\n");
 fclose(fp);
 return 0;
}
struct student *del(struct student *head)
{
 struct student *p1,*p2;
 printf("\t\t請輸入要刪除學生的學號\n");
 scanf("%d",&num2);
 p1=head;
 if(head->num==num2)
 {
 head=head->next;
 free(p1);
 n--;
 }
 else
 {
 
 p2=head;
 while(p2->num!=num2&&p2->next!=NULL)
 {
  p1=p2;
  p2=p2->next;
 }
 if(p2->num==num2)
 {
  p1->next=p2->next;
  n--;
 }
 printf("delete:%ld\n",num2);
 }
 return (head);
}
int mod(struct student *head); //修改資訊函式
struct student *modify(struct student *head)
{
 if(login()==0)
 {
 return 0;
 }
 else
 {
 struct student *p1;
 j=0;
 p1=(struct student *)malloc(sizeof(struct student));
 printf("\t\t\t請輸入你要更改的學號\n");
 scanf("%d",&num2);
 printf("\t\t\t學號\n");
 scanf("%d",&num3);
 printf("\t\t\t姓名\n");
 scanf("%s",name3);
 printf("\t\t\t性別\n");
 scanf("%s",sex3);
 printf("\t\t\t年齡\n");
 scanf("%d",&age3);
 printf("\t\t\t備註\n");
 scanf("%s",bz3);
 p1=head;
 if(head->num==num2)
 {
  head->num=num3;
  strcpy(head->name,name3);
  strcpy(head->sex,sex3);
  head->age=age3;
  strcpy(head->bz,bz3);
  j=1;
 }
 else
 {
  p1=head->next;
  if(p1!=NULL)
  {
  while(p1->num!=num2)
  {
   p1=p1->next;
  }
  p1->num=num2;
  strcpy(p1->name,name3);
  strcpy(p1->sex,sex3);
  p1->age=age3;
  strcpy(p1->bz,bz3);
  j=1;
  }
 }
 if(j==0)
 {
  printf("\t\t\t更改失敗\n");
 }
 else
 {
  printf("\t\t\t更改成功\n");
 }
 }
 system("cls");
 mod(head);
}
int mod(struct student *head)
{
 printf("\t\t\t請選擇\n");
 printf("\t\t\t1:按學號修改學生資訊\n");
 printf("\t\t\t2:輸出修改後的學生資訊\n");
 printf("\t\t\t3:返回主選單\n");
 scanf("%d",&m);
 switch(m)
 {
 case 1:head=modify(head);break;
 case 2:print(head);break;
 case 3:menu();break;
 default:printf("\t\t\tinput error!\n");
 mod(head);
 }
}
int find(struct student *head);
int find1(struct student *head) //以學號方式查詢
{
 struct student *p1;
 p1=(struct student *)malloc(sizeof(struct student));
 printf("\t\t\t請輸入你要查詢的學生學號\n");
 scanf("%d",&num2);
 p1=head;
 while(p1!=NULL)
 {
 if(p1->num==num2)
 {
  k=1;
  printf("\t\t\t學號:%d\t姓名:%s\t性別:%s\t年齡:%d\t備註:%s\n\n",p1->num,p1->age,p1->bz);
  break;
 }
 p1=p1->next;
 }
 if(k==0)
 {
 printf("\t\t\t沒有查詢到您要找的學生資訊\n\n");
 }
 else
 {
 printf("\t\t\t這就是您要找的學生資訊\n\n");
 }
 find(head);
}
int find2(struct student *head) //以姓名方式查詢
{
 struct student *p1;
 p1=(struct student *)malloc(sizeof(struct student));
 printf("\t\t\t請輸入您要查詢的學生姓名\n");
 scanf("%s",name3);
 p1=head;
 while(p1!=NULL)
 {
 if((strcmp(p1->name,name3))==0)
 {
  k=1;
  printf("\t\t\t學號:%d\t姓名:%s\t性別:%s\t年齡:%d\t備註:%s\n\n",p1->bz);
  break;
 }
 p1=p1->next;
 }
 if(k==0)
 {
 printf("\t\t\t沒有找到該學生資訊\n\n");
 }
 else
 {
 printf("\t\t\t這就是您要查詢的學生資訊\n\n");
 }
 find(head);
}
int find3(struct student *head) //以性別方式查詢
{ 
 struct student *p1;
 p1=(struct student *)malloc(sizeof(struct student));
 printf("\t\t\t請輸入你要查詢的學生的性別\n");
 scanf("%s",sex3);
 p1=head;
 while(p1!=NULL)
 {
 if((strcmp(p1->sex,sex3))==0)
 {
  k=1;
  printf("\t\t\t學號:%d\t姓名:%s\t性別:%s\t年齡:%d\t備註:%s\n\n",p1->bz);
  break;
 }
 p1=p1->next;
 }
 if(k==0)
 {
 printf("\t\t\t沒有找到該學生資訊\n\n");
 }
 else
 {
 printf("\t\t\t這就是您要查詢的學生的資訊\n\n");
 }
 find(head);
}
int find4(struct student *head) //以年齡方式查詢
{
 struct student *p1;
 p1=(struct student *)malloc(sizeof(struct student));
 printf("\t\t\t請輸入您要查詢的學生的年齡\n");
 scanf("%d",&age3);
 p1=head;
 while(p1!=NULL)
 {
 if(p1->age==age3)
 {
  k=1;
  printf("\t\t\t學號:%d\t姓名:%s\t性別:%s\t年齡:%d\t備註:%s\n\n",p1->bz);
  break;
 }
 p1=p1->next;
 }
 if(k==0)
 {
 printf("\t\t\t沒有找到該學生的資訊\n\n");
 }
 else
 {
 printf("\t\t\t這就是您要找的學生的資訊\n\n");
 }
 find(head);
}
int find(struct student *head)
{
 printf("\t\t\t請選擇您要查詢學生資訊的方式\n");
 printf("\t\t\t1:按學生學號查詢\n");
 printf("\t\t\t2:按學生姓名查詢\n");
 printf("\t\t\t3:按學生性別查詢\n");
 printf("\t\t\t4:按學生年齡查詢\n");
 printf("\t\t\t5:返回主選單\n");
 scanf("%d",&m);
 switch(m)
 {
 case 1:find1(head);break;
 case 2:find2(head);break;
 case 3:find3(head);break;
 case 4:find4(head);break;
 case 5:system("cls");menu();break;
 default:printf("\t\t\tinput error,please input again\n");
 }
}
int main() //主函式
{
 struct student *phead;
 if(login()==0)
 {
 return 0;
 }
 
 printf("\n");
 while(1)
 {
 switch(menu())
 {
 case 1:system("cls");phead=creat();break;
 case 2:system("cls");print(phead);break;
 case 3:system("cls");save(phead);break;
 case 4:system("cls");phead=del(phead);break;
 case 5:system("cls");mod(phead);break;
 case 6:system("cls");find(phead);break;
 case 7:system("cls");printf("\t\t\t歡迎使用,再見!\n");return 0;
 default:printf("\t\t\t輸入有錯,請重新輸入\n");
 }
 }
}

更多學習資料請關注專題《管理系統開發》。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。