表达式求值

自己写的乱七八糟的代码。。


#include <iostream>
#include <cstring>
#include <string>
#include <map>
#include <cmath>

using namespace std;

int toint(const char* str,int length)
{
	int i=0;
	int ans=0;
	int s;
  for (i=0;i<=length-1;i++)
  {
  	s=pow(10,length-1-i);
  	ans+=(str[i]-'0')*s;
  }
  return ans;
}

class Calculator
{
public:
	int getResult(const char* str)
	{
		char ope[100];
		int num[100];
		int length1=0,length2=0;
		int length=strlen(str);
		int counter=0;
		int pos=0;
		int flag=0;
		int temppos;
    while (pos!=length)
    {
			  while (str[pos]>='0' && str[pos]<='9')
			  {
			  	if (counter==0)
			  		temppos=pos;
			  	pos++;
				  counter++;
				  flag=1;
			  }
			  if (flag)
			  {
		  	  num[length2++]=toint(str+temppos,counter);
		  	  counter=0;
		  	  flag=0;
		  	  if (ope[length1-1]=='*' )
		  	  {
		  	  	num[length2-2]=num[length2-2]*num[length2-1];
		  	  	length1--;
		  	  	length2--;
		  	  }
		  	  else if (ope[length1-1]=='/')
		  	  {
		  	  	num[length2-2]=num[length2-2]/num[length2-1];
		  	  	length1--;
		  	  	length2--;
		  	  }
			  }
			  if (pos!=length)
			  {
			  	flag=0;
          ope[length1++]=str[pos];
          pos++;
			  	if (ope[length1-1]=='+' || ope[length1-1]=='-' )
			  	{
            if (length1-2>=0 && ope[length1-2]=='+')
            {
            	num[length2-2]=num[length2-1]+num[length2-2];
            	length2--;
            	ope[length1-2]=ope[length1-1];
            	length1--;
            }
            else if (length1-2>=0 && ope[length1-2]=='-')
            {
            	num[length2-2]=num[length2-2]-num[length2-1];
            	length2--;
            	ope[length1-2]=ope[length1-1];
            	length1--;
            }

			  	}
			  	else if (ope[length1-1]=='*' || ope[length1-1]=='/')
			  	{
			  		if (length1-2>=0 && ope[length1-2]=='*')
			  		{
			  		  num[length2-2]=num[length2-2]*num[length2-1];
			  		  length2--;
			  		  ope[length1-2]=ope[length1-1];
			  		  length1--;
			  		}
			  	  else if (length1-2>=0 && ope[length1-2]=='/')
			  		{
			  		  num[length2-2]=num[length2-2]/num[length2-1];
			  		  length2--;
			  		  ope[length1-2]=ope[length1-1];
			  		  length1--;
			  		}
			  	}
			  	else if (ope[length1-1]==')')
			  	{
            if (length1-2>=0 && ope[length1-2]=='+')
            {
            	num[length2-2]=num[length2-1]+num[length2-2];
              length1=length1-3;
              length2--;
            }
            else if (length1-2>=0 && ope[length1-2]=='-')
            {
            	num[length2-2]=num[length2-2]-num[length2-1];
              length1=length1-3;
              length2--;
            }
            else if (length1-2>=0 && ope[length1-2]=='*')
            {
            	num[length2-2]=num[length2-1]*num[length2-2];
              length1=length1-3;
              length2--;
            }
            else if (length1-2>=0 && ope[length1-2]=='/')
            {
            	num[length2-2]=num[length2-2]/num[length2-1];
              length1=length1-3;
              length2--;
            }
            else
            {
            	length1=length1-2;
            }
			  	}
			  }
    }
  if (length1!=0)
  {
    if (ope[0]=='+')
  	  num[0]=num[0]+num[1];
    else if (ope[0]=='-')
  	  num[0]=num[0]-num[1];
    else if (ope[0]=='/')
  	  num[0]=num[0]/num[1];
    else if (ope[0]=='*')
  	  num[0]=num[0]*num[1];
  }
  return num[0];
	}
};

int main()
{
	Calculator c;
	cout<<c.getResult("1+2+3")<<endl;
	cout<<c.getResult("((3+4)*5+6)*7")<<endl;
	cout<<c.getResult("1+2*3")<<endl;
	cout<<c.getResult("2*5+10")<<endl;
	cout<<c.getResult("3*(5+4)")<<endl;
	cout<<c.getResult("(4+5)*(2+2)")<<endl;
	cout<<c.getResult("1/2+1/2")<<endl;
	cout<<c.getResult("0+0")<<endl;
	cout<<c.getResult("4*5-7*8")<<endl;
	cout<<c.getResult("378+456-500*12/2")<<endl;
	cout<<c.getResult("((11*(12+13)*(14+15))+(16+17))*(18+19)")<<endl;
	cout<<c.getResult("4+5")<<endl;
	cout<<c.getResult("4")<<endl;
	cout<<c.getResult("77+44-22*33/11")<<endl;
  return 0;
}

标准代码

#include <iostream>
#include <string>
#include <stack>
#include <cctype>
using namespace std;

class Calculator
{
public:
	Calculator(){};
	int getResult(string);
private:
	bool can_do(char c);
	void exe_cacu();
	void set(string);
	string exp;
	stack<int> number;
	stack<char> op;
};
void Calculator::set(string s)
{
	exp = s;
	while(!number.empty())	number.pop();
	while(!op.empty()) op.pop();
}
bool Calculator::can_do(char c)
{
	if(op.empty()) return false;
	if(op.top() == '(')	return false;
	if(c == '+' || c == '-')	return true;
	if(c == '*' && (op.top() == '*' || op.top() == '/'))	return true;
	if(c == '/' && (op.top() == '*' || op.top() == '/'))	return true;
	return false;
}

void Calculator::exe_cacu()
{
	int b = number.top();
	number.pop();
	int a = number.top();number.pop();
	int result;
	switch(op.top())
	{
	case '+':
		result = a+b;
		break;
	case '-':
		result = a-b;
		break;
	case '*':
		result = a*b;
		break;
	case '/':
		result = a/b;
		break;
	}
	number.push(result);
	op.pop();
}


int Calculator::getResult(string s)
{
	set(s);
	int temp = 0;
	for(unsigned int i=0; i<exp.length(); i++)
	{
		if(isdigit(exp[i]))
		{
			temp = temp*10 + exp[i]-'0';
			if(i+1 == exp.length() || !isdigit(exp[i+1]))
			{
				number.push(temp);
				temp = 0;
			}
		}
		else if(exp[i] == '+' || exp[i] == '-' || exp[i] == '*' || exp[i] == '/')
		{
			if(can_do(exp[i]))
				exe_cacu();
			op.push(exp[i]);
		}
		else if(exp[i] == '(')
			op.push(exp[i]);
		else
		{
			while(op.top() != '(')
				exe_cacu();
			op.pop();
		}
	}
	while(!op.empty())
		exe_cacu();

	return number.top();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值