poj 中缀表达式的值
2013-12-21 00:55
302 查看
好久没写代码了,这种100行的竟然RE了好几次,真是智商捉急~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
呐,本题分两步进行。
我是先用栈op来完成中缀到后缀表达式的转换,再用另外一个栈cal来计算后缀表达式的值。
上代码:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <stack>
#include <vector>
using namespace std;
#define MAXSIZE 610
#define IS_NUM(ch) (ch >= '0' && ch <= '9')
#define IS_OP(ch) (ch == '+' || ch == '-' || ch == '*' || ch == '/')
string str;
stack<char> op;
stack<int> cal;
vector<string> post;
int prior(char ch) {
if (ch == '+' || ch == '-') return 0;
else return 1;
}
void post_add(char tchar) {
char tmp[2];
tmp[0] = tchar; tmp[1] = '\0';
string s_tmp(tmp);
post.push_back(s_tmp);
}
void make_post() {
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str[i];
if (ch == '(')
op.push(ch);
else if (ch == ')') {
while (!op.empty()) {
char c = op.top();
op.pop();
if (c == '(') break;
post_add(c);
}
}
else if (IS_OP(ch)) {
if (!op.empty()) {
char tchar = op.top();
while (tchar != '(' && prior(tchar) >= prior(ch)) {
op.pop();
post_add(tchar);
if (!op.empty()) tchar = op.top();
else break;
}
}
op.push(ch);
}
else {
int &pos = i;
char tmp[MAXSIZE]; int ptr = 0;
while (IS_NUM(str[pos]))
tmp[ptr++] = str[pos++];
pos--;
tmp[ptr] = '\0';
string s_tmp(tmp);
post.push_back(s_tmp);
}
}
while (!op.empty()) {
char tchar = op.top(); op.pop();
post_add(tchar);
}
return;
}
void calculate() {
int size = post.size();
for (int iter = 0; iter < size; iter++) {
string &s = post[iter];
if (IS_NUM(s[0]))
cal.push(atoi(s.c_str()));
else {
int b = cal.top(); cal.pop();
int a = cal.top(); cal.pop();
int c;
char ch = s[0];
switch(ch) {
case '+': c = a + b; break;
case '-': c = a - b; break;
case '*': c = a * b; break;
case '/': c = a / b; break;
}
cal.push(c);
}
}
}
int main() {
int N; cin >> N;
while (N--) {
str.clear(); post.clear();
cin >> str;
make_post();
calculate();
int res = cal.top(); cal.pop();
cout << res << endl;
}
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
呐,本题分两步进行。
我是先用栈op来完成中缀到后缀表达式的转换,再用另外一个栈cal来计算后缀表达式的值。
上代码:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <stack>
#include <vector>
using namespace std;
#define MAXSIZE 610
#define IS_NUM(ch) (ch >= '0' && ch <= '9')
#define IS_OP(ch) (ch == '+' || ch == '-' || ch == '*' || ch == '/')
string str;
stack<char> op;
stack<int> cal;
vector<string> post;
int prior(char ch) {
if (ch == '+' || ch == '-') return 0;
else return 1;
}
void post_add(char tchar) {
char tmp[2];
tmp[0] = tchar; tmp[1] = '\0';
string s_tmp(tmp);
post.push_back(s_tmp);
}
void make_post() {
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str[i];
if (ch == '(')
op.push(ch);
else if (ch == ')') {
while (!op.empty()) {
char c = op.top();
op.pop();
if (c == '(') break;
post_add(c);
}
}
else if (IS_OP(ch)) {
if (!op.empty()) {
char tchar = op.top();
while (tchar != '(' && prior(tchar) >= prior(ch)) {
op.pop();
post_add(tchar);
if (!op.empty()) tchar = op.top();
else break;
}
}
op.push(ch);
}
else {
int &pos = i;
char tmp[MAXSIZE]; int ptr = 0;
while (IS_NUM(str[pos]))
tmp[ptr++] = str[pos++];
pos--;
tmp[ptr] = '\0';
string s_tmp(tmp);
post.push_back(s_tmp);
}
}
while (!op.empty()) {
char tchar = op.top(); op.pop();
post_add(tchar);
}
return;
}
void calculate() {
int size = post.size();
for (int iter = 0; iter < size; iter++) {
string &s = post[iter];
if (IS_NUM(s[0]))
cal.push(atoi(s.c_str()));
else {
int b = cal.top(); cal.pop();
int a = cal.top(); cal.pop();
int c;
char ch = s[0];
switch(ch) {
case '+': c = a + b; break;
case '-': c = a - b; break;
case '*': c = a * b; break;
case '/': c = a / b; break;
}
cal.push(c);
}
}
}
int main() {
int N; cin >> N;
while (N--) {
str.clear(); post.clear();
cin >> str;
make_post();
calculate();
int res = cal.top(); cal.pop();
cout << res << endl;
}
return 0;
}
相关文章推荐
- POJ 1686 Lazy Math Instructor (中缀表达式计算)
- POJ 1422 Air Raid
- poj 3767 I Wanna Go Home
- poj 1258 Agri-Net
- poj 2492 A Bug's Life(并查集)
- POJ 1915 简单 广搜题
- poj 1006 hdu1370 Biorhythms
- poj-3899-The Lucky Numbers 模拟+数学
- poj 3608 Bridge Across Islands(两凸包最近距离)
- poj 1284 Primitive Roots(求原根的个数)
- poj 3176 Cow Bowling
- poj 1700 Crossing River C++/Java
- [笔记] 网络流-最大流 POJ-1273\HDU-4240
- POJ1860——Currency Exchange(BellmanFord算法求最短路)
- POJ 3301
- POJ 3616 Milking Time 排序Dp
- poj 1265 Area
- POJ-1450
- poj 1837 Balance 分组背包
- POJ【1661】Help Jimmy