能不能加个收取租金的功能
时间: 2023-10-26 10:18:32 浏览: 48
当然可以,在前面的收租系统代码基础上,我们可以添加收取租金的功能。以下是一个示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义租户信息结构体
struct Tenant {
char name[20]; // 姓名
int room_num; // 房间号
float rent; // 租金
float paid; // 已付租金
};
// 定义租户信息数组
struct Tenant tenants[10];
// 定义租户数量变量
int num_tenants = 0;
// 添加租户函数
void addTenant() {
if (num_tenants >= 10) {
printf("租户数量已达上限,无法添加新租户!\n");
return;
}
struct Tenant new_tenant;
printf("请输入租户姓名:");
scanf("%s", new_tenant.name);
printf("请输入租户房间号:");
scanf("%d", &new_tenant.room_num);
printf("请输入租户租金:");
scanf("%f", &new_tenant.rent);
new_tenant.paid = 0;
tenants[num_tenants] = new_tenant;
num_tenants++;
printf("租户添加成功!\n");
}
// 删除租户函数
void deleteTenant() {
int room_num;
printf("请输入要删除的租户房间号:");
scanf("%d", &room_num);
int i;
for (i = 0; i < num_tenants; i++) {
if (tenants[i].room_num == room_num) {
int j;
for (j = i; j < num_tenants - 1; j++) {
tenants[j] = tenants[j+1];
}
num_tenants--;
printf("租户删除成功!\n");
return;
}
}
printf("未找到该租户!\n");
}
// 显示租户信息函数
void showTenants() {
int i;
printf("租户姓名\t房间号\t租金\t已付租金\n");
for (i = 0; i < num_tenants; i++) {
printf("%s\t\t%d\t%.2f\t%.2f\n", tenants[i].name, tenants[i].room_num, tenants[i].rent, tenants[i].paid);
}
}
// 收取租金函数
void collectRent() {
int room_num;
printf("请输入要收取租金的租户房间号:");
scanf("%d", &room_num);
int i;
for (i = 0; i < num_tenants; i++) {
if (tenants[i].room_num == room_num) {
float amount;
printf("请输入要收取的租金金额:");
scanf("%f", &amount);
tenants[i].paid += amount;
printf("租金收取成功!\n");
return;
}
}
printf("未找到该租户!\n");
}
// 主函数
int main() {
int choice;
while (1) {
printf("请选择操作:\n");
printf("1.添加租户\t2.删除租户\t3.显示租户信息\t4.收取租金\t5.退出\n");
scanf("%d", &choice);
switch (choice) {
case 1:
addTenant();
break;
case 2:
deleteTenant();
break;
case 3:
showTenants();
break;
case 4:
collectRent();
break;
case 5:
exit(0);
default:
printf("无效的选择!\n");
}
}
return 0;
}
```
在这个示例代码中,我们添加了一个收取租金的功能,包括输入要收取租金的租户房间号以及租金金额。在租户信息结构体中,我们添加了一个已付租金的属性,用于记录租户已经付了多少租金。在收取租金函数中,我们找到对应的租户,并将收取的租金金额加到已付租金中。在显示租户信息函数中,我们添加了一个已付租金的列,用于显示租户已经付了多少租金。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)