当前位置: 首页 > news >正文

C语言实战系列二:简单超市收银系统

从一个简单的超市收银系统,我们来练习一个系统如何设计,然后如何实现的思路。

在Ubuntu环境下使用C语言编写一个简单的超市收银系统。以下是一个基本的示例,涵盖了商品管理、购物车、交易处理等功能。

代码

#include <stdio.h>
#include <stdlib.h>// 商品结构体
struct Product {int id;char name[50];float price;
};// 购物车项结构体
struct ShoppingCartItem {struct Product product;int quantity;
};// 交易结构体
struct Transaction {struct ShoppingCartItem *items;int itemCount;
};// 商品管理结构体
struct Inventory {struct Product *products;int productCount;
};// 初始化商品管理系统
struct Inventory initializeInventory() {struct Inventory inventory;inventory.products = NULL;inventory.productCount = 0;return inventory;
}// 添加商品到库存
void addProduct(struct Inventory *inventory, struct Product product) {inventory->products = realloc(inventory->products, (inventory->productCount + 1) * sizeof(struct Product));inventory->products[inventory->productCount++] = product;
}// 显示库存信息
void displayInventory(struct Inventory inventory) {printf("Inventory:\n");for (int i = 0; i < inventory.productCount; ++i) {printf("%d. %s - $%.2f\n", inventory.products[i].id, inventory.products[i].name, inventory.products[i].price);}
}// 获取商品信息
struct Product getProductInfo(struct Inventory inventory, int productId) {for (int i = 0; i < inventory.productCount; ++i) {if (inventory.products[i].id == productId) {return inventory.products[i];}}struct Product notFound;notFound.id = -1;return notFound;
}// 初始化交易
struct Transaction initializeTransaction() {struct Transaction transaction;transaction.items = NULL;transaction.itemCount = 0;return transaction;
}// 添加商品到购物车
void addToCart(struct Transaction *transaction, struct Product product, int quantity) {transaction->items = realloc(transaction->items, (transaction->itemCount + 1) * sizeof(struct ShoppingCartItem));transaction->items[transaction->itemCount].product = product;transaction->items[transaction->itemCount].quantity = quantity;++transaction->itemCount;printf("Added %d %s(s) to the cart.\n", quantity, product.name);
}// 显示购物车
void displayCart(struct Transaction transaction) {float total = 0.0;printf("Shopping Cart:\n");for (int i = 0; i < transaction.itemCount; ++i) {struct ShoppingCartItem item = transaction.items[i];printf("%s - Quantity: %d\n", item.product.name, item.quantity);total += item.product.price * item.quantity;}printf("Total Price: $%.2f\n", total);
}// 完成交易
void completeTransaction(struct Transaction transaction) {printf("Transaction completed. Thank you!\n");free(transaction.items);
}int main() {struct Inventory inventory = initializeInventory();// 添加一些商品addProduct(&inventory, (struct Product){1, "Milk", 2.5});addProduct(&inventory, (struct Product){2, "Bread", 1.0});addProduct(&inventory, (struct Product){3, "Eggs", 3.0});struct Transaction transaction = initializeTransaction();// 超市收银系统while (1) {displayInventory(inventory);int productId;printf("Enter product ID to add to cart (or 0 to finish): ");scanf("%d", &productId);if (productId == 0) {break;}struct Product product = getProductInfo(inventory, productId);if (product.id != -1) {int quantity;printf("Enter quantity: ");scanf("%d", &quantity);addToCart(&transaction, product, quantity);} else {printf("Product not found.\n");}}displayCart(transaction);completeTransaction(transaction);// 释放资源free(inventory.products);return 0;
}

编译:

gcc supermarket.c -o supermarket
./supermarket

程序设计的详细过程:

步骤 1: 定义数据模型

在程序设计的起始阶段,定义了程序需要用到的数据模型,包括商品、购物车项和交易。使用结构体表示这些概念,其中包括商品(Product)、购物车项(ShoppingCartItem)和交易(Transaction)。

struct Product {int id;char name[50];float price;
};struct ShoppingCartItem {struct Product product;int quantity;
};struct Transaction {struct ShoppingCartItem *items;int itemCount;
};

步骤 2: 商品管理

设计商品管理系统,包括添加商品到库存和获取商品信息的功能。使用结构体 Inventory 来保存商品信息,并定义相应的函数。

struct Inventory {struct Product *products;int productCount;
};// 添加商品到库存
void addProduct(struct Inventory *inventory, struct Product product) {// 通过realloc动态分配内存以保存商品inventory->products = realloc(inventory->products, (inventory->productCount + 1) * sizeof(struct Product));inventory->products[inventory->productCount++] = product;
}// 获取商品信息
struct Product getProductInfo(struct Inventory inventory, int productId) {for (int i = 0; i < inventory.productCount; ++i) {if (inventory.products[i].id == productId) {return inventory.products[i];}}struct Product notFound;notFound.id = -1;return notFound;
}

