“To become a master, it is not achieved overnight, unless one is a natural talent in martial arts, but such people… are one in a million.”
—— LandladyThis principle also applies to learning C language. There are indeed few people with extraordinary talent in programming; most of us need to go through continuous learning to progress from a beginner in C language to an expert.So how do we learn?Of course, by practicing a C language problem every day!
Author
Yan Xiaolin
Working during the day, dreaming at night. I have stories, do you have wine?
Example 48: Implementation of a hotel room management system in C language.
Source code demonstration:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <time.h>
#define MAX_ROOMS 100#define MAX_NAME_LENGTH 50
// Room status enumeration
typedef enum { AVAILABLE, // Available RESERVED, // Reserved OCCUPIED, // Occupied MAINTENANCE // Under maintenance} RoomStatus;
// Room structure
typedef struct { int roomNumber; RoomStatus status; float price; char guestName[MAX_NAME_LENGTH]; char checkInDate[20]; char checkOutDate[20];} Room;
// Global variables
Room rooms[MAX_ROOMS];int totalRooms = 0;float totalIncome = 0.0;
// Initialize rooms
void initializeRooms() { for (int i = 0; i < MAX_ROOMS; i++) { rooms[i].roomNumber = i + 1; rooms[i].status = AVAILABLE; rooms[i].price = 200.0 + (i % 5) * 50.0; // Prices range from 200 to 400 strcpy(rooms[i].guestName, ""); strcpy(rooms[i].checkInDate, ""); strcpy(rooms[i].checkOutDate, ""); } totalRooms = MAX_ROOMS;}
// Display all room statuses
void displayRooms() { printf("\nRoom Status Overview:\n"); printf("Room No.\tStatus\t\tPrice\t\tGuest Name\n"); printf("------------------------------------------------\n");
for (int i = 0; i < totalRooms; i++) { char statusStr[20]; switch (rooms[i].status) { case AVAILABLE: strcpy(statusStr, "Available"); break; case RESERVED: strcpy(statusStr, "Reserved"); break; case OCCUPIED: strcpy(statusStr, "Occupied"); break; case MAINTENANCE: strcpy(statusStr, "Under maintenance"); break; }
printf("%d\t%s\t%.2f\t\t%s\n", rooms[i].roomNumber, statusStr, rooms[i].price, rooms[i].guestName); }}
// Reserve a room
void reserveRoom() { int roomNumber; char guestName[MAX_NAME_LENGTH]; char checkInDate[20];
printf("\nPlease enter the room number to reserve: "); scanf("%d", &roomNumber);
if (roomNumber < 1 || roomNumber > totalRooms) { printf("Invalid room number!\n"); return; }
if (rooms[roomNumber-1].status != AVAILABLE) { printf("This room is currently not available for reservation!\n"); return; }
printf("Please enter guest name: "); scanf("%s", guestName); printf("Please enter check-in date (YYYY-MM-DD): "); scanf("%s", checkInDate);
rooms[roomNumber-1].status = RESERVED; strcpy(rooms[roomNumber-1].guestName, guestName); strcpy(rooms[roomNumber-1].checkInDate, checkInDate);
printf("Room reserved successfully!\n");}
// Check-in
void checkIn() { int roomNumber;
printf("\nPlease enter the room number to check in: "); scanf("%d", &roomNumber);
if (roomNumber < 1 || roomNumber > totalRooms) { printf("Invalid room number!\n"); return; }
if (rooms[roomNumber-1].status != RESERVED) { printf("This room is currently not available for check-in!\n"); return; }
rooms[roomNumber-1].status = OCCUPIED; printf("Check-in successful!\n");}
// Check-out
void checkOut() { int roomNumber; char checkOutDate[20];
printf("\nPlease enter the room number to check out: "); scanf("%d", &roomNumber);
if (roomNumber < 1 || roomNumber > totalRooms) { printf("Invalid room number!\n"); return; }
if (rooms[roomNumber-1].status != OCCUPIED) { printf("This room currently has no guests!\n"); return; }
printf("Please enter check-out date (YYYY-MM-DD): "); scanf("%s", checkOutDate);
// Calculate income totalIncome += rooms[roomNumber-1].price;
// Reset room status rooms[roomNumber-1].status = AVAILABLE; strcpy(rooms[roomNumber-1].guestName, ""); strcpy(rooms[roomNumber-1].checkInDate, ""); strcpy(rooms[roomNumber-1].checkOutDate, "");
printf("Check-out successful!\n");}
// Display income statistics
void displayIncome() { printf("\nCurrent total income: %.2f\n", totalIncome);}
// Main menu
void displayMenu() { printf("\n===== Hotel Room Management System =====\n"); printf("1. Display all room statuses\n"); printf("2. Reserve a room\n"); printf("3. Check-in\n"); printf("4. Check-out\n"); printf("5. Display income statistics\n"); printf("0. Exit system\n"); printf("Please select an operation: ");}
int main() { int choice;
// Initialize rooms initializeRooms();
while (1) { displayMenu(); scanf("%d", &choice);
switch (choice) { case 1: displayRooms(); break; case 2: reserveRoom(); break; case 3: checkIn(); break; case 4: checkOut(); break; case 5: displayIncome(); break; case 0: printf("Thank you for using the hotel management system!\n"); return 0; default: printf("Invalid choice, please try again!\n"); } }
return 0;}
1. Basic Functions
Manage 100 hotel rooms: You can view room statuses, reserve rooms, check in, and check out.
Record income: The system automatically calculates all income generated from check-outs.
Simple menu operations: Select different functions by number.
2. Room Information
Each room has a number (1-100), status (available/reserved/occupied/under maintenance), and price (ranging from 200 to 400 yuan)
It also records the guest’s name, check-in, and check-out dates.
3. How to Use
For example, if you want to reserve a room:
First, select 1 to see which rooms are available
Then select 2 to reserve, entering the room number, your name, and check-in date
On the day of arrival, select 3 to check in
When leaving, select 4 to check out
The hotel manager can select 5 at any time to view total income
The compilation and execution results are as follows:Follow the public account below, reply with 1, and you will be added to a programming community of 18,000 people for free, plus receive 100 C language source code examples, including Snake, Tetris, heart-shaped confession, Christmas tree source code~Learn C/C++ without getting lost