您的位置:首页 > 理论基础 > 数据结构算法

数据结构:栈的应用之中缀转后缀C++

2012-12-18 20:44 405 查看
            最近刚学数据结构,写了一个中缀转后缀的程序,虽然勉强达到了目的,但是感觉写的很繁琐。希望各位大神能够帮忙指出我程序中的问题,代码风格、语法错误、实现算法等方面的问题都可以指出,谢谢!

头文件:

#include <iostream>
#include <string>

using namespace std;

typedef struct
{
char elements[100];
int top;
} body;

class stack
{
public :
body S;
stack()
{
S.top = -1;
}

bool Empty()
{
if(S.top<0)
return true;
else
return false;
}

char Pop()
{
char x;
if(Empty())
x = ' ';
else
{
x = S.elements[S.top];
S.top--;
}
return x;
}

void Push(char x)
{
if(S.top == 99)
cout << "the stack is already full";
else
{
S.top = S.top +1;
S.elements[S.top] = x;
}
}
};


正文:

#include <iostream>
#include <string>
#include "STACK.h"
#include <stdlib.h>
#include <string.h>

using namespace std;

void operate(string lines, stack &pre, stack &post);//将中缀转为后缀
int priority(char op);//比较运算符的优先级
void caculate(stack &post);//将字符串转换为数字
float expression(float x, float y, char z);//运算

int main()
{
string lines;//将输入存储为字符串
stack pre ;//用于存储运算符
stack post ;//存储后缀表达式
cout << "请输入算数中缀表达式:" ;
cin >> lines;
cout << "后缀表达式为:";
operate(lines, pre, post);
cout << endl;
caculate(post);
return 0;
}

void operate(string lines, stack &pre, stack &post)
{
int len = lines.length();
char x;
for (int i=0; i<len; i++)
{
//分为两大类,若是数字或是小数点,则直接进入到post栈里
//否则先进入到pre中处理后在进入到post栈中
//用空格进行操作数的分离,和输出的排版
if ((lines[i]>='0' && lines[i]<='9') || lines[i]=='.')
{
post.Push(lines[i]);
cout << lines[i];
}
else
{
post.Push(' ');//将数字分开,以便后面进行多位数的区分
if (lines[i] == '(')
pre.Push(lines[i]);
else if (lines[i] == ')')
{
cout << ' ';
x = pre.Pop();
while (x != '(')    //将栈中'('前的所有优先级比
{                   //此时的运算符大的弹出
post.Push(x);   //再将此时的值压入
cout << x;
x = pre.Pop();
}
}
else if (lines[i]=='+' || lines[i]=='-' || lines[i]=='*' || lines[i]=='/')
{
cout << ' ';
if (!pre.Empty())
{
x = pre.Pop();
while (priority(lines[i])<=priority(x))//比较优先级
{
post.Push(x);
cout << x;
cout << ' ';
if(pre.Empty())
break;
x = pre.Pop();
}
if (priority(x)<priority(lines[i]))
pre.Push(x);
}
pre.Push(lines[i]);
cout << ' ';
}
}
}
while (!pre.Empty())//将pre中未出栈的操作符弹出
{
x = pre.Pop();
post.Push(x);
cout << ' ';
cout << x;
cout << ' ';
}
}

int priority(char op)//优先级比较函数
{
switch (op)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '(':
return 0;
default:
break;
}
}

void caculate(stack &post)
{
char z, m[15];
float x, y;
int n, i=0, j=0;
float value[100];

while(i <= post.S.top)
{
if((post.S.elements[i]>='0' && post.S.elements[i]<='9') || post.S.elements[i]=='.')
{
n = 0;
memset(m,0,15);//清空m中残存的干扰值;
while((post.S.elements[i]>='0' && post.S.elements[i]<='9') || post.S.elements[i]=='.')
{//将字符型操作数分开存储在value中
m[n++] = post.S.elements[i];
i++;
}
value[j++]  = atof(m);//字符串型数字转换为float型;
}
if(post.S.elements[i] == ' ')
i++;
else
{
y = value[--j];
x = value[--j];
z = post.S.elements[i];
value[j++] = expression(x,y,z);
i++;
}
}
cout << " the result is : " << value[--j] << endl;
}

float expression(float x, float y, char z)//返回单次计算值
{
if(z == '+')
return(x+y);
else if(z == '-')
return(x-y);
else if(z == '*')
return(x*y);
else if(z == '/')
return(x/y);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息