From bb7a7dd43661f066711c296cdbb88ced3864c57e Mon Sep 17 00:00:00 2001 From: pamkeuxyv <2096010034@qq.com> Date: Wed, 29 May 2024 22:22:02 +0800 Subject: [PATCH] ADD file via upload --- untitled1.cpp | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 untitled1.cpp diff --git a/untitled1.cpp b/untitled1.cpp new file mode 100644 index 0000000..6083385 --- /dev/null +++ b/untitled1.cpp @@ -0,0 +1,167 @@ +#include +#include +#include + +//定义商品类型 +typedef struct goods{ + int id; + char name[20]; + int classification; + float price; + struct goods *next; +}Goods; + +//创建带头结点的商品信息单链表 +Goods *createList(){ + Goods *head = (Goods*)malloc(sizeof(Goods)); + head->next = NULL; + return head; +} + +//输入一个商品 +void inputWorker(Goods *goods){ + printf("请输入商品代码:"); + scanf("%d",&goods->id); + printf("请输入名称:"); + scanf("%s",goods->name); + printf("请输入分类:"); + scanf("%d",&goods->classification); + printf("请输入单价:"); + scanf("%f",&goods->price); +} + +//显示所有商品 +void displayAll(Goods *head){ + if(head->next == NULL){ + printf("列表为空!\n"); + return; + } + Goods *p = head->next; + printf("商品代码\t名称\t分类\t单价\n"); + while(p != NULL){ + printf("%d\t %s\t %d\t%.2f\n",p->id,p->name,p->classification,p->price); + p = p->next; + } +} + +//按照商品代码、商品名称和商品单价对所有商品进行递增排序 +void sort(Goods *head){ + Goods *p,*q,*tail; + tail = NULL; + while(head->next != tail){ + p = head; + q = p->next; + while(q->next != tail){ + if(q->id > q->next->id){ + p->next = q->next; + q->next = q->next->next; + p->next->next = q; + q = p->next; + } + p = p->next; + q = q->next; + } + tail = q; + } +} + +//删除指定商品代码的记录 +void deleteWorker(Goods *head,int id){ + Goods *p = head->next, *pre = head; + while(p != NULL){ + if(p->id == id){ + pre->next = p->next; + free(p); + printf("删除成功!\n"); + return; + } + pre = p; + p = p->next; + } + printf("未找到该商品代码!\n"); +} + +//查找指定商品代码的记录 +void searchWorker(Goods *head,int id){ + Goods *p = head->next; + while(p != NULL){ + if(p->id == id){ + printf("商品代码\t名称\t分类\t单价\n"); + printf("%d\t %s\t %d\t%.2f\n",p->id,p->name,p->classification,p->price); + return; + } + p = p->next; + } + printf("未找到该商品代码!\n"); +} + +//统计指定商品分类的商品数 +void countWorker(Goods *head,int classification){ + int count = 0; + Goods *p = head->next; + while(p != NULL){ + if(p->classification == classification){ + count++; + } + p = p->next; + } + printf("分类%d共有%d种商品\n",classification,count); +} + +int main(){ + Goods *head = createList(); + int choice = 0, id, classification; + while(1){ + printf("分类:1:服装 2.数码产品 3.护肤产品 4.食品 5.图书\n"); + printf("******************\n"); + printf("1.输入一个商品\n"); + printf("2.显示所有商品\n"); + printf("3.递增排序\n"); + printf("4.删除指定商品代码的商品\n"); + printf("5.查找指定商品代码的商品\n"); + printf("6.统计指定分类的商品数量\n"); + printf("0.退出系统\n"); + printf("******************\n"); + printf("请选择:"); + scanf("%d",&choice); + switch(choice){ + case 0: + printf("感谢使用!\n"); + return 0; + case 1: + { + Goods *goods = (Goods*)malloc(sizeof(Goods)); + inputWorker(goods); + goods->next = head->next; + head->next = goods; + break; + } + case 2: + displayAll(head); + break; + case 3: + sort(head); + printf("排序完成!\n"); + break; + case 4: + printf("请输入要删除的商品代码:"); + scanf("%d",&id); + deleteWorker(head,id); + break; + case 5: + printf("请输入要查找的商品代码:"); + scanf("%d",&id); + searchWorker(head,id); + break; + case 6: + printf("请输入要统计的分类:"); + scanf("%d",&classification); + countWorker(head,classification); + break; + default: + printf("输入有误,请重新输入!\n"); + break; + } + } + return 0; +}