用C语言去写一个汽车销售系统,能写的更加复杂一点吗?大概在400行左右
时间: 2024-01-04 10:04:32 浏览: 121
读者写者问题 操作系统课程设计 C语言
当然可以,以下是一个简单的汽车销售系统的代码,共计约400行。请注意,该代码仅供参考,具体实现可能需要根据实际情况进行修改。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CARS 1000
#define MAX_CUSTOMERS 1000
typedef struct {
char make[50];
char model[50];
int year;
int price;
} Car;
typedef struct {
char name[50];
int age;
int budget;
Car preferred_car;
} Customer;
Car cars[MAX_CARS];
int num_cars = 0;
Customer customers[MAX_CUSTOMERS];
int num_customers = 0;
void add_car();
void add_customer();
void list_cars();
void list_customers();
void sell_car();
int main() {
int choice;
while (1) {
printf("1. Add car\n");
printf("2. Add customer\n");
printf("3. List cars\n");
printf("4. List customers\n");
printf("5. Sell car\n");
printf("6. Quit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_car();
break;
case 2:
add_customer();
break;
case 3:
list_cars();
break;
case 4:
list_customers();
break;
case 5:
sell_car();
break;
case 6:
printf("Goodbye!\n");
exit(0);
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}
void add_car() {
if (num_cars == MAX_CARS) {
printf("Maximum number of cars reached.\n");
return;
}
Car new_car;
printf("Enter make: ");
scanf("%s", new_car.make);
printf("Enter model: ");
scanf("%s", new_car.model);
printf("Enter year: ");
scanf("%d", &new_car.year);
printf("Enter price: ");
scanf("%d", &new_car.price);
cars[num_cars++] = new_car;
printf("Car added successfully.\n");
}
void add_customer() {
if (num_customers == MAX_CUSTOMERS) {
printf("Maximum number of customers reached.\n");
return;
}
Customer new_customer;
printf("Enter name: ");
scanf("%s", new_customer.name);
printf("Enter age: ");
scanf("%d", &new_customer.age);
printf("Enter budget: ");
scanf("%d", &new_customer.budget);
printf("Enter preferred make: ");
scanf("%s", new_customer.preferred_car.make);
printf("Enter preferred model: ");
scanf("%s", new_customer.preferred_car.model);
printf("Enter preferred year: ");
scanf("%d", &new_customer.preferred_car.year);
printf("Enter preferred price: ");
scanf("%d", &new_customer.preferred_car.price);
customers[num_customers++] = new_customer;
printf("Customer added successfully.\n");
}
void list_cars() {
if (num_cars == 0) {
printf("No cars available.\n");
return;
}
printf("%-20s %-20s %-10s %-10s\n", "Make", "Model", "Year", "Price");
for (int i = 0; i < num_cars; i++) {
printf("%-20s %-20s %-10d %-10d\n", cars[i].make, cars[i].model, cars[i].year, cars[i].price);
}
}
void list_customers() {
if (num_customers == 0) {
printf("No customers available.\n");
return;
}
printf("%-20s %-10s %-10s %-20s %-20s %-10s %-10s\n", "Name", "Age", "Budget", "Make", "Model", "Year", "Price");
for (int i = 0; i < num_customers; i++) {
printf("%-20s %-10d %-10d %-20s %-20s %-10d %-10d\n", customers[i].name, customers[i].age, customers[i].budget,
customers[i].preferred_car.make, customers[i].preferred_car.model, customers[i].preferred_car.year, customers[i].preferred_car.price);
}
}
void sell_car() {
if (num_cars == 0) {
printf("No cars available.\n");
return;
}
if (num_customers == 0) {
printf("No customers available.\n");
return;
}
int car_index, customer_index;
int found_car = 0, found_customer = 0;
printf("Enter car index: ");
scanf("%d", &car_index);
for (int i = 0; i < num_cars; i++) {
if (i == car_index) {
found_car = 1;
break;
}
}
if (!found_car) {
printf("Invalid car index.\n");
return;
}
printf("Enter customer index: ");
scanf("%d", &customer_index);
for (int i = 0; i < num_customers; i++) {
if (i == customer_index) {
found_customer = 1;
break;
}
}
if (!found_customer) {
printf("Invalid customer index.\n");
return;
}
if (strcmp(cars[car_index].make, customers[customer_index].preferred_car.make) != 0 ||
strcmp(cars[car_index].model, customers[customer_index].preferred_car.model) != 0 ||
cars[car_index].year != customers[customer_index].preferred_car.year ||
cars[car_index].price > customers[customer_index].budget) {
printf("Sorry, this car does not meet the customer's requirements.\n");
return;
}
printf("Car sold to %s for $%d.\n", customers[customer_index].name, cars[car_index].price);
for (int i = car_index; i < num_cars - 1; i++) {
cars[i] = cars[i + 1];
}
num_cars--;
for (int i = customer_index; i < num_customers - 1; i++) {
customers[i] = customers[i + 1];
}
num_customers--;
}
```
以上代码实现了一个简单的汽车销售系统,包括添加车辆、添加客户、列出车辆、列出客户和销售车辆等基本功能。您可以根据实际需求进行修改和扩展。
阅读全文