请问这个怎么解决?
你的代码逻辑有问题,贴这几行代码没办法解决问题
head是个空指针,那你要到head的初始化里去找它为什么会是空指针呀
源代码
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include <time.h>
typedef struct room {
int prize;
int room_num;
}ROOM;
struct customer {
char name[21];
int sex; //1男 0无 -1女
char date_1[20], date_2[20]; //入,出时间
ROOM num;
int spend_prize; //费用
struct customer* front;
struct customer* next;
};
typedef struct customer CUS;
int a = 0, b = 0;
CUS* create();
CUS* connect(CUS*);
CUS* makelist(int);
int output(CUS*);
void clear(CUS*);
void clear(CUS*);
int day(char*);
void prize_cot(CUS*);
CUS* file_set();
void fun1(CUS*);//完成
void fun2(CUS*);
void fun3(CUS*);
void show();
void moon();
int daydiff(char*, char*);
int U, V, X, D, T, L;
void moon() {
switch (V) {
case 1:
X++; break;
case 2:
T++; if (!(U % 400) || !(U % 4) && U % 100) L++; break;
case 3:
D++; break;
case 4:
X++; break;
case 5:
D++; break;
case 6:
X++; break;
case 7:
D++; break;
case 8:
D++; break;
case 9:
X++; break;
case 10:
D++; break;
case 11:
X++; break;
case 12:
D++, U++, V = 0; break;
default:
printf("MonthDigit error...I maked mon=%d!", V = 12);
}
return;
}
int daydiff(char* date1, char* date2) {
int W, a, b, c, days = 0;
sscanf_s(date1, "%4d-%2d-%2d", &U, &V, &W);
sscanf_s(date2, "%4d-%2d-%2d", &a, &b, &c);
for (; U < a; V++) moon();
for (; U == a && V < b; V++) moon();
if (U > a || V > b || c < W) printf("时间错误。");
else days = 31 * D + 30 * X + 28 * T + L - W + c;
return days;
}
CUS* create(void) {
CUS* head;
head = (CUS*)malloc(sizeof(CUS));
head->next = 0;
if (head != NULL) {
head->name[0] = '\0';
head->date_1[0] = '\0';
head->date_2[0] = '\0';
head->sex = 0;
head->num.prize = 0;
head->num.room_num = 0;
head->spend_prize = 0;
head->next = NULL;
head->front = NULL;
}
return head;
}
CUS* connect(CUS* p) {
CUS* q = (CUS*)malloc(sizeof(CUS));
if (q != NULL) {
q->next = p->next;
p->next = q;
q->front = p;
q->name[0] = '\0';
q->date_1[0] = '\0';
q->date_2[0] = '\0';
q->sex = 0;
q->num.prize = 0;
q->num.room_num = 0;
q->spend_prize = 0;
}
return q;
}
void clear(CUS* q) {
q->name[0] = '\0';
q->date_1[0] = '\0';
q->date_2[0] = '\0';
q->sex = 0;
q->spend_prize = 0;
}
void prize_cot(CUS* q) {//计算房间产生的费用
int time = 0;
time = daydiff(q->date_1, q->date_2);
q->spend_prize = time * q->num.prize;
}
CUS* makelist(int n) {
int i;
CUS* head = NULL, * tail;
head = create();
if (head != NULL) {
tail = head;
if (n != 0) for (i = 0; i < n; i++) tail = connect(tail);
}
return head;
}
int output(CUS* p)
{
int i = 0;
CUS* tail = p->next;
printf("姓名\t性别\t房间号\t入住时间 /退房时间\t总价\n");
while (tail != NULL) {
printf("%s\t%d\t%d\t%10s/%10s\t%d\n", tail->name, tail->sex,
tail->num.room_num, tail->date_1, tail->date_2, tail->spend_prize);
i++;
tail = tail->next;
};
return i;
}
CUS* first_def(int& a, int& b) //初始定义楼层数及房间个数及价格
{
CUS* head = NULL, * tail;
int i, j;
head = create();
if (head != NULL) head = makelist(a * b);
tail = head->next;
for (i = 1; i <= a; i++) for (j = 1; j <= b; j++) {
tail->num.room_num = i * 100 + j;
tail = tail->next;
}
for (tail = head->next; tail != NULL; tail = tail->next) {
printf("输入%d号房间价格。", tail->num.room_num);
scanf_s(" %d", &tail->num.prize);
}
return head;
}
CUS* re_def() {
CUS* head = NULL, * tail;
FILE* fp;
int i;
fopen_s(&fp, "record.txt", "r");
if (fp != NULL) {
fscanf_s(fp, "%d,%d", &a, &b);
head = create();
if (head != NULL) head = makelist(a * b);
tail = head;
for (tail = head->next; tail != NULL; tail = tail->next) {
fscanf_s(fp, " %3d,%d", &tail->num.room_num, &tail->num.prize);
fscanf_s(fp, "%20s", tail->name);
if (tail->name[0] == 0) clear(tail);
else {
fscanf_s(fp, "%2d", &tail->sex);
fscanf_s(fp, "%11s", tail->date_1);
fscanf_s(fp, "%11s", tail->date_2);
fscanf_s(fp, "%5d", &tail->spend_prize);
}
}
fclose(fp);
}
else {
printf("没有记录文件,请确认是否存在。\n");
printf("1.返回初次设定\n");
printf("2.退出\n");
scanf_s(" %d", &i);
switch (i) {
case 1:
file_set();
case 2:
exit(0);
}
}
return head;
}
void show()
{
system("cls");
printf("*******************请选择操作************************\n");
printf("\t\t1.旅客入住\n");
printf("\t\t2.旅客退房\n");
printf("\t\t3.信息查询\n");
printf("\t\t4.保存退出\n");
printf("请输入你要选择的操作: ");
}
void save(CUS* head) {
FILE* fp;
CUS* tail;
fopen_s(&fp, "record.txt", "w");
if (fp != NULL) {
fprintf(fp, "%d,%d ", a, b);
for (tail = head->next; tail != NULL; tail = tail->next) {
fprintf(fp, " %3d,%d", tail->num.room_num, tail->num.prize);
if (tail->name[0] == '\0') {
fprintf(fp, " 0");
}
else {
fprintf(fp, "%20s ", tail->name);
fprintf(fp, "%2d ", tail->sex);
fprintf(fp, "%11s ", tail->date_1);
fprintf(fp, "%11s ", tail->date_2);
fprintf(fp, "%5d", tail->spend_prize);
}
}
fclose(fp);
}
else printf("文件打开失败。");
}
CUS* file_set() {
int swich = -1;
CUS* head = NULL;
printf("是否进行初次设定(1/0)?\n若否则从记录中读取房间信息");
scanf_s(" %d", &swich);
if (swich == 1) {
printf("输入楼层数,房间数(格式A,B)");
scanf_s(" %d,%d", &a, &b);
head = first_def(a, b);
}
if (swich == 0) head = re_def();
return head;
}
void main()
{
int i;
CUS* head = NULL;
FILE* fp;
fopen_s(&fp, "record.txt", "r");
printf("*******************酒店房间登记与计费管理系统*******************\n");
if (fp == NULL) head = file_set();
while (1) {
show();
scanf_s(" %d", &i);
switch (i) {
case 1:
fun1(head);
break;
case 2:
fun2(head);
break;
case 3:
fun3(head);
break;
case 4:
save(head);
exit(0);
}
}
return;
}
void fun1(CUS* head) {
char name[20], date_in[11], date_out[11];
int sex, j = 0, k;
CUS* tail = NULL;
printf("\n输入入住旅客姓名和性别(空格隔开,1为男,-1为女):");
scanf_s(" %s", name,10);
scanf_s(" %d",&sex);
do {
printf("\n开始入住时间(格式为YYYY-MM-DD)\n");
scanf_s(" %s", date_in,11);
printf("\n退房时间(格式为YYYY-MM-DD) \n");
scanf_s(" %s", date_out,11);
k = strcmp(date_in, date_out);
if (k >= 0) printf("\n您的输入有误(退房时间要大于入住时间)\n请重新输入:");
} while (strcmp(date_in, date_out) >= 0);
printf("现有的空房间及其价格为:");
for (tail = head->next; tail != NULL; tail = tail->next) if (tail->sex == 0)
printf("%d %d/天\t", tail->num.room_num, tail->num.prize);
C:
printf("\n请选择入住房间号:");
scanf_s(" %d", &j);
for (tail = head->next; tail != NULL; tail = tail->next) if (tail->num.room_num == j) {
if (tail->sex == 0) {
printf("入住成功\n");
strcpy_s(tail->name, name);
strcpy_s(tail->date_1, date_in);
strcpy_s(tail->date_2, date_out);
tail->sex = sex;
prize_cot(tail);
system("pause");
}
else {
printf("该房间有人入住,请换房间");
goto C;
}
}
}
void fun2(CUS* head) {
char name[20];
int j = -1;
CUS* tail = NULL;
printf("共有%d个房间:\n", output(head));
printf("按姓名退房/房间号退房(1/0)?");
scanf_s(" %d", &j);
if (j == 1) {
printf("请输入客户姓名:");
scanf_s(" %s", name,10);
for (tail = head->next; tail != NULL; tail = tail->next) if (strcmp(tail->name, name) == 0) {
printf("退房成功,为%d号房间,", tail->num.room_num);
prize_cot(tail);
printf("总价为:%d元。\n", tail->spend_prize);
clear(tail);
}
}
if (j == 0) {
printf("请输入房间号:");
scanf_s(" %d", &j);
for (tail = head->next; tail != NULL; tail = tail->next) if (j == tail->num.room_num) {
printf("退房成功,客人为%s,", tail->name);
prize_cot(tail);
printf("总价为:%d元。\n", tail->spend_prize);
clear(tail);
}
}
system("pause");
}
void fun3(CUS* head) {
int index = -1, j = 0;
char name[20];
int number;
CUS* tail;
system("cls");
printf("***************请选择要查询的种类***************\n");
printf(" 1.所有房间入住信息显示\n");
printf(" 2.按照姓名查询\n");
printf(" 3.按照房号查询\n");
scanf_s(" %d", &index);
if (index == 1) printf("共有%d个房间:\n", output(head));
if (index == 2) {
printf("请输入你要查询房客的姓名:");
scanf_s(" %s", name,10);
for (tail = head->next; tail != NULL; tail = tail->next) {
if (strcmp(tail->name, name) == 0) {
printf("姓名\t性别\t房间号\t入住时间 /退房时间\t总价\n");
printf("%s\t%d\t%d\t%10s/%10s\t%d\n", tail->name, tail->sex,
tail->num.room_num, tail->date_1, tail->date_2, tail->spend_prize);
}
}
if (tail == NULL) printf("没找到。");
}
if (index == 3) {
printf("请输入你要查询的房间号:");
scanf_s(" %d", &number);
for (tail = head->next; tail != NULL; tail = tail->next) {
if (tail->num.room_num == number) {
if (tail->sex != 0) {
printf("姓名\t性别\t房间号\t入住时间 /退房时间\t总价\n");
printf("%s\t%d\t%d\t%10s/%10s\t%d\n", tail->name, tail->sex,
tail->num.room_num, tail->date_1, tail->date_2, tail->spend_prize);
}
else printf("房内无人入住。房价为%d/天", tail->num.prize);
}
}
}
printf("\n还要继续查找?(1/0)");
scanf_s(" %d", &j);
if (j == 1) fun3(head);
system("pause");
}