用C语言编写网上购物系统代码
时间: 2023-09-28 08:05:45 浏览: 221
C语言实现网上购物系统
<width>46</width>
<height>13</height>
</rect>
</property>
<property name="由于网上购物系统是一个比较复杂的系统,代码量比较大,这里提供一个基于text">
<string>Port:</string>
</property>
</widget>
<widget class="QLabel" name="ba控制台的简单实现代码供你参考,仅供学习交流使用:
```c
#include <stdio.hudRateLabel">
<property name="geometry">
<rect>
<x>170</x>
<y>40</>
#include <stdlib.h>
#include <string.h>
// 商品结构体
typedef struct {
char name[20]; // 商品名称y>
<width>66</width>
<height>13</height>
</rect>
</property>
<property
float price; // 商品价格
int stock; // 商品库存
} Goods;
// 用户结构体
name="text">
<string>Baud Rate:</string>
</property>
</widget>
<widget class="QLabel"typedef struct {
char name[20]; // 用户名
char pwd[20]; // 密码
float balance; // name="parityLabel">
<property name="geometry">
<rect>
<x>270</x>
<y>40 账户余额
} User;
// 购物车结构体
typedef struct CartNode {
Goods goods; // 商品信息</y>
<width>46</width>
<height>13</height>
</rect>
</property>
<
int num; // 商品数量
struct CartNode *next; // 指向下一个节点的指针
} CartNode, *CartList;
// 商品信息文件
const char *GOODS_FILE = "goods.txt";
// 用户信息文件
constproperty name="text">
<string>Parity:</string>
</property>
</widget>
<widget class="QLabel char *USER_FILE = "user.txt";
// 订单信息文件
const char *ORDER_FILE = "order.txt";
// 清空输入缓" name="dataBitsLabel">
<property name="geometry">
<rect>
<x>370</x>
<y冲区
void clearInput() {
char c;
while ((c = getchar()) != '\n' && c != EOF);
}
>40</y>
<width>61</width>
<height>13</height>
</rect>
</property>
// 读取商品信息
void readGoods(Goods *goods, FILE *fp) {
fscanf(fp, "%s %f % <property name="text">
<string>Data Bits:</string>
</property>
</widget>
<widget class="Qd", goods->name, &goods->price, &goods->stock);
}
// 读取用户信息
void readUser(User *Label" name="stopBitsLabel">
<property name="geometry">
<rect>
<x>470</x>
<user, FILE *fp) {
fscanf(fp, "%s %s %f", user->name, user->pwd, &user->y>40</y>
<width>51</width>
<height>13</height>
</rect>
</propertybalance);
}
// 写入用户信息
void writeUser(User *user, FILE *fp) {
fprintf(fp, "%s %s %f\n", user->name, user->pwd, user->balance);
}
// 显示商品信息
void showGoods() {
FILE>
<property name="text">
<string>Stop Bits:</string>
</property>
</widget>
<widget class *fp = fopen(GOODS_FILE, "r");
if (fp == NULL) {
printf("商品信息文件不存在!\n="QLabel" name="flowControlLabel">
<property name="geometry">
<rect>
<x>430</x>
");
return;
}
Goods goods;
printf("商品名称\t商品价格\t商品库存\n");
printf("---------------------------------- <y>40</y>
<width>71</width>
<height>13</height>
</rect>
\n");
while (!feof(fp)) {
readGoods(&goods, fp);
printf("%s\t\t%.2f\t\t </property>
<property name="text">
<string>Flow Control:</string>
</property>
</widget>
<%d\n", goods.name, goods.price, goods.stock);
}
fclose(fp);
}
// 用户注册
void registerUser() {
User user;
printf("请输入用户名:");
scanf("%s", user.name);
FILE *fp = fopen(USER_FILE, "rwidget class="QPushButton" name="startButton">
<property name="geometry">
<rect>
<x>10</x>
");
if (fp == NULL) {
printf("用户信息文件不存在!\n");
return;
}
User tmp;
<y>70</y>
<width>75</width>
<height>23</height>
</rect>
while (!feof(fp)) {
readUser(&tmp, fp);
if (strcmp(tmp.name, user.name) == 0) {
</property>
<property name="text">
<string>Start</string>
</property>
<property name="default printf("用户名已存在!\n");
fclose(fp);
return;
}
}
fclose(fp);
printf("请输入">
<bool>true</bool>
</property>
</widget>
<widget class="QPushButton" name="stopButton">
密码:");
scanf("%s", user.pwd);
printf("请输入账户余额:");
scanf("%f", &user.balance);
fp = fopen(USER_FILE, "a");
writeUser(&user, fp);
fclose(fp);
printf("注册成功 <property name="geometry">
<rect>
<x>90</x>
<y>70</y>
<width!\n");
}
// 用户登录
User *login() {
User *user = (User *)malloc(sizeof(User));
printf("请输入>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
用户名:");
scanf("%s", user->name);
printf("请输入密码:");
scanf("%s", user->pwd);
<string>Stop</string>
</property>
</widget>
<widget class="QLineEdit" name="saveFilePathLineEdit">
FILE *fp = fopen(USER_FILE, "r");
if (fp == NULL) {
printf("用户信息文件不存在!\n");
<property name="geometry">
<rect>
<x>10</x>
<y>100</y>
< return NULL;
}
User tmp;
while (!feof(fp)) {
readUser(&tmp, fp);
if (strcmp(tmp.name, user->name) == 0 && strcmp(tmp.pwd, user->pwd) == 0) {
user->balancewidth>271</width>
<height>20</height>
</rect>
</property>
</widget>
<widget = tmp.balance;
fclose(fp);
return user;
}
}
fclose(fp);
free(user);
printf("用户名或密码错误!\n");
return NULL;
}
// 加入购物车
void addToCart(CartList *cart, Goods *goods class="QLabel" name="saveFilePathLabel">
<property name="geometry">
<rect>
<x>10</x) {
int num;
printf("请输入购买数量:");
scanf("%d", &num);
if (num <= >
<y>130</y>
<width>71</width>
<height>13</height>
</rect>
0) {
printf("购买数量不能小于等于0!\n");
return;
}
if (num > goods </property>
<property name="text">
<string>Received Data:</string>
</property>
</widget>
->stock) {
printf("库存不足!\n");
return;
}
CartList node = *cart;
while <widget class="QTextEdit" name="dataTextEdit">
<property name="geometry">
<rect>
<x>10 (node != NULL) {
if (strcmp(node->goods.name, goods->name) == 0) {
node->num +=</x>
<y>150</y>
<width>501</width>
<height>211</height>
</ num;
goods->stock -= num;
printf("已加入购物车!\n");
return;
}
noderect>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
= node->next;
}
CartNode *newNode = (CartNode *)malloc(sizeof(CartNode));
newNode->goods = </widget>
</widget>
<resources/>
<connections/>
</ui>
```
最后,在mainwindow.cpp文件中初始化界面和 *goods;
newNode->num = num;
newNode->next = NULL;
if (*cart == NULL) {
*cart =串口列表:
```c++
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSerialPortInfo>
MainWindow:: newNode;
} else {
node = *cart;
while (node->next != NULL) {
node = node->next;
MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
m }
node->next = newNode;
}
goods->stock -= num;
printf("已加入购物车!\n_serialThread = new SerialThread(this);
connect(m_serialThread, &SerialThread::finished, m_serialThread, &SerialThread::");
}
// 从购物车中删除商品
void removeFromCart(CartList *cart, const char *name) {
CartList node = *cart, prev = NULL;
while (node != NULL) {
if (strcmp(node->goods.name, name)deleteLater);
connect(m_serialThread, &SerialThread::dataReceived, this, &MainWindow::onDataReceived);
foreach ( == 0) {
Goods *goods = &node->goods;
goods->stock += node->num;
if (prev ==const QSerialPortInfo &portInfo, QSerialPortInfo::availablePorts())
{
ui->portComboBox->addItem(portInfo.port NULL) {
*cart = node->next;
} else {
prev->next = node->next;
}
freeName());
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_startButton_clicked()
{
m_serialThread(node);
printf("已从购物车中删除!\n");
return;
}
prev = node;
node =->setPortName(ui->portComboBox->currentText());
m_serialThread->setBaudRate(ui->baudRateComboBox->current node->next;
}
printf("购物车中不存在该商品!\n");
}
// 显示购物车中的商品
void showCart(CartList cart) {
if (cart == NULL) {
printf("购物车为空!\n");
returnText().toInt());
m_serialThread->setParity((QSerialPort::Parity)ui->parityComboBox->currentIndex());
m;
}
printf("商品名称\t商品单价\t商品数量\t商品总价\n");
printf("----------------------------------------------\n");
_serialThread->setDataBits((QSerialPort::DataBits)ui->dataBitsComboBox->currentIndex());
m_serialThread->setStopBits float totalPrice = 0;
while (cart != NULL) {
Goods *goods = &cart->goods;
printf("%s((QSerialPort::StopBits)ui->stopBitsComboBox->currentIndex());
m_serialThread->setFlowControl((QSerialPort::\t\t%.2f\t\t%d\t\t%.2f\n", goods->name, goods->price, cart->num, goods->FlowControl)ui->flowControlComboBox->currentIndex());
m_serialThread->setSaveFilePath(ui->saveFilePathLineEdit->text());
price * cart->num);
totalPrice += goods->price * cart->num;
cart = cart->next;
}
printf(" m_serialThread->start();
}
void MainWindow::on_stopButton_clicked()
{
m_serialThread->stop();
}
void MainWindow::on总价:%0.2f元\n", totalPrice);
}
// 生成订单
void generateOrder(User *user, CartList cart) {
if (cart == NULL) {
printf("购物车为空!\n");
return;
}
FILE *fp =DataReceived(const QByteArray &data)
{
ui->dataTextEdit->insertPlainText(data);
}
```
这样,就完成了一个基于QT的多线程串口数据读取和保存的程序。
阅读全文