Calculator(1.0)

Calculator(1.0)

Github链接

解题过程中遇到的困难

  1. 对于c++中类和对象的使用不够明确,看了c++的视频教程学会了用类和对象来写程序。
  2. 不会使用<queue>,在网上查阅资料,初步了解了<queue>的用法。
  3. 实在用不来指针orz,在对输入的数据进行处理时,只好全程用数组。
  4. 处理数字时,一开始对于一位以上数字的处理过于繁琐,问了同学才知道<string>可以相加,这样就可以方便地存储一位以上的数字。
  5. 忽略了对小数点的处理,在判断数字或符号时加上一个对小数点的处理就行了。
  6. 代码规范,用alt+F8调了缩进,然后按照规范调了一下代码。

代码

Scan.h

#pragma once
#include <iostream>
#include <queue>
#include <string>
using namespace std;


class Scan
{

private:

	queue <string> q;
	bool error;

public:

    Scan();
    void ToStringQueue (string input);  /*对输入的数字或符号进行处理并存到队列中*/
    queue <string> ReturnQueue();  /*返回处理后得到的队列*/
    bool IsError();  /*用于在数字超出十位时报错*/

};

Print.h

#pragma once
#include <iostream>
#include <queue>
#include <string>
using namespace std;


class Print
{

public:

    void print (queue<string>q);  /*用于输出处理后的队列*/

};

Scan.app

#include "Scan.h"
using namespace std;


Scan::Scan()
{
    error = false;
}


void Scan::ToStringQueue(string input)
{
	int i;
	string a , b;
	for(i = 0;i < input.size();i++)
    {

    	if((input[i] < '0' || input[i] > '9') && input[i]!='.')
    	{
    		a = input[i];
    		q.push(a);
    		a = "";
    	}
    	else
    	{
    		b = input[i];
	    	a = a + b;

	    	if(a.size() > 10)
	        {
			    error = true;
	        }

		    if (i == input.size() - 1)
		    {
			    q.push(a);
			    a = "";
		    }	
		    else

			    if(input[i+1]!='.')
			    {
				    if(input[i+1] < '0' || input[i+1] > '9')
				    {	
					    q.push(a);
					    a = "";
				    }
			    }  
	    }
    }
}


queue <string> Scan::ReturnQueue()
{
    return q;
}


bool Scan::IsError()
{
    return error;
}

Print.cpp

#include "Print.h"
using namespace std;


void Print::print(queue<string>q)
{
    for(;q.empty() == false;)
    {
	    cout << q.front() << endl;
	    q.pop();
    }
}

main.cpp

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


int main()
{
    Scan scan;
    Print print;
    string input;
    cin >> input;
    scan.ToStringQueue (input);

    if(scan.IsError() == false)
    	print.print(scan.ReturnQueue());
    else
	    cout << "超过十位啦!";

    return 0;
}
原文地址:https://www.cnblogs.com/zhengshuhao/p/5253627.html