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中只剩下一個值

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章