用c语言写房屋租赁管理系统的代码
时间: 2023-03-26 14:02:30 浏览: 159
C#语言编写 房屋出租管理系统
很高兴回答你的问题。以下是一个简单的房屋租赁管理系统的 C 语言代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ROOMS 100
struct Room {
int id;
char address[50];
int rent;
int occupied;
};
struct Room rooms[MAX_ROOMS];
int numRooms = ;
void addRoom() {
if (numRooms >= MAX_ROOMS) {
printf("Error: Maximum number of rooms reached.\n");
return;
}
struct Room newRoom;
newRoom.id = numRooms + 1;
printf("Enter address: ");
scanf("%s", newRoom.address);
printf("Enter rent: ");
scanf("%d", &newRoom.rent);
newRoom.occupied = ;
rooms[numRooms] = newRoom;
numRooms++;
printf("Room added successfully.\n");
}
void listRooms() {
printf("ID\tAddress\t\tRent\tOccupied\n");
for (int i = ; i < numRooms; i++) {
printf("%d\t%s\t%d\t%s\n", rooms[i].id, rooms[i].address, rooms[i].rent, rooms[i].occupied ? "Yes" : "No");
}
}
void rentRoom() {
int roomId;
printf("Enter room ID: ");
scanf("%d", &roomId);
if (roomId < 1 || roomId > numRooms) {
printf("Error: Invalid room ID.\n");
return;
}
if (rooms[roomId - 1].occupied) {
printf("Error: Room is already occupied.\n");
return;
}
rooms[roomId - 1].occupied = 1;
printf("Room rented successfully.\n");
}
void returnRoom() {
int roomId;
printf("Enter room ID: ");
scanf("%d", &roomId);
if (roomId < 1 || roomId > numRooms) {
printf("Error: Invalid room ID.\n");
return;
}
if (!rooms[roomId - 1].occupied) {
printf("Error: Room is not occupied.\n");
return;
}
rooms[roomId - 1].occupied = ;
printf("Room returned successfully.\n");
}
int main() {
int choice;
while (1) {
printf("\n");
printf("1. Add room\n");
printf("2. List rooms\n");
printf("3. Rent room\n");
printf("4. Return room\n");
printf("5. Exit\n");
printf("\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addRoom();
break;
case 2:
listRooms();
break;
case 3:
rentRoom();
break;
case 4:
returnRoom();
break;
case 5:
exit();
default:
printf("Error: Invalid choice.\n");
}
}
return ;
}
阅读全文