以下是一个实现超市总营业额分析的程序,具体实现细节可以根据需求进行调整:
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// 销售记录结构体
struct SalesRecord {
string name; // 品名
int quantity; // 数量(个)
double price; // 单价(元)
};
// 存储一天的销售记录到文件
void saveSalesRecord(SalesRecord* sales, int size, const string& filename) {
ofstream outfile(filename.c_str());
if (outfile.is_open()) {
for (int i = 0; i < size; i++) {
outfile << sales[i].name << " " << sales[i].quantity << " " << sales[i].price << endl;
}
outfile.close();
}
}
// 从文件读取销售记录并计算总销售额
double calculateWeeklySales(const string& filename) {
double totalSales = 0.0;
ifstream infile(filename.c_str());
if (infile.is_open()) {
string name;
int quantity;
double price;
while (infile >> name >> quantity >> price) {
totalSales += quantity * price;
}
infile.close();
}
return totalSales;
}
int main() {
// 模拟一周的销售记录
SalesRecord sales[7][3] = {
{ {"得力固体胶", 10, 22.5}, {"可口可乐", 100, 3.0}, {"雀巢咖啡", 50, 15.0} },
{ {"晨光中性笔", 20, 2.5}, {"蒙牛牛奶", 80, 4.5}, {"农夫山泉", 200, 1.0} },
{ {"苏泊尔电饭煲", 5, 189.0}, {"海天酱油", 30, 8.0}, {"红星二锅头", 20, 35.0} },
{ {"康师傅方便面", 100, 2.0}, {"百事可乐", 120, 3.5}, {"维他奶", 60, 6.0} },
{ {"小浣熊抽纸", 40, 4.0}, {"统一老坛酸菜牛肉面", 50, 5.0}, {"矿泉水", 150, 1.5} },
{ {"飞利浦电动牙刷", 3, 399.0}, {"统一阿萨姆奶茶", 70, 4.0}, {"康师傅冰红茶", 100, 2.5} },
{ {"三只松鼠坚果", 10, 25.0}, {"美汁源果粒橙", 90, 3.0}, {"达利园蛋糕", 30, 10.0} }
};
// 存储每天的销售记录到文件
for (int i = 0; i < 7; i++) {
string filename = "sales_record_" + to_string(i+1) + ".txt";
saveSalesRecord(sales[i], 3, filename);
}
// 计算周销售额
double totalWeeklySales = 0.0;
for (int i = 0; i < 7; i++) {
string filename = "sales_record_" + to_string(i+1) + ".txt";
double dailySales = calculateWeeklySales(filename);
cout << "第" << i+1 << "天销售额:" << dailySales << " 元" << endl;
totalWeeklySales += dailySales;
}
cout << "本周总销售额:" << totalWeeklySales << " 元" << endl;
return 0;
}
```
注:本程序中使用了 C++11 中新增的 `std::to_string()` 函数,如果编译器不支持该函数,可以自己实现一个转换函数。