?? program of student file.c
字號:
#include "stdio.h"
#include "string.h"
#include "malloc.h"
typedef struct /* 聲明結構體類型 */
{
char name[24];
char num[16];
char birth[16];
char schtim[16]; /* 數組成員 */
char addr[28];
char tel[16];
char age[8];
char sex[8];
}student;
void search(void); /* 實現詳細查詢信息的功能 */ /* 以下8行是聲明各個被調用的函數,且在函數的外部 */
void consearch(void); /* 實現簡明查詢信息的功能 */
void input(void); /* 實現輸入信息的功能 */
void del(void); /* 實現刪除信息的功能 */
void rew(void); /* 實現修改重寫信息的功能 */
void next(void); /* 實現瀏覽信息的功能 */
void getinf(void); /* 實現讀入信息的功能 */
void output(void); /* 實現輸出信息的功能 */
FILE *fp; /* fopen函數,用來打開文件 */
student *p;
int length, len;
int stunum, stupos;
int main(void) /* 主函數 */
{
char ch; int k=1;
printf(" ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n");
printf(" ☆ *學生檔案信息管理系統* ☆\n");
printf(" ☆ *歡迎您* ☆\n");
printf(" ☆ ☆\n");
printf(" ☆Prodtcer: 金開春 劉莉 ☆\n");
printf(" ☆ ☆\n");
printf(" ☆Assistant Teacher: 劉素華 ☆\n");
printf(" ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n");
printf("The student's information:\n");
p=(student *)malloc(sizeof(student));
if(!p)
{
printf("Overflow\n"); return 0;}
length=sizeof(student); len=sizeof(int); /* 輸出文件中一個學生的信息 */
stunum=0; stupos=0;
fp=fopen("student","rb+");
if(!fp)
{
printf("File not exit! Please input the informatoin first!\n"); /* 若文件中無內容,則無輸出結果 */
fp=fopen("student","wb+");
if(!fp) {printf("Cannot open file!\n"); return 0;
}
fwrite(&stunum,len,1,fp);
}
else
{
fread(&stunum,len,1,fp); next();
}
while(k)
{
printf("--------------------------------------------------------------\n");
printf("Please select the operation!\n");
printf("(s)earch(詳細查詢)/(c)onsearch(簡明查詢)/(i)nput/(d)elet/(r)ewrite/(n)ext/(q)uit:\n ");
scanf("%c",&ch); getchar(); /* 輸入要執行的命令 */
switch(ch) /* 多分支選擇語句 */
{
case 'S' :
case 's' : search(); break; /* 執行搜索學生信息命令 */
case 'C' :
case 'c' : consearch(); break; /* 執行簡明搜索學生信息命令 */
case 'I' :
case 'i' : input(); break; /* 執行添加學生信息命令 */
case 'D' :
case 'd' : del(); break; /*執行刪除學生信息命令 */
case 'R' :
case 'r' : rew(); break; /* 執行修改重寫學生信息命令 */
case 'N' :
case 'n' : next(); break; /* 執行瀏覽下一個學生信息命令 */
case 'Q' :
case 'q' : k=0; break; /* 退出 */
default : printf("Unknown or bad command!\n"); /* 錯誤命令 */
}
}
rewind(fp);
fwrite(&stunum,len,1,fp);
fclose(fp);
free(p);
return 1;
}
void getinf(void) /* 定義getinf函數,實現讀入學生信息 */
{
printf("full name: ");
gets(p->name); /* 讀入學生姓名 */
printf("school number: ");
gets(p->num); /* 讀入學生學號 */
printf("sex ---female/male: ");
gets(p->sex); /* 讀入學生性別 */
printf("age: ");
gets(p->age); /* 讀入學生年齡 */
printf("birthday: ");
gets(p->birth); /* 讀入學生出生年月 */
printf("time of attending the school: ");
gets(p->schtim); /* 讀入學生入學時間 */
printf("home address: ");
gets(p->addr); /* 讀入學生住址 */
printf("telephone number ---if no, input 'no': ");
gets(p->tel); /* 讀入學生電話 */
}
void output(void) /* 定義output函數,實現輸出學生信息,各語句的功能不再贅述 */
{
printf("--------------------------------------------------------------\n");
printf("name: ");
puts(p->name);
printf("school number: ");
puts(p->num);
printf("sex: ");
puts(p->sex);
printf("age: ");
puts(p->age);
printf("birthday: ");
puts(p->birth);
printf("time of attending the school: ");
puts(p->schtim);
printf("address: ");
puts(p->addr);
printf("telephone: ");
puts(p->tel);
}
void next(void) /* 定義next函數,實現翻頁瀏覽功能 */
{
if(stupos<stunum)
{
fread(p,length,1,fp); output(); stupos++; /* 輸出下一個學生信息 */
}
else printf("Not found information!\n"); /* 若“下一個學生不存在”,則無輸出結果 */
}
void input(void) /* 定義input函數,實現輸入學生信息 */
{
long k=(long)(stunum*length+len);
getinf();
fseek(fp,k,0);
fwrite(p,length,1,fp);
stunum++; stupos=stunum;
}
void rew(void) /* 定義rew函數,實現修改重寫學生信息 */
{
long k=(long)((stupos-1)*length+len);
printf("Please input the new information:\n");
getinf();
fseek(fp,k,0);
fwrite(p,length,1,fp);
printf("Rewrite OK!\n");
}
void del(void) /* 定義del函數,實現刪除學生信息 */
{
char ch; long k;
printf("Warning! This student's information will be deleted!\n");
printf("Are you sure? ---y/n: ");
scanf("%c",&ch); getchar(); /* 輸入要執行的命令 */
if(ch=='Y'||ch=='y') /* 以下是執行刪除學生信息的命令 */
{
k=(long)((stunum-1)*length+len);
fseek(fp,k,0);
fread(p,length,1,fp);
k=(long)((stupos-1)*length+len);
fseek(fp,k,0);
fwrite(p,length,1,fp);
fseek(fp,k,0);
stunum--; stupos--;
printf("This student's information has been deleted!\n");
}
}
void search(void) /* 定義search函數,實現詳細查詢功能 */
{
int m,n=0; long k; char ch; char mode[24];
printf("--------------------------------------------------------------\n");
printf("Please input the search way!\n");
printf("(n)ame/num(b)er/(a)ge/sch(t)ime: ");
scanf("%c",&ch); getchar(); /* 輸入要執行的命令 */
printf("Please input the information of the student:\n");
gets(mode);
k=(long)len;
fseek(fp,k,0);
for(stupos=0;stupos<stunum;stupos++)
{
fread(p,length,1,fp);
switch(ch) /* 多分支選擇語句,根據選擇結果的不同,執行不同的語句 */
{
case 'N' :
case 'n' : if(!strcmp(p->name,mode)) {output(); n++; m=stupos;} break; /* 按姓名查找 */
case 'B' :
case 'b' : if(!strcmp(p->num,mode)) {output(); n++; m=stupos;} break; /* 按學號查找 */
case 'A' :
case 'a' : if(!strcmp(p->age,mode)) {output(); n++; m=stupos;} break; /* 按年齡查找 */
case 'T' :
case 't' : if(!strcmp(p->schtim,mode)) {output(); n++; m=stupos;} break; /* 按入學時間查找 */
default : printf("Unknown or bad order!\n");
}
}
if(!n) printf("Not found!\n"); /* 找不到符合要求的信息 */
else
{
printf("Have found %d student!\n",n); /* 找到符合要求的信息 */
stupos=m+1;
k=(long)(stupos*length+len);
fseek(fp,k,0);
}
}
void consearch(void) /* 定義search函數,實現簡明查詢功能 */
{
int m,n=0; long k; char ch; char mode[24];
printf("--------------------------------------------------------------\n");
printf("Please input the consearch way!\n");
printf("(n)ame/num(b)er/(a)ge/sch(t)ime: ");
scanf("%c",&ch); getchar(); /* 輸入要執行的命令 */
printf("Please input the information of the student\n");
gets(mode);
k=(long)len;
fseek(fp,k,0);
printf("--------------------------------------------------------------\n");
for(stupos=0;stupos<stunum;stupos++)
{
fread(p,length,1,fp);
switch(ch) /* 多分支選擇語句,根據選擇結果的不同,執行不同的語句 */
{
case 'N' : /* 按姓名查找 */
case 'n' : if(!strcmp(p->name,mode))
{
printf("name: ");puts(p->name);
printf("age: ");puts(p->age);
printf("--------------------------------------------------------------\n");
n++;
m=stupos;} break;
case 'B' : /* 按學號查找 */
case 'b' : if(!strcmp(p->num,mode))
{
printf("name: ");puts(p->name);
printf("age: ");puts(p->age);
printf("--------------------------------------------------------------\n");
n++;
m=stupos;} break;
case 'A' : /* 按年齡查找 */
case 'a' : if(!strcmp(p->age,mode))
{
printf("name: ");puts(p->name);
printf("age: ");puts(p->age);
printf("--------------------------------------------------------------\n");
n++;
m=stupos;} break;
case 'T' : /* 按入學時間查找 */
case 't' : if(!strcmp(p->schtim,mode))
{
printf("name: ");puts(p->name);
printf("age: ");puts(p->age);
printf("--------------------------------------------------------------\n");
n++;
m=stupos;} break;
default : printf("Unknown or bad order!\n");
}
}
if(!n) printf("Not found!\n"); /* 找不到符合要求的信息 */
else
{
printf("Have found %d student(s)!\n",n); /* 找到符合要求的信息 */
stupos=m+1;
k=(long)(stupos*length+len);
fseek(fp,k,0);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -