C语言文具销售,有简单的菜单,实现信息录入功能,在已有信息上添加新的记录,能够删除指定商品编号的记录,能够按编号查询设备,能够计算当日销售额,创建的链表或数组可存储于文件中,并能从文件中读取所存储的数据。带注释
时间: 2024-04-12 13:32:10 浏览: 76
以下是一个简单的C语言程序,实现了你描述的功能,并附有注释说明:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义商品结构体
typedef struct {
int id; // 商品编号
char name[50]; // 商品名称
float price; // 商品价格
} Product;
// 定义链表节点结构体
typedef struct Node {
Product product; // 商品信息
struct Node* next; // 下一个节点指针
} Node;
// 全局变量 - 链表头指针
Node* head = NULL;
// 函数声明
void displayMenu();
void addProduct();
void deleteProduct();
void searchProduct();
void calculateSales();
void saveDataToFile();
void loadDataFromFile();
int main() {
int choice;
loadDataFromFile(); // 从文件中加载数据
do {
displayMenu(); // 显示菜单
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addProduct();
break;
case 2:
deleteProduct();
break;
case 3:
searchProduct();
break;
case 4:
calculateSales();
break;
case 5:
saveDataToFile();
break;
case 6:
loadDataFromFile();
break;
case 0:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
printf("\n");
} while (choice != 0);
return 0;
}
// 显示菜单选项
void displayMenu() {
printf("========== Menu ==========\n");
printf("1. Add product\n");
printf("2. Delete product\n");
printf("3. Search product\n");
printf("4. Calculate sales\n");
printf("5. Save data to file\n");
printf("6. Load data from file\n");
printf("0. Exit\n");
}
// 添加商品
void addProduct() {
Node* newNode = (Node*)malloc(sizeof(Node));
printf("Enter product ID: ");
scanf("%d", &newNode->product.id);
printf("Enter product name: ");
scanf("%s", newNode->product.name);
printf("Enter product price: ");
scanf("%f", &newNode->product.price);
newNode->next = head;
head = newNode;
printf("Product added successfully.\n");
}
// 删除商品
void deleteProduct() {
int id;
if (head == NULL) {
printf("No products available.\n");
return;
}
printf("Enter product ID to delete: ");
scanf("%d", &id);
Node* current = head;
Node* prev = NULL;
while (current != NULL) {
if (current->product.id == id) {
if (prev == NULL) {
// 删除头节点
head = current->next;
} else {
// 删除中间或尾部节点
prev->next = current->next;
}
free(current);
printf("Product deleted successfully.\n");
return;
}
prev = current;
current = current->next;
}
printf("Product not found.\n");
}
// 查询商品
void searchProduct() {
int id;
if (head == NULL) {
printf("No products available.\n");
return;
}
printf("Enter product ID to search: ");
scanf("%d", &id);
Node* current = head;
while (current != NULL) {
if (current->product.id == id) {
printf("Product found:\n");
printf("ID: %d\n", current->product.id);
printf("Name: %s\n", current->product.name);
printf("Price: %.2f\n", current->product.price);
return;
}
current = current->next;
}
printf("Product not found.\n");
}
// 计算销售额
void calculateSales() {
float totalSales = 0;
Node* current = head;
while (current != NULL) {
totalSales += current->product.price;
current = current->next;
}
printf("Total sales: %.2f\n", totalSales);
}
// 将数据保存到文件
void saveDataToFile() {
FILE* file = fopen("data.txt", "w");
if (file == NULL) {
printf("Failed to open file.\n");
return;
}
Node* current = head;
while (current != NULL) {
fprintf(file, "%d,%s,%.2f\n", current->product.id, current->product.name, current->product.price);
current = current->next;
}
fclose(file);
printf("Data saved to file.\n");
}
// 从文件中加载数据
void loadDataFromFile() {
FILE* file = fopen("data.txt", "r");
if (file == NULL) {
printf("Failed to open file.\n");
return;
}
char line[100];
// 清空链表
while (head != NULL) {
Node* temp = head;
head = head->next;
free(temp);
}
// 逐行读取文件并创建链表节点
while (fgets(line, sizeof(line), file) != NULL) {
Node* newNode = (Node*)malloc(sizeof(Node));
sscanf(line, "%d,%[^,],%f", &newNode->product.id, newNode->product.name, &newNode->product.price);
newNode->next = head;
head = newNode;
}
fclose(file);
printf("Data loaded from file.\n");
}
```
请注意,此示例只是一个基本实现,可能需要根据实际需求进行进一步修改和完善。例如,可以添加输入验证、错误处理和其他功能。另外,此示例使用了简单的链表数据结构来存储商品信息,你还可以尝试使用数组或其他数据结构来实现。
阅读全文