C++大学课程作业

这篇博客详细列举了C++大学课程中涉及的作业章节,包括第14章的6-9节,第15章的24和26节,以及第16章的10, 12和13节。" 130792988,11183300,Matlab ABC-BP算法优化多特征分类预测,"['算法', '人工蜂群算法优化BP神经网络', 'ABC-BP分类预测']
摘要由CSDN通过智能技术生成

14.6-14.9  

#include <fstream>
#include <iostream>
#include <string>

int main(int argc, const char* argv[]) {
    // open oldmast.dat for input
    std::ifstream inOldMaster("oldmast.dat", std::ios::in);

    if (!inOldMaster) {
        std::cerr << "oldmast.dat could not be opened" << std::endl;
        return 0;
    }
    // open transaction file for input
    std::ifstream inTransaction("trans.dat", std::ios::in);

    if (!inTransaction) {
        std::cerr << "trans.dat could not be opened" << std::endl;
        return 0;
    }
    // open newmast.dat for output (and creation)
    std::ofstream outNewMaster("newmast.dat", std::ios::out);

    if (!outNewMaster) {
        std::cerr << "newmast.dat could not be created" << std::endl;
        return 0;
    }

    int transAccountNum = 0;
    int mastAccountNum = 0;
    double transDollarAmount;
    double mastDollarAmount;
    std::string fName;
    std::string lName;

    // handling files with one record
    inTransaction >> transAccountNum >> transDollarAmount;
    inOldMaster >> mastAccountNum >> fName >> lName >> mastDollarAmount;

    // process changes
    while (!inOldMaster.eof() && !inTransaction.eof()) {
         if (transAccountNum < mastAccountNum) {
            std::cout << "Unmatched transaction record for account number: "
                      << transAccountNum << std::endl;
            inTransaction >> transAccountNum >> transDollarAmount;
        } else if (transAccountNum == mastAccountNum) {
            do {
                mastDollarAmount += transDollarAmount;
                inTransaction >> transAccountNum >> transDollarAmount;
            } while (transAccountNum == mastAccountNum && !inTransaction.eof());
            outNewMaster << mastAccountNum << " " << fName << " " << lName
                         << " " << mastDollarAmount << std::endl;
            inOldMaster >> mastAccountNum >> fName >> lName >> mastDollarAmount;
        } else if (transAccountNum > mastAccountNum) {
            outNewMaster << mastAccountNum << " " << fName << " " << lName
                         << " " << mastDollarAmount << std::endl;
            inOldMaster >> mastAccountNum >> fName >> lName >> mastDollarAmount;
        }
    }

    // if trans.dat reachs to end of file before oldmast.dat
    while ( !inOldMaster.eof() ) {
        outNewMaster << mastAccountNum << " " << fName << " " << lName
                     << " " << mastDollarAmount << std::endl;
        inOldMaster >> mastAccountNum >> fName >> lName >> mastDollarAmount;
    }

    // if oldmast.dat reachs to end of file before trans.dat
    while ( !inTransaction.eof() ) {
        std::cout << "Unmatched transaction record for account number: "
                  << transAccountNum << std::endl;
            inTransaction >> transAccountNum >> transDollarAmount;
    }

    return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值