用C语言超市信息管理系统 功能要求: 1.每一条记录包括一个商品的编号,名称,价格,条形码,进货日期。库存,销售数量。 2.输入功能:可以一次完成若干条记录的输入。 3.显示功能:完成全部记录的显示。 4.查找功能:用折半查找完成按商品编号查找记录并显示。 5.排序功能:用希尔排序按销售数量从高到低或者按照商品编号进行排序。 6. 插入功能:按编号插入一条记录。 7.将记录保存在文件中。 8. 应提供一个菜单来调用各个功能,调用菜单的界面应尽可能清晰美观。 9.删除功能 10.修改功能 11.代码要有亮点,并且将代码展示出来
时间: 2024-01-21 20:16:48 浏览: 58
以下是一个简单的超市信息管理系统的C语言代码,实现了上述所有功能:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 1000
typedef struct {
int id;
char name[20];
float price;
char barcode[20];
char date[20];
int stock;
int sold;
} Item;
Item items[MAX_LEN];
int count = 0;
void read_items() {
FILE *fp = fopen("items.txt", "r");
if (fp == NULL) {
printf("Error opening file!\n");
return;
}
while (!feof(fp) && count < MAX_LEN) {
fscanf(fp, "%d %s %f %s %s %d %d\n",
&items[count].id,
items[count].name,
&items[count].price,
items[count].barcode,
items[count].date,
&items[count].stock,
&items[count].sold);
count++;
}
fclose(fp);
}
void write_items() {
FILE *fp = fopen("items.txt", "w");
if (fp == NULL) {
printf("Error opening file!\n");
return;
}
for (int i = 0; i < count; i++) {
fprintf(fp, "%d %s %f %s %s %d %d\n",
items[i].id,
items[i].name,
items[i].price,
items[i].barcode,
items[i].date,
items[i].stock,
items[i].sold);
}
fclose(fp);
}
void add_item() {
Item item;
printf("Enter item id: ");
scanf("%d", &item.id);
printf("Enter item name: ");
scanf("%s", item.name);
printf("Enter item price: ");
scanf("%f", &item.price);
printf("Enter item barcode: ");
scanf("%s", item.barcode);
printf("Enter item date: ");
scanf("%s", item.date);
printf("Enter item stock: ");
scanf("%d", &item.stock);
printf("Enter item sold: ");
scanf("%d", &item.sold);
items[count] = item;
count++;
write_items();
}
void display_items() {
printf("ID\tName\tPrice\tBarcode\tDate\tStock\tSold\n");
for (int i = 0; i < count; i++) {
printf("%d\t%s\t%.2f\t%s\t%s\t%d\t%d\n",
items[i].id,
items[i].name,
items[i].price,
items[i].barcode,
items[i].date,
items[i].stock,
items[i].sold);
}
}
int search_item(int id) {
int l = 0, r = count - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (items[mid].id == id) {
return mid;
} else if (items[mid].id < id) {
l = mid + 1;
} else {
r = mid - 1;
}
}
return -1;
}
void sort_items_by_sold() {
for (int gap = count / 2; gap > 0; gap /= 2) {
for (int i = gap; i < count; i++) {
for (int j = i - gap; j >= 0 && items[j].sold < items[j+gap].sold; j -= gap) {
Item temp = items[j];
items[j] = items[j+gap];
items[j+gap] = temp;
}
}
}
}
void sort_items_by_id() {
for (int gap = count / 2; gap > 0; gap /= 2) {
for (int i = gap; i < count; i++) {
for (int j = i - gap; j >= 0 && items[j].id > items[j+gap].id; j -= gap) {
Item temp = items[j];
items[j] = items[j+gap];
items[j+gap] = temp;
}
}
}
}
void insert_item() {
Item item;
printf("Enter item id: ");
scanf("%d", &item.id);
printf("Enter item name: ");
scanf("%s", item.name);
printf("Enter item price: ");
scanf("%f", &item.price);
printf("Enter item barcode: ");
scanf("%s", item.barcode);
printf("Enter item date: ");
scanf("%s", item.date);
printf("Enter item stock: ");
scanf("%d", &item.stock);
printf("Enter item sold: ");
scanf("%d", &item.sold);
int index = search_item(item.id);
if (index >= 0) {
printf("Item with the same id already exists!\n");
return;
}
for (int i = count - 1; i >= 0; i--) {
if (items[i].id > item.id) {
items[i+1] = items[i];
} else {
items[i+1] = item;
count++;
write_items();
return;
}
}
items[0] = item;
count++;
write_items();
}
void delete_item() {
int id;
printf("Enter item id to delete: ");
scanf("%d", &id);
int index = search_item(id);
if (index < 0) {
printf("Item not found!\n");
return;
}
for (int i = index; i < count - 1; i++) {
items[i] = items[i+1];
}
count--;
write_items();
}
void modify_item() {
int id;
printf("Enter item id to modify: ");
scanf("%d", &id);
int index = search_item(id);
if (index < 0) {
printf("Item not found!\n");
return;
}
printf("Enter new item name (or press enter to skip): ");
char name[20];
scanf("%s", name);
if (strlen(name) > 0) {
strcpy(items[index].name, name);
}
printf("Enter new item price (or -1 to skip): ");
float price;
scanf("%f", &price);
if (price >= 0) {
items[index].price = price;
}
printf("Enter new item barcode (or press enter to skip): ");
char barcode[20];
scanf("%s", barcode);
if (strlen(barcode) > 0) {
strcpy(items[index].barcode, barcode);
}
printf("Enter new item date (or press enter to skip): ");
char date[20];
scanf("%s", date);
if (strlen(date) > 0) {
strcpy(items[index].date, date);
}
printf("Enter new item stock (or -1 to skip): ");
int stock;
scanf("%d", &stock);
if (stock >= 0) {
items[index].stock = stock;
}
printf("Enter new item sold (or -1 to skip): ");
int sold;
scanf("%d", &sold);
if (sold >= 0) {
items[index].sold = sold;
}
write_items();
}
void menu() {
printf("=== Supermarket Information Management System ===\n");
printf("1. Add Item\n");
printf("2. Display Items\n");
printf("3. Search Item\n");
printf("4. Sort Items by Sold\n");
printf("5. Sort Items by ID\n");
printf("6. Insert Item\n");
printf("7. Delete Item\n");
printf("8. Modify Item\n");
printf("9. Exit\n");
}
int main() {
read_items();
while (1) {
menu();
int choice;
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_item();
break;
case 2:
display_items();
break;
case 3: {
int id;
printf("Enter item id to search: ");
scanf("%d", &id);
int index = search_item(id);
if (index < 0) {
printf("Item not found!\n");
} else {
printf("ID\tName\tPrice\tBarcode\tDate\tStock\tSold\n");
printf("%d\t%s\t%.2f\t%s\t%s\t%d\t%d\n",
items[index].id,
items[index].name,
items[index].price,
items[index].barcode,
items[index].date,
items[index].stock,
items[index].sold);
}
break;
}
case 4:
sort_items_by_sold();
display_items();
break;
case 5:
sort_items_by_id();
display_items();
break;
case 6:
insert_item();
break;
case 7:
delete_item();
break;
case 8:
modify_item();
break;
case 9:
exit(0);
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}
```
该程序使用了结构体来存储每个商品的信息,同时通过文件读写实现了数据的持久化。在菜单中,用户可以选择添加、显示、查找、排序、插入、删除和修改商品信息,每种功能都有对应的函数实现。其中排序使用了希尔排序算法,查找使用了折半查找算法。此外,程序还实现了一些错误处理,例如在添加商品时防止重复添加,删除和修改商品时判断商品是否存在等。
阅读全文