利用C语言写出具有下列功能的程序使用链表 设计简单的菜单,能够进行系统功能选择 2.实现信息的录入功能。 3.在已有信息的基础上添加新的记录。 4.删除指定商品编号的记录。 5.修改指定商品编号的记录6按设备编号查询功能 7.能够计算出当日销售额 带注释 1.
时间: 2024-04-12 11:32:33 浏览: 26
下面是一个使用链表实现菜单功能的C语言程序:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义商品结构体
typedef struct {
int id;
char name[20];
float price;
int quantity;
} Product;
// 定义链表节点结构体
typedef struct Node {
Product data;
struct Node* next;
} Node;
// 初始化链表
Node* initList() {
Node* head = (Node*)malloc(sizeof(Node));
head->next = NULL;
return head;
}
// 添加商品记录
void addRecord(Node* head) {
Node* p = head;
while (p->next != NULL) {
p = p->next;
}
Node* newNode = (Node*)malloc(sizeof(Node));
printf("请输入商品编号:");
scanf("%d", &newNode->data.id);
printf("请输入商品名称:");
scanf("%s", newNode->data.name);
printf("请输入商品价格:");
scanf("%f", &newNode->data.price);
printf("请输入商品数量:");
scanf("%d", &newNode->data.quantity);
newNode->next = NULL;
p->next = newNode;
}
// 删除指定商品编号的记录
void deleteRecord(Node* head, int id) {
Node* p = head->next;
Node* prev = head;
while (p != NULL) {
if (p->data.id == id) {
prev->next = p->next;
free(p);
printf("删除成功!\n");
return;
}
prev = p;
p = p->next;
}
printf("未找到指定商品编号的记录!\n");
}
// 修改指定商品编号的记录
void modifyRecord(Node* head, int id) {
Node* p = head->next;
while (p != NULL) {
if (p->data.id == id) {
printf("请输入新的商品名称:");
scanf("%s", p->data.name);
printf("请输入新的商品价格:");
scanf("%f", &p->data.price);
printf("请输入新的商品数量:");
scanf("%d", &p->data.quantity);
printf("修改成功!\n");
return;
}
p = p->next;
}
printf("未找到指定商品编号的记录!\n");
}
// 按设备编号查询功能
void searchRecord(Node* head, int id) {
Node* p = head->next;
while (p != NULL) {
if (p->data.id == id) {
printf("商品编号:%d\n", p->data.id);
printf("商品名称:%s\n", p->data.name);
printf("商品价格:%.2f\n", p->data.price);
printf("商品数量:%d\n", p->data.quantity);
return;
}
p = p->next;
}
printf("未找到指定商品编号的记录!\n");
}
// 计算当日销售额
float calculateSales(Node* head) {
Node* p = head->next;
float totalSales = 0;
while (p != NULL) {
totalSales += p->data.price * p->data.quantity;
p = p->next;
}
return totalSales;
}
int main() {
Node* head = initList();
int choice;
int id;
while (1) {
printf("\n1.退出\n");
printf("2.信息录入\n");
printf("3.添加新记录\n");
printf("4.删除指定商品编号的记录\n");
printf("5.修改指定商品编号的记录\n");
printf("6.按设备编号查询\n");
printf("7.计算当日销售额\n");
printf("请选择功能:");
scanf("%d", &choice);
switch (choice) {
case 1:
return 0;
case 2:
addRecord(head);
break;
case 3:
addRecord(head);
break;
case 4:
printf("请输入要删除的商品编号:");
scanf("%d", &id);
deleteRecord(head, id);
break;
case 5:
printf("请输入要修改的商品编号:");
scanf("%d", &id);
modifyRecord(head, id);
break;
case 6:
printf("请输入要查询的商品编号:");
scanf("%d", &id);
searchRecord(head, id);
break;
case 7:
printf("当日销售额为:%.2f\n", calculateSales(head));
break;
default:
printf("无效的选择!\n");
}
}
return 0;
}
```
这个程序使用链表来存储商品信息,通过菜单选择不同的功能实现信息录入、添加记录、删除记录、修改记录、查询记录和计算当日销售额等功能。每个功能都有相应的函数实现,并且带有注释说明。你可以根据自己的需求进行修改和扩展。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)