c语言超市管理系统源代码

时间: 2023-06-26 15:05:22 浏览: 55
以下是一个简单的 C 语言超市管理系统的源代码,仅供参考。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> struct product { int id; char name[50]; float price; int quantity; }; int main() { int n, i, choice, id, quantity, flag = 0; char name[50]; float price; struct product *products; printf("Enter the number of products: "); scanf("%d", &n); products = (struct product*) malloc(n * sizeof(struct product)); for (i = 0; i < n; i++) { printf("\nEnter product %d details:\n", i+1); printf("ID: "); scanf("%d", &products[i].id); printf("Name: "); scanf("%s", products[i].name); printf("Price: "); scanf("%f", &products[i].price); printf("Quantity: "); scanf("%d", &products[i].quantity); } while (1) { printf("\n\n"); printf("1. Display all products\n"); printf("2. Search for a product by ID\n"); printf("3. Search for a product by name\n"); printf("4. Add a new product\n"); printf("5. Update a product\n"); printf("6. Delete a product\n"); printf("7. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("\nAll products:\n"); for (i = 0; i < n; i++) { printf("ID: %d, Name: %s, Price: %0.2f, Quantity: %d\n", products[i].id, products[i].name, products[i].price, products[i].quantity); } break; case 2: printf("\nEnter the ID to search: "); scanf("%d", &id); for (i = 0; i < n; i++) { if (products[i].id == id) { printf("Product found:\n"); printf("ID: %d, Name: %s, Price: %0.2f, Quantity: %d\n", products[i].id, products[i].name, products[i].price, products[i].quantity); flag = 1; break; } } if (!flag) { printf("Product not found.\n"); } break; case 3: printf("\nEnter the name to search: "); scanf("%s", name); for (i = 0; i < n; i++) { if (strcmp(products[i].name, name) == 0) { printf("Product found:\n"); printf("ID: %d, Name: %s, Price: %0.2f, Quantity: %d\n", products[i].id, products[i].name, products[i].price, products[i].quantity); flag = 1; break; } } if (!flag) { printf("Product not found.\n"); } break; case 4: n++; products = (struct product*) realloc(products, n * sizeof(struct product)); printf("\nEnter product details:\n"); printf("ID: "); scanf("%d", &products[n-1].id); printf("Name: "); scanf("%s", products[n-1].name); printf("Price: "); scanf("%f", &products[n-1].price); printf("Quantity: "); scanf("%d", &products[n-1].quantity); printf("Product added successfully.\n"); break; case 5: printf("\nEnter the ID of the product to update: "); scanf("%d", &id); flag = 0; for (i = 0; i < n; i++) { if (products[i].id == id) { printf("Enter new details:\n"); printf("Name: "); scanf("%s", products[i].name); printf("Price: "); scanf("%f", &products[i].price); printf("Quantity: "); scanf("%d", &products[i].quantity); printf("Product updated successfully.\n"); flag = 1; break; } } if (!flag) { printf("Product not found.\n"); } break; case 6: printf("\nEnter the ID of the product to delete: "); scanf("%d", &id); flag = 0; for (i = 0; i < n; i++) { if (products[i].id == id) { for (int j = i; j < n - 1; j++) { products[j] = products[j+1]; } n--; products = (struct product*) realloc(products, n * sizeof(struct product)); printf("Product deleted successfully.\n"); flag = 1; break; } } if (!flag) { printf("Product not found.\n"); } break; case 7: free(products); exit(0); default: printf("Invalid choice. Try again.\n"); } } return 0; } ``` 注意:此代码仅供参考,可能存在漏洞和不完善之处。建议在实际应用中进行必要的修改和测试。

相关推荐

最新推荐

recommend-type

餐馆点菜系统C语言源代码

主要为大家详细介绍了餐馆点菜系统C语言源代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C语言实现简单航班管理系统

主要为大家详细介绍了C语言实现简单航班管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

学籍管理系统源代码 c++.docx

为了学校更加高效,快捷,方便的管理学生信息,并实现以下功能: (1)对学生信息进行录入:先输入学生的学籍,然后输入学生姓名,年龄,性别,籍贯,系别,专业,班级等,最后输入学生状态(入学)。 (2)对已录取...
recommend-type

C语言实现银行管理系统

主要为大家详细介绍了C语言实现银行管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C语言实现电话簿管理系统

主要为大家详细介绍了C语言实现电话簿管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

hive中 的Metastore

Hive中的Metastore是一个关键的组件,它用于存储和管理Hive中的元数据。这些元数据包括表名、列名、表的数据类型、分区信息、表的存储位置等信息。Hive的查询和分析都需要Metastore来管理和访问这些元数据。 Metastore可以使用不同的后端存储来存储元数据,例如MySQL、PostgreSQL、Oracle等关系型数据库,或者Hadoop分布式文件系统中的HDFS。Metastore还提供了API,使得开发人员可以通过编程方式访问元数据。 Metastore的另一个重要功能是跟踪表的版本和历史。当用户对表进行更改时,Metastore会记录这些更改,并且可以让用户回滚到
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。