数据结构作业—表达式求值
2011-10-17 11:56
429 查看
嘛...就是转换成反波兰式嘛......
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
string changeToReversePolish(string& expression);
double calculateByReversePolish(string reversePolish);
// return true if stackTop is higher or equal than readIn
bool isHigh(char stackTop, char readIn);
bool isOperator(char readIn);
int main()
{
string expression = "";
int num = 0;
cin >> num;
int count = 0;
while (count < num)
{
cin >> expression;
cout << calculateByReversePolish(changeToReversePolish(expression)) << endl;
count++;
}
return 0;
}
bool isHigh(char stackTop, char readIn)
{
if ((stackTop == '+') && (readIn == '+'))
return true;
if ((stackTop == '+') && (readIn == '-'))
return true;
if ((stackTop == '-') && (readIn == '+'))
return true;
if ((stackTop == '-') && (readIn == '-'))
return true;
if ((stackTop == '*') && (readIn == '*'))
return true;
if ((stackTop == '*') && (readIn == '/'))
return true;
if ((stackTop == '/') && (readIn == '*'))
return true;
if ((stackTop == '/') && (readIn == '/'))
return true;
if (readIn == ')')
return true;
return false;
}
bool isOperator(char readIn)
{
if (readIn == '+' || readIn == '-' || readIn == '*' || readIn == '/')
return true;
return false;
}
string changeToReversePolish(string& expression)
{
stack<char> operandStack;
string result;
char operand = NULL;
istringstream iss(expression);
while (iss >> operand)
{
if (isdigit(operand) || operand == '.')
{
result.push_back(operand);
}
if (operand == '(')
{
operandStack.push(operand);
}
else
{
if (isOperator(operand))
{
result.push_back(' ');
if (operandStack.empty())
{
operandStack.push(operand);
}
else if (operandStack.top() != '(' && isHigh(operandStack.top(), operand))
{
result.push_back(operandStack.top());
operandStack.pop();
}
else
{
operandStack.push(operand);
}
}
else if (operand == ')')
{
while (!operandStack.empty() && operandStack.top() != '(')
{
result.push_back(operandStack.top());
operandStack.pop();
}
if (operandStack.top() == '(')
operandStack.pop();
}
}
}
while (!operandStack.empty())
{
result.push_back(operandStack.top());
operandStack.pop();
}
return result;
}
double calculateByReversePolish(string reversePolish)
{
stack<double> digitStack;
string tempDigit = "";
for (int i = 0; i < reversePolish.length(); i++)
{
if (isdigit(reversePolish[i]))
{
tempDigit.push_back(reversePolish[i++]);
while ((isdigit(reversePolish[i]) || reversePolish[i] == '.') && i < reversePolish.length())
{
tempDigit.push_back(reversePolish[i]);
i++;
}
i--;
double digit = strtod(tempDigit.c_str(), NULL);
digitStack.push(digit);
tempDigit.clear();
}
else if(reversePolish[i] == '+' && !digitStack.empty())
{
double rightOperand = digitStack.top();
digitStack.pop();
double leftOperand = digitStack.top();
digitStack.pop();
double result = leftOperand + rightOperand;
digitStack.push(result);
}
else if(reversePolish[i] == '-' && !digitStack.empty())
{
double rightOperand = digitStack.top();
digitStack.pop();
double leftOperand = digitStack.top();
digitStack.pop();
double result = leftOperand - rightOperand;
digitStack.push(result);
}
else if(reversePolish[i] == '*' && !digitStack.empty())
{
double rightOperand = digitStack.top();
digitStack.pop();
double leftOperand = digitStack.top();
digitStack.pop();
double result = leftOperand * rightOperand;
digitStack.push(result);
}
else if(reversePolish[i] == '/' && !digitStack.empty())
{
double rightOperand = digitStack.top();
digitStack.pop();
double leftOperand = digitStack.top();
digitStack.pop();
double result = leftOperand / rightOperand;
digitStack.push(result);
}
}
return digitStack.top();
}
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
string changeToReversePolish(string& expression);
double calculateByReversePolish(string reversePolish);
// return true if stackTop is higher or equal than readIn
bool isHigh(char stackTop, char readIn);
bool isOperator(char readIn);
int main()
{
string expression = "";
int num = 0;
cin >> num;
int count = 0;
while (count < num)
{
cin >> expression;
cout << calculateByReversePolish(changeToReversePolish(expression)) << endl;
count++;
}
return 0;
}
bool isHigh(char stackTop, char readIn)
{
if ((stackTop == '+') && (readIn == '+'))
return true;
if ((stackTop == '+') && (readIn == '-'))
return true;
if ((stackTop == '-') && (readIn == '+'))
return true;
if ((stackTop == '-') && (readIn == '-'))
return true;
if ((stackTop == '*') && (readIn == '*'))
return true;
if ((stackTop == '*') && (readIn == '/'))
return true;
if ((stackTop == '/') && (readIn == '*'))
return true;
if ((stackTop == '/') && (readIn == '/'))
return true;
if (readIn == ')')
return true;
return false;
}
bool isOperator(char readIn)
{
if (readIn == '+' || readIn == '-' || readIn == '*' || readIn == '/')
return true;
return false;
}
string changeToReversePolish(string& expression)
{
stack<char> operandStack;
string result;
char operand = NULL;
istringstream iss(expression);
while (iss >> operand)
{
if (isdigit(operand) || operand == '.')
{
result.push_back(operand);
}
if (operand == '(')
{
operandStack.push(operand);
}
else
{
if (isOperator(operand))
{
result.push_back(' ');
if (operandStack.empty())
{
operandStack.push(operand);
}
else if (operandStack.top() != '(' && isHigh(operandStack.top(), operand))
{
result.push_back(operandStack.top());
operandStack.pop();
}
else
{
operandStack.push(operand);
}
}
else if (operand == ')')
{
while (!operandStack.empty() && operandStack.top() != '(')
{
result.push_back(operandStack.top());
operandStack.pop();
}
if (operandStack.top() == '(')
operandStack.pop();
}
}
}
while (!operandStack.empty())
{
result.push_back(operandStack.top());
operandStack.pop();
}
return result;
}
double calculateByReversePolish(string reversePolish)
{
stack<double> digitStack;
string tempDigit = "";
for (int i = 0; i < reversePolish.length(); i++)
{
if (isdigit(reversePolish[i]))
{
tempDigit.push_back(reversePolish[i++]);
while ((isdigit(reversePolish[i]) || reversePolish[i] == '.') && i < reversePolish.length())
{
tempDigit.push_back(reversePolish[i]);
i++;
}
i--;
double digit = strtod(tempDigit.c_str(), NULL);
digitStack.push(digit);
tempDigit.clear();
}
else if(reversePolish[i] == '+' && !digitStack.empty())
{
double rightOperand = digitStack.top();
digitStack.pop();
double leftOperand = digitStack.top();
digitStack.pop();
double result = leftOperand + rightOperand;
digitStack.push(result);
}
else if(reversePolish[i] == '-' && !digitStack.empty())
{
double rightOperand = digitStack.top();
digitStack.pop();
double leftOperand = digitStack.top();
digitStack.pop();
double result = leftOperand - rightOperand;
digitStack.push(result);
}
else if(reversePolish[i] == '*' && !digitStack.empty())
{
double rightOperand = digitStack.top();
digitStack.pop();
double leftOperand = digitStack.top();
digitStack.pop();
double result = leftOperand * rightOperand;
digitStack.push(result);
}
else if(reversePolish[i] == '/' && !digitStack.empty())
{
double rightOperand = digitStack.top();
digitStack.pop();
double leftOperand = digitStack.top();
digitStack.pop();
double result = leftOperand / rightOperand;
digitStack.push(result);
}
}
return digitStack.top();
}
相关文章推荐
- C++实现数据结构作业——表达式求值
- C++表达式求值(Stack and Expression)加州大学伯克利分校计算机专业数据结构与算法作业
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)
- 表达式求值 ACM 数据结构
- 数据结构课程设计---------用栈来实现表达式求值
- 数据结构课程设计---------用栈来实现表达式求值
- 栈的应用-表达式求值-数据结构
- 数据结构上机题目4--后缀表达式求值
- 数据结构课程设计-用栈实现表达式求值的方法详解
- 数据结构表达式求值----大神帮忙找下错误
- 数据结构(java语言描述)链栈的使用——表达式求值
- 数据结构之栈的应用——四则表达式求值
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)
- 数据结构课程设计四则运算表达式求值(C语言版)
- 数据结构(6)--栈的应用之中缀表达式求值
- 数据结构——栈—表达式求值
- 数据结构之---C语言实现栈的表达式求值(表达式树)
- 数据结构——表达式求值
- 数据结构课程设计-表达式求值
- 数据结构表达式求值有关问题