面向对象程序设计第五次作业

Github地址点这里

题目要求

  • 添加一种读取四则运算的方式:输入第一个参数若为-f,则再输入两个参数,为两个文件的路径。分别为程序读取输入的文件和输出答案的文件。
  • 将输出的代码都写在Print类中。
  • 描述框架图

代码

#include<iostream> 
#include<string>
#include<queue>
#include<stdlib.h>
#include"Calculator.h"
#pragma warning(disable:4996)
using namespace std;

int main(int argc, char* argv[])
{
    Calculation calculation;
    Scan scan;
    Print print;
    string strData1;
    string strData2;
    strData1 = argv[1];
    queue<string>queue;
    double answer;
    if (strData1 == "-a")
    {
        strData2 = argv[2];
        queue = scan.ToStringQueue(argv[2]);
        answer = calculation.ToCalculate(queue);
        print.ToOutput(answer, strData2);
    }
    else if (strData1 == "-f")
    {
        string strData3;
        strData3 = scan.Open(argv[2]);
        queue = scan.ToStringQueue(strData3);
        answer = calculation.ToCalculate(queue);
        print.ToOutput(answer,strData3, argv[3]);
        strData3 = "";
    }

    else
    {
        queue = scan.ToStringQueue(strData1);
        answer = calculation.ToCalculate(queue);
        print.ToOutput(answer);
    }
    system("pause");
    return 0;
}

scan
把文件读取放在了scan类中

string Scan::Open(char *in)
{
    string temp;
    fstream file;
    file.open(in);
    file >> temp;
    return temp;
}

print
依照不同情况进行输出

void Print::ToOutput(double answer, string exp, char *out)
{
    ofstream file;
    file.open(out);
    file << exp  <<  answer << endl;
}

void Print::ToOutput(double answer)
{
    cout << answer << endl;
}

void Print::ToOutput(double answer, string exp)
{
cout << exp << answer << endl;

}

描述框架图

886235-20160509165251765-817380555.png

心得

这一次修改因为增加了多组数据输入,发现除了第一个答案之后都是错的。研究之后才发现是在每一组处理过后有数据残留在字符串和队列中影响了下一组,在增加了清空队列和字符串的部分才正确。所以明白以后应注意在使用各种操作之后应当还原。

转载于:https://www.cnblogs.com/Wjianting/p/5474554.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值