您的位置:首页 > 其它

用数组创建一个栈(stack),并利用栈来计算后缀表达式的值

2014-04-05 23:13 471 查看
  今天复习了栈,栈可以用数组也可以用链表来实现,利用数组当然更好,因为可以避免指针。在用数组实现栈以后,我利用这个栈实现了计算后缀表达式的值。这当中有一点需要动点小脑筋。比如后缀表达式:4.99 1.06 * 5.99 + 6.99 1.06 * +这个表达式中有double型数字,有空格,有特殊符号,那么怎么来获得一个统一的输入接口呢?

  我的做法是定义一个char类型的变量in,这样in接收到的便有4中类型的字符(‘\n’除外):(‘0-9’);‘.’; 空格;以及4个操作符。当in不等于回车时我们便一直保持输入。这里利用了一个细节:我们的push操作只发生在遇到空格的时候。于是我们在外部定义一个double型变量digit,用于存储数据。当遇到数字型字符时,我们将其累加到digit,当然这里不是简单的累加,因为存在小数点。于是我们定义一个AfterPoint用于记录在小数点后拥有多少位数字,所以表达式就变成了:Digit=Digit+数字*pow(10,AfterPoint)。当然还额外需要一个变量用于记录我们是否遇到了小数点(否则我们怎么判断一个数字是否位于小数点后呢)。MeetPoint在遇到‘.’后被置为1。此后我们只要每次遇到数字的时候只要MeetPoint为1,将AfterPoint减一即可。在遇到空格的时候Push,所有的这些预置的变量重置即可。

输入示例:4.99 1.06 * 5.99 + 6.99 1.06 * +

输出示例:The result of the postfix expression is:18.6888

以下是代码,可以运行

//achive a stack using array and use it to compute the value of postfix expression
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
#ifndef _Stack_h
struct StackRecord;
typedef StackRecord *Stack;
int IsEmpty(Stack S);
int IsFull(Stack S);
Stack CreateStack(int MaxElements);
void MakeEmpty(Stack S);
void Push(double X,Stack S);
void Pop(Stack S);
double Top(Stack S);
void DisposeStack(Stack S);
#endif
/*head*/
#define EmptyTOS -1
//define the base struct of stack
struct StackRecord
{
int Capacity;
int TopOfStack;
double *Array;
};
//method used to create stack
Stack CreateStack(int MaxElements)
{
Stack S;
S=(Stack)malloc(sizeof(struct StackRecord));
if(S==NULL)
{
cout<<"Out Of Space!";
return NULL;
}
S->Array=(double*)malloc(sizeof(double)*MaxElements);
if(S->Array==NULL)
{
cout<<"Out Of Space!";
return NULL;
}
S->Capacity=MaxElements;
MakeEmpty(S);
return S;
}

//method to despose stack
void DisposeStack(Stack S)
{
if(S!=NULL)
{
free(S->Array);
free(S);
}
}
//method to test if empty
int IsEmpty(Stack S)
{
return S->TopOfStack==EmptyTOS;
}
//method used to test if full
int IsFull(Stack S)
{
return S->TopOfStack==S->Capacity;
}
//method to make a empty stack
void MakeEmpty(Stack S)
{
S->TopOfStack=EmptyTOS;
}
//method used to push
void Push(double X,Stack S)
{
if(IsFull(S))
{
cout<<"The Stack Is Full!";
return;
}
else
S->Array[++(S->TopOfStack)]=X;
}
//method used to Pop
void Pop(Stack S)
{
if(IsEmpty(S))
{
cout<<"The Stack Is Empty!";
return;
}
else
S->TopOfStack--;
}
//method used to return the top of stack
double Top(Stack S)
{
if(IsEmpty(S))
{
cout<<"The Stack Is empty";
return NULL;
}
else
return S->Array[S->TopOfStack];
}

//the main method used to output the result of a postfix expression
int main()
{
Stack S=CreateStack(20);
char in;
int MeetPoint=0;//used to judge if we have meet the point
int AfterPoint=0;//used to compute the value,set to -1 when there is a space
double Digit=0;//used to hold the value of input
//note the truth that we push if and only if we meet a space, and after the space every original setting should be reseted.
while((in=getchar())!='\n')
{
if(in>='0'&&in<='9')
{
if(MeetPoint)
AfterPoint--;
Digit+=(in-48)*pow(10,AfterPoint);
}
if(in=='.')
{
MeetPoint=1;
}
if(in==' ')
{
Push(Digit,S);
Digit=0;
MeetPoint=0;
AfterPoint=0;
}
if(in=='+'||in=='-'||in=='*'||in=='/')
{
double Operator1,Operator2;
Operator1=Top(S);
Pop(S);
Operator2=Top(S);
Pop(S);
if(in=='+')
Digit=Operator1+Operator2;
if(in=='-')
Digit=Operator1-Operator2;
if(in=='*')
Digit=Operator1*Operator2;
if(in=='/')
Digit=Operator1/Operator2;
}
}
Push(Digit,S);
cout<<"The result of the postfix expression is:";
cout<<Digit<<endl;
DisposeStack(S);
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  后缀表达式
相关文章推荐