expression 5+4*(7-15) or have parenthesis in any order // 波兰表示法

Answers write code in java /c for expression 5+4*(7-15) or have parenthesis in any order .

idea: Use two stack, one for number, one for operator. Every time push a number or an operation into stack.
         1) After pushing a number, check the top of the operator, if it is ‘*’ or '/', pop two number and do the multiple or divide operation. Then
              push the result into number stack.

  2) if the operation is ')', then do the operation till meeting the '('. Then push the result into number stack.

 After traversing all of the character, I will do all of left add and minus operation.  when I have just one number into the numberStack and I am done evaluating the exp , I will return the head of the number Stack.

void Multi (stack<float>& num, stack<char>& operation){
	float value_1 = num.top();
	num.pop();
	float value_2 = num.top();
	num.pop();
	operation.pop();
	num.push(value_1*value_2);
}

void Divi (stack<float>& num, stack<char>& operation){
	float value_1 = num.top();
	num.pop();
	float value_2 = num.top();
	num.pop();
	operation.pop();
	num.push(value_2/value_1);
}

void addOrMinus(stack<float>& num, stack<char>& operation){
	float value_1 = num.top();
	num.pop();
	float value_2 = num.top();
	num.pop();
	if (operation.top() == '+')
		num.push(value_1+value_2);
	else
		num.push(value_2-value_1);
	operation.pop();
}

float readnum(string data,int& index){
	float value = 0;
	bool numeration = false;
	int location = 0;
	while (index < data.size()){
		if (data[index] == '.')
			numeration = true;
		else if (data[index] <= '9' && data[index] >= '0'){
			if (numeration == false)
				value = value*10 + (data[index] - '0');
			else{
				location++;
				value += ((data[index]- '0')*pow(10.0,-location));
			}
		}
		else
			break;
		index++;
	}
	index--;
	return value;
}
double express(string data){
	int len = data.size();
	stack<float> num;
	stack<char> operation; 
	operation.push('%');
	for(int i = 0; i<len;){
		if (data[i] <= '9'&& data[i]>= '0'){
			float value = readnum(data, i);
			num.push(value);
			if (operation.top() == '*')
				Multi (num, operation);
			else if (operation.top() == '/')
				Divi (num, operation);
		}
		else if (data[i] == ')'){
			while (operation.top() != '('){
				addOrMinus(num,operation);
			}
			operation.pop();
			if (operation.top() == '*')
				Multi (num, operation);
			else if (operation.top() == '/')
				Divi (num, operation);
		}
		else
			operation.push(data[i]);
		i++;
	}

	while(num.size() > 1){
		addOrMinus(num,operation);
	}

	return num.top();
}

出现的错误:1. operator 这个关键是不能用的,这是保留关键字

2. 注意value_1和value_2的顺序

3. pow include “math.h”

4. 处理数字是不要忘了 data[i] - '0'

——————————————————————————————华丽分割线——————————————————————

其实这道题就是波兰表示法。波兰法介绍:http://baike.baidu.com/view/552648.htm

如何由中序遍历改为波兰法:

  1、建立运算符栈stackOperator用于运算符的存储,压入'\0'。

  2、预处理表达式,正、负号前加0(如果一个加号(减号)出现在最前面或左括号后面,则该加号(减号)为正负号) 。(上面的解法忘了考虑这一条)
  3、顺序扫描表达式,如果当前字符是数字(优先级为0的符号),则直接输出该数字;如果当前字符为运算符或括号(优先级不为0的符号),则判断第4点 。
  4、若当前运算符为'(',直接入栈;
  若为')', 出栈 并顺序输出 运算符 直到遇到第一个'(',遇到的第一个'(' 出栈 但不输出;
  若为四则运算符,比较栈顶元素与当前元素的优先级:
  如果 栈顶元素运算符优先级 >= 当前元素的优先级, 出栈 并顺序输出运算符直到 栈顶元素优先级 < 当前元素优先级,然后当前元素入栈;
  如果 栈顶元素 < 当前元素,直接入栈。
  5、重复第3点直到表达式扫描完毕。
  6、顺序出栈并输出 运算符 直到栈顶元素为'\0'。

如何根据波兰法计算结果:就是一个stack:数字stack。 每次遇到一个operator,取出两个数字进行运算,并且把结果push 进stack,直至stack中只剩下一个值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值