第五次作业(更新)

题目

代码入口

以下是我改动的代码,大概就是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与类之间的关系好像并不是那么简单)

posted @ 2016-05-12 11:26  咸疑人  阅读(177)  评论(10编辑  收藏  举报