数据结构:栈的应用之中缀转后缀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); }
相关文章推荐
- 堆栈的应用(2) 中缀算术表达式到后缀(逆波兰记法reverse polish notation)的转换及其计算 C++实现
- 堆栈的应用(2) 中缀算术表达式到后缀(逆波兰记法reverse polish notation)的转换及其计算 C++实现
- 栈应用之中缀转后缀表达式计算(C++、JAVA)
- 数据结构-栈应用(中缀转后缀并计算结果)
- python数据结构与算法 8栈的应用之中缀前缀后缀
- 数据结构—中缀表达式转后缀表达式算法及实现—栈的应用—计算表达式(C++代码实现)(1)
- 数据结构 中缀转后缀 后缀转二叉树并打印 后缀计算值
- 数据结构、算法与应用 (C++描述) 第二版 1.2
- 数据结构、算法与应用 (C++描述) 第二版 1.10
- 表达式求值 数据结构 C/C++ 栈的应用
- 数据结构学习实录三—中缀表达式转后缀表达式(C++)
- 栈的应用(括号匹配、后缀表式计算、中缀转后缀)
- 数据结构-----栈(逆波兰表达式)----中缀转后缀
- 栈应用——中缀转后缀+后缀计算
- [C++]数据结构:散列表HashTable的实现与简单应用
- 数据结构、算法与应用 (C++描述) 第二版 1.1
- 数据结构-前缀、中缀、后缀表达式
- 数据结构、算法与应用 (C++描述) 第二版 1.12
- 中缀表达式转后缀表达式(逆波兰式)的C++代码
- 数据结构、算法与应用(C++描述) 第二版 1.20