后缀表达式
题目1 只求后缀表达式的值
后缀表达式的值 (改进了链接里的写法)
输入
16 9 4 3 +*-@
输出
-47
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<stdlib.h>
#define PI acos(-1.0)
#define E 1e-9
#define INF 0x3f3f3f3f
#define LL long long
#pragma warning(disable:4996)
const int MOD
= 1000000007;
const int N
= 10000 + 5;
const int dx
[] = { -1,1,0,0 };
const int dy
[] = { 0,0,-1,1 };
using namespace std
;
string str
;
stack
<float>st
;
int cal(int a
, int b
, char c
)
{
if (c
== '+')return a
+ b
;
if (c
== '-')return a
- b
;
if (c
== '*')return a
* b
;
if (c
== '/')return a
/ b
;
return -1;
}
int main()
{
char multNum
[10];
strcpy(multNum
, "\0");
char cur
[2];
float data
;
getline(cin
, str
);
for (int i
= 0; i
< str
.length(); i
++)
{
if (str
[i
] == '+' || str
[i
] == '-'|| str
[i
] == '*' || str
[i
] == '/')
{
LL a
= st
.top();
st
.pop();
LL b
= st
.top();
st
.pop();
st
.push(cal(b
, a
, str
[i
]));
}
else if(str
[i
]=='@')
{
i
= str
.length();
}
else
{
while (str
[i
] != ' ')
{
cur
[0] = str
[i
];
cur
[1] = '\0';
strcat(multNum
, cur
);
i
++;
}
data
= (float)atof(multNum
);
cout
<< data
<< endl
;
strcpy(multNum
, "\0");
st
.push(data
);
}
}
cout
<< st
.top() << endl
;
}
题目2 中缀表达式转为后缀表达式,再求值
输入
2
1+2*3-4/5#
(66+(((11+22)*2-33)/3+6)*2)-45.6789#
输出
6.2000
54.3211
代码
#include<iostream>
#include<stack>
#include<string>
#include<iomanip>
#pragma warning(disable:4996)
using namespace std
;
typedef int Status
;
#define OPSETSIZE 7
float cal(float a
, float b
, char c
)
{
if (c
== '+')return a
+ b
;
if (c
== '-')return a
- b
;
if (c
== '*')return a
* b
;
if (c
== '/')return a
/ b
;
}
char Prior
[7][7] = {
'>','>','<','<','<','>','>',
'>','>','<','<','<','>','>',
'>','>','>','>','<','>','>',
'>','>','>','>','<','>','>',
'<','<','<','<','<','=',' ',
'>','>','>','>',' ','>','>',
'<','<','<','<','<',' ','=',
};
char fuhao
[OPSETSIZE
] = { '+','-','*','/','(',')','#' };
char diff(char a
,char b
)
{
int x
, y
;
for (int i
= 0; i
< OPSETSIZE
; i
++)
{
if (a
== fuhao
[i
])x
= i
;
if (b
== fuhao
[i
])y
= i
;
}
return Prior
[x
][y
];
}
Status
IN(char c
)
{
for (int i
= 0; i
< OPSETSIZE
; i
++)
{
if (c
== fuhao
[i
])return 1;
}
return 0;
}
float culExp(string exp
)
{
stack
<char>fuhao
;
stack
<float>num
;
char resNum
[20];
strcpy(resNum
, "\0");
char curNum
[2];
float data
;
fuhao
.push('#');
int i
= 0;
while(exp
[i
]!='#'||fuhao
.top()!='#')
{
if (IN(exp
[i
]))
{
switch (diff(fuhao
.top(), exp
[i
]))
{
case '<':
fuhao
.push(exp
[i
++]);
break;
case '=':
fuhao
.pop();
i
++;
break;
case '>':
float a
= num
.top(); num
.pop();
float b
= num
.top(); num
.pop();
num
.push(cal(b
, a
, fuhao
.top()));
fuhao
.pop();
break;
}
}
else
{
curNum
[0] = exp
[i
];
curNum
[1] = '\0';
strcat(resNum
, curNum
);
i
++;
if (IN(exp
[i
]))
{
data
= (float)atof(resNum
);
strcpy(resNum
, "\0");
num
.push(data
);
}
}
}
return num
.top();
}
int main()
{
int t
;
cin
>> t
;
string exp
;
while (t
--)
{
cin
>> exp
;
cout
<< fixed
<< setprecision(4)<<culExp(exp
) << endl
;
}
}
转载请注明原文地址: https://lol.8miu.com/read-9989.html