帮我写一个c语言代码已知一银行有 8—10 个客户(客户信息为:编号、客户姓名、存款金额、取款金额)。设计一程序, 完成以下功能: 1) 系统以菜单方式工作(用键盘输入1~7之间的数来选择功能,其中7为退出); 2) 银行账户的录入功能(编号不能重复); 3) 能从指定菜单选项中读取客户所有的信息; 4) 查询指定客户“编号”的存款记录; 5) 分别统计并显示“存款金额”和“取款金额”累计总额信息; 6) 按输入“编号”修改取款金额; 7) 按照“存款金额”升序排序; 8) 删除指定客户名的信息; 9) 所有数据符合实际应用要求。
时间: 2023-04-01 08:00:30 浏览: 49
以下是C语言代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CUSTOMERS 10
struct customer {
int id;
char name[20];
float deposit;
float withdrawal;
};
struct customer customers[MAX_CUSTOMERS];
int num_customers = ;
void add_customer() {
if (num_customers >= MAX_CUSTOMERS) {
printf("银行客户已满,无法添加新客户。\n");
return;
}
struct customer new_customer;
printf("请输入客户编号:");
scanf("%d", &new_customer.id);
for (int i = ; i < num_customers; i++) {
if (customers[i].id == new_customer.id) {
printf("客户编号已存在,请重新输入。\n");
return;
}
}
printf("请输入客户姓名:");
scanf("%s", new_customer.name);
printf("请输入客户存款金额:");
scanf("%f", &new_customer.deposit);
printf("请输入客户取款金额:");
scanf("%f", &new_customer.withdrawal);
customers[num_customers++] = new_customer;
printf("客户信息已添加。\n");
}
void print_all_customers() {
printf("客户编号\t客户姓名\t存款金额\t取款金额\n");
for (int i = ; i < num_customers; i++) {
printf("%d\t\t%s\t\t%.2f\t\t%.2f\n", customers[i].id, customers[i].name, customers[i].deposit, customers[i].withdrawal);
}
}
void search_customer_by_id() {
int id;
printf("请输入要查询的客户编号:");
scanf("%d", &id);
for (int i = ; i < num_customers; i++) {
if (customers[i].id == id) {
printf("客户编号\t客户姓名\t存款金额\t取款金额\n");
printf("%d\t\t%s\t\t%.2f\t\t%.2f\n", customers[i].id, customers[i].name, customers[i].deposit, customers[i].withdrawal);
return;
}
}
printf("未找到客户编号为 %d 的客户。\n", id);
}
void calculate_total_deposit_and_withdrawal() {
float total_deposit = ., total_withdrawal = .;
for (int i = ; i < num_customers; i++) {
total_deposit += customers[i].deposit;
total_withdrawal += customers[i].withdrawal;
}
printf("存款金额累计总额为:%.2f\n", total_deposit);
printf("取款金额累计总额为:%.2f\n", total_withdrawal);
}
void modify_withdrawal_by_id() {
int id;
printf("请输入要修改取款金额的客户编号:");
scanf("%d", &id);
for (int i = ; i < num_customers; i++) {
if (customers[i].id == id) {
printf("请输入新的取款金额:");
scanf("%f", &customers[i].withdrawal);
printf("取款金额已修改。\n");
return;
}
}
printf("未找到客户编号为 %d 的客户。\n", id);
}
void sort_by_deposit() {
for (int i = ; i < num_customers - 1; i++) {
for (int j = i + 1; j < num_customers; j++) {
if (customers[i].deposit > customers[j].deposit) {
struct customer temp = customers[i];
customers[i] = customers[j];
customers[j] = temp;
}
}
}
printf("按存款金额升序排序完成。\n");
}
void delete_customer_by_name() {
char name[20];
printf("请输入要删除的客户姓名:");
scanf("%s", name);
for (int i = ; i < num_customers; i++) {
if (strcmp(customers[i].name, name) == ) {
for (int j = i; j < num_customers - 1; j++) {
customers[j] = customers[j + 1];
}
num_customers--;
printf("客户 %s 的信息已删除。\n", name);
return;
}
}
printf("未找到姓名为 %s 的客户。\n", name);
}
int main() {
int choice;
while (1) {
printf("\n请选择以下功能:\n");
printf("1. 银行账户的录入\n");
printf("2. 读取客户所有的信息\n");
printf("3. 查询指定客户编号的存款记录\n");
printf("4. 统计存款金额和取款金额累计总额\n");
printf("5. 修改指定客户编号的取款金额\n");
printf("6. 按存款金额升序排序\n");
printf("7. 删除指定客户名的信息\n");
printf("8. 退出\n");
printf("请输入选择的功能编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_customer();
break;
case 2:
print_all_customers();
break;
case 3:
search_customer_by_id();
break;
case 4:
calculate_total_deposit_and_withdrawal();
break;
case 5:
modify_withdrawal_by_id();
break;
case 6:
sort_by_deposit();
break;
case 7:
delete_customer_by_name();
break;
case 8:
printf("谢谢使用,再见!\n");
exit();
default:
printf("无效的选择,请重新输入。\n");
break;
}
}
return ;
}