用c语言编写商店销售管理系统
时间: 2023-10-23 15:08:33 浏览: 61
商店销售管理系统的开发需要使用C语言的基本语法、数据结构和文件操作等知识。以下是一个简单的商店销售管理系统的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ITEMS 100
struct item {
char name[50];
int quantity;
float price;
};
struct item inventory[MAX_ITEMS];
int num_items = 0;
void add_item() {
struct item new_item;
printf("Enter name of item: ");
scanf("%s", new_item.name);
printf("Enter quantity: ");
scanf("%d", &new_item.quantity);
printf("Enter price: ");
scanf("%f", &new_item.price);
inventory[num_items] = new_item;
num_items++;
}
void delete_item() {
char name[50];
printf("Enter name of item to delete: ");
scanf("%s", name);
int index = -1;
for (int i = 0; i < num_items; i++) {
if (strcmp(inventory[i].name, name) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("Item not found.\n");
} else {
for (int i = index; i < num_items - 1; i++) {
inventory[i] = inventory[i + 1];
}
num_items--;
printf("Item deleted.\n");
}
}
void display_inventory() {
printf("Name\tQuantity\tPrice\n");
for (int i = 0; i < num_items; i++) {
printf("%s\t%d\t\t%.2f\n", inventory[i].name, inventory[i].quantity, inventory[i].price);
}
}
void save_inventory() {
FILE *f = fopen("inventory.txt", "w");
if (f == NULL) {
printf("Error opening file.\n");
return;
}
for (int i = 0; i < num_items; i++) {
fprintf(f, "%s,%d,%.2f\n", inventory[i].name, inventory[i].quantity, inventory[i].price);
}
fclose(f);
printf("Inventory saved.\n");
}
void load_inventory() {
FILE *f = fopen("inventory.txt", "r");
if (f == NULL) {
printf("Error opening file.\n");
return;
}
char line[100];
while (fgets(line, 100, f) != NULL) {
char *token = strtok(line, ",");
strcpy(inventory[num_items].name, token);
token = strtok(NULL, ",");
inventory[num_items].quantity = atoi(token);
token = strtok(NULL, ",");
inventory[num_items].price = atof(token);
num_items++;
}
fclose(f);
printf("Inventory loaded.\n");
}
int main() {
int choice = 0;
while (choice != 6) {
printf("\n1. Add item\n");
printf("2. Delete item\n");
printf("3. Display inventory\n");
printf("4. Save inventory\n");
printf("5. Load inventory\n");
printf("6. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_item();
break;
case 2:
delete_item();
break;
case 3:
display_inventory();
break;
case 4:
save_inventory();
break;
case 5:
load_inventory();
break;
case 6:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice.\n");
break;
}
}
return 0;
}
```
该程序通过结构体数组存储库存信息,可以添加、删除、显示、保存和加载库存数据。在程序中,库存数据被保存在名为“inventory.txt”的文本文件中。
阅读全文