第四次作业

说实话,在看到第四次作业的时候,真的望而生畏。以至于拖了挺久都没有付诸行动(惭愧。。)。但自己想了想,逃避不是办法,不行动,未解的问题永远在那里。因此在临近截止期限才开始尝试。

以至于目前还未完全实现目标。(才差不多做完将中缀表达式转变成前缀表达式以及计算)。

Calculation.cpp

    int Calculation::receive (queue<string> strs)
{
char ch[10000];
stack<string> s1;
stack<string> s2;
stack<double> num;
string temp="";

int top=0;
int Calculation::level(char c)
{
	int q;
	if(c=='+' || c=='-')
	{
		q=1;
	}
	if(c=='*' || c=="/")
	{
		q=2;
	}

	return (q);
}

bool Calculatoion::isOperator(char c)
{
	if(c=='+'|| c=='-'||c=='*'||c=='/')
		return true;
	else
		return false;
}

double Calculatoion::calculate(char c,int a,int b)
{
	if(c=='+')
	{
		return (a+b);
	}
	if(c=='-')
	{
		return (a-b);
	}
	if(c=='*')
	{
		return (a*b);
	}
	if(c=='/')
	{
		return (a/b);
	}
}
    for(int i=str.size()-1; i>=0 ; )
    {
	if(strs[i]>=48 && strs[i]<=57)
	{
		temp=temp+strs[i];
	}

	if(strs[i]==')')
	{
		top++;
		ch[top]=strs;
	}

	while(isOperator(strs[i]))
	{
		if(level(strs[i])>=level(ch[top]) || top==0 || ch[top]==')')
		{
			top++;
			ch[top]=strs[i];
			break;

		}
		else
		{
			temp=temp+ch[top];
			top--;
		}
	}

	if(strs[i]=='(')
	{
		while(ch[top]!=')')
		{
			temp=temp+ch[top];
			top--;

		}
		top--;
	}

	i--;
}

while(top!=0)
{
	temp=temp+ch[top];
	top--;
}

for(int i=temp.size()-1; i>=0; i--)
{
	int x,y;
	if(temp[i]>=48 && temp<=57)
	{
		s1.push(temp[i]) ;
	}
	else
	{
		x=st.top();
		s1.pop();
		y=st.top();
		s1.pop();
		s1.push(calculate(temp[i], x, y));

	}
    }

	int output=0;
	output=s1.top();

	return output;





}

在接下来的时间里我会尝试着将作业完善。

原文地址:https://www.cnblogs.com/cjqcjq/p/5391166.html