/*大作业SIMS*/
//头文件
#ifndef __FUNC_C__
#define __FUNC_C__ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 128
#define M 100 //账户最多人数
#define N 1000 //学生最多人数
#define USR_NAME_LEN 20
#define USR_PWD_LEN 20 typedef int USR_ROLE; //账号信息
typedef struct usr_account_tag
{
char usr_name[USR_NAME_LEN];
char usr_pwd[USR_PWD_LEN];
USR_ROLE usr_role; //管理员设置为1,其它设置为0
struct usr_account_tag *pNext_Usr_Account;
}USR_ACCOUNT, *pUSR_ACCOUNT; //学生信息
typedef struct usr_infor_tag
{
int usr_id; //学号
char usr_name[USR_NAME_LEN]; //姓名
int usr_course_id; //课程号
double usr_course_score; //课程分数
struct usr_infor_tag *pNext_Usr_Infor; //下一个学生信息 }USR_INFOR, *pUSR_INFOR; USR_ROLE system_init(pUSR_ACCOUNT *account_head, pUSR_INFOR *usr_infor_head, char *addres); USR_ROLE system_menu(USR_ROLE type);
USR_ROLE search_menu(USR_ROLE type);
void usr_infor_delete_menu(void);
void account_search_menu(void);
void show_usr_info(pUSR_INFOR pphead);
void show_usr_account(pUSR_ACCOUNT pphead); void search_usr_info_id(pUSR_INFOR *pphead, int id);
void search_usr_info_name(pUSR_INFOR *pphead, char *name); void save_usr_info(pUSR_INFOR *pphead, char *addres);
void add_usr_info(pUSR_INFOR *pphead, char *addres);
void delet_usr_info_by_id(pUSR_INFOR *pphead, int id);
void delet_usr_info_by_name(pUSR_INFOR *pphead, char name);
void delet_usr_account_by_name(pUSR_ACCOUNT *pphead, char* name);
void updat_usr_infor_by_id(pUSR_INFOR *pphead, int id);
void update_usr_account_by_name(pUSR_ACCOUNT *pphead, char* name);
void add_usr_account(pUSR_ACCOUNT *pphead, char *addres);
void save_usr_account(pUSR_ACCOUNT *pphead, char *addres);
void search_usr_account_by_name(pUSR_ACCOUNT *pphead, char* name); #endif
//函数部分,没有实现返回功能,因为考虑不周没有,函数没有返回值,每次打开只能进行一次操作就退出,
#include "sims.h" USR_ROLE system_menu(USR_ROLE type)
{
int number;
int search_number;
if (type == ){
system("cls");
printf("\n*******************************************************************************\n");
printf("***************Student Information Management System*****************\n");
printf("*******************************************************************************\n");
printf("\n\n\t\t1. search student information");
printf("\n\t\t2. add student information");
printf("\n\t\t3. update student information");
printf("\n\t\t4. delete student information");
printf("\n\t\t5. add user account");
printf("\n\t\t6. update user account");
printf("\n\t\t7. delete user account");
printf("\n\t\t8. search user account");
printf("\n\t\t9. exit");
printf("\n\n\nselect a number between 1--9:");
return type;
}
else if (type == )
{
system("cls");
printf("\n*******************************************************************************\n");
printf("***************Student Information Management System****************\n");
printf("*******************************************************************************\n");
printf("\n\n\t\t1. search student information");
/*
printf("\n\t\t2. add student information");
printf("\n\t\t3. update student information");
printf("\n\t\t4. delete student information");
printf("\n\t\t5. add user account");
printf("\n\t\t6. update user account");
printf("\n\t\t7. delete user account");
printf("\n\t\t8. search user account");
*/
printf("\n\t\t9. exit");
printf("\n\n\nselect a number:");
return type;
}
else
{
return;
}
} USR_ROLE search_menu(USR_ROLE type)
{
system("cls");
if (type == ){
printf("\n0. search all");
printf("\n1. search by id");
printf("\n2. search by name");
printf("\n3. return");
printf("\nselect a number:");
}
else
{
printf("\n1. search by id");
printf("\n2. search by name");
printf("\n3. return");
printf("\nselect a number:"); }
return type;
} void account_search_menu(void)
{
system("cls");
printf("\n0. search all account");
printf("\n1. search account by name");
printf("\n2. exit");
printf("\nselect a number:"); } void usr_infor_delete_menu(void)
{
system("cls");
printf("\n1. delete by id");
printf("\n2. delete by name");
printf("\n3. return");
printf("\nselect a number:");
fflush(stdin);
} void show_usr_info(pUSR_INFOR pphead)
{
system("cls");
while (pphead != NULL){
printf("%-5d%-10s%-3d%7.2f\n", pphead->usr_id, pphead->usr_name, pphead->usr_course_id, pphead->usr_course_score);
pphead = pphead->pNext_Usr_Infor;
}
printf("\n");
} void show_usr_account(pUSR_ACCOUNT pphead)
{
system("cls");
while (pphead != NULL){
fprintf(stdout, "%-15s%-15s%3d\n", pphead->usr_name, pphead->usr_pwd, pphead->usr_role);
pphead = pphead->pNext_Usr_Account;
printf("\n");
}
} void search_usr_info_id(pUSR_INFOR *pphead, int id)
{
pUSR_INFOR pcur;
FILE *fp;
pcur = *pphead;
while (pcur != NULL){
if (pcur->usr_id == id){
break;
}
else
pcur = pcur->pNext_Usr_Infor;
}
if (pcur != NULL){
printf("%-5d%-10s%-3d%7.2f\n", pcur->usr_id, pcur->usr_name, pcur->usr_course_id, pcur->usr_course_score);
}
else
{
printf("cannot find\n");
}
} void search_usr_info_name(pUSR_INFOR *pphead, char *name)
{
pUSR_INFOR pcur;
FILE *fp;
pcur = *pphead;
while (pcur != NULL){
if (strcmp(name,pcur->usr_name)==){
break;
}
else
pcur = pcur->pNext_Usr_Infor;
}
if (pcur != NULL){
printf("%-5d%-10s%-3d%7.2f\n", pcur->usr_id, pcur->usr_name, pcur->usr_course_id, pcur->usr_course_score);
}
else
{
printf("cannot find\n");
}
} void search_usr_account_by_name(pUSR_ACCOUNT *pphead, char* name)
{
pUSR_ACCOUNT pcur;
pcur = *pphead;
while (pcur != NULL){
if (strcmp(pcur->usr_name, name) == )
break;
else
pcur = pcur->pNext_Usr_Account;
}
if (pcur != NULL){
fprintf(stdout, "%-15s%-15s%3d\n", pcur->usr_name, pcur->usr_pwd, pcur->usr_role);
}
else{
fprintf(stdout, "donot exist the name\n");
}
} void add_usr_info(pUSR_INFOR *pphead, char *addres)
{
pUSR_INFOR pnew = NULL;
pUSR_INFOR pcur, ppre;
char line[LEN];
system("cls");
fflush(stdin);
printf("input: id name course_id course_score\n");
while (memset(line, , sizeof(line)), fgets(line, sizeof(line), stdin) != NULL){
pnew = (pUSR_INFOR)calloc(, sizeof(USR_INFOR));
sscanf(line, "%d%s%d%lf", &pnew->usr_id, pnew->usr_name, &pnew->usr_course_id, &pnew->usr_course_score);
pcur = *pphead;
ppre = NULL;
//想增加判断学号是否已经存在,存在就拒接添加。
while (pcur != NULL)
{
if (pcur->usr_id == pnew->usr_id)
break;
else
pcur = pcur->pNext_Usr_Infor;
}
if (pcur == NULL){
pcur = *pphead;
ppre = NULL;
while (pcur != NULL && pcur->usr_id <= pnew->usr_id){ //按照学号升序排列
ppre = pcur;
pcur = pcur->pNext_Usr_Infor;
}
if (ppre == NULL){
pnew->pNext_Usr_Infor = *pphead;
*pphead = pnew;
}
else
{//中间和结尾都是这样,结尾时pcur为空
ppre->pNext_Usr_Infor = pnew;
pnew->pNext_Usr_Infor = pcur;
}
}
else
{
fprintf(stdout, "the %d usr_infor already exist.\n",pcur->usr_id);
}
} } void add_usr_account(pUSR_ACCOUNT *pphead, char *addres)
{
pUSR_ACCOUNT pnew = NULL;
pUSR_ACCOUNT pcur, ppre;
char line[LEN];
system("cls");
fflush(stdin);
printf("input: usr_name\tusr_pwd\tusr_role\n");
while (memset(line, , sizeof(line)), fgets(line, sizeof(line), stdin) != NULL){
pnew = (pUSR_ACCOUNT)calloc(, sizeof(USR_ACCOUNT));
sscanf(line, "%s%s%d", pnew->usr_name, pnew->usr_pwd, &pnew->usr_role);
pnew->pNext_Usr_Account = *pphead;
*pphead = pnew;
}
} void save_usr_account(pUSR_ACCOUNT *pphead, char *addres)
{
pUSR_ACCOUNT pcur;
FILE *fp_config, *fp_usr_account;
char line[LEN];
pcur = *pphead;
if ((fp_config = fopen(addres, "r")) == NULL){
printf("file cannot open fp_config\n");
return;
}
//已知usr_account在第一行,不知道怎么判断?
memset(line, , sizeof(line));
fgets(line, sizeof(line), fp_config);
line[strlen(line)-] = '\0';
if ((fp_usr_account = fopen(line, "w")) == NULL){
printf("file cannot open fp_usr_account\n");
return;
}
fclose(fp_config);
while (pcur != NULL){
if (fprintf(fp_usr_account, "%-10s%-10s%-5d\n", pcur->usr_name,pcur->usr_pwd,pcur->usr_role) == EOF)
printf("write error\n");
pcur = pcur->pNext_Usr_Account;
}
fclose(fp_usr_account);
} void save_usr_info(pUSR_INFOR *pphead, char *addres)
{
pUSR_INFOR pcur;
FILE *fp_config, *fp_usr_infor;
char line[LEN];
pcur = *pphead;
if ((fp_config = fopen(addres, "r")) == NULL){
printf("file cannot open fp_config\n");
return;
}
//已知usr_info在第二行所以采取读两次来调用,没有想到怎么用数组或者链表?
memset(line, , sizeof(line));
//第一行有\n,第二行没有\n
fgets(line, sizeof(line), fp_config);
memset(line, , sizeof(line));
fgets(line, sizeof(line), fp_config);
if ((fp_usr_infor = fopen(line, "w")) == NULL){
printf("file cannot open fp_usr_infor\n");
return;
}
fclose(fp_config);
while (pcur != NULL){
if (fprintf(fp_usr_infor, "%-5d%-10s%-5d%8.2f\n", pcur->usr_id,pcur->usr_name,pcur->usr_course_id,pcur->usr_course_score) == EOF)
printf("write error\n");
pcur = pcur->pNext_Usr_Infor;
}
fclose(fp_usr_infor);
} void delet_usr_info_by_id(pUSR_INFOR *pphead, int id)
{
pUSR_INFOR pcur, ppre;
FILE *fp;
pcur = *pphead;
ppre = NULL;
while (pcur != NULL){
if (pcur->usr_id == id){
break;
}
else{
ppre = pcur;
pcur = pcur->pNext_Usr_Infor;
} }
if (pcur != NULL){
if (ppre == NULL){
//删除第一个结点
*pphead = pcur->pNext_Usr_Infor;
}
else{
ppre->pNext_Usr_Infor = pcur->pNext_Usr_Infor; }
free(pcur);
pcur = NULL;
}
else
{
printf("donot exist in the file.\n");
}
if (*pphead == NULL){
free(*pphead);
*pphead = NULL;
}
} void delet_usr_info_by_name(pUSR_INFOR *pphead, char name)
{
pUSR_INFOR pcur, ppre;
FILE *fp;
pcur = *pphead;
ppre = NULL;
while (pcur != NULL){
if (strcmp(pcur->usr_name,name)==){
break;
}
else{
ppre = pcur;
pcur = pcur->pNext_Usr_Infor;
} }
if (pcur != NULL){
if (ppre == NULL){
//删除第一个结点
*pphead = pcur->pNext_Usr_Infor;
}
else{
ppre->pNext_Usr_Infor = pcur->pNext_Usr_Infor; }
free(pcur);
pcur = NULL;
}
else
{
printf("donot exist in the file.\n");
}
if (*pphead == NULL){
free(*pphead);
*pphead = NULL;
}
} //账户信息不能全部删除,需要加判断???
void delet_usr_account_by_name(pUSR_ACCOUNT *pphead, char* name)
{
pUSR_ACCOUNT pcur, ppre;
char line[LEN];
pcur = *pphead;
ppre = NULL;
while (pcur != NULL && strcmp(pcur->usr_name, name) != ){
ppre = pcur;
pcur = pcur->pNext_Usr_Account;
}
if (pcur != NULL){
ppre->pNext_Usr_Account = pcur->pNext_Usr_Account;
fprintf(stdout, "delete ok.\n");
}
else
{
fprintf(stdout, "delete error, donot exit\n");
} } void updat_usr_infor_by_id(pUSR_INFOR *pphead, int id)
{
pUSR_INFOR pcur;
char line[LEN];
pcur = *pphead;
while (pcur != NULL){
if (pcur->usr_id == id){
break;
}
else
pcur = pcur->pNext_Usr_Infor;
}
if (pcur != NULL){
//只能修改后面三项内容
fflush(stdin);
memset(line, , sizeof(line));
printf("updat NO.%d new infor\nname\tcouse_id\tcours_score:\n ", pcur->usr_id);
fgets(line, sizeof(line), stdin);
sscanf(line, "%s%d%lf", pcur->usr_name, &pcur->usr_course_id, &pcur->usr_course_score); }
else
{
printf("cannot find the id_infor\n");
}
} void update_usr_account_by_name(pUSR_ACCOUNT *pphead, char* name)
{
pUSR_ACCOUNT pcur;
char line[LEN];
pcur = *pphead;
while (pcur != NULL && strcmp(pcur->usr_name, name) != ){
pcur = pcur->pNext_Usr_Account;
}
if (pcur != NULL){
memset(line, , sizeof(line));
fflush(stdin);
fprintf(stdout, "usr_pwd\t\tusr_role:\n");
fgets(line, sizeof(line), stdin);
sscanf(line, "%s%d", pcur->usr_pwd, &pcur->usr_role);
}
else
{
fprintf(stdout, "updata usr_account error\n");
} }
//函数接口设计不好,因此都是主函数调用,程序比较混乱
#include "sims.h" int main(int argc, char *argv[])
{
pUSR_INFOR usr_infor_head = NULL;
pUSR_ACCOUNT account_head = NULL;
char *paddres; //传递配置文件地址
int digit;
int s_digit, d_digit;
int usr_id, delet_id;
char usr_name[LEN] = "";
char usr_account_name[LEN] = "";
USR_ROLE type,s_type; //登陆者身份标示
paddres = (char *)calloc(, sizeof(char));
paddres = argv[];
//加载初始账户密码和学生信息
type = system_init(&account_head, &usr_infor_head, paddres);
s_type = system_menu(type); //以管理员身份登录
if (s_type == ){ fflush(stdin);
scanf("%d", &digit);
while (digit < || digit > ) {
fflush(stdin);
//system("cls");
scanf("%d", &digit);
}
switch (digit){
case :
search_menu(s_type);
scanf("%d", &s_digit);
while (s_digit < || s_digit > ) {
fflush(stdin);
//system("cls");
scanf("%d", &s_digit);
}
switch (s_digit)
{
case :
//show all usr_infor
show_usr_info(usr_infor_head);
break;
case :
//search by id
fflush(stdout);
system("cls");
fflush(stdin);
printf("input usr_id:\n");
scanf("%d", &usr_id);
search_usr_info_id(&usr_infor_head, usr_id);
break;
case :
//search by name
system("cls");
fflush(stdin);
printf("input usr_name:\n");
scanf("%s", usr_name);
search_usr_info_name(&usr_infor_head, usr_name);
break;
case :
return;
//s_type = system_menu(type);
default:
break;
}
break; case :
//add
add_usr_info(&usr_infor_head, argv[]);
save_usr_info(&usr_infor_head, argv[]);
break;
case :
//update by id
fflush(stdout);
system("cls");
fflush(stdin);
printf("input usr_id:\n");
scanf("%d", &usr_id);
updat_usr_infor_by_id(&usr_infor_head, usr_id);
save_usr_info(&usr_infor_head, argv[]);
break;
case :
//delet by id
usr_infor_delete_menu();
scanf("%d", &d_digit);
if (d_digit == ){
system("cls");
fflush(stdin);
printf("input delet_usr_id:\n");
scanf("%d", &delet_id);
delet_usr_info_by_id(&usr_infor_head, delet_id);
save_usr_info(&usr_infor_head, argv[]);
}
else if (d_digit == ){
system("cls");
fflush(stdin);
printf("input delet_usr_name:\n");
scanf("%s",usr_name);
delet_usr_info_by_name(&usr_infor_head, usr_name);
save_usr_info(&usr_infor_head, argv[]);
}
else
{
return;
}
break;
case :
//add user account
add_usr_account(&account_head, argv[]);
//save
save_usr_account(&account_head, argv[]);
break;
case :
//update usr_account
system("cls");
fflush(stdin);
printf("input usr_account_name:\n");
scanf("%s", usr_account_name);
update_usr_account_by_name(&account_head, usr_account_name);
save_usr_account(&account_head, argv[]);
break;
case :
//delet usr_account_by_name
system("cls");
fflush(stdin);
printf("input usr_account_name:\n");
scanf("%s", usr_account_name);
delet_usr_account_by_name(&account_head, usr_account_name);
save_usr_account(&account_head, argv[]);
break;
case :
//search usr_account
account_search_menu();
scanf("%d", &s_digit);
while (s_digit < || s_digit > ) {
fflush(stdin);
//system("cls");
scanf("%d", &s_digit);
} if (s_digit == ){
//show all account
show_usr_account(account_head);
}
else if (s_digit == ){
//search by name
system("cls");
fflush(stdin);
printf("input usr_name:\n");
scanf("%s", usr_account_name);
search_usr_account_by_name(&account_head, usr_account_name);
}
else
exit;
break;
case :
exit;
break;
default:
printf("select a digit between 1--9:");
}
}
else if (s_type == )
//以访客身份登录
{
fflush(stdin);
scanf("%d", &digit);
while (digit < || digit > ) {
fflush(stdin);
//system("cls");
scanf("%d", &digit);
} if (digit == ){
search_menu(s_type);
scanf("%d", &s_digit);
while (s_digit < || s_digit > ) {
fflush(stdin);
scanf("%d", &s_digit);
}
}
if (s_digit == ){
//search by id
fflush(stdout);
system("cls");
fflush(stdin);
printf("input usr_id:\n");
scanf("%d", &usr_id);
search_usr_info_id(&usr_infor_head, usr_id);
}
else if (s_digit == ){
//search by name
system("cls");
fflush(stdin);
printf("input usr_name:\n");
scanf("%s", usr_name);
search_usr_info_name(&usr_infor_head, usr_name);
}
else //(s_digit == 3) 退出
{
exit;
}
}
else
exit; system("pause");
return ;
}
//算是我自己写的比较长的代码mark//