using namespace std
#include <stdio.h>
#include <stdlib.h>
#define add 43
#define subs 45
#define mult 42
#define div 47
#define MAXSIZE 100
typedef struct
{
int stkdata[MAXSIZE]
int top
} STKzone
typedef STKzone *STK
typedef enum {True=1,False=0} boo
typedef enum {ok,error} status
STKzone expSTKzone
STK expSTK
STK initSTK(STKzone *stack_zone)
{
stack_zone->top=-1
return stack_zone
}
status push(int*term,STK pstk)
{
if(pstk->top==MAXSIZE-1)
return error
pstk->stkdata[++pstk->top]=*term
return ok
}/*push*/
bool emptySTK(STK pstk)
{
return(pstk->top==-1)
}
status pop(int *pdata,STK pstk)
{
if(emptySTK(pstk))
return error
else
{
*pdata=pstk->stkdata[pstk->top]
(pstk->top)--
return ok
}
}
void synerror()
{
printf("\n 表达式语法错误!\n")
exit(0)
}
int eval(int a1,int a2,char tag)
{
switch(tag)
{
case add:
return(a1+a2)
case subs:
return(a1-a2)
case mult:
return(a1*a2)
case div:
return(a1/a2)
}
}
int main()
{
char c
int opd1,opd2,temp,c1
expSTK=initSTK(&expSTKzone)
opd1 = 1
opd2 = 2
push(&opd1,expSTK)
push(&opd2,expSTK)
printf("\n后缀表达式:")
while((c=getchar())!='/n')
{
if(c==' ')continue
if((c>47)&&(c<58))
{
c1=c-48
if(push(&c1,expSTK)==error)
{
printf("\表达式太长\n")
exit(0)
}
}
else if((c==add)||(c==subs)||(c==mult)||(c==div))
{
if(pop(&opd1,expSTK)==error)
synerror()
if(pop(&opd2,expSTK)==error)
synerror()
temp=eval(opd1,opd2,c)
push(&temp,expSTK)
}
else {
// 你输入的c是多少,根本就不对
printf("c = [%c], c=%0X\n",c,c)
synerror()
}
}/*while*/
if(pop(&temp,expSTK)==error) synerror()
if(!(emptySTK(expSTK))) synerror()
printf("=%-3d\n",temp)
return 0
}/*main_end*/
// 执行程序看看,你输入的c值是多少,重新设计一下吧
#include <iostream>
#include <string.h>
using namespace std
template <class T>
class Stack
{
private:
T *s
int maxlength
int top
public:
Stack(int max)
{
maxlength=max
s=new T[max]
top=-1
}
bool isFull()
{
return top==maxlength-1
}
bool isEmpty()
{
return top==-1
}
T getTopElement()
{
return s[top]
}
void push(T x)
{
if(isFull())
return
s[++top]=x
}
T pop()
{
if(isEmpty())
return '!'
T tmp=s[top]
top--
return tmp
}
void clear()
{
top=-1
}
void traverse()
{
for (int i=0i<=topi++)
{
cout<<s[i]<<" "
}
cout<<endl
}
}
class Calculator
{
private:
char *f
int maxlength
Stack<double>*s
Stack<char>*t
int getP(char t)
{
switch(t)
{
case '+':
return 0
break
case '-':
return 0
break
case '*':
return 1
break
case '/':
return 1
break
}
return -1
}
double getResult(char *f)
{
s->clear()
int l=strlen(f)
for(int i=0i<li++)
{
if(f[i]>='0'&&f[i]<='9')
s->push(f[i]-'0')
else
{
double x2=s->pop()
double x1=s->pop()
switch(f[i])
{
case '+':
s->push(x1+x2)
break
case '-':
s->push(x1-x2)
break
case '*':
s->push(x1*x2)
break
case '/':
s->push(x1/x2)
break
}
}
// s->traverse()
}
double res=s->pop()
return res
}
public:
Calculator(int max)
{
maxlength=max
s=new Stack<double>(max)
t=new Stack<char>(max)
}
double run(char *input)
{
char *opt=new char[maxlength]
char *inp=new char[strlen(input)+2]
int cnt=0,l=strlen(input)+2
for(int i=0i<l-2i++)
inp[i]=input[i]
inp[l-2]='#'
inp[l-1]=0
cout<<inp<<endl
t->push('#')
for(int i=0i<li++)
{
if(inp[i]>='0'&&inp[i]<='9')
opt[cnt++]=inp[i]
else
{
if(t->getTopElement()=='#' || getP(t->getTopElement())<getP(inp[i]) || inp[i]=='(')
t->push(inp[i])
else if(inp[i]=='#')
{
while(!t->isEmpty()&&t->getTopElement()!='#')
opt[cnt++]=t->pop()
}
else if(inp[i]==')')
{
while(!t->isEmpty()&&t->getTopElement()!='(')
opt[cnt++]=t->pop()
t->pop()
}
else if(getP(t->getTopElement())>=getP(inp[i]))
{
while(!t->isEmpty()
&&getP(t->getTopElement())>=getP(inp[i])
&&t->getTopElement()!='(')
opt[cnt++]=t->pop()
t->push(inp[i])
}
}
cout<<"stack:T:\t"
t->traverse()
}
opt[cnt]=0
cout<<opt<<endl
return getResult(opt)
}
}
int main()
{
Calculator c(30)
int a[2005]
freopen("rpncalc.in","r",stdin)
freopen("rpncalc.out","w",stdout)
cin>>a
cout<<c.run("a")<<endl
}