用st语言杭州有一仓储中心,存放 A BC 三种货物,库存信息如下,该仓储中心收到多个客户的订单,每个订单信息包括:订单号(word ) 、地址(枚举体)、客户名称(Wstring)、货物型号及数量( A B C )(Uint) ; 1 .用以上数据类型标识每个订单信息(结构体); 2 .生成 FB ,并在主程序中调用,要求实现输入 A B C 任意一种货物即可查询仓储中心在完成订单后,对应类型的货物的余量,货物数量不足无法出库则输出:无法出库.否则输出:出库成功; 3 .生成FB,并在主程序中调用,要求实现输入对应城市即可查询该城市所有的货物订单数量;
时间: 2023-05-14 13:06:38 浏览: 113
行业资料-交通装置-一种汽车BC柱外加强板零件模具.zip
1. 以下是用以上数据类型标识每个订单信息的结构体:
struct OrderInfo {
std::string orderNumber;
enum Address { Hangzhou, Shanghai, Beijing } address;
std::wstring customerName;
struct Goods {
uint32_t A;
uint32_t B;
uint32_t C;
} goods;
};
2. 以下是生成FB,并在主程序中调用,实现输入 A B C 任意一种货物即可查询仓储中心在完成订单后,对应类型的货物的余量,货物数量不足无法出库则输出:无法出库.否则输出:出库成功:
#include <iostream>
#include <string>
#include <vector>
#include <flatbuffers/flatbuffers.h>
#include "OrderInfo_generated.h"
using namespace MyNamespace;
int main() {
// 初始化仓储中心的库存信息
uint32_t A_count = 100;
uint32_t B_count = 200;
uint32_t C_count = 300;
// 生成订单信息
OrderInfo order;
order.orderNumber = "20220101001";
order.address = OrderInfo::Hangzhou;
order.customerName = L"张三";
order.goods.A = 10;
order.goods.B = 20;
order.goods.C = 30;
// 序列化订单信息
flatbuffers::FlatBufferBuilder builder;
auto order_offset = CreateOrderInfo(builder,
builder.CreateString(order.orderNumber),
order.address,
builder.CreateString(order.customerName),
CreateGoods(builder, order.goods.A, order.goods.B, order.goods.C));
builder.Finish(order_offset);
// 反序列化订单信息
auto order_info = GetOrderInfo(builder.GetBufferPointer());
// 查询货物余量
uint32_t count = 0;
if (order_info->goods()->A() > 0) {
count = A_count;
} else if (order_info->goods()->B() > 0) {
count = B_count;
} else if (order_info->goods()->C() > 0) {
count = C_count;
}
// 出库
if (count >= order_info->goods()->A() && count >= order_info->goods()->B() && count >= order_info->goods()->C()) {
A_count -= order_info->goods()->A();
B_count -= order_info->goods()->B();
C_count -= order_info->goods()->C();
std::cout << "出库成功" << std::endl;
} else {
std::cout << "无法出库" << std::endl;
}
return 0;
}
3. 以下是生成FB,并在主程序中调用,实现输入对应城市即可查询该城市所有的货物订单数量:
#include <iostream>
#include <string>
#include <vector>
#include <flatbuffers/flatbuffers.h>
#include "OrderInfo_generated.h"
using namespace MyNamespace;
int main() {
// 初始化订单信息
std::vector<OrderInfo> orders;
OrderInfo order1;
order1.orderNumber = "20220101001";
order1.address = OrderInfo::Hangzhou;
order1.customerName = L"张三";
order1.goods.A = 10;
order1.goods.B = 20;
order1.goods.C = 30;
orders.push_back(order1);
OrderInfo order2;
order2.orderNumber = "20220101002";
order2.address = OrderInfo::Shanghai;
order2.customerName = L"李四";
order2.goods.A = 5;
order2.goods.B = 10;
order2.goods.C = 15;
orders.push_back(order2);
OrderInfo order3;
order3.orderNumber = "20220101003";
order3.address = OrderInfo::Hangzhou;
order3.customerName = L"王五";
order3.goods.A = 15;
order3.goods.B = 30;
order3.goods.C = 45;
orders.push_back(order3);
// 序列化订单信息
flatbuffers::FlatBufferBuilder builder;
std::vector<flatbuffers::Offset<OrderInfo>> order_offsets;
for (auto& order : orders) {
auto order_offset = CreateOrderInfo(builder,
builder.CreateString(order.orderNumber),
order.address,
builder.CreateString(order.customerName),
CreateGoods(builder, order.goods.A, order.goods.B, order.goods.C));
order_offsets.push_back(order_offset);
}
auto orders_offset = builder.CreateVector(order_offsets);
builder.Finish(CreateOrderList(builder, orders_offset));
// 反序列化订单信息
auto order_list = GetOrderList(builder.GetBufferPointer());
// 查询城市所有货物订单数量
uint32_t A_count = 0;
uint32_t B_count = 0;
uint32_t C_count = 0;
for (auto& order : *order_list->orders()) {
if (order->address() == OrderInfo::Hangzhou) {
A_count += order->goods()->A();
B_count += order->goods()->B();
C_count += order->goods()->C();
}
}
std::cout << "杭州的 A 货物订单数量为:" << A_count << std::endl;
std::cout << "杭州的 B 货物订单数量为:" << B_count << std::endl;
std::cout << "杭州的 C 货物订单数量为:" << C_count << std::endl;
return 0;
}
阅读全文