C语言酒店管理系统账单计算函数始终按200元房价计算问题求助
问题原因与修复方案
1. 核心逻辑错误:条件判断误用赋值运算符
你在getBill()函数的所有if判断中,都使用了赋值运算符=而非相等判断运算符==:
// 错误写法:属于赋值操作,返回值永远为真,所以每次都会优先进入第一个判断分支 if(t = 1)
赋值操作的结果是被赋予的值,因此t=1的结果永远是1(真值),导致程序每次都直接按200美元的标准间计算,不会走到后面的房型判断分支。
2. 结构体定义类型不匹配错误
你定义的struct Customer中room_type被声明为int*指针类型,但实际使用中你直接把输入的整型房型编号存到了这个指针变量里,属于类型不匹配的错误,会带来不可预期的运行异常:
// 原错误定义 struct Customer { // 其他字段省略 int *room_type; // 此处不该定义为指针类型 };
将其改为普通int类型即可正常存储房型编号。
修正后可正常运行的完整代码
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> #include<conio.h> // 客户身份结构体 struct Identity { char name_first[50]; char name_last[50]; int phone; int IC[10]; char email[50]; int age; }; // 日期结构体 struct Date { int day; int month; int year; }; // 客户全信息结构体 struct Customer { struct Identity iden; int room_number; int room_type; // 修正:将指针改为普通int类型存储房型编号 int bill; struct Date check_in; struct Date check_out; int day_num; int cleaning; }; const int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 计算闰年数量 int countLeapYear(struct Date d) { int years = d.year; if(d.month <= 2) { years--; } return years / 4 - years / 100 + years / 400; } // 计算入住总天数 int getDays(struct Date d1, struct Date d2) { long int n1 = d1.year * 365 + d1.day; for(int i = 0; i < d1.month - 1; i++) { n1 += monthDays[i]; } n1 += countLeapYear(d1); long int n2 = d2.year * 365 + d2.day; for(int i = 0; i < d2.month - 1; i++) { n2 += monthDays[i]; } n2 += countLeapYear(d2); return (n2 - n1); } // 计算账单金额:修正所有if判断的赋值运算符为相等判断运算符 int getBill(int days, int type, int num_of_room) { int t = type; if(t == 1) { printf("\n200\n"); return 200 * days * num_of_room; } else if(t == 2) { printf("500\n"); return 500 * days * num_of_room; } else if(t == 3) { printf("1200\n"); return 1200 * days * num_of_room; } else { return 0; } } int main() { int n = 0; printf("Input number of customers:\n"); scanf("%d", &n); struct Customer cust[n]; int num_of_room = 0; printf("\n***Please choose your room type***\n"); printf("How many room do you want?\n"); scanf("%d", &num_of_room); printf("Which type of room do you want?\n"); printf("1.Standard Room ($200/night)\n"); printf("2.Deluxe Room ($500/night)\n"); printf("3.Ocean view room ($1200/night)\n"); printf("Input the type of room number (1,2,3):\n"); scanf("%d", &cust->room_type); int type = cust->room_type; printf("***Please Confirm The Length of Your Stay***\n"); printf("Input check-in date (DD,MM,YYYY):\n"); scanf("%d,%d,%d", &cust->check_in.day, &cust->check_in.month, &cust->check_in.year); printf("\nInput check-out date (DD,MM,YYYY):\n"); scanf("%d,%d,%d", &cust->check_out.day, &cust->check_out.month, &cust->check_out.year); struct Date D1 = cust->check_in, D2 = cust->check_out; int days = getDays(D1, D2); printf("The total days you are staying is %d days, type of room %d, number of room %d", days, type, num_of_room); cust->bill = getBill(days, type, num_of_room); printf("\nYou need to pay for $%d", cust->bill); return 0; }
内容的提问来源于stack exchange,提问作者Maria Marchella Gandhi
相关产品推荐
相关产品推荐

