资源描述
C语言实践应用
客房管理系统
一.总体设计
本程序包含用户登记、统计、查询、修改等四大功能。其中用户登记功能包含用户姓名、性别、年龄、身份证、入住年份、入住月份、入住日期、房间号、房间电话及房间价格;统计功能包含按性别统计、按年龄统计及按入住时间(年月日)统计;查询功能包含按房间号查询、按姓名查询及按性别查询;修改功能包含性别修改、年龄修改、入住年份修改、入住月份修改及入住日期修改。下面以流程图的形式展示本程序设计思路:
开始
输出 1.登记2.统计3.查询4.修改5.退出
输入 n=1
选择用户登记功能
………………
输入用户信息及客房信息
系统自动分配房间号
是否继续输入用户信息
输入k
………………
k=1?
是
否
输出 1.登记2.统计3.查询4.修改5.退出
输入n
功能菜单选择
n=2 ……………………
n=3 n=4 n=5
退出
输入姓名
输出1.按房间号2.按姓名3. 按性别查询
输出1.按性别统计2.按年龄统计3.按入住日期统计
结束
输出用户信息
输出1.性别2.年龄3.年份4.月份5.日期
输入i
输入i
输入a
i=1 i=2 i=3 i=1 i=2 i=3
输入性别
输入姓名
输入房间号
输出各日期段人数
输出各年龄段人数
输出男女性别人数
输入k
输入日期
输入月份
输入年份
输入年龄
输入性别
a=1 a=2 a=3 a=4 a=5
输入k
k=1 k=1?
k=1 k=1?
k=1 k=1?
是 是 是
否 否 否
二.设计模块
一个较大的C语言程序不外乎由多个函数组成,本程序也并不例外。现就本程序中涉及的各个函数的代码和功能分别进行说明。
1.main函数
void main()
{
ini();
menu();
}
本函数用于调用ini函数和menu函数(介绍见下文)。
2.ini函数
void ini()
{
int i=0;
for(;i<100;i++)
{
Room[i].sign=false;
Room[i].Room_ID=i;
}
}
本函数功能为先定义所有的房间为空并依次定义各个房间的序号。
3.menu函数
void menu()
{
int n,k;
do
{
printf("1.登记\n");
printf("2.统计\n");
printf("3.查询\n");
printf("4.修改\n");
printf("5.退出\n");
printf("Choice your number(1-5):");
scanf("%d",&n);
if(n<1||n>5)
{
k=1;
puts("Please enter again\n");
}
else k=0;
}while(k==1);
switch(n)
{
case 1:dengji();break;
case 2:tongji();break;
case 3:chaxun();break;
case 4:xiugai();break;
case 5:exit(0);
}
}
本函数的功能为输出功能菜单选项,其中包括登记、统计、查询、修改及退出。系统根据输入的选项调用相应的功能函数。
4.dengji函数
void dengji()
{
int k=1,i=0;
while(k)
{
printf("Please enter the Name:");
scanf("%s",Room[i].Client_list.Name);
printf(" Please enter the Sex, Men 1, Women 2 :");
scanf("%d",&Room[i].Client_list.Sex);
printf("Please enter the Age:");
scanf("%d",&Room[i].Client_list.Age);
printf("Please enter the ID_card:");
scanf("%d",&Room[i].Client_list.ID_card);
printf("Please enter the year:");
scanf("%d",&Room[i].Client_list.year);
printf("Please enter the month:");
scanf("%d",&Room[i].Client_list.month);
printf("Please enter the date:");
scanf("%d",&Room[i].Client_list.date);
printf("Please enter the Tel:");
scanf("%d",&Room[i].Tel);
printf("Please enter the Price:");
scanf("%d",&Room[i].Price);
Room[i].sign=true;
printf("The Room_ID is:%d\n",Room[i].Room_ID);
i++;
printf("Do you want to continue?, Yes 1, No 0: ");
scanf("%d",&k);
if(k!=1&&k!=0)
{
printf("You have entered the wrong number, please enter again\n");
printf("Do you want to continue?, Yes 1, No 0: ");
scanf("%d",&k);
}
}
menu();
}
本函数的功能为登记用户及房间信息,包括姓名、性别、年龄、身份证、年份、月份、日期、房间电话及房间价格。系统依次分配房间号并定义此房间为非空。管理员可根据具体情况登记相应用户人数的信息。在此申明由于本程序并未将用户信息存入磁盘,因此务必先登记用户信息再执行功能菜单中的其他功能。
5.tongji函数
void tongji()
{
int i;
printf("1.According to the sex 2.According to the age 3.According to the time:");
scanf("%d",&i);
if(i!=1&&i!=2&&i!=3)
{
printf("You have entered the wrong number, please enter again\n");
tongji();
}
switch(i)
{
case 1: xingbie();break;
case 2: nianling();break;
case 3: shijian();break;
}
}
本函数的功能为输出统计功能菜单选项,包括根据按性别统计、根据年龄统计及根据入住时间统计。系统根据输入的选项执行相应的函数(介绍见下文)。
6.xingbie函数
void xingbie()
{
int i,a=0,b=0,k=0;
for(i=0;Room[i].sign;++i)
{
switch(Room[i].Client_list.Sex)
{
case 1:a++;break;
case 2:b++;break;
}
}
printf("Men:%d\n Women:%d\n",a,b);
printf("Do you want to continue? Yes 1, No 0: ");
scanf("%d",&k);
if(k!=1&&k!=0)
{
printf("You have entered the wrong number, please enter again\n");
printf("Do you want to continue?, Yes 1, No 0: ");
scanf("%d",&k);
}
if(k) tongji();
else menu();
}
本函数的功能为分别输出用户中男女性别的人数。
7.nianling函数
void nianling()
{
int i,a=0,b=0,c=0,k=0;
for(i=0;Room[i].sign;i++)
{
switch(Room[i].Client_list.Age/20)
{
case 0:a++;break;
case 1:b++;break;
default:c++;
}
}
printf("Age<20:%d\n20<=Age<40:%d\nAge>=40:%d\n",a,b,c);
printf("Do you want to continue? Yes 1, No 0: ");
scanf("%d",&k);
if(k!=1&&k!=0)
{
printf("You have entered the wrong number, please enter again\n");
printf("Do you want to continue?, Yes 1, No 0: ");
scanf("%d",&k);
}
if(k) tongji();
else menu();
}
本函数的功能为分别输出用户中年龄在20岁以下、20至40岁及40岁以上的人数。
8.shijian函数
void shijian()
{
int i,a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,j=0,k=0;
for(i=0;Room[i].sign;i++)
{
switch(Room[i].Client_list.year)
{
case 2009:a++;break;
case 2010:b++;break;
}
}
for(i=0;Room[i].sign;i++)
{
if(Room[i].Client_list.date<=10) c++;
else if(Room[i].Client_list.date<=20&&Room[i].Client_list.date>10) d++;
else e++;
}
for(i=0;Room[i].sign;i++)
{
if(Room[i].Client_list.month<=3) f++;
else if(Room[i].Client_list.month<=6&&Room[i].Client_list.month>3) g++;
else if(Room[i].Client_list.month<=9&&Room[i].Client_list.month>6) h++;
else j++;
}
printf("2009:%d\n 2010:%d\n month<=3:%d\n3<month<=6:%d\n6<month<=9:%d\n9<month<=12:%d\n date<=10:%d\n10<date<=20:%d\n20<date<30:%d\n",a,b,f,g,h,j,c,d,e);
printf("Do you want to continue? Yes 1, No 0:");
scanf("%d",&k);
if(k!=1&&k!=0)
{
printf("You have entered the wrong number, please enter again\n");
printf("Do you want to continue?, Yes 1, No 0: ");
scanf("%d",&k);
}
if(k) tongji();
else menu();
}
本函数的功能为分别输出用户中入住年份在2009年及2010年的人数,入住月份在3月以下、3月至6月、6月至9月、9月至12月份的人数及入住日期在10日以下、10日至20日、20日以上的人数。
9.shuchu函数
void shuchu(int i)
{
printf("Name:%s Sex: %d Age: %d ID_card: %d Time: %d-%d-%d Room_ID: %d Tel: %d Price: %d\n",Room[i].Client_list.Name,Room[i].Client_list.Sex,Room[i].Client_list.Age,Room[i].Client_list.ID_card,Room[i].Client_list.year,Room[i].Client_list.month,Room[i].Client_list.date,Room[i].Room_ID,Room[i].Tel,Room[i].Price);
}
本函数的功能为输出相应用户及所在房间信息,包括姓名、性别、年龄、身份证、入住时间(年月日)、房间号、房间电话及房间价格。
10.chaxun
void chaxun()
{
int i;
printf("1.According to the Room_ID 2.According to the name 3.According to the sex:");
scanf("%d",&i);
if(i!=1&&i!=2&&i!=3)
{
printf("You have entered the wrong number, please enter again\n");
chaxun();
}
switch(i)
{
case 1: Room_ID();break;
case 2: name();break;
case 3: Sex();break;
}
}
本函数的功能为输出查询功能菜单选项,包括根据房间号查询、根据姓名查询及根据性别查询。系统根据输入的选项执行相应的函数(介绍见下文)。
11.Room_ID函数
void Room_ID()
{
int i=0,a,k;
printf("Please enter the Room_ID:");
scanf("%d",&a);
for(;i<100;++i)
if(Room[i].Room_ID==a)
{
if(Room[i].sign==true) shuchu(i);
else printf("There is no person in the room\n");
break;
}
printf("Do you want to continue? Yes 1, No 0:");
scanf("%d",&k);
if(k!=1&&k!=0)
{
printf("You have entered the wrong number, please enter again\n");
printf("Do you want to continue?, Yes 1, No 0: ");
scanf("%d",&k);
}
if(k) chaxun();
else menu();
}
本函数的功能为系统根据输入的房间号调出该房间中的用户信息及房间信息。
12.name函数
void name()
{
int i=0,k,l=0;
char str[20];
printf("Please enter the Name:");
scanf("%s",str);
for(;Room[i].sign;i++)
{if(strcmp(str,Room[i].Client_list.Name)==0) {shuchu(i);l=1;}}
if(!l) printf("There is not the person\n");
printf("Do you want to continue? Yes 1, No 0:");
scanf("%d",&k);
if(k!=1&&k!=0)
{
printf("You have entered the wrong number, please enter again\n");
printf("Do you want to continue?, Yes 1, No 0: ");
scanf("%d",&k);
}
if(k) chaxun();
else menu();
}
本函数的功能为系统根据输入的姓名调出该用户的信息及所在房间信息,若无此用户则输出查无此人。
13.Sex函数
void Sex()
{
int i=0,k,a,l=0;
printf("Please enter the sex, Men 1, Women 2:");
scanf("%d",&a);
for(;Room[i].sign;++i)
{if(Room[i].Client_list.Sex==a) {shuchu(i);l=1;}}
if(!l) printf("There is no the sex\n");
printf("Do you want to continue? Yes 1, No 0:");
scanf("%d",&k);
if(k) chaxun();
else menu();
}
本函数的功能为系统根据输入的性别调出该性别的用户信息及所在房间信息。
14.xiugai函数
void xiugai()
{
int i=0,k,a,l=0;
char str[20];
printf("Please enter the name:");
scanf("%s",str);
for(;Room[i].sign;++i)
if(strcmp(str,Room[i].Client_list.Name)==0)
{
l++;shuchu(i);break;
}
if(!l)
{
printf("You have entered the wrong name, please enter again\n");
xiugai();
}
printf("What you want to corret is as follow:\n1.Sex 2.Age 3.year 4.month 5.date:");
scanf("%d",&a);
if(a!=1&&a!=2&&a!=3&&a!=4&&a!=5)
{
printf("You have entered the wrong number, please enter again\n");
printf("What you want to corret is as follow:\n1.Sex 2.Age 3.year 4.month 5.date:");
scanf("%d",&a);
}
switch(a)
{
case 1:printf("Please enter the sex:");scanf("%d",&Room[i].Client_list.Sex);break;
case 2:printf("Please enter the age:");scanf("%d",&Room[i].Client_list.Age);break;
case 3:printf("Please enter the year:");scanf("%d",&Room[i].Client_list.year);break;
case 4:printf("Please enter the month:");scanf("%d",&Room[i].Client_list.month);break;
default:printf("Please enter the date:");scanf("%d",&Room[i].Client_list.date);
}
printf("Do you want to continue? Yes 1, No 0:");
scanf("%d",&k);
if(k!=1&&k!=0)
{
printf("You have entered the wrong number, please enter again\n");
printf("Do you want to continue?, Yes 1, No 0: ");
scanf("%d",&k);
}
if(k) xiugai();
else menu();
}
本函数的功能为系统根据输入的姓名调出该用户及所在房间信息,同时输出修改功能菜单选项,包括修改性别、修改年龄、修改入住年份、修改月份及修改日期。系统根据输入的选项修改用户相应的信息。
三.结束语
C语言是一门计算机语言,如同其他计算机语言甚至日常交际中常说的外语一样需要不断的练习和实践才能逐渐掌握并熟练运用。尤其是对于带有创造性的编写一个较大C语言程序时更能凸现出这一点。由于本人学习该语言理论知识时间仓促加之课下没有进行大量的程序编写练习,因此在编写及调试本程序过程中难免遇到了诸多问题。现就出现的这些问题及解决办法作简要分析。
1.设计思路
起初对于本程序的思路都是一片空白,对于题目中要求实现的用户登记、统计、查询、修改功能不知如何实现,重新对书中有关循环、数组、结构体、文件等知识进行了回顾,并且读了一些相关例题。本程序包含用户登记、统计、查询、修改等四大功能。其中用户登记功能包含用户姓名、性别、年龄、身份证、入住年份、入住月份、入住日期、房间号、房间电话及房间价格;统计功能包含按性别统计、按年龄统计及按入住时间(年月日)统计;查询功能包含按房间号查询、按姓名查询及按性别查询;修改功能包含性别修改、年龄修改、入住年份修改、入住月份修改及入住日期修改。按题目的要求,编写能实现相应功能的函数,最后再从主函数中调用这些函数来。
2.联系实际
在上机调试过程中经常出现输入未给出的选项,此时系统会发生各种难以预料的结果,为避免此类情况的发生,又反复对程序代码进行了修改,以便系统能对输入的各种选项进行正确处理。例如:
void tongji()
{
int i;
printf("1.According to the sex 2.According to the age 3.According to the time:");
scanf("%d",&i);
if(i!=1&&i!=2&&i!=3)
{
printf("You have entered the wrong number, please enter again\n");
tongji();
}
switch(i)
{
case 1: xingbie();break;
case 2: nianling();break;
case 3: shijian();break;
}
}
若i的输入值不是1、2或3则输出输入错误,请重新输入。若不考虑此情况则上机调式时当输入错误i值时会结束程序,造成不便处理的麻烦。
3.代码细节
在程序编译时常常会出现各种或多或少的错误提示,其原因往往是程序代码出现了一些细节上的简单错误,或是分号缺失,或是大括号不对应,或是未定义变量等各种常见错误。这些错误虽不是致命性的严重失误但却影响了程序的编译和连接,最终仍不能使程序正常执行。为了能够消除这些错误,必须不断地、反复地修改程序代码,并且不断的调试运行。
展开阅读全文