|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define Stack_InitSize 20
#define addSize 5
#pragma warning(disable:4996)
typedef double ElemType;
typedef struct sqStack
{
ElemType* base;
ElemType* top;
int stackSize;
}sqStack;
void InitStack(sqStack* s)
{
s->base = (ElemType*)malloc(Stack_InitSize * sizeof(ElemType));
if (!s->base)
exit(0);
s->top = s->base;
s->stackSize = Stack_InitSize;
}
void Push(sqStack* s, ElemType e)
{
if (s->top - s->base >= s->stackSize)
{
s->base = (ElemType*)realloc(s->base, (addSize + s->stackSize) * sizeof(ElemType));
if (!s->base)
exit(0);
s->top = s->base + s->stackSize;
s->stackSize = s->stackSize + addSize;
}
*s->top = e;
s->top++;
}
void Pop(sqStack* s, ElemType* e)
{
if (s->top == s->base)
{
*e = *(--s->top);
}
}
int main()
{
sqStack s;
char c;
int i = 0;
double d, e;
char str[10];
InitStack(&s);
printf("请按逆波兰表达式输入运算式 : \n");
scanf("%c", &c);
while (c != '\n')
{
while (isdigit(c) || c == '.')//判断c的ASCII码是否为48-57 即是否为数字
{
str[i++] = c;
str[i] = '\0';
if (i >= 10)
{
printf("出错,输入的单个数据过大\n");
return -1;
}
scanf("%c", &c);
if (c == ' ')
{
d = atof(str);
//将字符串转化为浮点数
Push(&s,d);
i = 0;
break;
}
}
switch (c)
{
case '+' :
Pop(&s, &e);
Pop(&s, &d);
Push(&s, d + e);
break;
case '-' :
Pop(&s, &e);
Pop(&s, &d);
Push(&s, d - e);
break;
case '*':
Pop(&s, &e);
Pop(&s, &d);
Push(&s, d * e);
break;
case '/':
Pop(&s, &e);
Pop(&s, &d);
if (d == 0)
{
printf("除数为0\n");
exit(0);
}
Push(&s, d / e);
break;
}
scanf("%c", &c);
}
Pop(&s, &d);
printf("最终结果为 : %f\n", d);
return 0;
}
/*
请按逆波兰表达式输入运算式 :
1 2 +
最终结果为 : 2.000000
D:\ly\逆波兰计算\x64\Debug\逆波兰计算.exe (进程 16904)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
*/
本帖最后由 傻眼貓咪 于 2022-3-23 21:24 编辑
试试我的代码吧,这是我第一次写逆波兰表达式运算式 ,如有错,见笑了:(只能作单个数字运算,0~9)
#include <iostream>
#include <utility>
#include <cctype>
#include <string>
#include <iomanip>
template <typename T>
class Operand {
public:
T val;
Operand* next;
};
auto isINT = [](double d) -> bool { return !(d - static_cast<int>(d)); };
template<typename T>
void push(Operand<T>** Opr, T val) {
Operand<T>* p = *Opr;
(*Opr) = new Operand<T>;
(*Opr)->val = val;
(*Opr)->next = p;
}
template<typename T>
std::pair<T, T> pop(Operand<T>** Opr) {
std::pair<T, T> val;
Operand<T>* p = *Opr;
(*Opr) = (*Opr)->next;
val.second = p->val;
delete p;
p = (*Opr);
(*Opr) = (*Opr)->next;
val.first = p->val;
delete p;
return val;
}
template<typename T>
double operation(std::pair<T, T> P, char c) {
T a = P.first;
T b = P.second;
switch (c)
{
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b) {
return a / b;
}
else {
std::cout
<< "error: division by zero"
<< std::endl;
exit(0);
}
}
exit(0);
return 0;
}
template<typename T>
void print(Operand<T>** Opr) {
Operand<T>* p = (*Opr);
while (p) {
if (isINT(p->val)) {
std::cout << std::fixed << std::setprecision(0);
}
std::cout
<< p->val
<< " ";
p = p->next;
}
std::cout << std::endl;
}
int main() {
Operand<double>* Opr = NULL;
std::string str;
std::getline(std::cin, str);
// 只能单个数值运算,比如:7、6、8 等
for (char c: str) {
if (c >= '0' && c <= '9') {
push(&Opr, static_cast<double>(c - '0'));
}
else if (c == '+' || c == '-' || c == '*' || c == '/') {
push(&Opr, operation(pop(&Opr), c));
}
}
print(&Opr);
delete Opr;
return 0;
}
以 5 + ((1 + 2) * 4) - 3 为例:
5 1 2 + 4 * + 3 -(逆波兰表示法)
|
|