从一个简单的超市收银系统,我们来练习一个系统如何设计,然后如何实现的思路。
在Ubuntu环境下使用C语言编写一个简单的超市收银系统。以下是一个基本的示例,涵盖了商品管理、购物车、交易处理等功能。
#include
#include
// 商品结构体
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;
}
这个设计过程包括了程序的整体结构、数据模型的定义、功能的实现以及用户界面的设计。当设计一个更大规模的系统时,可能需要考虑更多的功能,例如支付处理、库存管理、用户认证等。这个简化的例子提供了一个基本的框架,可以根据实际需求进行扩展。