C++项目第五次作业之文件的读取

前言

乍看题目,用文件读取数据,这不是很简单的事嘛ps:以前写单个.cpp就是用freopen读取数据,然而当开始写的时候就出现了问题(什么叫做实力作死,有一种痛叫too young too simple),因为第一次使用C++中的文件流fstream,不是很熟悉,所以出现各种bug,然后又各种debug,整个人都不好了!!!

传送门:

repository
information of problem

写在之前:因为西瓜犇犇贴出了我程序中的bug,于是我在某年某月某天走上了不归路,于某年某月某日卒于debug!(珍爱生命,远离debug->然而并不可能)

这里贴出自己处理不规范表达式(又臭又长 不要介意)的代码(赌5毛 肯定还有bug[笑着哭]):

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
//--------------------------------------------
	string input,temp="";
	int i,j,n,t; 
//--------------------------------------------
int main()
{
 	freopen ( "xx.in ","r",stdin );
 	freopen ( "xx.out","w",stdout);
	cin >> input;
	int l=input.size();
	for (i=0;i<l;i++)
	{
		if (input[i] != '=')
			temp += input[i];
	}
	input = "";
	input = temp;
	cout << temp << endl << input << endl;
	for (i=0;i<l;i++)
	{
		if(input[i]=='(')
			n++;
		if(input[i]==')')
			t++;
	}
	if(n>t)
	{
		for (i = 0; i < n-t; i++)
		{
			input += ")";
		}
	}
	if(n < t)
	{
		for(i=0;i<t-n;i++)
			temp += "(";
		input=temp+input;
	}
	temp = "";
	l=input.size();
	for (i=0;i<l;i++)
	{
		if ((i+1)<l&&input[i]=='('&&input[i+1]=='(')
			temp+=input[i];
		else if((i+1)&&input[i]=='('&&input[i+1]<'0')
		{
			temp+=input[i];
			temp+="0";
		}
		else
			temp+=input[i];
	}
	l = temp.size();
	input="";
	for (i=0; i<l;i++)
	{
		
		if((i+1)<l&&((temp[i] >= '0' && temp[i+1] == '(') || (temp[i] == ')' && temp[i+1] >= '0')))
		{	
			if (temp[i+1] == '=')
				continue;
			input += temp[i];
			input += "*";
		}
		else
			if((i+1) < l && temp[i] < '0' && temp[i+1] < '0' && temp[i] != temp[i+1] && temp[i] >= 42 && temp[i+1] >= 42)
		{
			input += temp[i];
			input += "(0";
			input += temp[++i];
			if(temp[++i] == '(')
			{
				n = 0;
				for ( i=i; i < l ; i++)
				{
					input += temp[i];
					if(temp[i]=='(')
					{
						n++;		
					}
					if(temp[i]==')')
					{
						n--;
						if(n==0)
						    break;
					}
				}
				if (i<l)
					input += ")";
			}
			else
			{
				input += temp[++i];
				input += temp[++i];
				input += ")";
			}
		}
		else 
			input += temp[i];
	}
	input += "=";
	cout << input << endl;
	return 0;
}

进入主题:

1.这次作业要求输出都在print类里面,这就要就在print类里动手脚,如下:
print.h区段

#ifndef PRINT_H
#define PRINT_H
#include <iostream>
#include <string>
#include "scan.h"
#include "calculate.h"

using namespace std;

/*创建Print类用来输出队列并定义类当中的函数*/ 
class Print
{
    public:
        Print();
        ~Print();
    void PrintStringQueue(string input);
	void Printanswer(string input);
	void Usedfile(string input, string read, string ans);  
    private:
};

#endif

从头文件里就可以看出端倪,在print类里面分别写三个函数用来执行不同指令PrintStringQueue函数用来接收指令-a输出表达式及结果,Printanswer只输出结果,Usedfile接收指令-f,顾名思义就是用文件读取数据;
2.新的读取方式的添加:
main.cpp区段

#include <iostream>
#include <string>
#include "print.h"

using namespace std; 

int main(int argc, char* argv[])
{
	/*创建Print的对象p*/
	Print p;
	string input;
	
	input = argv[1];                                
	if (input == "-a")
	{
		input = argv[2];
		//调用Print类 输出队列和结果 
		p.PrintStringQueue(input);                   
        	
	}
	else
		if(input == "-f")
		{
			input = "";
			string write  = argv[2];
			string result = argv[3];
			p.Usedfile(input,write,result);
		}
	else 
	{
		input = argv[1];
        	p.Printanswer(input);
	}
	return 0;
}

以上采用命令行输入的方式运行程序;

3.对项目的整体框架描述:


(没有下visor,自己瞎搞了框架图)结合main.cppprint.h,整个项目以main函数为入口,通过不同的指令,调用print类里面的3个含参函数,然后在print类里面执行相应的指令,得出输出结果;
ps:待我下个visor,补个完整的给你!
补充的框架图:

END:

1.问题:写文件的时候遇到读不出文件的问题,在各路大神的点拨及自己debug中,逐步逐步的排查语句,最终解决问题;
2.收获:①虽然只是简单的文件输入输出,但是使用过程中仍需注意许多细节,还是初步学会了C++fstrem的简单使用;
②虽然自己调试了很久,浪费了很多时间和精力,但是最后,自己的debug能力还是有所提升,还是有所收获;
3.参考资料:
C++之文件IO操作流

未完待续~~~~~~~~to be continue!

posted @ 2016-05-11 00:38  Distances  阅读(457)  评论(3编辑  收藏  举报