说明:
1 代码没有经过编译,只是从可读性、可维护性角度做概念上的提升
2 没有特别修改代码中的错误(只在实在忍不住了才修改)
3 暂时没有做软件设计方面的修改的计划
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void add(void);//新片上架函数
int check(void);//碟片查询函数
int rent(void);//碟片借阅函数
void useradd(void);//会员添加函数
void returnn (void);//碟片归还函数
int user_number=1000;//定义会员数目
int cd_number=1000;//定义碟片数目
struct cd//碟片结构体
{
char film_name[10];//碟片名称
char country[10]; //碟片国家
char type[10];//碟片类型
int n; /*标记是否被租借,已经租借:0;未被租借:1;没有此碟片:2*/
};
struct rent//碟片借阅结构体
{
char username[10];//使用者名
char film_name[10][10]; //碟片名称
int rent_timey;//借阅年份
int rent_timem;//借阅月份
int rent_timed;//借阅日份
int return_timey;//归还年份
int return_timem;//归还月份
int return_timed;//归还日份
int money;//租金
};
struct cd CD[1000];//定义cd数目
struct rent RENT[1000];//定义出租数目
void main (void) //主函数
{
char choice;//菜单
a: printf(" ---------欢迎进入影碟出租管理系统------- \n");
printf(" A: 新片上架 \n");
printf(" B: 碟片查询 \n");
printf(" C: 碟片借阅(非会员不能借阅) \n");
printf(" D: 碟片归还 \n");
printf(" E: 添加会员 \n");
printf(" F: 退出系统 \n");
printf(" -----请输入你的选择(大写字母哦)----- \n");
rewind(stdin);
scanf("%c",&choice);
switch(choice)//多条件选择判断
{
case 'A':
{add(); /*碟片添加函数*/break;}
case 'B':
{check();/*碟片查询函数*/break;}
case 'C':
{rent();/*碟片借阅函数*/break;}
case 'D':
{returnn();/*碟片归还函数*/break;}
case'E':
{useradd();/*添加会员函数*/break;}
case'F':
{exit(0);/*退出程序*/}
}
goto a;//返回菜单
}
void add(void)//新片上架
{
FILE *a;//文件指针
printf("请输入碟片的名称\n");
scanf("%s",CD[cd_number].film_name);
printf("请输入碟片国家\n");
scanf("%s",CD[cd_number].country);
printf("请输入碟片类型(如动作片,科幻片,喜剧片等)\n");
scanf("%s",CD[cd_number].type);
CD[cd_number].n=1;//修改借阅标记
if(0==cd_number)
{
a=fopen("cd.txt","wb");
fwrite(&CD[cd_number], sizeof(struct cd), 1, a);
}
else
{
a=fopen("cd.txt","ab");
fwrite(&CD[cd_number], sizeof(struct cd), 1, a);
}
cd_number++;
printf("影片添加成功\n");
fclose(a);
}
int check(void)//碟片查询
{
int i;
FILE *a;//文件指针
char cd_name[10];
a = fopen("cd.txt","rb");
for(i = 0; i < cd_number; i++)
{
fread(&CD[i], sizeof(struct cd), 1, a);//把a中一个cd结构体赋给CD[i]
}
printf("请输入碟片名称进行查询\n");
scanf("%s",cd_name);
for(i = 0; i < cd_number; i++)
{
if(!(strcmp(CD[i].film_name, cd_name)))//若影片名相同
{
if(1 == CD[i].n)//借阅标记为1
{
printf("碟片未被租借\n");
fclose(a);
return 1;
}
else
{
printf("碟片已租借\n");
fclose(a);
return 1;
}
}
}
fclose(a);//关闭a文件
return 0;//返回菜单
}
void useradd(void)//添加会员
{
FILE *a;//文件指针
printf("请建立新的用户名\n");
scanf("%s",RENT[user_number].username);
if (0 == user_number)
{
a = fopen("rent.txt","wb");//追加方式打开
fwrite(&RENT[user_number], sizeof(struct rent), 1, a);
}
else
{
a = fopen("rent.txt","ab");
fwrite(&RENT[user_number], sizeof(struct rent), 1, a);//把a中一个rent结构体赋给RENT[user_number]
printf("恭喜你,添加会员成功\n");
}
user_number++;
fclose(a);
}
int rent(void)//碟片借阅
{
char user_name[10];
char cd_name[10];
FILE *a;//文件指针
FILE *b;//文件指针
int i = 0;
int j;
int k = 0;
char choice;
a = fopen("rent.txt","r");//只读方式打开
b = fopen("cd.txt","r");//只读方式打开
for (i = 0; i < cd_number; i++)
{
fread(&CD[i], sizeof(struct cd), 1, b);//把b中一个cd结构体赋给CD[i]
}
for (i = 0; i < user_number; i++)
{
fread(&RENT[i], sizeof(struct rent), 1, a);//把a中一个rent结构体赋给RENT[i]
}
printf("请输入用户名\n");
scanf("%s",user_name);
for (i = 0; i < user_number; i++)
{
if (!(strcmp(user_name, RENT[i].username)))
{
a:printf("请输入碟片名称\n");
scanf("%s",cd_name);
printf("请再次输入碟片名称进行确认\n");
scanf("%s", RENT[i].film_name[k]);
k++;
for (j=0;j<cd_number;j++)
{
if(!(strcmp(CD[j].film_name, cd_name)))
{CD[j].n = 0;break;}
}
}
}
printf("请输入租借日期(格式:中间用空格隔开。例:2012 04 10)\n");
scanf("%d %d %d",&RENT[i].rent_timey,&RENT[i].rent_timem,&RENT[i].rent_timed);
getchar();
printf("A:继续租借\n");
printf("B: 退出\n");
scanf("%c",&choice);
switch(choice)//多条件选择判断
{
case 'A': goto a;
break;
case 'B': return 0;
}
fclose(a);
fclose(b);
}
void returnn (void)//碟片归还
{
char user_name[10];
char cd_name[10];
FILE *a;
FILE *b;
int i;
int j;
int k = 0;
char choice;
a = fopen("rent.txt","r");//只读方式打开
b = fopen("cd.txt","r");//只读方式打开
for (i = 0; i < cd_number; i++)
{
fread(&CD[i], sizeof(struct cd), 1, b);//把b中一个cd结构体赋给CD[i]
}
for (i = 0; i < user_number; i++)
{
fread(&RENT[i], sizeof(struct rent), 1, a);//把a中一个rent结构体赋给RENT[i]
}
printf("请输入用户名\n");
scanf("%s",user_name);
for (i = 0; i < user_number; i++)
{
if (!(strcmp(user_name, RENT[i].username)))
{
a:printf("请输入碟片名称进行归还 \n");
scanf("%s",cd_name);
for (j = 0; j < cd_number; j++)
{
if(!(strcmp(CD[j].film_name, cd_name)))
{CD[j].n = 1;break;}
}
}
}
printf("请输入归还日期(格式:中间用空格隔开。例:2012 05 10)\n");
scanf("%d %d %d",&RENT[i].return_timey,&RENT[i].return_timem,&RENT[i].return_timed);
if (0 == ((RENT[i].return_timey*360+RENT[i].return_timem*30+RENT[i].return_timed-RENT[i].rent_timey*360-RENT[i].rent_timem*30-RENT[i].rent_timed) % 3))
{
RENT[i].money = (RENT[i].return_timey*360+RENT[i].return_timem*30+RENT[i].return_timed-RENT[i].rent_timey*360-RENT[i].rent_timem*30-RENT[i].rent_timed) / 3;
}
else
{
RENT[i].money = (RENT[i].return_timey*360+RENT[i].return_timem*30+RENT[i].return_timed-RENT[i].rent_timey*360-RENT[i].rent_timem*30-RENT[i].rent_timed) / 3 + 1;
}
printf("谢谢你的观赏。本片你应支付的金额为%d元\n",RENT[i].money);
getchar();
printf("A: 继续\n");
printf("B: 退出\n");
scanf("%c",&choice);
switch(choice)
{
case 'A': goto a;
break;
case 'B': return 0;
}
fclose(a);
fclose(b);
}
#include <stdio.h> #include <stdlib.h> #include <string.h> #define CD_MAX 1000 #define RENT_MAX 1000 #define NAME_LEN_MAX 10 #define RENTS_ALLOWED_MAX 10 typedef enum{ RENTED, UNRENTED, UNAVAILABLE, }CdStatus; typedef struct _cd { char name[NAME_LEN_MAX]; char country[NAME_LEN_MAX]; char type[NAME_LEN_MAX]; CdStatus status; }CdInfo; typedef struct _time { int year; int month; int day; }Time; typedef struct _rent { char UserName[NAME_LEN_MAX]; char FilmName[RENTS_ALLOWED_MAX][NAME_LEN_MAX]; Time RentDate; Time ReturnDate; int Fee; }RentInfo; struct CdInfo Cds[CD_MAX]; struct RentInfo Rents[RENT_MAX]; int user_idx = 1000; int cd_idx = 1000; void add(void); int check(void); void useradd(void); int rent(void); void returnn (void); void ShowMainMenu() { printf(" ---------WELCOME TO CD RENT MANAGEMENT SYSTEM------- \n"); printf(" A: ADD NEW CD \n"); printf(" B: QUERY CD \n"); printf(" C: RENT CD (ONLY FOR MEMBERS) \n"); printf(" D: RETURN CD \n"); printf(" E: ADD NEW MEMBER \n"); printf(" F: EXIT \n"); printf(" -----PLEASE INPUT YOUR OPTION(UPPER CASE ONLY)----- \n"); } int GetOption() { int opt = 0; rewind(stdin); scanf("%c",&opt); return opt; } void Handle(int option) { switch(option) { case 'A': add(); break; case 'B': check(); break; case 'C': rent(); break; case 'D': returnn(); break; case'E': useradd(); break; case'F': break;; } } void main (void) { char option = 0; while( option!='F' ) { ShowMainMenu(); option = GetOption(); Handle(option); } exit(0); } void UpdateNewCdInfo() { printf("please input cd name : \n"); scanf("%s",Cds[cd_idx].film_name); printf("please input cd country : \n"); scanf("%s",Cds[cd_idx].country); printf("please input cd type(such as act/fiction/comedy etc) : \n"); scanf("%s",Cds[cd_idx].type); Cds[cd_idx].status=UNRENTED; } #define FILE_CD "cd.txt" #define FILE_USER "rent.txt" #define FILE_CREATE "wb" #define FILE_APPEND "ab" #define FILE_READ "rb" #define FILE_READ2 "r" void StoreCdInfo() { FILE *a = NULL; char flag = (cd_idx==0)?FILE_CREATE:FILE_APPEND; a = fopen(FILE_NAME,flag); fwrite(&Cds[cd_idx], sizeof(CdInfo), 1, a); fclose(a); } void add(void) { UpdateNewCdInfo(); StoreCdInfo(); cd_idx++; printf("cd info added successfully\n"); } void LoadCdInfos() { FILE *a = NULL; int i = 0; a = fopen(FILE_NAME,FILE_READ); for(i = 0; i < cd_idx; i++) { fread(&Cds[i], sizeof(CdInfo), 1, a); } fclose(a); } #define OK 0 #define NOT_FOUNTD 1 int CheckCd(char *name,CdStatus *status) { int i = 0; for(i=0; i<cd_idx; i++) { if( !( strcmp(Cds[i].name, name) ) ) { *status = Cds[i].status; return OK; } } return NOT_FOUNTD; } void ShowCdStatus(CdStatus status) { if( status==RENTED ) { printf("the cd is rented or unavailable\n"); } else { printf("the cd is available\n"); } } int check(void) { char name[NAME_LEN_MAX] = {0}; int result = NOT_FOUNTD; CdStatus status = UNAVAILABLE; printf("please input the cd you wanted : \n"); scanf("%s",name); LoadCdInfos(); result = CheckCd(name,&status); if( result==OK ) { ShowCdStatus(status); return 1; } else { return 0; } } void UpdateNewUserInfo() { printf("please input new user name : \n"); scanf("%s",Rents[user_idx].username); } void StoreUserInfo() { FILE *a = NULL; int flag = (0==user_idx)?FILE_CREATE:FILE_APPEND; a = fopen(FILE_USER,flag); fwrite(&Rents[user_idx], sizeof(RentInfo), 1, a); if ( flag==FILE_APPEND ) { printf("congratualations, new user added successfully\n"); } fclose(a); } void useradd(void) { UpdateNewUserInfo(); StoreUserInfo(); user_idx++; } void LoadCdInfos2() { FILE *a = NULL; int i = 0; a = fopen(FILE_CD,FILE_READ2); for(i = 0; i < cd_idx; i++) { fread(&Cds[i], sizeof(CdInfo), 1, a); } fclose(a); } void LoadUserInfos() { FILE *a = NULL; int i = 0; a = fopen(FILE_USER,FILE_READ2); for(i = 0; i < user_idx; i++) { fread(&Rents[i], sizeof(CdInfo), 1, a); } fclose(a); } int FindUser(char *name) { int i = 0; for (i = 0; i < user_idx; i++) { if ( !( strcmp(name, Rents[i].username) ) ) { return i; } } } void RentCd(char *name) { int i = 0; for (i=0;i<cd_idx;i++) { if( !( strcmp(Cds[i].film_name, name) ) ) { Cds[i].status = RENTED; return; } } } void ReturnCd(char *name) { int i = 0; for (i=0;i<cd_idx;i++) { if( !( strcmp(Cds[i].film_name, name) ) ) { Cds[i].status = UNRENTED; return; } } } void UserRent(int userId,int *cdId,char *cdName,Time *rentDay) { RentCd(cdName); memcpy( Rents[userId].FilmName[cdId], cdName, strlen(cdName) ); (*cdId)++; memcpy( &Rents[userId].RentTime, rentDay, sizeof(rentDay) ); } int rent(void) { char userName[NAME_LEN_MAX] = {0}; int userId = 0; int cdId = 0; int option = 0; LoadCdInfos2(); LoadUserInfos(); printf("please input user name : \n"); scanf("%s",userName); userId = FindUser(userName); while( option!='B' ) { char cdName[NAME_LEN_MAX] = {0}; Time rentDate = {0} printf("please input cd name : \n"); scanf("%s",cdName); printf("please input date(format :2012 04 10) : \n"); scanf("%d %d %d",&rentDate.year,&rentDate.month,&rentDate.day); UserRent(userId,&cdId,cdName,rentDate); getchar(); printf("A:rent another cd\n"); printf("B:finish\n"); scanf("%c",&option); } } int CountDay(Time* date) { return (date.year*360 + date.month*30 + date.day); } int LastingDay(Time* start,Time* end) { return (CountDay(end)-CountDay(start)); } void UserReturn(int userId,Time *returnDate) { int rentedDay = LastingDay( returnDate, &Rents[userId].RentDate ); memcpy( &Rents[userId].ReturnDate, returnDate, sizeof(returnDate) ); if ( 0 == rentedDay % 3 ) { Rents[userId].fee = rentedDay / 3; } else { Rents[userId].fee = rentedDay / 3 + 1; } } void returnn (void) { char userName[NAME_LEN_MAX] = {0}; int userId = 0; char option = 0; LoadCdInfos2(); LoadUserInfos(); printf("please input user name : \n"); scanf("%s",userName); userId = FindUser(userName); while( option!='B' ) { char cdName[NAME_LEN_MAX] = {0}; Time returnDate = {0}; int fee = 0; printf("please input cd name to return : \n"); scanf("%s",cdName); printf("please input date(format :2012 04 10) : \n"); scanf("%d %d %d",&returnDate.year,&returnDate.month,&returnDate.day); ReturnCd(cdName); UserReturn(userId,returnDate,&fee); printf("thanks for , you are expected to pay %d for the cd\n",fee); getchar(); printf("A:rent another cd\n"); printf("B:finish\n"); scanf("%c",&option); } }
#include <stdio.h> #include <stdlib.h> #include <string.h> #define CD_MAX 1000 #define USER_MAX 1000 #define NAME_LEN_MAX 10 #define RENTS_ALLOWED_MAX 10 #define FILE_CD "cd.txt" #define FILE_USER "rent.txt" #define FILE_CREATE "wb" #define FILE_APPEND "ab" #define FILE_READ "rb" #define FILE_READ2 "r" #define OK 0 #define NOT_FOUNTD 1 typedef enum{ RENTED, UNRENTED, UNAVAILABLE, }CdStatus; typedef struct _cd { char name[NAME_LEN_MAX]; char country[NAME_LEN_MAX]; char type[NAME_LEN_MAX]; CdStatus status; }CdInfo; typedef struct _time { int year; int month; int day; }Time; typedef struct _user { char name[NAME_LEN_MAX]; char cds[RENTS_ALLOWED_MAX][NAME_LEN_MAX]; Time rentDate; Time returnDate; int fee; }UserInfo; /**************************************************** USER INTERFACE ****************************************************/ void UI_ShowMainMenu() { printf(" ---------WELCOME TO CD RENT MANAGEMENT SYSTEM------- \n"); printf(" A: ADD NEW CD \n"); printf(" B: QUERY CD \n"); printf(" C: RENT CD (ONLY FOR MEMBERS) \n"); printf(" D: RETURN CD \n"); printf(" E: ADD NEW MEMBER \n"); printf(" F: EXIT \n"); } void UI_ShowCdStatus(CdStatus status) { if( status!=UNRENTED ) { printf("the cd is rented or unavailable\n"); } else { printf("the cd is available\n"); } } int UI_GetOption(int *option) { printf(" -----PLEASE INPUT YOUR OPTION(UPPER CASE ONLY)----- \n"); rewind(stdin); scanf("%c",option); } void UI_GetCdInfo(CdInfo *cd) { printf("please input cd name : \n"); scanf("%s",cd->name); printf("please input cd country : \n"); scanf("%s",cd->country); printf("please input cd type(such as act/fiction/comedy etc) : \n"); scanf("%s",cd->type); } void UI_GetCdName(char *name) { printf("please input the cd you wanted : \n"); scanf("%s",name); } void UI_GetUserName(char *name) { printf("please input user name : \n"); scanf("%s",name); } void UI_GetDate(Time *date) { printf("please input date(format :2012 04 10) : \n"); scanf("%d %d %d",date->year,date->month,date->day); } void UI_GetSuccessiveOption(int *option) { getchar(); printf("A:continue\n"); printf("B:finish\n"); scanf("%c",option); } void UI_ShowCdAdded() { printf("cd info added successfully\n"); } void UI_ShowUserAdded() { if ( gUsers.Idx!=1 ) { printf("congratualations, new user added successfully\n"); } } void UI_ShowFee(int fee) { printf("thanks for patronage, you are expected to pay %d for the cd\n",fee); } /**************************************************** LOCAL INFOs ****************************************************/ typedef struct _cd_record { CdInfo cds[CD_MAX]; int idx;/* why should be 1000 at first? */ }CdRecord; typedef struct _user_record { UserInfo users[USER_MAX]; int idx;/* why should be 1000 at first? */ }UserRecord; CdRecord gCds = {{0},1000}; UserRecord gUsers = {{0},1000}; int INFO_CdNum() { return gCds.idx; } CdInfo *INFO_CdPtr(const int idx) { return &(gCds.cds[idx]); } void INFO_AddCdInfo(const CdInfo *newCd) { memcpy( gCds.cds[gCds.idx], newCd, sizeof(*newCd) ); gCds.cds[gCds.idx].status=UNRENTED; gCds.idx++; } int INFO_FindCd(const char *name) { int i = 0; for(i=0; i<gCds.idx; i++) { if( !( strcmp(gCds.cds[i].name, name) ) ) { return i; } } return NOT_FOUNTD; } int INFO_GetCdStatus(const char *name,CdStatus *status) { int idx = INFO_FindCd(name); if( idx!=NOT_FOUNTD ) { *status = gCds.cds[idx].status; return OK; } return NOT_FOUNTD; } int INFO_SetCdStatus(const char *name,const CdStatus *status) { int idx = INFO_FindCd(name); if( idx!=NOT_FOUNTD ) { gCds.cds[idx].status = *status; return OK; } return NOT_FOUNTD; } int INFO_UserNum() { return gUsers.idx; } UserInfo INFO_UserPtr(const int idx) { return &(gUsers.Users[idx]); } void INFO_AddUserInfo(const char *name) { memcpy( gUsers.Users[gUsers.idx].name, name, strlen(name) ); gUsers.idx++; } int INFO_FindUser(const char *name) { int i = 0; for (i = 0; i < gUsers.idx; i++) { if ( !( strcmp(name, gUsers.Users[i].name) ) ) { return i; } } } void INFO_UserRent(int userId,int *cdId,char *cdName,Time *rentDay) { memcpy( gUsers.users[userId].cds[cdId], cdName, strlen(cdName) ); (*cdId)++; memcpy( &gUsers.users[userId].RentTime, rentDay, sizeof(rentDay) ); } static int CountDay(Time* date) { return (date.year*360 + date.month*30 + date.day); } static int LastingDay(Time* start,Time* end) { return (CountDay(end)-CountDay(start)); } void INFO_UserReturn(int userId,Time *returnDate) { UserInfo *user = &gUsers.users[userId]; int rentedDay = LastingDay( returnDate, user.rentDate ); memcpy( user.rentDate, returnDate, sizeof(returnDate) ); if ( 0==rentedDay%3 ) { user.fee = rentedDay / 3; } else { user.fee = rentedDay / 3 + 1; } } /**************************************************** files ****************************************************/ void FILE_StoreCdInfo(const CdInfo *cd) { FILE *a = NULL; char flag = (INFO_CdNum()==1)?FILE_CREATE:FILE_APPEND; a = fopen(FILE_CD,flag); fwrite(cd, sizeof(*cd), 1, a); fclose(a); } void FILE_LoadCdInfos() { FILE *a = NULL; int i = 0; a = fopen(FILE_CD,FILE_READ); for(i=0; i<INFO_CdNum(); i++) { fread(INFO_CdPtr(i), sizeof(CdInfo), 1, a); } fclose(a); } void FILE_StoreUserInfo() { FILE *a = NULL; int flag = (1==INFO_UserNum())?FILE_CREATE:FILE_APPEND; a = fopen(FILE_USER,flag); fwrite( INFO_UserPtr(INFO_UserNum()-1), sizeof(UserInfo), 1, a ); fclose(a); } void FILE_LoadUserInfos() { FILE *a = NULL; int i = 0; a = fopen(FILE_USER,FILE_READ2); for(i = 0; i < INFO_UserNum(); i++) { fread(INFO_UserPtr(i), sizeof(CdInfo), 1, a); } fclose(a); } /**************************************************** kernel logic ****************************************************/ void add(void) { CdInfo cd = {0}; UI_GetCdInfo(&cd); INFO_AddCdInfo(&cd); FILE_StoreCdInfo(&cd); UI_ShowCdAdded(); } int check(void) { char name[NAME_LEN_MAX] = {0}; int result = NOT_FOUNTD; CdStatus status = UNAVAILABLE; UI_GetCdName(name); FILE_LoadCdInfos(); result = INFO_GetCdStatus(name,&status); if( result==OK ) { UI_ShowCdStatus(status); return 1; } else { return 0; } } void useradd(void) { char name[NAME_LEN_MAX] = {0}; UI_GetUserName(name); INFO_AddUserInfo(name); FILE_StoreUserInfo(); UI_ShowUserAdded(); } int rent(void) { char userName[NAME_LEN_MAX] = {0}; int userId = 0; int cdId = 0; int option = 0; FILE_LoadCdInfos(); FILE_LoadUserInfos(); UI_GetUserName(userName); userId = INFO_FindUser(userName); while( option!='B' ) { char cdName[NAME_LEN_MAX] = {0}; Time rentDate = {0}; UI_GetCdName(cdName); UI_GetDate(&rentDate); INFO_SetCdStatus(cdName,RENTED); INFO_UserRent(userId,&cdId,cdName,rentDate); UI_GetSuccessiveOption(&option); } } void returnn (void) { char userName[NAME_LEN_MAX] = {0}; int userId = 0; char option = 0; FILE_LoadCdInfos(); FILE_LoadUserInfos(); UI_GetUserName(userName); userId = INFO_FindUser(userName); while( option!='B' ) { char cdName[NAME_LEN_MAX] = {0}; Time returnDate = {0}; int fee = 0; UI_GetCdName(cdName); UI_GetDate(&returnDate); INFO_SetCdStatus(cdName,UNRENTED); INFO_UserReturn(userId,returnDate,&fee); UI_ShowFee(fee); UI_GetSuccessiveOption(&option); } } void Handle(const int option) { switch(option) { case 'A': add(); break; case 'B': check(); break; case 'C': rent(); break; case 'D': returnn(); break; case 'E': useradd(); break; case 'F': break;; } } void main (void) { char option = 0; while( option!='F' ) { UI_ShowMainMenu(); UI_GetOption(&option); Handle(option); } exit(0); }