c语言数据结构实现小商品信息管理系统实现增加商品信息,删除商品记录,修改商品信息,购买管理,查询商品信息,商品排序,显示商品信息等功能并在其后注释作用,其中排序功能采用冒泡排序,快速排序,直接插入排序三种方法。
时间: 2024-06-14 12:06:10 浏览: 12
以下是使用C语言实现小商品信息管理系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义商品结构体
typedef struct {
char name[50];
int id;
char manufacturer[50];
int stock;
float price;
} Product;
// 声明全局变量
Product products[100];
int count = 0;
// 增加商品信息
void addProduct() {
printf("请输入商品名称:");
scanf("%s", products[count].name);
printf("请输入商品编号:");
scanf("%d", &products[count].id);
printf("请输入商品厂家:");
scanf("%s", products[count].manufacturer);
printf("请输入商品库存量:");
scanf("%d", &products[count].stock);
printf("请输入商品售价:");
scanf("%f", &products[count].price);
count++;
printf("商品信息添加成功!n");
}
// 删除商品记录
void deleteProduct() {
int id;
printf("请输入要删除的商品编号:");
scanf("%d", &id);
int i;
for (i = 0; i < count; i++) {
if (products[i].id == id) {
int j;
for (j = i; j < count - 1; j++) {
products[j] = products[j + 1];
}
count--;
printf("商品记录删除成功!\n");
return;
}
}
printf("未找到该商品编号的记录!\n");
}
// 修改商品信息
void modifyProduct() {
int id;
printf("请输入要修改的商品编号:");
scanf("%d", &id);
int i;
for (i = 0; i < count; i++) {
if (products[i].id == id) {
printf("请输入新的商品名称:");
scanf("%s", products[i].name);
printf("请输入新的商品厂家:");
scanf("%s", products[i].manufacturer);
printf("请输入新的商品库存量:");
scanf("%d", &products[i].stock);
printf("请输入新的商品售价:");
scanf("%f", &products[i].price);
printf("商品信息修改成功!\n");
return;
}
}
printf("未找到该商品编号的记录!\n");
}
// 购买管理
void purchaseManagement() {
int id;
printf("请输入要购买的商品编号:");
scanf("%d", &id);
int i;
for (i = 0; i < count; i++) {
if (products[i].id == id) {
int quantity;
printf("请输入购买数量:");
scanf("%d", &quantity);
if (quantity <= products[i].stock) {
products[i].stock -= quantity;
float totalPrice = products[i].price * quantity;
printf("购买成功!总价为:%.2f\n", totalPrice);
} else {
printf("库存不足,购买失败!\n");
}
return;
}
}
printf("未找到该商品编号的记录!\n");
}
// 查询商品信息
void queryProduct() {
int choice;
printf("请选择查询方式:\n");
printf("1. 根据商品名称查询\n");
printf("2. 根据商品编号查询\n");
printf("3. 根据商品厂家查询\n");
scanf("%d", &choice);
char keyword[50];
printf("请输入查询关键字:");
scanf("%s", keyword);
int i;
int found = 0;
for (i = 0; i < count; i++) {
if (choice == 1 && strcmp(products[i].name, keyword) == 0) {
printf("商品名称:%s\n", products[i].name);
printf("商品编号:%d\n", products[i].id);
printf("商品厂家:%s\n", products[i].manufacturer);
printf("商品库存量:%d\n", products[i].stock);
printf("商品售价:%.2f\n", products[i].price);
found = 1;
} else if (choice == 2 && products[i].id == atoi(keyword)) {
printf("商品名称:%s\n", products[i].name);
printf("商品编号:%d\n", products[i].id);
printf("商品厂家:%s\n", products[i].manufacturer);
printf("商品库存量:%d\n", products[i].stock);
printf("商品售价:%.2f\n", products[i].price);
found = 1;
} else if (choice == 3 && strcmp(products[i].manufacturer, keyword) == 0) {
printf("商品名称:%s\n", products[i].name);
printf("商品编号:%d\n", products[i].id);
printf("商品厂家:%s\n", products[i].manufacturer);
printf("商品库存量:%d\n", products[i].stock);
printf("商品售价:%.2f\n", products[i].price);
found = 1;
}
}
if (!found) {
printf("未找到符合条件的商品记录!\n");
}
}
// 冒泡排序
void bubbleSort() {
int i, j;
for (i = 0; i < count - 1; i++) {
for (j = 0; j < count - i - 1; j++) {
if (products[j].id > products[j + 1].id) {
Product temp = products[j];
products[j] = products[j + 1];
products[j + 1] = temp;
}
}
}
printf("商品信息已按商品编号排序!\n");
}
// 快速排序
void quickSort(int low, int high) {
if (low < high) {
int i = low, j = high;
Product pivot = products[low];
while (i < j) {
while (i < j && products[j].stock >= pivot.stock) {
j--;
}
if (i < j) {
products[i] = products[j];
i++;
}
while (i < j && products[i].stock <= pivot.stock) {
i++;
}
if (i < j) {
products[j] = products[i];
j--;
}
}
products[i] = pivot;
quickSort(low, i - 1);
quickSort(i + 1, high);
}
}
// 直接插入排序
void insertionSort() {
int i, j;
for (i = 1; i < count; i++) {
Product temp = products[i];
j = i - 1;
while (j >= 0 && products[j].price > temp.price) {
products[j + 1] = products[j];
j--;
}
products[j + 1] = temp;
}
printf("商品信息已按售价排序!\n");
}
// 显示商品信息
void displayProducts() {
int i;
for (i = 0; i < count; i++) {
printf("商品名称:%s\n", products[i].name);
printf("商品编号:%d\n", products[i].id);
printf("商品厂家:%s\n", products[i].manufacturer);
printf("商品库存量:%d\n", products[i].stock);
printf("商品售价:%.2f\n", products[i].price);
printf("--\n");
}
}
int main() {
int choice;
while (1) {
printf("请选择操作:\n");
printf("1. 增加商品信息\n");
printf("2. 删除商品记录\n");
printf("3. 修改商品信息\n");
printf("4. 购买管理\n");
printf("5. 查询商品信息\n");
printf("6. 商品排序\n");
printf("7. 显示商品信息\n");
printf("0. 退出\n");
scanf("%d", &choice);
switch (choice) {
case 1:
addProduct();
break;
case 2:
deleteProduct();
break;
case 3:
modifyProduct();
break;
case 4:
purchaseManagement();
break;
case 5:
queryProduct();
break;
case 6:
printf("请选择排序方法:\n");
printf("1. 冒泡排序\n");
printf("2. 快速排序\n");
printf("3. 直接插入排序\n");
scanf("%d", &choice);
switch (choice) {
case 1:
bubbleSort();
break;
case 2:
quickSort(0, count - 1);
printf("商品信息已按库存量排序!\n");
break;
case 3:
insertionSort();
break;
default:
printf("无效的选择!\n");
break;
}
break;
case 7:
displayProducts();
break;
case 0:
printf("感谢使用,再见!\n");
exit(0);
default:
printf("无效的选择!\n");
break;
}
}
return 0;
}
```
相关推荐
![](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)