超市仓库管理系统结构体
时间: 2023-12-18 22:29:09 浏览: 34
以下是一个简单的超市仓库管理系统结构体的例子:
```c
#include <stdio.h>
#include <string.h>
#define MAX_PRODUCTS 100
struct Product {
char name[50];
int id;
float price;
int quantity;
};
struct Warehouse {
struct Product products[MAX_PRODUCTS];
int num_products;
};
void add_product(struct Warehouse *warehouse, struct Product product) {
if (warehouse->num_products >= MAX_PRODUCTS) {
printf("Warehouse is full!\n");
return;
}
warehouse->products[warehouse->num_products] = product;
warehouse->num_products++;
}
void print_products(struct Warehouse warehouse) {
printf("Products in warehouse:\n");
for (int i = 0; i < warehouse.num_products; i++) {
printf("%d. %s (ID: %d) - $%.2f - %d in stock\n", i+1, warehouse.products[i].name, warehouse.products[i].id, warehouse.products[i].price, warehouse.products[i].quantity); }
}
void search_product(struct Warehouse warehouse, int id) {
for (int i = 0; i < warehouse.num_products; i++) {
if (warehouse.products[i].id == id) {
printf("%s (ID: %d) - $%.2f - %d in stock\n", warehouse.products[i].name, warehouse.products[i].id, warehouse.products[i].price, warehouse.products[i].quantity);
return;
}
}
printf("Product not found!\n");
}
void modify_product(struct Warehouse *warehouse, int id, struct Product new_product) {
for (int i = 0; i < warehouse->num_products; i++) {
if (warehouse->products[i].id == id) {
warehouse->products[i] = new_product;
return;
}
}
printf("Product not found!\n");
}
void delete_product(struct Warehouse *warehouse, int id) {
for (int i = 0; i < warehouse->num_products; i++) {
if (warehouse->products[i].id == id) {
for (int j = i; j < warehouse->num_products-1; j++) {
warehouse->products[j] = warehouse->products[j+1];
}
warehouse->num_products--;
return;
}
}
printf("Product not found!\n");
}
void insert_product(struct Warehouse *warehouse, int index, struct Product product) {
if (warehouse->num_products >= MAX_PRODUCTS) {
printf("Warehouse is full!\n");
return;
}
if (index < 0 || index > warehouse->num_products) {
printf("Invalid index!\n");
return;
}
for (int i = warehouse->num_products; i > index; i--) {
warehouse->products[i] = warehouse->products[i-1];
}
warehouse->products[index] = product;
warehouse->num_products++;
}
void sort_products(struct Warehouse *warehouse) {
for (int i = 0; i < warehouse->num_products-1; i++) {
for (int j = 0; j < warehouse->num_products-i-1; j++) {
if (warehouse->products[j].quantity < warehouse->products[j+1].quantity) {
struct Product temp = warehouse->products[j];
warehouse->products[j] = warehouse->products[j+1];
warehouse->products[j+1] = temp;
}
}
}
}
int main() {
struct Warehouse warehouse = {0};
int choice;
do {
printf("\n1. Add product\n");
printf("2. Print products\n");
printf("3. Search product\n");
printf("4. Modify product\n");
printf("5. Delete product\n");
printf("6. Insert product\n");
printf("7. Sort products by quantity\n");
printf("0. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
struct Product product;
printf("Enter product name: ");
scanf("%s", product.name);
printf("Enter product ID: ");
scanf("%d", &product.id);
printf("Enter product price: ");
scanf("%f", &product.price);
printf("Enter product quantity: ");
scanf("%d", &product.quantity);
add_product(&warehouse, product);
break;
}
case 2: {
print_products(warehouse);
break;
}
case 3: {
int id;
printf("Enter product ID: ");
scanf("%d", &id);
search_product(warehouse, id);
break;
}
case 4: {
int id;
printf("Enter product ID: ");
scanf("%d", &id);
struct Product new_product;
printf("Enter new product name: ");
scanf("%s", new_product.name);
printf("Enter new product ID: ");
scanf("%d", &new_product.id);
printf("Enter new product price: ");
scanf("%f", &new_product.price);
printf("Enter new product quantity: ");
scanf("%d", &new_product.quantity);
modify_product(&warehouse, id, new_product);
break;
}
case 5: {
int id;
printf("Enter product ID: ");
scanf("%d", &id);
delete_product(&warehouse, id);
break;
}
case 6: {
int index;
printf("Enter index to insert at: ");
scanf("%d", &index);
struct Product product;
printf("Enter product name: ");
scanf("%s", product.name);
printf("Enter product ID: ");
scanf("%d", &product.id);
printf("Enter product price: ");
scanf("%f", &product.price);
printf("Enter product quantity: ");
scanf("%d", &product.quantity);
insert_product(&warehouse, index, product);
break;
}
case 7: {
sort_products(&warehouse);
break;
}
case 0: {
printf("Exiting...\n");
break;
}
default: {
printf("Invalid choice!\n");
break;
}
}
} while (choice != 0);
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)