请问怎样用c语言实现商品查询功能
时间: 2023-12-29 20:52:17 浏览: 24
要实现商品查询功能,需要先建立一个商品数据库,可以使用文件或者数据库来存储商品信息。然后,根据用户的输入,从数据库中查询符合条件的商品,并将结果显示给用户。
以下是一个简单的实现过程:
1. 定义商品结构体,包括商品编号、名称、价格等信息。
```
typedef struct {
int id; // 商品编号
char name[50]; // 商品名称
float price; // 商品价格
// 其他商品信息
} Product;
```
2. 读取商品信息并存储到数组或者链表中。
```
Product products[MAX_PRODUCTS]; // 假设最多有MAX_PRODUCTS个商品
// 从文件中读取商品信息
void loadProductsFromFile() {
FILE* f = fopen("products.txt", "r");
int i = 0;
while (fscanf(f, "%d%s%f", &products[i].id, products[i].name, &products[i].price) != EOF) {
i++;
}
fclose(f);
}
```
3. 实现查询函数,根据用户输入的条件查询符合条件的商品,并将结果显示给用户。
```
// 根据商品名称查询
void searchByName(char* name) {
int found = 0;
for (int i = 0; i < MAX_PRODUCTS; i++) {
if (strcmp(products[i].name, name) == 0) {
printf("%d\t%s\t%.2f\n", products[i].id, products[i].name, products[i].price);
found = 1;
}
}
if (!found) {
printf("没有找到符合条件的商品\n");
}
}
// 根据商品价格范围查询
void searchByPriceRange(float minPrice, float maxPrice) {
int found = 0;
for (int i = 0; i < MAX_PRODUCTS; i++) {
if (products[i].price >= minPrice && products[i].price <= maxPrice) {
printf("%d\t%s\t%.2f\n", products[i].id, products[i].name, products[i].price);
found = 1;
}
}
if (!found) {
printf("没有找到符合条件的商品\n");
}
}
// 根据商品编号查询
void searchById(int id) {
int found = 0;
for (int i = 0; i < MAX_PRODUCTS; i++) {
if (products[i].id == id) {
printf("%d\t%s\t%.2f\n", products[i].id, products[i].name, products[i].price);
found = 1;
break;
}
}
if (!found) {
printf("没有找到符合条件的商品\n");
}
}
```
4. 在主函数中调用查询函数,根据用户输入的命令调用不同的查询函数。
```
int main() {
loadProductsFromFile(); // 读取商品信息
char cmd[20], arg1[50], arg2[50];
while (1) {
printf("请输入命令(name/price/id quit):");
scanf("%s", cmd);
if (strcmp(cmd, "quit") == 0) {
break;
} else if (strcmp(cmd, "name") == 0) {
printf("请输入商品名称:");
scanf("%s", arg1);
searchByName(arg1);
} else if (strcmp(cmd, "price") == 0) {
printf("请输入价格范围(最小价格 最大价格):");
scanf("%s %s", arg1, arg2);
float minPrice = atof(arg1);
float maxPrice = atof(arg2);
searchByPriceRange(minPrice, maxPrice);
} else if (strcmp(cmd, "id") == 0) {
printf("请输入商品编号:");
scanf("%s", arg1);
int id = atoi(arg1);
searchById(id);
} else {
printf("无效命令\n");
}
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)