1.中缀表达式求值
两个栈:OPND(double类数),OPTR(操作符)。
需要比较栈里和栈外ch的操作符优先级。
运用到atof()函数(stdlib.h),将*char 转换为 double。
#include<iostream>
#include<iomanip>
#include<string.h>
#include<stdlib.h>
using namespace std;
#define MAXSIZE 1000
typedef struct
{
double *base;
double *top;
int stacksize;
}SqStack_d;
void Init(SqStack_d &S)
{
S.base = new double[MAXSIZE];
S.top = S.base;
S.stacksize = MAXSIZE;
}
void Push(SqStack_d &S, double e)
{
if(S.top - S.base != S.stacksize)
{
*S.top = e;
S.top++;
}
}
void Pop(SqStack_d &S, double &e)
{
if(S.top !=S.base)
{
S.top--;
e = *S.top;
}
}
double GetTop(SqStack_d S)
{
if(S.base !=S.top)
{
return *(S.top-1);
}
}
typedef struct
{
char *base;
char *top;
int stacksize;
}SqStack_c;
void Init(SqStack_c &S)
{
S.base = new char[MAXSIZE];
S.top = S.base;
S.stacksize = MAXSIZE;
}
void Push(SqStack_c &S, char e)
{
if(S.top - S.base != S.stacksize)
{
*S.top = e;
S.top++;
}
}
void Pop(SqStack_c &S, char &e)
{
if(S.top !=S.base)
{
S.top--;
e = *S.top;
}
}
char GetTop(SqStack_c S)
{
if(S.base !=S.top)
{
return *(S.top-1);
}
}
bool In( char ch)
{
if(ch>='0'&&ch<='9')
{
return true;
}
else return false;
}
char Com(char c1,char c2) //c1:in c2:out or ch
{
if((c1=='('&&c2==')')||(c1=='='&&c2=='='))
return '=';
else if(((c1=='+'||c