main.c
#include <unistd.h>
#include "SeqList.h"
#include "User.h"
int main()
{
SL user;
SLInit(&user);
char ans = 0;
printf("是否需要导入昨日续住客人的数据:y/n\n");
scanf(" %c", &ans);
if (ans == 'y')
{
LoadUser(&user);
}else if(ans!='n')
{
printf("请重新输入y/n\n");
scanf(" %c",&ans);
}
menu();
printf("请输入选项:");
int choice = 0;
while (scanf("%d", &choice))
{
switch (choice)
{
case 1:
userInfoGet(&user);
break;
case 2:
find(&user);
break;
case 3:
remedy(&user);
break;
case 4:
deleteUser(&user);
break;
case 5:
sortUser(&user);
break;
case 6:
printUser(&user);
if(user.size==0)
{
printf("当前没有客人入住");
} else{
printf("当前入住有%d位客人\n",user.size);
}
break;
case 7:
printf("当前入住有%d位客人\n剩余%d间房间\n",user.size,user.Max);
break;
case 8:
destroyUser(&user);
printf("谢谢使用");
return 1;
default:
printf("请按照菜单重新输入:");
break;
}
usleep(1000);
menu();
printf("请输入选项:");
}
}
Seqlist.c
#include "SeqList.h"
//顺序表的初始化
void SLInit(SL* ps)
{
assert(ps);//确保没有传入空指针
ps->SeqList = NULL;
ps->size = 0;
ps->Max = 50;//初始化房间最大数量
ps->capacity = 0;
}
//顺序表的销毁
void SLDestroy(SL* ps)
{
assert(ps);
if (ps->SeqList)
{
free(ps->SeqList);
}
ps->SeqList = NULL;
ps->size = 0;
ps->capacity = 0;
}
//顺序表的扩容
void SLCheckCapacity(SL* ps)
{
assert(ps);
SLDataType* temp = NULL;
if (ps->size == ps->capacity)
{
int newCapacity = ps->capacity;
temp = (SLDataType*)realloc(ps->SeqList, (newCapacity = (ps->capacity == 0) ? 4 : (ps->capacity * 2)) * sizeof(SLDataType));//扩容
assert(temp);
ps->SeqList = temp;
ps->capacity = newCapacity;
temp = NULL;
}
}
//顺序表的尾部插入
//当顺序表有足够的空间插入时(包括有内容和顺序表为空)
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps);
//顺序表的扩容
SLCheckCapacity(ps);//确保SeqList指针不为空之后判断容量是否足够
//当顺序表有足够的空间插入时(包括有内容和顺序表为空)
ps->SeqList[ps->size] = x;
ps->size++;
ps->Max--;
}
//顺序表的指定位置删除
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(0 <= pos && pos <= ps->size);//确保pos为有效位置
for (int i = pos + 1; i < ps->size; i++)
{
ps->SeqList[i - 1] = ps->SeqList[i];
}
ps->size--;
ps->Max++;
}
Seqlsit.h
#include "SeqList.h"
//顺序表的初始化
void SLInit(SL* ps)
{
assert(ps);//确保没有传入空指针
ps->SeqList = NULL;
ps->size = 0;
ps->Max = 50;//初始化房间最大数量
ps->capacity = 0;
}
//顺序表的销毁
void SLDestroy(SL* ps)
{
assert(ps);
if (ps->SeqList)
{
free(ps->SeqList);
}
ps->SeqList = NULL;
ps->size = 0;
ps->capacity = 0;
}
//顺序表的扩容
void SLCheckCapacity(SL* ps)
{
assert(ps);
SLDataType* temp = NULL;
if (ps->size == ps->capacity)
{
int newCapacity = ps->capacity;
temp = (SLDataType*)realloc(ps->SeqList, (newCapacity = (ps->capacity == 0) ? 4 : (ps->capacity * 2)) * sizeof(SLDataType));//扩容
assert(temp);
ps->SeqList = temp;
ps->capacity = newCapacity;
temp = NULL;
}
}
//顺序表的尾部插入
//当顺序表有足够的空间插入时(包括有内容和顺序表为空)
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps);
//顺序表的扩容
SLCheckCapacity(ps);//确保SeqList指针不为空之后判断容量是否足够
//当顺序表有足够的空间插入时(包括有内容和顺序表为空)
ps->SeqList[ps->size] = x;
ps->size++;
ps->Max--;
}
//顺序表的指定位置删除
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(0 <= pos && pos <= ps->size);//确保pos为有效位置
for (int i = pos + 1; i < ps->size; i++)
{
ps->SeqList[i - 1] = ps->SeqList[i];
}
ps->size--;
ps->Max++;
}
user.c
#include <unistd.h>
#include "SeqList.h"
#include "User.h"
// 系统主菜单
void menu()
{
usleep(500000);
printf("*****************************\n");
printf("******客房管理系统*******\n");
printf("*1. 办理入住\n");
printf("*2. 查找客人\n");
printf("*3. 修改客人信息\n");
printf("*4. 退房\n");
printf("*5. 以排序输出客人信息\n");
printf("*6. 查看所有客人信息\n");
printf("*7. 查看当前入住客人人数以及剩余房间数\n");
printf("*8. 退出系统以及保存客人信息\n");
printf("*****************************\n");
}
//导入历史信息
void LoadUser(SL* user)
{
FILE* pf = fopen("/Users/meiyouyu/Desktop/2/user.txt", "rb");
if (pf == NULL)
{
perror("fopen error!\n");
return;
}
User tmp;
while (fread(&tmp, sizeof(User), 1, pf))
{
SLPushBack(user, tmp);
}
fclose(pf);
printf("****成功导入历史入住数据****\n");
}
// 打印所有客人信息
void printUser(SL* user)
{
for (int i = 0; i < user->size; i++)
{
printf("第%d名客人:", i + 1);
printf("\t身份证:%ld", user->SeqList[i].id);
printf("\t姓名:%s", user->SeqList[i].name);
printf("\t性别:%s", user->SeqList[i].gender);
printf("\t入住房间档位:%d", user->SeqList[i].level);
printf("\t入住房间号:%d\n", user->SeqList[i].location);
}
}
//按照房间号排序
int cmp_loc(const void* p1, const void* p2)
{
return ((User *)p1)->location - ((User *)p2)->location;
}
// 打印排序后的内容
void printSortedUser(User* tmp, int size)
{
for (int i = 0; i < size; i++)
{
printf("第%d名客人:", i + 1);
printf("\t客人身份证:%ld", tmp[i].id);
printf("\t姓名:%s", tmp[i].name);
printf("\t性别:%s", tmp[i].gender);
printf("\t入住房间价格:%d", tmp[i].level);
printf("\t房间号:%d\n", tmp[i].location);
}
}
//身份证去重
int repeat_id(User tmp,SL user)
{
int i;
for (i=0;i<user.size;i++)
{
if(tmp.id==user.SeqList[i].id)
{
return 0;
}
}
return 1;
}
// 获取输入
void userInfoGet(SL* user)
{
int num = 0;
char a=0;
printf("请输入入住用户个数:");
scanf("%d", &num);
for (int i = 0; i < num; i++)
{
User tmp;
printf("请输入第%d个客人信息\n", i + 1);
printf("请输入客人身份证:");
scanf("%ld", &(tmp.id));
while (!repeat_id(tmp,*user))
{
printf("身份证重复,请仔细检查重新输入\n");
scanf("%ld", &(tmp.id));
}
printf("请输入客人姓名:");
scanf("%s", tmp.name);
printf("请输入客人性别:");
scanf("%s", tmp.gender);
printf("要查看已被入住的房间号吗?y/n\n");
scanf(" %c",&a);
if(a=='y')
{
printf("已经入住的有:\n");
qsort(user->SeqList, user->size, sizeof(User), cmp_loc);
printSortedUser(user->SeqList, user->size);
} else if(a!='n')
{
printf("请重新输入y/n\n");
scanf(" %c",&a);
}
printf("请输入客人想要入住的价格:1.199大床房,2.299双人床,3.999豪华大床房 : ");
scanf("%d", &tmp.level);
while (tmp.level!=999&&tmp.level!=299&&tmp.level!=199)
{
printf("\n输入价格格式错误,请重新输入:1.199大床房,2.299双人床,3.999豪华大床房 : ");
scanf("%d", &tmp.level);
}
printf("请输入客人入住对应等级的房间号: ");
scanf("%d", &tmp.location);
SLPushBack(user, tmp);
printf("入住成功\n");
}
}
// 查找方式菜单
void menuForFind()
{
printf("**************\n");
printf("*1. 客人身份证\n");
printf("*2. 客人姓名\n");
printf("*3. 房间号\n");
printf("*4. 退出查找\n");
printf("**************\n");
}
// 按照身份证查找,返回在表中位置
int findBy_id(SL* user, long target)
{
for (int i = 0; i < user->size; i++)
{
if (user->SeqList[i].id == target)
{
return i;// 返回对应位置下标
}
}
return -1;
}
//按照房间号
int findBy_loc(SL* user, long loc)
{
for (int i = 0; i < user->size; i++)
{
if (user->SeqList[i].location == loc)
{
return i;// 返回对应位置下标
}
}
return -1;
}
// 按照名字查找
int findBy_name(SL* user, char* target)
{
for (int i = 0; i < user->size; i++)
{
if (strcmp(user->SeqList[i].name, target) == 0)
{
return i;// 返回对应位置下标
}
}
return -1;
}
// 选择删除——返回下标
int findUser(SL* user)
{
int choice = 0;
int pos ;
menuForFind();
printf("请选择查找方式:");
while (scanf("%d", &choice))
{
switch (choice)
{
case 1:
{
long target = 0;
printf("请输入需要查找的身份证id:");
scanf("%ld", &target);
pos = findBy_id(user, target);
return pos;
}
case 2:
{
char name[50] = { 0 };
printf("请输入需要查找的客人姓名:");
scanf("%s", name);
pos = findBy_name(user, name);
return pos;
}
case 3:
{
long loc = 0;
printf("请输入需要查找的身份证id:");
scanf("%ld", &loc);
pos = findBy_loc(user, loc);
return pos;
}
case 4:
return -1;
default:
menuForFind();
printf("请重新按照菜单输入:\n");
break;
}
menuForFind();
printf("请选择查找方式:");
}
return -2;
}
// 查找身份证
void findnumber(SL* a, long id)
{
int i = 0;
while (i != a->size)
{
if (id == a->SeqList[i].id)
{
printf("\n身份证:%ld 姓名:%s 性别:%s 房间号:%d 房间价格:%d\n",
a->SeqList[i].id, a->SeqList[i].name, a->SeqList[i].gender,a->SeqList[i].location,a->SeqList[i].level);
return;
}
i++;
}
printf("没有检索到对应信息的客人\n");
}
//查找房间号
void findlocation(SL* a, int location)
{
int i = 0;
while (i != a->size)
{
if (location == a->SeqList[i].location)
{
printf("身份证:%ld 姓名:%s 性别:%s 房间号:%d 房间价格:%d\n",
a->SeqList[i].id, a->SeqList[i].name, a->SeqList[i].gender,a->SeqList[i].location,a->SeqList[i].level);
return;
}
i++;
}
printf("没有检索到对应信息的客人\n");
}
// 查找姓名
void findname(SL* a, char* name)
{
int i = 0;
while (i != a->size)
{
int j = 0;
while (a->SeqList[i].name[j] == name[j] && name[j] != NULL)
{
j++;
}
if (j == strlen(name))
{
printf("用户信息\n身份证:%ld 姓名:%s 性别:%s 入住房间号:%d 房间价格:%d\n",
a->SeqList[i].id, a->SeqList[i].name, a->SeqList[i].gender,a->SeqList[i].location,a->SeqList[i].level);
return;
}
i++;
}
printf("没有检索到对应信息的客人\n");
}
// 查找
void find(SL* a)
{
printf("请输入需要怎么查找 1.通过身份证查找 2.通过姓名查找 3.通过房间号查找:");
int num = 0;
scanf("%d", &num);
if (num == 1)
{
printf("请输入需要查找的身份证:");
long id = 0;
scanf("%ld", &id);
findnumber(a, id);
}
else if(num==2)
{
printf("请输入需要查找的姓名:");
char name[50];
scanf(" %s", name);
findname(a, name);
} else if(num==3){
printf("请输入需要查找的房间号:");
int location;
scanf(" %d", &location);
findlocation(a,location);
} else{
printf("输入有误,请仔细检查选项\n");
find(a);
}
}
// 修改员工
void remedy(SL* a)
{
printUser(a);
printf("请输入需要修改信息的人员身份证号:");
long id = 0;
scanf("%ld", &id);
int i = 0;
while (i != a->size) {
if (id == a->SeqList[i].id)
{
break;
}
i++;
}
if(i>=a->size)
{
printf("没有找到对应客人信息\n");
return;
}
printf("输入需要修改的信息编号: 1.身份证 2.姓名 3.性别 4.房间号以及价格:");
int num = 0;
scanf("%d", &num);
long id1 = 0;
char name[50];
char gender[10];
int location;
int level;
switch (num)
{
case 1:
printf("请输入修改后身份证:");
scanf("%ld", &id1);
a->SeqList[i].id = id1; break;
case 2:
printf("请输入修改后姓名:");
scanf(" %s", name);
strcpy(a->SeqList[i].name, name); break;
case 3:printf("请输入修改后性别:");
scanf(" %s", gender);
strcpy(a->SeqList[i].gender, gender); break;
case 4:printf("请输入修改后的房间号和价格:");
scanf(" %d", &location);
scanf(" %d", &level);
a->SeqList[i].location = location;
a->SeqList[i].level = level;
break;
}
printf("修改完成\n");
}
// 退房
void deleteUser(SL* user)
{
char a=0;
// 调用查找函数
int pos = findUser(user);
if(pos==0){
return;
}
if (pos < -1)
{
printf("没有此用户,请仔细核对,或者用户已经办理退房\n");
return;
}
printf("确定为身份证为%ld的客人办理退房吗?y/n\n",user->SeqList[pos].id);
scanf(" %c",&a);
if(a=='y')
{
SLErase(user, pos);
printf("退房成功\n");
}else{
printf("返回主菜单");
}
}
// 排序菜单
void menuForSort()
{
printf("**************\n");
printf("*1. 客人身份证\n");
printf("*2. 客人姓名\n");
printf("*3. 房间号\n");
printf("*4. 房间价格\n");
printf("*5. 退出排序\n");
printf("**************\n");
}
// 按照身份证排序
int cmp_id(const void* p1, const void* p2)
{
return ((User*)p1)->id - ((User*)p2)->id;
}
//等级排序
int cmp_lev(const void* p1, const void* p2)
{
return ((User*)p1)->level- ((User*)p2)->level;
}
// 名字比较
int cmp_name(const void* p1, const void* p2)
{
return strcmp(((User*)p1)->name, ((User*)p2)->name);
}
// 排序主体
void sortUser(SL* user)
{
int choice = 0;
menuForSort();
printf("请选择需要排序的字段:");
while (scanf("%d", &choice))
{
switch (choice)
{
case 1:
qsort(user->SeqList, user->size, sizeof(User), cmp_id);
printSortedUser(user->SeqList, user->size);
break;
case 2:
qsort(user->SeqList, user->size, sizeof(User), cmp_name);
printSortedUser(user->SeqList, user->size);
break;
case 3:
qsort(user->SeqList, user->size, sizeof(User), cmp_loc);
printSortedUser(user->SeqList, user->size);
break;
case 4:
qsort(user->SeqList, user->size, sizeof(User), cmp_lev);
printSortedUser(user->SeqList, user->size);
break;
case 5:
return;
default:
menuForSort();
printf("请重新按照菜单输入:");
break;
}
menuForSort();
printf("请选择需要排序的字段:");
}
}
// 向文件中写入数据
void writeIntoFile(SL* user)
{
FILE* pf = fopen("/Users/meiyouyu/Desktop/2/user.txt", "wb");
if (pf == NULL)
{
perror("fopen error!\n");
return;
}
//将通讯录数据写⼊⽂件
for (int i = 0; i < user->size; i++)
{
fwrite(user->SeqList + i, sizeof(User), 1, pf);
}
fclose(pf);
printf("数据保存成功!\n");
}
// 信息销毁函数
void destroyUser(SL* user)
{
char ans = 0;
printf("当前剩余客户是否需要续住:y/n\n");
scanf(" %c", &ans);
if (ans == 'y')
{
writeIntoFile(user);
}else if(ans!='n')
{
printf("请重新输入y/n\n");
scanf(" %c",&ans);
}
SLDestroy(user);
}
user.h
#pragma once
#include "SeqList.h"
typedef struct SeqList_dynamic SL;
typedef struct User
{
long id;//身份证
char name[50];//名字
char gender[10];//性别
int level;//入住房间价格
int location;//入住房间号
}User;
// 程序菜单
void menu();
// 导入数据
void LoadUser(SL* employees);
// 初始化——输入
void userInfoGet(SL* employees);
// 查找方式菜单
void menuForFind();
// 查找——返回下标
int findUser(SL* user);
// 查找——显示对应客人信息
void find(SL* a);
// 修改信息
void remedy(SL* a);
// 删除员工信息
void deleteUser(SL* user);
// 排序菜单
void menuForSort();
// 按照指定内容排序
void sortUser(SL* user);
// 打印排序后的内容
void printSortedUser(User* tmp, int size);
// 向文件中写入数据
void writeIntoFile(SL* user);
// 销毁系统数据
void destroyUser(SL* user);
// 打印所有员工信息
void printUser(SL* user);
建立如图所示的结构就可以
user.txt打开是乱码是正常的