(2016.3.26)第3次附加作业随笔

第三次作业附加作业

目标:

规范化代码 参考以下网址修改

http://wenku.baidu.com/link?url=WIJB7CAfMEyN1fGSCiUaxnnMmMUubRMHR8I1KXMsQmmhDBVGI_lYICFMKTjt36MODh1qgx0mkIuKIIWlvMX6J7cOCNRWVxFCIW80bKiiPTC&qq-pf-to=pcqq.group

代码链接

代码链接

反思

    我觉得学习规范的确的是好习惯,如果不是我加了注释,这次回看就根本看不懂了。
    而且写多了一些,规范这东西真的很方便
    就是不知道这次改没改成老师要求的

具体代码

main.cpp

#include <iostream> 
#include <string>
#include<stdlib.h>
#include <queue>
#include "Scan.h"
#include "Print.h"
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

/*修改版*/ 

int main(int argc, char** argv) {
    
	cout<<"请输入一个四则运算表达式:"<<endl; 
	
//	输入字符串 
	string demo; 
	cin>>demo;
	
//	实例化Scan和Print 
	Scan s;
	Print p;
	queue<string> temp=s.ToStringQueue(demo);
	if (temp.empty()){
		cout<<"输入数字超过10位(包括小数位"<<endl; 
	}else{
		p.printString(temp);
	}
	
	system("pause");
	return 0;
}

print.cpp

<code>#include <iostream> 
#include <string>
#include <queue>
#include "print.h"
using namespace std;

void Print::printString(queue<string> temp){
//    当队列不为空时循环 
	while (!temp.empty())
	{
//		输出并删除队列 首位 
		cout << temp.front()<<endl;
		temp.pop();
	}
}

print.h

<code>#include <iostream> 
#include <string>
#include <queue>
using namespace std;
class Print
{
    public 	:
		void printString(queue<string> input);//打印字符串 
};

Scan.cpp

#include <iostream> 
#include <string>
#include <queue>
#include "Scan.h"
using namespace std;

queue<string> Scan::ToStringQueue(string input){

//定义长度 
    int length=0;
    
//    初始化字符串和列表 
	string temp1="";
	string temp2="";
	queue<string> ans;
	queue<string> enmpty;
	
//	遍历input 
	for (int i=0;i<input.length();i++)
	{
		if (input[i]>='0'&&input[i]<='9'||input[i]=='.')
		{
//			符合条件 
			length++;
//			超过10位报错 
			if (length>10) 
			{
				return enmpty;
			}
			
//			储存非单个或单个的数字 
			temp1=temp1+input[i];
			
//			判断除除了最后一位的input的 下一位 
			if (i!=(input.length()-1))
			{
//				断后一位是否符合条件 ,符合交给下一位去做 
				if (input[i+1]>='0'&&input[i+1]<='9'||input[i+1]=='.')
				{
				}
//				不符合就将储存的temp1加入队列中 
				else
				{
					ans.push(temp1);
					temp1="";
					length=0;
				}
			}
//			把单个数字的input加入 
			else
			{
				ans.push(temp1);
				temp1="";
				length=0;
			}
			
		}
//		将出了数字的加队列 
		else
		{
			temp2=input[i];
			ans.push(temp2);
		}
		
	}
	
	return ans;
}```

####Scan.h

include

include

include

using namespace std;
class Scan{
public:
queue ToStringQueue(string input);//整理字符串
};

原文地址:https://www.cnblogs.com/UNWILL2LOSE/p/5323956.html