用C语言写一个客户订单信息管理系统
时间: 2023-07-18 11:11:46 浏览: 49
好的,客户订单信息管理系统可以采用基本的CRUD操作,包括创建(Create)、读取(Read)、更新(Update)、删除(Delete)四个操作。以下是一个用C语言实现的简单示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CUSTOMERS 100
struct customer {
char name[50];
char address[100];
char phone[20];
};
struct customer customers[MAX_CUSTOMERS];
int num_customers = 0;
void print_menu();
void create_customer();
void read_customers();
void update_customer();
void delete_customer();
int main() {
int choice;
do {
print_menu();
scanf("%d", &choice);
switch (choice) {
case 1:
create_customer();
break;
case 2:
read_customers();
break;
case 3:
update_customer();
break;
case 4:
delete_customer();
break;
case 5:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 5);
return 0;
}
void print_menu() {
printf("\n");
printf("CUSTOMER ORDER MANAGEMENT SYSTEM\n");
printf("================================\n");
printf("1. Add a new customer\n");
printf("2. View all customers\n");
printf("3. Update customer information\n");
printf("4. Delete a customer\n");
printf("5. Quit\n");
printf("Enter your choice: ");
}
void create_customer() {
if (num_customers == MAX_CUSTOMERS) {
printf("Maximum number of customers reached.\n");
return;
}
printf("Enter customer name: ");
scanf("%s", customers[num_customers].name);
printf("Enter customer address: ");
scanf("%s", customers[num_customers].address);
printf("Enter customer phone number: ");
scanf("%s", customers[num_customers].phone);
num_customers++;
printf("Customer added successfully.\n");
}
void read_customers() {
if (num_customers == 0) {
printf("No customers found.\n");
return;
}
printf("%-20s %-50s %-20s\n", "Name", "Address", "Phone");
printf("====================================================================\n");
for (int i = 0; i < num_customers; i++) {
printf("%-20s %-50s %-20s\n", customers[i].name, customers[i].address, customers[i].phone);
}
}
void update_customer() {
if (num_customers == 0) {
printf("No customers found.\n");
return;
}
char name[50];
printf("Enter customer name to update: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_customers; i++) {
if (strcmp(customers[i].name, name) == 0) {
printf("Enter new address: ");
scanf("%s", customers[i].address);
printf("Enter new phone number: ");
scanf("%s", customers[i].phone);
found = 1;
break;
}
}
if (found) {
printf("Customer information updated successfully.\n");
} else {
printf("Customer not found.\n");
}
}
void delete_customer() {
if (num_customers == 0) {
printf("No customers found.\n");
return;
}
char name[50];
printf("Enter customer name to delete: ");
scanf("%s", name);
int found = 0;
for (int i = 0; i < num_customers; i++) {
if (strcmp(customers[i].name, name) == 0) {
for (int j = i+1; j < num_customers; j++) {
strcpy(customers[j-1].name, customers[j].name);
strcpy(customers[j-1].address, customers[j].address);
strcpy(customers[j-1].phone, customers[j].phone);
}
num_customers--;
printf("Customer deleted successfully.\n");
found = 1;
break;
}
}
if (!found) {
printf("Customer not found.\n");
}
}
```
这个程序使用了一个`struct`来表示每个客户的信息,并且使用一个数组`customers`来存储所有客户的信息。主程序使用一个`do-while`循环来实现菜单功能,根据用户输入的选择调用不同的函数来实现不同的操作。`create_customer`函数可以添加新客户,`read_customers`函数可以查看所有客户,`update_customer`函数可以更新客户信息,`delete_customer`函数可以删除客户信息。