用c语言写一个商品管理系统,实现的功能包括商品的添加、修改、删除、查询、商品排序以及对商品信息的展示等
时间: 2024-01-03 17:05:41 浏览: 30
这是一个基本的商品管理系统的C语言代码:
```
#include <stdio.h>
#include <string.h>
#define MAX_PRODUCTS 100
struct Product {
char name[50];
int id;
float price;
};
struct Product products[MAX_PRODUCTS];
int numProducts = 0;
void addProduct() {
struct Product p;
printf("Enter product name: ");
scanf("%s", p.name);
printf("Enter product ID: ");
scanf("%d", &p.id);
printf("Enter product price: ");
scanf("%f", &p.price);
products[numProducts++] = p;
printf("Product added successfully!\n");
}
void modifyProduct() {
int id, i;
printf("Enter product ID to modify: ");
scanf("%d", &id);
for (i = 0; i < numProducts; i++) {
if (products[i].id == id) {
printf("Enter new product name: ");
scanf("%s", products[i].name);
printf("Enter new product price: ");
scanf("%f", &products[i].price);
printf("Product modified successfully!\n");
return;
}
}
printf("Product not found!\n");
}
void deleteProduct() {
int id, i, j;
printf("Enter product ID to delete: ");
scanf("%d", &id);
for (i = 0; i < numProducts; i++) {
if (products[i].id == id) {
for (j = i; j < numProducts - 1; j++) {
products[j] = products[j + 1];
}
numProducts--;
printf("Product deleted successfully!\n");
return;
}
}
printf("Product not found!\n");
}
void searchProduct() {
int id, i;
printf("Enter product ID to search: ");
scanf("%d", &id);
for (i = 0; i < numProducts; i++) {
if (products[i].id == id) {
printf("Product found: %s, %d, %.2f\n", products[i].name, products[i].id, products[i].price);
return;
}
}
printf("Product not found!\n");
}
void showProducts() {
int i;
printf("Product list:\n");
for (i = 0; i < numProducts; i++) {
printf("%s, %d, %.2f\n", products[i].name, products[i].id, products[i].price);
}
}
void sortProducts() {
int i, j;
struct Product temp;
for (i = 0; i < numProducts - 1; i++) {
for (j = i + 1; j < numProducts; j++) {
if (products[i].price > products[j].price) {
temp = products[i];
products[i] = products[j];
products[j] = temp;
}
}
}
printf("Products sorted successfully!\n");
}
int main() {
int choice;
while (1) {
printf("\n1. Add product\n");
printf("2. Modify product\n");
printf("3. Delete product\n");
printf("4. Search product\n");
printf("5. Show products\n");
printf("6. Sort products\n");
printf("7. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addProduct();
break;
case 2:
modifyProduct();
break;
case 3:
deleteProduct();
break;
case 4:
searchProduct();
break;
case 5:
showProducts();
break;
case 6:
sortProducts();
break;
case 7:
return 0;
default:
printf("Invalid choice!\n");
}
}
}
```
该代码使用了结构体来存储商品信息,包括名称、编号和价格。用户可以选择添加、修改、删除、查询、排序或展示商品信息。该程序还使用了循环来允许用户进行多次操作,直到选择退出程序。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)