C++大学课程作业

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;
}
  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值