/* Name: mis_sims.c Author: x-bit Description: 简单的学生信息管理系统 Date: 07-11-2004 */
#include <stdio.h> #include <string.h> #include <conio.h> #include <process.h> #define MAX 500 /*定义存储容量*/
typedef struct { int month, day, year; /*出生年月*/ }BT; typedef struct { int number; /*学号*/ char name[13]; /*姓名*/ char sex[7]; /*性别:male or female*/ BT birth; /*出生年月MM-DD-YY*/ char addr[35]; /*家庭住址*/ }ST;
ST student[MAX]; /*定义student[MAX]为全局变量*/
/****************函数声明**********************/ void menu(); /*显示菜单*/ void menu_done(); /*菜单响应*/ char get_menu_choice(); /*获取菜单选择信息*/ FILE *file_operate(char *mode); /*文件操作模块*/ void inf_add(FILE *fp); /*添加学生信息*/ void inf_list(FILE *fp); /*显示学生信息*/ void inf_search(FILE *fp); /*查找学生信息*/ void inf_change(FILE *fp); /*修改学生信息*/ void inf_del(FILE *fp); /*删除学生信息*/ void file_backup(); /*文件备份*/ void psw_check(); /*密码验证机制*/ void set_psw(); /*设置密码*/
/*-------------------------The main function-----------------------*/ int main() { system("cls"); psw_check(); menu_done(); return 0; } /*-------------------------The main function-----------------------*/
/*******************************************************************/ /* 菜单 */ /*******************************************************************/ void menu() { printf("\t \\\\\\|/// \n"); printf("\t \\\\~~ ~~// \n"); printf("\t ( @ @ ) \n"); printf("\t+-------oOOo---------(_)-----------oOOo-----+\n"); printf("\t| MIS-----SMIS |\n"); printf("\t| Student Management Information System |\n"); printf("\t|-------------------------------------------|\n"); printf("\t| 1. Input data | 5. Delete data |\n"); printf("\t| 2. Display data | 6. Backup data |\n"); printf("\t| 3. Search data | 7. Set password |\n"); printf("\t| 4. Change data | 0. Exit |\n"); printf("\t|-------------------------------------------|\n"); printf("\t| x-bit (C) 2004. |\n"); printf("\t+-------------------------------------------+\n"); }
/*******************************************************************/ /* 菜单响应模块 */ /*******************************************************************/ void menu_done() { while(1) { menu(); switch(get_menu_choice()) { case '1': inf_add(file_operate("a")); system("pause"); system("cls"); break; case '2': inf_list(file_operate("rb")); system("pause"); system("cls"); break; case '3': inf_search(file_operate("rb")); system("pause"); system("cls"); break; case '4': inf_change(file_operate("rb")); system("pause"); system("cls"); break; case '5': inf_del(file_operate("r")); system("pause"); system("cls"); break; case '6': file_backup(); system("pause"); system("cls"); break; case '7': set_psw(); system("pause"); system("cls"); break; case '0': printf("Thank you for use this program.\n"); system("pause"); system("cls"); exit(1); /*程序正常退出*/ } } }
/*******************************************************************/ /* 接收菜单选择 */ /*******************************************************************/ char get_menu_choice() { char menu_choice; do { fflush(stdin); printf("Please choice: "); scanf("%c", &menu_choice); if(menu_choice<'0' || menu_choice>'7') puts("input error, try again."); }while(menu_choice<'0' || menu_choice>'7'); return menu_choice; }
/*******************************************************************/ /* 文件操作 */ /*******************************************************************/ FILE *file_operate(char *mode) { char choise; FILE *fp; do { fflush(stdin); if((fp=fopen("student.dat", mode))==NULL) /*打开文件*/ { puts("File operation failure"); puts("Try Again(y/n)?"); scanf("%c", &choise); } }while(choise=='y' || choise=='Y'); if(choise=='n' || choise=='N') exit(0); /*出现异常而退出*/ return fp; }
/*******************************************************************/ /* 输入资料 */ /*******************************************************************/ void inf_add(FILE *fp) { int i=0; /*初始化索引值变量*/ char choice='y';
do { printf("No.(20040001): "); scanf("%d", &student[i].number); printf("Name(less than 12 characters): "); scanf("%s", student[i].name); printf("Sex(male or female): "); scanf("%s", student[i].sex); printf("Birthday:\n"); printf(" year: "); scanf("%d", &student[i].birth.year); printf(" month: "); scanf("%d", &student[i].birth.month); printf(" day: "); scanf("%d", &student[i].birth.day); printf("Address: "); scanf("%s", student[i].addr);
if(fwrite(&student[i], sizeof(ST), 1, fp)!=1) puts("data write error."); i++; fflush(stdin); printf("Continue(y/n)?"); scanf("%c",&choice); }while((choice=='y' || choice=='Y')); fclose(fp); }
/*******************************************************************/ /* 查找资料 */ /*******************************************************************/ void inf_search(FILE *fp) { int i, m; char search_name[20]; /*查找姓名变量*/ char choice='y'; for(i=0; feof(fp)==0; i++) /*读取文件*/ { if(fread(&student[i], sizeof(ST), 1, fp)!=1 && feof(fp)==0) puts("error"); } m=i-1; /*m被赋值为数组非空数据最大索引值*/ do { fflush(stdin); puts("Enter the name for searching: "); gets(search_name); for(i=0; i<m; i++) /*遍历数组*/ { if(strcmp(search_name, student[i].name)==0) /*判断是否有要查找的学生姓名,有则显示数据*/ { printf("No.: %d Name: %s Sex: %s Birth: %d-%d-%d
Address: %s\n", student[i].number, student[i].name, student
[i].sex, student[i].birth.month, student[i].birth.day, student[i].birth.year, student[i].addr); } } if(feof(fp)!= 0) { puts("searching done."); } puts("continue(y/n)?"); scanf("%c",&choice); }while(choice=='y' || choice=='Y'); fclose(fp); } /*******************************************************************/ /* 显示资料 */ /*******************************************************************/ void inf_list(FILE *fp) { int i, m; for(i=0; feof(fp)==0; i++) { if((fread(&student[i], sizeof(ST), 1, fp))!=1 && feof(fp)==0) { puts("error"); } } m=i-1; printf("There %d Record:\n", m); printf("Number Name Sex Birth Address\n"); for(i=0; i<m; i++) { printf("%-8d %-12s %-6s %-2d-%-2d-%-4d %-35s\n", student[i].number, student[i].name, student[i].sex, student[i].birth.month, student[i].birth.day, student
[i].birth.year, student[i].addr); } if(feof(fp)!=0) { puts("Display Informtion Done."); } fclose(fp); }
/*******************************************************************/ /* 修改资料 */ /*******************************************************************/ void inf_change(FILE *fp) { int number_temp; char choice; int i, m, j=0; FILE *fp_update;
for(i=0; feof(fp)==0; i++) /*读取文件*/ if(fread(&student[i], sizeof(ST), 1, fp)!=1 && feof
(fp)==0) puts("error"); m=i-1; do { fflush(stdin); printf("Enter number for change: "); scanf("%d", &number_temp); for(i=0; i<m; i++) if(number_temp==student[i].number) { j=i; printf("No.: %d Name: %s Sex: %s Birth: %d-%d-%d
Address: %s\n", student[i].number, student[i].name, student
[i].sex, student[i].birth.month, student[i].birth.day, student[i].birth.year, student[i].addr); } if(j==0) { puts("No this record."); break; } fflush(stdin); printf("change data %d (y/n)?", number_temp); /*修改确认*/ scanf("%c",&choice); if (choice=='n' || choice=='N') /*修改资料*/ continue; printf("No.: "); scanf("%d", &student[j].number); printf("Name: "); scanf("%s", student[j].name); printf("Sex: "); scanf("%s", student[j].sex); printf("Birthday:\n"); printf(" Year: "); scanf("%d", &student[j].birth.year); printf(" month: "); scanf("%d", &student[j].birth.month); printf(" day: "); scanf("%d", &student[j].birth.day); printf("Address: "); scanf("%s", student[j].addr);
fflush(stdin); printf("continue(y/n)?"); scanf("%c",&choice); }while ( choice == 'y' || choice == 'Y' ); fp_update=file_operate("w"); for(i=0; i<m; i++) if(fwrite(&student[i], sizeof(ST), 1, fp_update)!=1) puts("update error."); puts("change done."); fclose(fp_update); } /*******************************************************************/ /* 删除资料 */ /*******************************************************************/ void inf_del(FILE *fp) { int i, m, t=-1; int number_temp; char choice='y';
for(i=0; feof(fp)==0; i++) { if(fread(&student[i], sizeof(ST), 1, fp)!=1 && feof(fp)==0) puts("error"); } m=i-2; do { fflush(stdin); printf("Enter the number for delete: "); scanf("%d", &number_temp); for(i=0; i<m+1; i++) { if(student[i].number==number_temp) { /*删除前显示学生资料*/ printf("No.: %d Name: %s Sex: %s Birth: %d-%d-%d
Address: %s\n", student[i].number, student[i].name, student
[i].sex, student[i].birth.month, student[i].birth.day, student[i].birth.year, student[i].addr);
fflush(stdin); printf("delete %d (y/n)?", number_temp); /*删除确认*/ scanf("%c",&choice); if (choice=='n' || choice=='N') break; for(t=i; t<m; t++) student[t]=student[t+1]; /*删除数据*/ puts("delete data done."); m--; } } if(t==-1 && choice!='n' && choice!='N') printf("No %d's data!\n",number_temp); fflush(stdin); printf("continue(y/n)?"); scanf("%c",&choice); }while ( choice == 'y' || choice == 'Y' ); fclose(fp); fp=file_operate("w"); for(i=0; i<m+1; i++) /*存盘*/ { if(fwrite(&student[i], sizeof(ST), 1, fp)!=1) puts("error"); } fclose(fp); }
/*******************************************************************/ /* 文件备份 */ /*******************************************************************/ void file_backup() { FILE *fp, *fp_bak; char ch; char filename[12]; /*存放备份文件文件名,文件名要求用传统的8.3式,如filename.bak*/
printf("Enter the backup file name: "); scanf("%s", filename); fp=file_operate("rb"); fp_bak=fp; if((fp_bak=fopen(filename, "wb"))==NULL) { puts("backup file error."); exit(0); } while(!feof(fp)) { ch=fgetc(fp); fputc(ch, fp_bak); } fclose(fp); fclose(fp_bak); puts("file backup success."); }
/*******************************************************************/ /* 密码验证 */ /*******************************************************************/ void psw_check() { char psw[8], psw_temp[8]; int i, leap; FILE *fp;
if((fp=fopen("sn.dat", "rb"))==NULL) /*读取密码,如不成功则要
求设置密码*/ { puts("You must set password first."); menu_done(); } if((fread(psw, sizeof(psw), 1, fp))!=1) puts("error."); for(i=0; i<8; i++) /*解密*/ psw[i]=~psw[i]; do { leap=0; printf("Enter password: "); for(i=0; i<8; i++) { psw_temp[i]=getch(); printf("*"); if(psw_temp[i]==psw[i]) ++leap; } if(leap!=8) { puts("\nInvalid password!"); } else printf("\n"); fclose(fp); }while(leap!=8); }
/*******************************************************************/ /* 设置密码 */ /*******************************************************************/ void set_psw() { char psw[8], psw_temp[8]; int i=0, leap=0; FILE *fp;
do { printf("Enter password: "); /*输入密码*/ for(i=0; i<8; i++) { fflush(stdin); psw[i]=getch(); printf("*"); } printf("\nConfirm password: "); /*再输入一次密码*/ for(i=0; i<8; i++) { fflush(stdin); psw_temp[i]=getch(); printf("*"); } for(i=0; i<8; i++) /*确认密码*/ if(psw[i]==psw_temp[i]) leap++; if(leap==8) { printf("\nSet password success.\n"); for(i=0; i<8; i++) psw[i]=~psw[i]; /*密码加密*/ if((fp=fopen("sn.dat", "wb"))==NULL) /*密码存
盘*/ puts("error."); if((fwrite(psw, sizeof(psw), 1, fp))!=1) puts("save password error."); fclose(fp); } else printf("\n"); }while(leap!=8); } 
|