思路1:(较易理解)
#include<iostream>
#include <iomanip>
using namespace std;
#define OK 1
#define ERROR 0
#define MaxSize 100
typedef double ElemType;
//定义栈_顺序栈
struct Stack
{
ElemType* top;
ElemType* base;
int stacksize;
};
int IsFull(Stack s);
int IsEmpty(Stack s);
//初始化
int InitStack(Stack& s)
{
s.base = new ElemType[MaxSize];
if (!s.base)
return ERROR;
s.top = s.base;
s.stacksize = MaxSize;
return OK;
}
//入栈
int PushbackStack(Stack& s, ElemType e)
{
//判断栈是否满
if (IsFull(s))
return ERROR;
*(s.top) = e;//error
(s.top)++;
return OK;
}
//出栈
ElemType Popback(Stack& s)
{
//判断栈是否空
if (IsEmpty(s))
return ERROR;
s.top--;
return *(s.top);
}
//判满
int IsFull(Stack s)
{
if (s.top - s.base == s.stacksize)
return 1;
return 0;
}
//判空
int IsEmpty(Stack s)
{
if (s.base == s.top)
return 1;
return 0;
}
//获取栈顶元素
ElemType StackTop(Stack s)
{
//判断栈是否空
if (IsEmpty(s))
return ERROR;
return *(s.top - 1);
}
//清除栈
int ClearStack(Stack& s)
{
s.top = s.base;
return OK;
}
int Destroy(Stack& s)
{
delete s.base;
s.top = s.base = NULL;
return OK;
}
//计算
double calculating(double num1, char s, double num2)
{
switch (s)
{
case'+':
return (double)(num1 + num2)*(1.0);
case'-':
return (double)(num1 - num2) * (1.0);
case'*':
return (double)(num1 * num2) * (1.0);
case'/':
return (double)((num1*1.00) /(double) num2) * (1.0);
}
}
double EvaluateExpressionSuffix(int &flag)
{
//OPND:存放数字
Stack OPND;
char ch;
InitStack(OPND);
cin >> ch;
if (ch == '=')
{
flag = 0;
return ERROR;
}
do
{
if (isdigit(ch))
{
PushbackStack(OPND, (double)(ch - 48));//将字符转为数字,如果是ch,结果为-1
cin >> ch;
}
else
{
PushbackStack(OPND, calculating(Popback(OPND),
ch, Popback(OPND)));//计算结果并入栈
cin >> ch;
}
} while (ch != '=');
return Popback(OPND);
}
void test1()
{
int flag = 1;
while (1)
{
double outcome=EvaluateExpressionSuffix(flag);
if (!flag)
return;
cout << fixed << setprecision(2) << outcome << endl;
}
}
int main()
{
test1();
return 0;
}
遇到的问题:类型的转换:从char类型转换到其他类型
double类型数据精度:保留两位小数
思路二:将每行输入存放到数组里,从数组中读取
栈的操作与之前相同,下面便不再写了
//计算
double calculating(double num1, char s, double num2)
{
switch (s)
{
case'+':
return (double)(num1 + num2)*(1.0);
case'-':
return (double)(num1 - num2) * (1.0);
case'*':
return (double)(num1 * num2) * (1.0);
case'/':
return (double)((num1*1.00) /(double) num2) * (1.0);
}
}
Status EvaluateExpressionSuffix(char *s)
{
//OPND:存放数字
Stack OPND;
InitStack(OPND);
int i = 0;
if (s[i] == '=')//防止死循环
return ERROR;
while (s[i] != '='&&s[i]!='\0')
{
if (isdigit(s[i]))
{
PushbackStack(OPND, (double)(s[i] - 48));//将字符转为数字,如果是ch,结果为-1
}
else if (s[i] == ' ')
;
else
{
//error
PushbackStack(OPND, calculating(Popback(OPND),
s[i], Popback(OPND)));//计算结果并入栈
}
i++;
}
cout << fixed << setprecision(2) << Popback(OPND) << endl;
return OK;
}
void test1()
{
while (1)
{
char s[MaxSize];//死循环打印0.00,为什么??
//因为cin.get操作后会在缓冲区存留0
cin.get(s, MaxSize);//读入数据并存放到数组s中,在字符串尾部加'\0'
char ch = cin.get();// 清除缓冲区结尾残留的字符\0
int flag = EvaluateExpressionSuffix(s);
if (!flag )
break;
}
return;
}
int main()
{
test1();
return 0;
}
问题:计算1 2/=时,结果总是为0.00,
解决:将整数除法转化为小数除法,影响转换成double在计算,注意顺序
使用cin>>读取每一行时遇到空格会停止,所以要使用cin.get函数
对cin函数不够了解导致出错
解决:查找资料,并自己写一些简单的例子测试,多多调试加深理解
cin相关知识点:https://blog.csdn.net/a3192048/article/details/80303547
如果对cin及其函数不太了解,推荐第一种写法
标签:ch,return,OJ,int,double,OPND,北林,求值,Stack From: https://blog.51cto.com/u_15805257/7901705