说明:
1 代码没有经过编译,只是从可读性、可维护性角度做概念上的提升
2 没有特别修改代码中的错误(只在实在忍不住了才修改)
3 暂时没有做软件设计方面的修改的计划
#include
#include
#include
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
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
#include
#include
#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
#include
#include
#include
#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