设计一个可进行商品管理的程序,具备基本的商品管理功能。
要求:程序菜单功能或详细的提示功能,商品信息输入、输出、添加(进货)、删除(销售)、查询(按商品名)、排序(按编号,品名,数量)、文件保存、文件读
入。
参考GPT和自己的思路:以下是一个简单的 C 语言商品信息管理系统,包括菜单功能、商品信息输入、输出、添加、删除、查询、排序、文件保存、文件读入等基本功能。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 50
#define MAX_ITEMS 100
struct item {
int id; // 商品编号
char name[MAX_NAME_LEN]; // 商品名称
int quantity; // 商品数量
};
struct item items[MAX_ITEMS]; // 商品列表
int num_items = 0; // 商品数量
// 显示菜单
void show_menu() {
printf("商品信息管理系统\n");
printf("-----------------\n");
printf("1. 显示商品列表\n");
printf("2. 添加商品\n");
printf("3. 删除商品\n");
printf("4. 查询商品\n");
printf("5. 按编号排序\n");
printf("6. 按名称排序\n");
printf("7. 按数量排序\n");
printf("8. 保存到文件\n");
printf("9. 从文件读入\n");
printf("0. 退出\n");
printf("-----------------\n");
printf("请选择操作:");
}
// 输入商品信息
void input_item(struct item *pitem) {
printf("请输入商品编号:");
scanf("%d", &pitem->id);
printf("请输入商品名称:");
scanf("%s", pitem->name);
printf("请输入商品数量:");
scanf("%d", &pitem->quantity);
}
// 输出商品信息
void output_item(const struct item *pitem) {
printf("%d\t%s\t%d\n", pitem->id, pitem->name, pitem->quantity);
}
// 显示商品列表
void show_items() {
int i;
printf("商品编号\t商品名称\t商品数量\n");
printf("---------------------------------------\n");
for (i = 0; i < num_items; i++) {
output_item(&items[i]);
}
}
// 添加商品
void add_item() {
struct item item;
input_item(&item);
items[num_items] = item;
num_items++;
printf("商品已添加。\n");
}
// 删除商品
void delete_item() {
int id, i, found = 0;
printf("请输入要删除的商品编号:");
scanf("%d", &id);
for (i = 0; i < num_items; i++) {
if (items[i].id == id) {
found = 1;
break;
}
}
if (found) {
for (; i < num_items - 1; i++) {
items[i] = items[i + 1];
}
num_items--;
printf("商品已删除。\n");
} else {
printf("未找到该商品。\n");
}
}
// 查询商品
void search_item() {
char name[MAX_NAME_LEN];
int i, found = 0;
printf("请输入要查询的商品名称:");
scanf("%s", name);
printf("商品编号\t商品名称\t商品数量\n");
printf("---------------------------------------\n");
for (i = 0; i < num_items; i++) {
if (strcmp(items[i].name, name) == 0) {
output_item(&items[i]);
found = 1;
}
}
if (!found) {
printf("未找到该商品。\n");
}
}
// 按编号排序
int compare_by_id(const void *p1, const void *p2) {
const struct item *item1 = (const struct item *)p1;
const struct item *item2 = (const struct item *)p2;
return item1->id - item2->id;
}
// 按名称排序
int compare_by_name(const void *p1, const void *p2) {
const struct item *item1 = (const struct item *)p1;
const struct item *item2 = (const struct item *)p2;
return strcmp(item1->name, item2->name);
}
// 按数量排序
int compare_by_quantity(const void *p1, const void *p2) {
const struct item *item1 = (const struct item *)p1;
const struct item *item2 = (const struct item *)p2;
return item1->quantity - item2->quantity;
}
// 排序商品列表
void sort_items() {
int choice;
printf("请选择排序方式:");
scanf("%d", &choice);
switch (choice) {
case 1:
qsort(items, num_items, sizeof(struct item), compare_by_id);
break;
case 2:
qsort(items, num_items, sizeof(struct item), compare_by_name);
break;
case 3:
qsort(items, num_items, sizeof(struct item), compare_by_quantity);
break;
default:
printf("无效的选择。\n");
return;
}
printf("商品已排序。\n");
}
// 保存商品列表到文件
void save_items(const char *filename) {
FILE *fp;
int i;
fp = fopen(filename, "wb");
if (fp == NULL) {
printf("无法打开文件。\n");
return;
}
fwrite(&num_items, sizeof(num_items), 1, fp);
for (i = 0; i < num_items; i++) {
fwrite(&items[i], sizeof(struct item), 1, fp);
}
fclose(fp);
printf("商品已保存到文件。\n");
}
// 从文件读入商品列表
void load_items(const char *filename) {
FILE *fp;
int i;
fp = fopen(filename, "rb");
if (fp == NULL) {
printf("无法打开文件。\n");
return;
}
fread(&num_items, sizeof(num_items), 1, fp);
for (i = 0; i < num_items; i++) {
fread(&items[i], sizeof(struct item), 1, fp);
}
fclose(fp);
printf("商品已从文件读入。\n");
}
int main() {
int choice;
char filename[MAX_NAME_LEN];
while (1) {
show_menu();
scanf("%d", &choice);
switch (choice) {
case 0:
printf("再见!\n");
return 0;
case 1:
show_items();
break;
case 2:
add_item();
break;
case 3:
delete_item();
break;
case 4:
search_item();
break;
case 5:
case 6:
case 7:
sort_items();
break;
case 8:
printf("请输入文件名:");
scanf("%s", filename);
save_items(filename);
break;
case 9:
printf("请输入文件名:");
scanf("%s", filename);
load_items(filename);
break;
default:
printf("无效的选择。\n");
break;
}
}
return 0;
}
代码解释:
main() 函数是程序的入口,它通过显示菜单并读取用户的选择来控制程序的流程。
choice 变量存储用户的选择。
filename 变量用于读取或保存商品列表的文件名。
while 循环保持程序运行,直到用户选择退出程序。
show_menu() 函数显示菜单,其中包括了各种商品管理功能。
根据用户的选择,使用 switch 语句调用相应的函数来完成商品管理任务,例如展示商品列表、添加商品、删除商品、查询商品、排序商品、保存商品列表到文件、从文件读入商品列表。
如果用户选择退出程序,程序会返回 0,结束运行。
小魔女参考了bing和GPT部分内容调写:
商品管理程序的设计思路:
定义商品信息的数据结构,包括商品编号、商品名称、商品数量等;
定义商品管理程序的基本功能,包括商品信息的输入、输出、添加、删除、查询、排序等;
定义商品管理程序的提示功能,提示用户输入商品信息,提示用户输入操作;
定义商品管理程序的文件保存功能,将商品信息保存到文件中;
定义商品管理程序的文件读取功能,将文件中的商品信息读取出来;
定义商品管理程序的主函数,实现商品管理程序的基本功能,并调用提示功能、文件保存功能、文件读取功能;
编写商品管理程序的代码,实现商品管理程序的功能;
进行测试,确保商品管理程序的正确性。
回答不易,记得采纳呀。
“Devil组”引证GPT后的撰写:
简单的商品管理程序,具有基本的商品管理功能,如商品信息输入、输出、添加(进货)、删除(销售)、查询(按商品名)、排序(按编号,品名,数量)、文件保存、文件读入。该程序使用C语言编写,采用了结构体数组来存储商品信息,并使用文件来保存和读取数据。程序使用菜单来进行交互,以便用户能够方便地使用各种功能。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ITEMS 100 // 商品数量的最大值
#define FILE_NAME "items.dat" // 商品数据保存的文件名
// 商品结构体
typedef struct {
int id; // 商品编号
char name[20]; // 商品名称
int quantity; // 商品数量
float price; // 商品价格
} Item;
// 商品数组和数量
Item items[MAX_ITEMS];
int num_items = 0;
// 函数声明
void display_menu();
void add_item();
void sell_item();
void list_items();
void search_item();
void sort_items();
void save_items();
void load_items();
// 主函数
int main() {
int choice;
load_items(); // 加载商品数据
do {
display_menu(); // 显示菜单
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: add_item(); break;
case 2: sell_item(); break;
case 3: list_items(); break;
case 4: search_item(); break;
case 5: sort_items(); break;
case 6: save_items(); break;
case 7: load_items(); break;
case 0: printf("Goodbye!\n"); break;
default: printf("Invalid choice.\n"); break;
}
} while (choice != 0);
return 0;
}
// 显示菜单
void display_menu() {
printf("\n");
printf("1. Add item\n");
printf("2. Sell item\n");
printf("3. List items\n");
printf("4. Search item\n");
printf("5. Sort items\n");
printf("6. Save items\n");
printf("7. Load items\n");
printf("0. Exit\n");
}
// 添加商品
void add_item() {
Item item;
printf("Enter item id: ");
scanf("%d", &item.id);
printf("Enter item name: ");
scanf("%s", item.name);
printf("Enter item quantity: ");
scanf("%d", &item.quantity);
printf("Enter item price: ");
scanf("%f", &item.price);
if (num_items >= MAX_ITEMS) {
printf("Error: too many items.\n");
return;
}
items[num_items] = item;
num_items++;
printf("Item added.\n");
}
// 销售商品
void sell_item() {
int id, quantity, i;
printf("Enter item id: ");
scanf("%d", &id);
printf("Enter item quantity: ");
scanf("%d", &quantity);
for (i = 0; i < num_items; i++) {
if (items[i].id == id) {
if (items[i].quantity >= quantity) {
items[i].quantity -= quantity;
printf("Item sold.\n");
else {
printf("Error: not enough quantity.\n");
}
return;
}
}
printf("Error: item not found.\n");
}
// 列出所有商品
void list_items() {
int i;
printf("%-5s %-20s %-10s %-10s\n", "ID", "Name", "Quantity", "Price");
for (i = 0; i < num_items; i++) {
printf("%-5d %-20s %-10d %-10.2f\n", items[i].id, items[i].name, items[i].quantity, items[i].price);
}
}
// 搜索商品
void search_item() {
char name[20];
int i, found = 0;
printf("Enter item name: ");
scanf("%s", name);
for (i = 0; i < num_items; i++) {
if (strcmp(items[i].name, name) == 0) {
printf("%-5s %-20s %-10s %-10s\n", "ID", "Name", "Quantity", "Price");
printf("%-5d %-20s %-10d %-10.2f\n", items[i].id, items[i].name, items[i].quantity, items[i].price);
found = 1;
}
}
if (!found) {
printf("Item not found.\n");
}
}
// 按编号,品名,数量排序
void sort_items() {
int choice, i, j;
Item temp;
printf("1. Sort by id\n");
printf("2. Sort by name\n");
printf("3. Sort by quantity\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
for (i = 0; i < num_items - 1; i++) {
for (j = i + 1; j < num_items; j++) {
if (items[i].id > items[j].id) {
temp = items[i];
items[i] = items[j];
items[j] = temp;
}
}
}
printf("Items sorted by id.\n");
break;
case 2:
for (i = 0; i < num_items - 1; i++) {
for (j = i + 1; j < num_items; j++) {
if (strcmp(items[i].name, items[j].name) > 0) {
temp = items[i];
items[i] = items[j];
items[j] = temp;
}
}
}
printf("Items sorted by name.\n");
break;
case 3:
for (i = 0; i < num_items - 1; i++) {
for (j = i + 1; j < num_items; j++) {
if (items[i].quantity > items[j].quantity) {
temp = items[i];
items[i] = items[j];
items[j] = temp;
}
}
}
printf("Items sorted by quantity.\n");
break;
default:
printf("Invalid choice.\n");
break;
}
}
// 保存商品数据到文件
void save_items() {
FILE *fp;
int i;
fp = fopen(FILE_NAME, "wb");
if (fp == NULL) {
printf("Error: could not open file.\n");
return;
}
fwrite(&num_items, sizeof(int), 1, fp
);
fwrite(items, sizeof(Item), num_items, fp);
fclose(fp);
printf("Items saved to file.\n");
}
// 从文件中读取商品数据
void load_items() {
FILE *fp;
int i;
fp = fopen(FILE_NAME, "rb");
if (fp == NULL) {
printf("Error: could not open file.\n");
return;
}
fread(&num_items, sizeof(int), 1, fp);
fread(items, sizeof(Item), num_items, fp);
fclose(fp);
printf("Items loaded from file.\n");
}
int main() {
int choice;load_items();
do {
printf("\n1. Add item\n");
printf("2. Sell item\n");
printf("3. List items\n");
printf("4. Search item\n");
printf("5. Sort items\n");
printf("6. Save items\n");
printf("7. Load items\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_item();
break;
case 2:
sell_item();
break;
case 3:
list_items();
break;
case 4:
search_item();
break;
case 5:
sort_items();
break;
case 6:
save_items();
break;
case 7:
load_items();
break;
case 0:
printf("Goodbye.\n");
break;
default:
printf("Invalid choice.\n");
break;
}
} while (choice != 0);
return 0;
}
该回答引用ChatGPT
以下是一个简单的商品信息管理系统的C语言实现,实现了基本的商品管理功能。程序中使用结构体来存储每个商品的信息,可以进行商品的信息输入、输出、添加、删除、查询、排序以及文件保存和读入等操作。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_PRODUCTS 100
// 商品信息结构体
typedef struct {
int id; // 商品编号
char name[MAX_NAME_LEN]; // 商品名称
int quantity; // 商品数量
} Product;
// 声明全局变量
Product products[MAX_PRODUCTS]; // 存储所有商品信息的数组
int num_products = 0; // 当前商品数量
// 函数声明
void print_menu();
void add_product();
void remove_product();
void search_product();
void sort_products();
void save_products();
void load_products();
void print_products();
void clear_input_buffer();
// 主函数
int main() {
int choice;
do {
print_menu();
printf("请输入操作编号:");
scanf("%d", &choice);
clear_input_buffer(); // 清空输入缓冲区
switch (choice) {
case 1:
add_product();
break;
case 2:
remove_product();
break;
case 3:
search_product();
break;
case 4:
sort_products();
break;
case 5:
save_products();
break;
case 6:
load_products();
break;
case 7:
print_products();
break;
case 8:
printf("程序已退出。\n");
break;
default:
printf("无效的操作编号,请重新输入。\n");
break;
}
} while (choice != 8);
return 0;
}
// 输出菜单
void print_menu() {
printf("商品信息管理系统\n");
printf("-----------------\n");
printf("1. 添加商品\n");
printf("2. 删除商品\n");
printf("3. 查询商品\n");
printf("4. 排序商品\n");
printf("5. 保存商品信息到文件\n");
printf("6. 从文件中读取商品信息\n");
printf("7. 输出所有商品信息\n");
printf("8. 退出程序\n");
}
// 添加商品
void add_product() {
Product product;
printf("请输入商品编号:");
scanf("%d", &product.id);
clear_input_buffer();
printf("请输入商品名称:");
fgets(product.name, MAX_NAME_LEN, stdin);
product.name[strcspn(product.name, "\n")] = '\0'; // 去掉字符串末尾的换行符
printf("请输入商品数量:");
scanf("%d", &product.quantity);
clear_input_buffer();
products[num_products++] = product;
printf("已添加商品 %s。\n", product.name);
}
// 删除商品
void remove_product() {
char name[MAX_NAME_LEN];
int i, j;
printf("请输入要删除的商品名称:");
fgets(name, MAX_NAME_LEN, stdin);
name[strcspn(name, "\n")] = '\0'; // 去掉字符串末尾的换行符
for (i = 0; i < num_products; i++) {
if (strcmp(products[i].name, name) == 0) {
// 找到了要删除的商品
printf("已删除商品 %s。\n", products[i].name);
// 将后面的商品信息向前移动
for (j = i + 1; j < num_products; j++) {
products[j - 1] = products[j];
}
num_products--; // 商品数量减1
return;
}
}
// 没有找到要删除的商品
printf("未找到要删除的商品 %s。\n", name);
}
// 查询商品
void search_product() {
char name[MAX_NAME_LEN];
int i;
printf("请输入要查询的商品名称:");
fgets(name, MAX_NAME_LEN, stdin);
name[strcspn(name, "\n")] = '\0'; // 去掉字符串末尾的换行符
for (i = 0; i < num_products; i++) {
if (strcmp(products[i].name, name) == 0) {
printf("商品编号:%d,商品名称:%s,商品数量:%d。\n", products[i].id, products[i].name, products[i].quantity);
return;
}
}
// 没有找到要查询的商品
printf("未找到要查询的商品 %s。\n", name);
}
// 排序商品
void sort_products() {
int choice, i, j;
Product temp;
printf("请选择排序方式:\n");
printf("1. 按编号排序\n");
printf("2. 按名称排序\n");
printf("3. 按数量排序\n");
printf("请输入操作编号:");
scanf("%d", &choice);
clear_input_buffer(); // 清空输入缓冲区
switch (choice) {
case 1:
// 按编号排序,使用冒泡排序
for (i = 0; i < num_products - 1; i++) {
for (j = 0; j < num_products - i - 1; j++) {
if (products[j].id > products[j + 1].id) {
temp = products[j];
products[j] = products[j + 1];
products[j + 1] = temp;
}
}
}
printf("已按编号排序。\n");
break;
case 2:
// 按名称排序,使用选择排序
for (i = 0; i < num_products - 1; i++) {
int min_index = i;
for (j = i + 1; j < num_products; j++) {
if (strcmp(products[j].name, products[min_index].name) < 0) {
min_index = j;
}
}
if (min_index != i) {
temp = products[i];
products[i] = products[min_index];
products[min_index] = temp;
}
}
printf("已按名称排序。\n");
break;
case 3:
// 按数量排序,使用插入排序
for (i = 1; i < num_products; i++) {
temp = products[i];
j = i - 1;
while (j >= 0 && products[j].quantity > temp.quantity) {
products[j + 1] = products[j];
j--;
}
products[j + 1] = temp;
}
printf("已按数量排序。\n");
break;
default:
printf("无效的操作编号,请重新输入。\n");
break;
}
}
// 保存商品信息到文件
void save_products() {
char filename[MAX_NAME_LEN];
FILE *fp;
int i;
printf("请输入要保存到的文件名:");
fgets(filename, MAX_NAME_LEN, stdin);
filename[strcspn(filename, "\n")] = '\0'; // 去掉字符串末尾的换行符
fp = fopen(filename, "w");
if (fp == NULL) {
printf("无法打开文件 %s。\n", filename);
return;
}
fprintf(fp, "%d\n", num_products); // 写入商品数量
for (i = 0; i < num_products; i++) {
fprintf(fp, "%d,%s,%d\n", products[i].id, products[i].name, products[i].quantity); // 写入每个商品的信息
}
fclose(fp);
printf("已将商品信息保存到文件 %s。\n", filename);
}
// 输出所有商品信息
void print_products() {
int i;
printf("所有商品信息:\n");
printf("编号\t名称\t数量\n");
for (i = 0; i < num_products; i++) {
printf("%d\t%s\t%d\n", products[i].id, products[i].name, products[i].quantity);
}
}
// 清空输入缓冲区
void clear_input_buffer() {
while (getchar() != '\n') {
// do nothing
}
}
//定义商品结构体
struct product {
int id; //编号
char name[20]; //品名
int quantity; //数量
float price; //价格
};
//定义商品链表节点
struct node {
struct product data; //商品数据
struct node *next; //指向下一个节点的指针
};
//定义全局变量,指向商品链表的头节点和尾节点
struct node *head = NULL;
struct node *tail = NULL;
//定义菜单函数,显示菜单选项,并返回用户输入的选择
int menu() {
int choice; //用户输入的选择
//显示菜单选项
printf("**********商品信息管理系统**********\n");
printf("1. 商品信息输入\n");
printf("2. 商品信息输出\n");
printf("3. 商品信息添加(进货)\n");
printf("4. 商品信息删除(销售)\n");
printf("5. 商品信息查询(按商品名)\n");
printf("6. 商品信息排序(按编号,品名,数量)\n");
printf("7. 文件保存\n");
printf("8. 文件读入\n");
printf("0. 程序退出\n");
//提示用户输入选择,并检查输入是否有效
do {
printf("请输入你的选择(0-8): ");
scanf("%d", &choice);
if (choice <0 || choice >8) {
printf("无效的输入,请重新输入。\n");
}
} while (choice <0 || choice >8);
return choice; //返回用户输入的选择
}
//定义创建新节点函数,根据用户输入的商品数据创建一个新的链表节点,并返回该节点的指针
struct node *create_new_node() {
struct node *new_node; //新创建的节点指针
struct product new_product; //新输入的商品数据
//提示用户输入商品数据,并检查输入是否有效
printf("请输入商品编号(正整数): ");
scanf("%d", &new_product.id);
if (new_product.id <=0) {
printf("无效的编号,请重新输入。\n");
return NULL;
}
printf("请输入商品品名(不超过20个字符): ");
scanf("%s", new_product.name);
if (strlen(new_product.name) >20) {
printf("无效的品名,请重新输入。\n");
return NULL;
}
printf("请输入商品数量(正整数): ");
scanf("%d", &new_product.quantity);
if (new_product.quantity <=0) {
printf("无效的数量,请重新输入。\n");
return NULL;
}
printf("请输入商品价格(正浮点数): ");
scanf("%f", &new_product.price);
if (new_product.price <=0.0) {
printf("无效的价格,请重新输入。\n");
return NULL;
}
//动态分配内存空间给新节点,并将商品数据赋值给新节点
new_node = (struct node *)malloc(sizeof(struct node));
if (new_node == NULL) {
printf("内存分配失败,请重试。\n");
return NULL;
}
}
你这种课程设计相关的,网上有很多案例,你可以搜索参考呢,gitee也有开源的可以参考
编译一下,可以试一下
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_PRODUCTS 100
// 商品信息结构体
typedef struct {
int id; // 商品编号
char name[MAX_NAME_LEN]; // 商品名称
int quantity; // 商品数量
} Product;
// 声明全局变量
Product products[MAX_PRODUCTS]; // 存储所有商品信息的数组
int num_products = 0; // 当前商品数量
// 函数声明
void print_menu();
void add_product();
void remove_product();
void search_product();
void sort_products();
void save_products();
void load_products();
void print_products();
void clear_input_buffer();
// 主函数
int main() {
int choice;
do {
print_menu();
printf("请输入操作编号:");
scanf("%d", &choice);
clear_input_buffer(); // 清空输入缓冲区
switch (choice) {
case 1:
add_product();
break;
case 2:
remove_product();
break;
case 3:
search_product();
break;
case 4:
sort_products();
break;
case 5:
save_products();
break;
case 6:
load_products();
break;
case 7:
print_products();
break;
case 8:
printf("程序已退出。\n");
break;
default:
printf("无效的操作编号,请重新输入。\n");
break;
}
} while (choice != 8);
return 0;
}
// 输出菜单
void print_menu() {
printf("商品信息管理系统\n");
printf("-----------------\n");
printf("1. 添加商品\n");
printf("2. 删除商品\n");
printf("3. 查询商品\n");
printf("4. 排序商品\n");
printf("5. 保存商品信息到文件\n");
printf("6. 从文件中读取商品信息\n");
printf("7. 输出所有商品信息\n");
printf("8. 退出程序\n");
}
// 添加商品
void add_product() {
Product product;
printf("请输入商品编号:");
scanf("%d", &product.id);
clear_input_buffer();
printf("请输入商品名称:");
fgets(product.name, MAX_NAME_LEN, stdin);
product.name[strcspn(product.name, "\n")] = '\0'; // 去掉字符串末尾的换行符
printf("请输入商品数量:");
scanf("%d", &product.quantity);
clear_input_buffer();
products[num_products++] = product;
printf("已添加商品 %s。\n", product.name);
}
// 删除商品
void remove_product() {
char name[MAX_NAME_LEN];
int i, j;
printf("请输入要删除的商品名称:");
fgets(name, MAX_NAME_LEN, stdin);
name[strcspn(name, "\n")] = '\0'; // 去掉字符串末尾的换行符
for (i = 0; i < num_products; i++) {
if (strcmp(products[i].name, name) == 0) {
// 找到了要删除的商品
printf("已删除商品 %s。\n", products[i].name);
// 将后面的商品信息向前移动
for (j = i + 1; j < num_products; j++) {
products[j - 1] = products[j];
}
num_products--; // 商品数量减1
return;
}
}
// 没有找到要删除的商品
printf("未找到要删除的商品 %s。\n", name);
}
// 查询商品
void search_product() {
char name[MAX_NAME_LEN];
int i;
printf("请输入要查询的商品名称:");
fgets(name, MAX_NAME_LEN, stdin);
name[strcspn(name, "\n")] = '\0'; // 去掉字符串末尾的换行符
for (i = 0; i < num_products; i++) {
if (strcmp(products[i].name, name) == 0) {
printf("商品编号:%d,商品名称:%s,商品数量:%d。\n", products[i].id, products[i].name, products[i].quantity);
return;
}
}
// 没有找到要查询的商品
printf("未找到要查询的商品 %s。\n", name);
}
// 排序商品
void sort_products() {
int choice, i, j;
Product temp;
printf("请选择排序方式:\n");
printf("1. 按编号排序\n");
printf("2. 按名称排序\n");
printf("3. 按数量排序\n");
printf("请输入操作编号:");
scanf("%d", &choice);
clear_input_buffer(); // 清空输入缓冲区
switch (choice) {
case 1:
// 按编号排序,使用冒泡排序
for (i = 0; i < num_products - 1; i++) {
for (j = 0; j < num_products - i - 1; j++) {
if (products[j].id > products[j + 1].id) {
temp = products[j];
products[j] = products[j + 1];
products[j + 1] = temp;
}
}
}
printf("已按编号排序。\n");
break;
case 2:
// 按名称排序,使用选择排序
for (i = 0; i < num_products - 1; i++) {
int min_index = i;
for (j = i + 1; j < num_products; j++) {
if (strcmp(products[j].name, products[min_index].name) < 0) {
min_index = j;
}
}
if (min_index != i) {
temp = products[i];
products[i] = products[min_index];
products[min_index] = temp;
}
}
printf("已按名称排序。\n");
break;
case 3:
// 按数量排序,使用插入排序
for (i = 1; i < num_products; i++) {
temp = products[i];
j = i - 1;
while (j >= 0 && products[j].quantity > temp.quantity) {
products[j + 1] = products[j];
j--;
}
products[j + 1] = temp;
}
printf("已按数量排序。\n");
break;
default:
printf("无效的操作编号,请重新输入。\n");
break;
}
}
// 保存商品信息到文件
void save_products() {
char filename[MAX_NAME_LEN];
FILE *fp;
int i;
printf("请输入要保存到的文件名:");
fgets(filename, MAX_NAME_LEN, stdin);
filename[strcspn(filename, "\n")] = '\0'; // 去掉字符串末尾的换行符
fp = fopen(filename, "w");
if (fp == NULL) {
printf("无法打开文件 %s。\n", filename);
return;
}
fprintf(fp, "%d\n", num_products); // 写入商品数量
for (i = 0; i < num_products; i++) {
fprintf(fp, "%d,%s,%d\n", products[i].id, products[i].name, products[i].quantity); // 写入每个商品的信息
}
fclose(fp);
printf("已将商品信息保存到文件 %s。\n", filename);
}
// 输出所有商品信息
void print_products() {
int i;
printf("所有商品信息:\n");
printf("编号\t名称\t数量\n");
for (i = 0; i < num_products; i++) {
printf("%d\t%s\t%d\n", products[i].id, products[i].name, products[i].quantity);
}
}
// 清空输入缓冲区
void clear_input_buffer() {
while (getchar() != '\n') {
// do nothing
}
}
该回答引用ChatGPT
不是之前的代码了,我改了一下,望采纳
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 1000
struct commdity
{
int num;
char name[N];
float price;
int inventory;
char type[N];
};
struct commdity com[N];
void input(struct commdity *ps,int i);//用于输入商品信息
void save(struct commdity com[],int n,FILE *fp);//用于将数据存储到文件中
void refer(struct commdity com[],FILE *fp);//查询商品信息
void deletecom1(FILE *fp,struct commdity com1[],char name1[]);//删除商品信息
void deletecom2(FILE *fp,struct commdity com1[],int num1);
void modifyGoods(FILE *fp,struct commdity com1[],int num1);//修改商品信息(进货或出售)
void statistics(FILE *fp,struct commdity com1[],int num1);//统计商品
int main()
{
int n;
int i;
int num,num1;
int m;
char name1[20];
FILE *fp;
printf("*********************************\n");
printf("* 商品信息管理系统 *\n");
printf("* 1---输入商品信息 *\n");
printf("* 2---查询商品信息 *\n");
printf("* 3---修改商品信息 *\n");
printf("* 4---删除商品信息 *\n");
printf("* 5---统计 *\n");
printf("* 6---退出系统功能 *\n");
printf("*********************************\n");
printf("\n");
printf("\n");
printf("请输入序号选择功能\n");
printf("请输入你的选择:");
scanf("%d",&num);
system("cls");
if(num==1)
{
printf("请输入您要输入商品的类型有几种:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
input(&com[i],i+1);
}
save(com,n,fp);
}
if(num==2)
{
refer(com,fp);
}
if(num==3)
{
printf("请输入您要修改商品的商品编号:");
scanf("%d",&m);
system("cls");
modifyGoods(fp,com,m);
}
if(num==4)
{
printf("请根据商品名称或商品编号删除商品信息\n");
printf("请输入序号(1--商品名称、2--商品编号):");
scanf("%d",&m);
if(m==1)
{
printf("请输入商品名称:");
scanf("%s",name1);
printf("--------------------------------\n");
deletecom1(fp,com,name1);
}
if(m==2)
{
printf("请输入商品编号:");
scanf("%d",&num1);
printf("--------------------------------\n");
deletecom2(fp,com,num1);
}
}
if(num==5)
{
printf("请根据商品编号或者商品类型统计商品数量!\n");
printf("请输入序号(1--商品编号、2--商品类型):");
scanf("%d",&m);
statistics(fp,com,m);
}
if(num==6)
{
printf("退出系统并按任意键退出!\n");
}
return 0;
}
void input(struct commdity *ps,int i)
{
printf("请输入第%d个商品的相关信息(编号、名称、价格、库存、类型):\n",i);
scanf("%d %s %f %d %s",&(*ps).num,(*ps).name,&(*ps).price,&(*ps).inventory,(*ps).type);
}
void save(struct commdity com[],int n,FILE *fp)
{
fp=fopen("D:\\商品信息.txt","a");
if(!fp)
{
printf("file open faile!\n");
exit(0);
}
for(int i=0;i<n;i++)
{
fprintf(fp,"%d %s %0.2f %d %s\n",com[i].num,com[i].name,com[i].price,com[i].inventory,com[i].type);
}
fclose(fp);
printf("Data save successfully!\n");
}
void refer(struct commdity com1[],FILE *fp)
{
int i;
int num;
int number;
printf("请输入序号(1--查询单个商品、2--查询全部商品):");
scanf("%d",&number);
if(number==1)
{
printf("请输入您要查询商品的商品编号:");
scanf("%d",&num);
system("cls");
fp=fopen("D:\\商品信息.txt","r+");
if(!fp)
{
printf("file open false!\n");
exit(0);
}
for(i=0;i<N;i++)
{
fscanf(fp,"%d %s %f %d %s",&com1[i].num,com1[i].name,&com1[i].price,&com1[i].inventory,com1[i].type);
if(num==com1[i].num)
{
printf("商品编号:%d\n",com1[i].num);
printf("商品名称:%s\n",com1[i].name);
printf("商品价格:%0.2f\n",com1[i].price);
printf("商品库存:%d\n",com1[i].inventory);
printf("商品类型:%s\n",com1[i].type);
}
}
fclose(fp);
}
if(number==2)
{
system("cls");
fp=fopen("D:\\商品信息.txt","r+");
if(!fp)
{
printf("file open false!\n");
exit(0);
}
for(i=0;i<N;i++)
fscanf(fp,"%d %s %f %d %s",&com1[i].num,com1[i].name,&com1[i].price,&com1[i].inventory,com1[i].type);
printf("全部商品信息如下(商品编号、商品名称、商品价格、商品库存、商品类型):\n");
for(i=0;i<N;i++)
{
if(com1[i].num==0)
continue;
printf("%10d%s\t\t%0.2f\t\t%d\t\t%s\t\t\n",com[i].num,com[i].name,com[i].price,com[i].inventory,com[i].type);
}
}
}
void modifyGoods(FILE *fp,struct commdity com1[],int num1)
{
int number;
int i;
int quantity;
int m;
fp=fopen("D:\\商品信息.txt","r");
if(!fp)
{
printf("file open false!\n");
exit(0);
}
for(i=0;i<N;i++)
fscanf(fp,"%d %s %f %d %s",&com1[i].num,com1[i].name,&com1[i].price,&com1[i].inventory,com1[i].type);
for(i=0;i<N;i++)
{
if(num1==com1[i].num)
{
printf("要修改的商品信息为:\n");
printf("商品编号:%d\n商品名称:%s\n",com1[i].num,com1[i].name);
printf("商品价格:%0.2f\n库存:%d\n",com1[i].price,com1[i].inventory);
printf("商品类型:%s\n",com1[i].type);
printf("--------------------------------\n");
m=i;
}
}
printf("请输入您要修改内容(1--进货、2--出售)!\n");
printf("请输入序号:");
scanf("%d",&number);
fp=fopen("D:\\商品信息.txt","w");
if(!fp)
{
printf("file open false!\n");
exit(0);
}
if(number==1)
{
printf("请输入您要进货的数量:");
scanf("%d",&quantity);
com1[m].inventory+=quantity;
}
if(number==2)
{
printf("请输入您要出售的数量:");
scanf("%d",&quantity);
com1[m].inventory-=quantity;
}
system("cls");
printf("修改成功!\n");
printf("修改后的商品信息:\n");
printf("商品编号:%d\n商品名称:%s\n",com1[m].num,com1[m].name);
printf("商品价格:%0.2f\n库存:%d\n",com1[m].price,com1[m].inventory);
printf("商品类型:%s\n",com1[m].type);
for(i=0;i<N;i++)
{
if(com1[i].num==0)
continue;
fprintf(fp,"%d %s %0.2f %d %s\n",com[i].num,com[i].name,com[i].price,com[i].inventory,com[i].type);
}
fclose(fp);
}
void deletecom1(FILE *fp,struct commdity com1[],char name1[])
{
int i;
int m;
char a[20];
char b[20]={"yes"},c[20]={"no"};
char d[20]="\0";
fp=fopen("D:\\商品信息.txt","r");
if(!fp)
{
printf("file open false!\n");
exit(0);
}
for(i=0;i<N;i++)
fscanf(fp,"%d %s %f %d %s",&com1[i].num,com1[i].name,&com1[i].price,&com1[i].inventory,com1[i].type);
for(i=0;i<N;i++)
{
if(strcmp(name1,com1[i].name)==0)
{
printf("要删除的商品信息为:\n");
printf("商品编号:%d 商品名称:%s\n",com1[i].num,com1[i].name);
printf("商品价格:%0.2f 库存:%d\n",com1[i].price,com1[i].inventory);
printf("商品类型:%s\n",com1[i].type);
printf("--------------------------------\n");
m=i;
printf("请问是否要删除?\n");
printf("情输入yes或no:\n");
scanf("%s",a);
if(strcmp(a,b)==0)
{
com1[m].num=0;
strcpy(com1[m].name,d);
com1[m].price=0.00;
com1[m].inventory=0;
strcpy(com1[m].type,d);
fp=fopen("D:\\商品信息.txt","w");
if(!fp)
{
printf("file open false!\n");
exit(0);
}
for(i=0;i<N;i++)
{
if(com1[i].num==0)
continue;
fprintf(fp,"%d %s %0.2f %d %s\n",com[i].num,com[i].name,com[i].price,com[i].inventory,com[i].type);
}
fclose(fp);
printf("删除成功!\n");
}
if(strcmp(a,c)==0)
printf("请按任意键退出该系统!\n");
}
}
fclose(fp);
}
void deletecom2(FILE *fp,struct commdity com1[],int num1)
{
int i;
int m;
char a[20];
char b[20]={"yes"},c[20]={"no"};
char d[20]="\0";
fp=fopen("D:\\商品信息.txt","r");
if(!fp)
{
printf("file open false!\n");
exit(0);
}
for(i=0;i<N;i++)
fscanf(fp,"%d %s %f %d %s",&com1[i].num,com1[i].name,&com1[i].price,&com1[i].inventory,com1[i].type);
for(i=0;i<N;i++)
{
if(num1==com1[i].num)
{
printf("要删除的商品信息为:\n");
printf("商品编号:%d 商品名称:%s\n",com1[i].num,com1[i].name);
printf("商品价格:%0.2f 库存:%d\n",com1[i].price,com1[i].inventory);
printf("商品类型:%s\n",com1[i].type);
printf("--------------------------------\n");
m=i;
printf("请问是否要删除?\n");
printf("情输入yes或no:\n");
scanf("%s",a);
if(strcmp(a,b)==0)
{
com1[m].num=0;
strcpy(com1[m].name,d);
com1[m].price=0.00;
com1[m].inventory=0;
strcpy(com1[m].type,d);
fp=fopen("D:\\商品信息.txt","w");
if(!fp)
{
printf("file open false!\n");
exit(0);
}
for(i=0;i<N;i++)
{
if(com1[i].num==0)
continue;
fprintf(fp,"%d %s %0.2f %d %s\n",com[i].num,com[i].name,com[i].price,com[i].inventory,com[i].type);
}
fclose(fp);
printf("删除成功!\n");
}
if(strcmp(a,c)==0)
printf("请按任意键退出该系统!\n");
}
}
fclose(fp);
}
void statistics(FILE *fp,struct commdity com1[],int num1)
{
int number;
int i;
int m;
char type1[20];
if(num1==1)
{
printf("请输入您要统计商品的商品编号:");
scanf("%d",&number);
fp=fopen("D:\\商品信息.txt","r");
if(!fp)
{
printf("file open false!\n");
exit(0);
}
for(i=0;i<N;i++)
{
fscanf(fp,"%d %s %f %d %s",&com1[i].num,com1[i].name,&com1[i].price,&com1[i].inventory,com1[i].type);
if(number==com1[i].num)
{
printf("您要统计的商品信息如下:\n");
printf("商品编号:%d\n",com1[i].num);
printf("商品名称:%s\n",com1[i].name);
printf("商品价格:%0.2f\n",com1[i].price);
printf("商品库存:%d\n",com1[i].inventory);
printf("商品类型:%s\n",com1[i].type);
m=i;
}
}
fclose(fp);
printf("--------------------------------\n");
printf("经过统计,该商品的数量为:%d",com1[m].inventory);
}
if(num1==2)
{
printf("请输入您要统计商品的商品类型:");
scanf("%s",type1);
fp=fopen("D:\\商品信息.txt","r");
if(!fp)
{
printf("file open false!\n");
exit(0);
}
for(i=0;i<N;i++)
{
fscanf(fp,"%d %s %f %d %s",&com1[i].num,com1[i].name,&com1[i].price,&com1[i].inventory,com1[i].type);
if(strcmp(type1,com1[i].type)==0)
{
printf("--------------------------------\n");
printf("您要统计的商品信息如下:\n");
printf("商品编号:%d\n",com1[i].num);
printf("商品名称:%s\n",com1[i].name);
printf("商品价格:%0.2f\n",com1[i].price);
printf("商品库存:%d\n",com1[i].inventory);
printf("商品类型:%s\n",com1[i].type);
m=i;
printf("经过统计,该商品的数量为:%d\n",com1[m].inventory);
}
}
fclose(fp);
}
}
参考代码……
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_PRODUCTS 100
// 商品信息结构体
typedef struct {
int id; // 商品编号
char name[MAX_NAME_LEN]; // 商品名称
int quantity; // 商品数量
} Product;
// 声明全局变量
Product products[MAX_PRODUCTS]; // 存储所有商品信息的数组
int num_products = 0; // 当前商品数量
// 函数声明
void print_menu();
void add_product();
void remove_product();
void search_product();
void sort_products();
void save_products();
void load_products();
void print_products();
void clear_input_buffer();
// 主函数
int main() {
int choice;
do {
print_menu();
printf("请输入操作编号:");
scanf("%d", &choice);
clear_input_buffer(); // 清空输入缓冲区
switch (choice) {
case 1:
add_product();
break;
case 2:
remove_product();
break;
case 3:
search_product();
break;
case 4:
sort_products();
break;
case 5:
save_products();
break;
case 6:
load_products();
break;
case 7:
print_products();
break;
case 8:
printf("程序已退出。\n");
break;
default:
printf("无效的操作编号,请重新输入。\n");
break;
}
} while (choice != 8);
return 0;
}
// 输出菜单
void print_menu() {
printf("商品信息管理系统\n");
printf("-----------------\n");
printf("1. 添加商品\n");
printf("2. 删除商品\n");
printf("3. 查询商品\n");
printf("4. 排序商品\n");
printf("5. 保存商品信息到文件\n");
printf("6. 从文件中读取商品信息\n");
printf("7. 输出所有商品信息\n");
printf("8. 退出程序\n");
}
// 添加商品
void add_product() {
Product product;
printf("请输入商品编号:");
scanf("%d", &product.id);
clear_input_buffer();
printf("请输入商品名称:");
fgets(product.name, MAX_NAME_LEN, stdin);
product.name[strcspn(product.name, "\n")] = '\0'; // 去掉字符串末尾的换行符
printf("请输入商品数量:");
scanf("%d", &product.quantity);
clear_input_buffer();
products[num_products++] = product;
printf("已添加商品 %s。\n", product.name);
}
// 删除商品
void remove_product() {
char name[MAX_NAME_LEN];
int i, j;
printf("请输入要删除的商品名称:");
fgets(name, MAX_NAME_LEN, stdin);
name[strcspn(name, "\n")] = '\0'; // 去掉字符串末尾的换行符
for (i = 0; i < num_products; i++) {
if (strcmp(products[i].name, name) == 0) {
// 找到了要删除的商品
printf("已删除商品 %s。\n", products[i].name);
// 将后面的商品信息向前移动
for (j = i + 1; j < num_products; j++) {
products[j - 1] = products[j];
}
num_products--; // 商品数量减1
return;
}
}
// 没有找到要删除的商品
printf("未找到要删除的商品 %s。\n", name);
}
// 查询商品
void search_product() {
char name[MAX_NAME_LEN];
int i;
printf("请输入要查询的商品名称:");
fgets(name, MAX_NAME_LEN, stdin);
name[strcspn(name, "\n")] = '\0'; // 去掉字符串末尾的换行符
for (i = 0; i < num_products; i++) {
if (strcmp(products[i].name, name) == 0) {
printf("商品编号:%d,商品名称:%s,商品数量:%d。\n", products[i].id, products[i].name, products[i].quantity);
return;
}
}
// 没有找到要查询的商品
printf("未找到要查询的商品 %s。\n", name);
}
// 排序商品
void sort_products() {
int choice, i, j;
Product temp;
printf("请选择排序方式:\n");
printf("1. 按编号排序\n");
printf("2. 按名称排序\n");
printf("3. 按数量排序\n");
printf("请输入操作编号:");
scanf("%d", &choice);
clear_input_buffer(); // 清空输入缓冲区
switch (choice) {
case 1:
// 按编号排序,使用冒泡排序
for (i = 0; i < num_products - 1; i++) {
for (j = 0; j < num_products - i - 1; j++) {
if (products[j].id > products[j + 1].id) {
temp = products[j];
products[j] = products[j + 1];
products[j + 1] = temp;
}
}
}
printf("已按编号排序。\n");
break;
case 2:
// 按名称排序,使用选择排序
for (i = 0; i < num_products - 1; i++) {
int min_index = i;
for (j = i + 1; j < num_products; j++) {
if (strcmp(products[j].name, products[min_index].name) < 0) {
min_index = j;
}
}
if (min_index != i) {
temp = products[i];
products[i] = products[min_index];
products[min_index] = temp;
}
}
printf("已按名称排序。\n");
break;
case 3:
// 按数量排序,使用插入排序
for (i = 1; i < num_products; i++) {
temp = products[i];
j = i - 1;
while (j >= 0 && products[j].quantity > temp.quantity) {
products[j + 1] = products[j];
j--;
}
products[j + 1] = temp;
}
printf("已按数量排序。\n");
break;
default:
printf("无效的操作编号,请重新输入。\n");
break;
}
}
// 保存商品信息到文件
void save_products() {
char filename[MAX_NAME_LEN];
FILE *fp;
int i;
printf("请输入要保存到的文件名:");
fgets(filename, MAX_NAME_LEN, stdin);
filename[strcspn(filename, "\n")] = '\0'; // 去掉字符串末尾的换行符
fp = fopen(filename, "w");
if (fp == NULL) {
printf("无法打开文件 %s。\n", filename);
return;
}
fprintf(fp, "%d\n", num_products); // 写入商品数量
for (i = 0; i < num_products; i++) {
fprintf(fp, "%d,%s,%d\n", products[i].id, products[i].name, products[i].quantity); // 写入每个商品的信息
}
fclose(fp);
printf("已将商品信息保存到文件 %s。\n", filename);
}
// 输出所有商品信息
void print_products() {
int i;
printf("所有商品信息:\n");
printf("编号\t名称\t数量\n");
for (i = 0; i < num_products; i++) {
printf("%d\t%s\t%d\n", products[i].id, products[i].name, products[i].quantity);
}
}
// 清空输入缓冲区
void clear_input_buffer() {
while (getchar() != '\n') {
// do nothing
}
}
```
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
以下是一个简单的C语言商品信息管理系统的设计,具备基本的商品管理功能,实现了商品信息的输入、输出、添加、删除、查询、排序以及文件的保存和读取。代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_GOODS_NUM 100 // 商品信息最大个数
#define MAX_GOODS_NAME_LEN 50 // 商品名最大长度
// 商品信息结构体
typedef struct GoodsInfo {
int id; // 商品编号
char name[50]; // 商品名称
int price; // 商品价格
int count; // 商品数量
} GoodsInfo;
// 商品信息数组
GoodsInfo goods[MAX_GOODS_NUM];
int numOfGoods = 0; // 商品个数
// 菜单功能
void menu() {
printf("===============================================\n");
printf("| 商品信息管理系统 |\n");
printf("|---------------------------------------------|\n");
printf("| 1. 商品信息输入 |\n");
printf("| 2. 商品信息输出 |\n");
printf("| 3. 添加商品(进货) |\n");
printf("| 4. 删除商品(销售) |\n");
printf("| 5. 查询商品(按名称) |\n");
printf("| 6. 排序商品(按编号,名称,数量) |\n");
printf("| 7. 文件保存 |\n");
printf("| 8. 文件读取 |\n");
printf("| 9. 退出系统 |\n");
printf("===============================================\n");
printf("请选择操作(1 ~ 9):");
}
// 商品信息输入
void input() {
printf("请输入商品个数:");
scanf("%d", &numOfGoods);
for (int i = 0; i < numOfGoods; i++) {
printf("请输入第 %d 个商品信息:\n", i + 1);
printf("商品编号:");
scanf("%d", &goods[i].id);
printf("商品名称:");
scanf("%s", goods[i].name); // 不要用gets函数,容易导致溢出
printf("商品价格:");
scanf("%d", &goods[i].price);
printf("商品数量:");
scanf("%d", &goods[i].count);
}
}
// 商品信息输出
void output() {
printf("商品编号\t商品名称\t商品价格\t商品数量\n");
for (int i = 0; i < numOfGoods; i++) {
printf("%d\t%s\t%d\t%d\n", goods[i].id, goods[i].name, goods[i].price, goods[i].count);
}
}
// 添加商品(进货)
void addGoods() {
if (numOfGoods == MAX_GOODS_NUM) {
printf("商品信息已满,无法添加!\n");
return;
}
printf("请输入添加商品的信息:\n");
printf("商品编号:");
scanf("%d", &goods[numOfGoods].id);
printf("商品名称:");
scanf("%s", goods[numOfGoods].name);
printf("商品价格:");
scanf("%d", &goods[numOfGoods].price);
printf("商品数量:");
scanf("%d", &goods[numOfGoods].count);
numOfGoods ++;
}
// 删除商品(销售)
void deleteGoods() {
int id;
printf("请输入要删除的商品编号:");
scanf("%d", &id);
for (int i = 0; i < numOfGoods; i++) {
if (goods[i].id == id) { // 找到了要删除的商品
for (int j = i; j < numOfGoods - 1; j++) {
// 将后面的商品向前移动一位
goods[j] = goods[j+1];
}
numOfGoods --;
printf("商品删除成功!\n");
return;
}
}
printf("没有找到该商品,删除失败!\n");
}
// 查询商品(按名称)
void searchGoods() {
char name[MAX_GOODS_NAME_LEN];
printf("请输入要查询的商品名称:");
scanf("%s", name);
for (int i = 0; i < numOfGoods; i++) {
if (strcmp(goods[i].name, name) == 0) { // 找到了要查询的商品
printf("商品编号\t商品名称\t商品价格\t商品数量\n");
printf("%d\t%s\t%d\t%d\n", goods[i].id, goods[i].name, goods[i].price, goods[i].count);
return;
}
}
printf("没有找到该商品,查询失败!\n");
}
// 排序商品(按编号,名称,数量)
int compareID(const void* a, const void* b) {
GoodsInfo* ga = (GoodsInfo*)a;
GoodsInfo* gb = (GoodsInfo*)b;
return ga->id - gb->id;
}
int compareName(const void* a, const void* b) {
GoodsInfo* ga = (GoodsInfo*)a;
GoodsInfo* gb = (GoodsInfo*)b;
return strcmp(ga->name, gb->name);
}
int compareCount(const void* a, const void* b) {
GoodsInfo* ga = (GoodsInfo*)a;
GoodsInfo* gb = (GoodsInfo*)b;
return ga->count - gb->count;
}
void sortGoods() {
int choice;
printf("请选择排序方式:\n");
printf("1. 按编号排序\n");
printf("2. 按名称排序\n");
printf("3. 按数量排序\n");
printf("请选择(1 ~ 3):");
scanf("%d", &choice);
switch (choice) {
case 1:
qsort(goods, numOfGoods, sizeof(GoodsInfo), compareID);
printf("已按编号排序完成!\n");
break;
case 2:
qsort(goods, numOfGoods, sizeof(GoodsInfo), compareName);
printf("已按名称排序完成!\n");
break;
case 3:
qsort(goods, numOfGoods, sizeof(GoodsInfo), compareCount);
printf("已按数量排序完成!\n");
break;
default:
printf("请选择正确的操作!\n");
break;
}
}
// 文件保存
void saveToFile() {
FILE* fp = fopen("goodsData.txt", "w");
if (fp == NULL) {
printf("文件打开失败,保存失败!\n");
return;
}
fprintf(fp, "%d\n", numOfGoods);
for (int i = 0; i < numOfGoods; i++) {
fprintf(fp, "%d\t%s\t%d\t%d\n", goods[i].id, goods[i].name, goods[i].price, goods[i].count);
}
fclose(fp);
printf("文件已保存到 goodsData.txt 中!\n");
}
// 文件读取
void readFromFile() {
FILE* fp = fopen("goodsData.txt", "r");
if (fp == NULL) {
printf("文件打开失败,读取失败!\n");
return;
}
fscanf(fp, "%d", &numOfGoods);
for (int i = 0; i < numOfGoods; i++) {
fscanf(fp, "%d%s%d%d", &goods[i].id, goods[i].name, &goods[i].price, &goods[i].count);
}
fclose(fp);
printf("文件已从 goodsData.txt 中读取!\n");
}
int main() {
int choice;
while(1) {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
input();
break;
case 2:
output();
break;
case 3:
addGoods();
break;
case 4:
deleteGoods();
break;
case 5:
searchGoods();
break;
case 6:
sortGoods();
break;
case 7:
saveToFile();
break;
case 8:
readFromFile();
break;
case 9:
printf("感谢使用,再见!\n");
return 0;
default:
printf("请选择正确的操作!\n");
break;
}
}
return 0;
}
以上就是商品信息管理系统的C语言实现,其中的数据结构和算法非常简单,可以根据需要进行扩展和优化。
如果我的回答解决了您的问题,请采纳!
以下是一个简单的 C 语言商品信息管理系统代码,可以实现基本的商品管理功能:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PRODUCTS 100 // 最多100个商品
#define MAX_NAME_LENGTH 50 // 商品名最大长度
typedef struct Product {
int id; // 商品编号
char name[MAX_NAME_LENGTH]; // 商品名
int quantity; // 商品数量
float price; // 商品价格
} Product;
Product products[MAX_PRODUCTS]; // 商品列表
int num_products = 0; // 商品数
// 添加一个商品
void add_product() {
if (num_products == MAX_PRODUCTS) {
printf("商品列表已满,无法添加新商品。\n");
return;
}
Product p;
printf("请输入商品编号:");
scanf("%d", &p.id);
printf("请输入商品名称:");
scanf("%s", p.name);
printf("请输入商品数量:");
scanf("%d", &p.quantity);
printf("请输入商品价格:");
scanf("%f", &p.price);
products[num_products++] = p;
printf("商品已添加。\n");
}
// 删除一个商品
void delete_product() {
int id;
printf("请输入要删除的商品编号:");
scanf("%d", &id);
for (int i = 0; i < num_products; i++) {
if (products[i].id == id) {
for (int j = i + 1; j < num_products; j++) {
products[j - 1] = products[j];
}
num_products--;
printf("商品已删除。\n");
return;
}
}
printf("找不到编号为%d的商品。\n", id);
}
// 查询一个商品
void find_product() {
char name[MAX_NAME_LENGTH];
printf("请输入要查询的商品名称:");
scanf("%s", name);
for (int i = 0; i < num_products; i++) {
if (strcmp(products[i].name, name) == 0) {
printf("编号:%d,名称:%s,数量:%d,价格:%f\n",
products[i].id, products[i].name, products[i].quantity, products[i].price);
return;
}
}
printf("找不到名称为%s的商品。\n", name);
}
// 排序商品列表
void sort_products() {
int sort_type;
printf("请选择排序方式:1.按编号 2.按名称 3.按数量\n");
scanf("%d", &sort_type);
switch (sort_type) {
case 1:
for (int i = 0; i < num_products; i++) {
for (int j = i + 1; j < num_products; j++) {
if (products[i].id > products[j].id) {
Product temp = products[i];
products[i] = products[j];
products[j] = temp;
}
}
}
printf("商品已按编号排序。\n");
break;
case 2:
for (int i = 0; i < num_products; i++) {
for (int j = i + 1; j < num_products; j++) {
if (strcmp(products[i].name, products[j].name) > 0) {
Product temp = products[i];
products[i] = products[j];
products[j] = temp;
}
}
}