步骤 3: 用户界面

设计用户界面,实现基本的用户交互,允许用户选择商品并添加到购物车。使用结构体 CashierUI 和相应的函数。

struct CashierUI {struct Inventory inventory;struct Transaction transaction;
};// 显示库存信息
void displayInventory(struct Inventory inventory) {// 输出商品列表
}// 添加商品到购物车
void addToCart(struct Transaction *transaction, struct Product product, int quantity) {// 将商品添加到购物车
}// 显示购物车信息
void displayCart(struct Transaction transaction) {// 输出购物车内容
}// 完成交易
void completeTransaction(struct Transaction transaction) {// 输出总价和完成交易信息
}

步骤 4: 交易处理

实现交易处理功能,计算购物车中商品的总价并完成交易。

// 初始化交易
struct Transaction initializeTransaction() {struct Transaction transaction;transaction.items = NULL;transaction.itemCount = 0;return transaction;
}// 完成交易
void completeTransaction(struct Transaction transaction) {// 输出总价和完成交易信息// 释放购物车内存free(transaction.items);
}

步骤 5: 主程序

在主程序中,创建超市收银系统实例,添加一些商品,然后启动收银系统。

int main() {// 初始化商品管理系统struct Inventory inventory = initializeInventory();// 添加一些商品addProduct(&inventory, (struct Product){1, "Milk", 2.5});addProduct(&inventory, (struct Product){2, "Bread", 1.0});addProduct(&inventory, (struct Product){3, "Eggs", 3.0});// 初始化交易struct Transaction transaction = initializeTransaction();// 超市收银系统while (1) {// 显示库存信息displayInventory(inventory);int productId;printf("Enter product ID to add to cart (or 0 to finish): ");scanf("%d", &productId);if (productId == 0) {break;}// 获取商品信息struct Product product = getProductInfo(inventory, productId);if (product.id != -1) {int quantity;printf("Enter quantity: ");scanf("%d", &quantity);// 添加商品到购物车addToCart(&transaction, product, quantity);} else {printf("Product not found.\n");}}// 显示购物车displayCart(transaction);// 完成交易completeTransaction(transaction);// 释放资源free(inventory.products);return 0;
}

这个设计过程包括了程序的整体结构、数据模型的定义、功能的实现以及用户界面的设计。当设计一个更大规模的系统时,可能需要考虑更多的功能,例如支付处理、库存管理、用户认证等。这个简化的例子提供了一个基本的框架,可以根据实际需求进行扩展。

http://www.lryc.cn/news/288166.html

相关文章:

  • coding推送代码Jenkins自动构建部署
  • Kettle-Docker部署+Sqlserver数据同步Mysql+Start定时任务
  • 《微信小程序开发从入门到实战》学习九十三
  • Java服务端使用freemarker+wkhtmltoimage生成Echart图片
  • 一款颜值与实力并存的翻页时钟(免费)
  • Objective-C方法的声明实现及调用
  • 第十四届蓝桥杯国赛 C++ B 组 C 题——班级活动(AC)
  • GraphQL的力量:简化复杂数据查询
  • python环境安装sklearn及报错解决
  • log4j:WARN Please initialize the log4j system properly的解决办法
  • 虹科分享丨汽车技术的未来:Netropy如何测试和确保汽车以太网的性能
  • 代码CE:reference to ‘XX‘ is ambiguous
  • 如果想将企业微信的组织架构同步到内部知识库咋搞?方法来也!
  • 【c语言】扫雷
  • 自然语言处理的崛起:从初步分析到深度理解
  • Git学习笔记:版本回滚
  • OpenCV图像的基本操作
  • 小白水平理解面试经典题目LeetCode 594 Longest Harmonious Subsequence(最大和谐字符串)
  • Vue-35、Vue中使用ref属性
  • 网络通信(15)-C#TCP客户端掉线重连实例
  • React进阶 - 14(说一说”虚拟DOM“中的”Diff算法“)
  • #GPU|LLM|AIGC#集成显卡与独立显卡|显卡在深度学习中的选择与LLM GPU推荐
  • HCIP-IPV6实验
  • 如何训练和导出模型
  • Springboot注解@Aspect(一)之@Aspect 作用和Aop关系详解
  • 自动化防DDoS脚本
  • ubuntu怎么查看有几个用户
  • Linux | makefile简单教程 | Makefile的工作原理
  • pcl+vtk(十四)vtkCamera相机简单介绍
  • TS基础知识点快速回顾(上)