zoukankan      html  css  js  c++  java
  • 第五次作业--计算器项目之学习文件读取方式

    一.

    传送门: github problem

    参考资料: fstream 相对路径与绝对路径

    Code:

    main.cpp

    int main (int argc, char *argv[])
    {    
    
        Print Put;    
        string str;   // 定义 str 用来存储表达式。
    
        bool flag=false; //判断是否为文件输入输出形式 
        string s=argv[1];    
    
        if(s == "-a") // 输出表达式以及结果 
        {
        	str = argv[2];
        	cout << str << " ";
        	Put.print_str(str,flag,"","");	//计算,输出 
        }
        else if(s == "-f") // 文件输入/输出 
        {
        	str="";
        	string input_txt = argv[2]; 	//输入文件路径 
        	string output_txt = argv[3];	//输出文件路径 
        	flag = true;  
        	Put.print_str(str,flag,input_txt,output_txt);
        }
        else 
        {
        	str = argv[1];	//直接输出结果 
        	Put.print_str(str,flag,"","");
        }
        return 0;
    }
    

    Print.cpp

    void Print::print_str( string str, bool flag , string input_txt, string results_txt)
    {
        Calculation cal; //计算 
        Scan Calculator;   // 对象实例化
        queue<string>Que;
    
        if(!flag)
        {
        	    Que=Calculator.ToStringQueue(str); //调用。
        		string result = cal.calculation(Que);//计算 
        		if ( j != 0 )   
        		{
        		    cout << "Error!" <<endl;
        		}
        		else
        		{
        		    cout  << result << endl;  
        		}		
        }
        else 
        {
        	ifstream in; //声明 
        	ofstream out;
        	
        	//文件打开 
        	in.open(input_txt.c_str(), ios::in); 	
        	out.open(results_txt.c_str(), ios::out);
        	
        	//读入文件 
        	while(!in.eof())  
        	{
        		getline(in,str,'
    '); //从文件中读取字符串 
        		Que=Calculator.ToStringQueue(str); //调用。
        		string result = cal.calculation(Que);//计算 
        		
        		// out << 表示向文件写入结果 
        		if ( j != 0 )
        		{
        		    out << "Error!
    ";
        		}
         		else
        		{
        		    out  << result << endl;  
        		}
        	}
        	in.close(); //文件关闭 
        	out.close();
        }
        return ;
    };
    

    三.

    解题思路:

    • 1.为避免main.cpp的代码臃肿性,将字符串处理以及计算输出置于Print中调用.
    • 2.定义bool flag判断是否为-f指令,从而进行相应的计算处理以及输出.
    • 3.-f指令处理:
      • 声明:
        ifstream in; //输入文件
        ofstream out;//输出文件

      • 文件打开:
        in.open(input_txt.c_str(), ios::in); out.open(results_txt.c_str(),ios::out);

      • 1.从文件中读取字符串 :
        getline(in,str,' ');//存入str中,遇' '停止;

        2.调用Scan以及Calculation类处理计算str,返回值赋予result;

        3.将结果输出到文件:
        out << result << endl;

      • 文件关闭:
        in.close();
        out.close();


    四.

    运行结果(截图):


    程序结构框图:




    In The End

  • 相关阅读:
    168. Excel Sheet Column Title
    171. Excel Sheet Column Number
    264. Ugly Number II java solutions
    152. Maximum Product Subarray java solutions
    309. Best Time to Buy and Sell Stock with Cooldown java solutions
    120. Triangle java solutions
    300. Longest Increasing Subsequence java solutions
    63. Unique Paths II java solutions
    221. Maximal Square java solutions
    279. Perfect Squares java solutions
  • 原文地址:https://www.cnblogs.com/FZUstu/p/5468092.html
Copyright © 2011-2022 走看看