读取CSV文件并用C++计算一个简单银行系统的余额可以通过以下步骤实现:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
struct BankAccount {
std::string accountNumber;
std::string accountName;
double balance;
};
std::vector<BankAccount> readCSV(const std::string& filename) {
std::vector<BankAccount> accounts;
std::ifstream file(filename);
std::string line;
while (std::getline(file, line)) {
std::stringstream ss(line);
std::string accountNumber, accountName, balanceStr;
double balance;
std::getline(ss, accountNumber, ',');
std::getline(ss, accountName, ',');
std::getline(ss, balanceStr, ',');
std::stringstream(balanceStr) >> balance;
BankAccount account;
account.accountNumber = accountNumber;
account.accountName = accountName;
account.balance = balance;
accounts.push_back(account);
}
file.close();
return accounts;
}
double calculateTotalBalance(const std::vector<BankAccount>& accounts) {
double totalBalance = 0.0;
for (const auto& account : accounts) {
totalBalance += account.balance;
}
return totalBalance;
}
int main() {
std::vector<BankAccount> accounts = readCSV("bank_accounts.csv");
double totalBalance = calculateTotalBalance(accounts);
std::cout << "Total balance: " << totalBalance << std::endl;
return 0;
}
以上代码假设CSV文件的格式为:账号,姓名,余额,每行代表一个银行账户。
请注意,由于题目要求不能提及特定的云计算品牌商,因此没有提供与腾讯云相关的产品和链接。
领取专属 10元无门槛券
手把手带您无忧上云