以下是我改動的代碼,大概就是main里面改成傳遞參數,print類里面是這次的主要部分,在這里面進行對文件的操作
main里面的內容
#include <iostream>
#include <stdio.h>
#include <string.h>
#include<queue>
#include"Scan.h"
#include"Print.h"
#include"Calculation.h"
#include<fstream> //新的頭文件(用於文件的輸入等操作)
using namespace std;
int main(int argc,char *argv[])
{
int judge=0;
double result=0;
Print output;
if((strcmp(argv[1],"-f") == 0))
{
string input;
string getsfile=argv[argc-2]; //輸入文件名
string putsfile=argv[argc-1]; //輸出文件名
output.print_format_one(getsfile,putsfile);
}
else if(strcmp(argv[1],"-a") == 0)
{
Scan get; //定義scan類
Calculation cal; //定義calculation類,用於計算結果
string input = argv[argc-1];
string input1 = argv[argc-1];
get.ToStringQueue(input);
if((get.outputqueue.front()=="ERROR") )
{
judge=1;
}
else
{
result=cal.Calculate(get.outputqueue);
}
output.print_format_two(input1,result,judge);
}
else
{
Scan get; //定義scan類
Calculation cal; //定義calculation類,用於計算結果
string input = argv[argc-1];
get.ToStringQueue(input);
if((get.outputqueue.front()=="ERROR") )
{
judge=1;
}
else
{
result=cal.Calculate(get.outputqueue);
}
output.print_format_three(result,judge);
}
return 0;
}
下面是print的頭文件
#include <iostream>
#include <stdio.h>
#include <string.h>
#include<queue>
#include<fstream> //新的頭文件(用於文件的輸入等操作)
using namespace std;
class Print
{
public:
void print_format_one(string getsfile,string putsfile);
void print_format_two(string input,double result,int judge);
void print_format_three(double result,int judge);
} ;
下面是print.cpp文件
#include <iostream>
#include <stdio.h>
#include <string.h>
#include<queue>
#include"Scan.h"
#include"Print.h"
#include"Calculation.h"
#include<fstream> //新的頭文件(用於文件的輸入等操作)
/*定義判斷是否輸入為文件格式,第五次作業添加的內容*/
void Print::print_format_one(string getsfile,string putsfile)
{
double result;
ifstream infile;
ofstream outfile;
infile.open(getsfile.c_str(),ios::in);
outfile.open(putsfile.c_str(),ios::out);
string input;
while(!infile.eof())
{
Scan get; //定義scan類
Calculation cal; //定義calculation類,用於計算結果
getline(infile,input,'\n');
get.ToStringQueue(input);
if((get.outputqueue.front()=="ERROR") )
{
outfile<<"ERROR"<<endl;
}
else
{
result=cal.Calculate(get.outputqueue);
outfile<<result<<endl;
}
}
infile.close(); //關閉文件
outfile.close();
}
/*無需文件操作的時候,跟第四次作一樣*/
void Print::print_format_two(string input,double result,int judge)
{
cout<<input; // 輸出式子
if(judge==1) //看是否輸入的式子是有問題的,如果有問題就輸出error
{
cout<<"ERROR"<<endl;
}
else
{
cout<<result<<endl; //輸出結果
}
}
void Print::print_format_three(double result,int judge)
{
if(judge==1) //看是否輸入的式子是有問題的,如果有問題就輸出error
{
cout<<"ERROR"<<endl;
}
else
{
cout<<result<<endl; //輸出結果
}
}
在學長的幫助下終於能把計算的結果改對了(開心)
框架圖
學長還有提的建議會一步步改,花了一個下午的時間在不斷的修改,main與print之間的關系。(突然發現main與類之間的關系好像並不是那么簡單)