/* * File name : LinkList.cpp * Function : 逆波兰表达式 转换和计算 的C++实现 中缀表达式转化为 后缀表达式: 1. 数字直接输出 2.左括号直接进栈 3.右括号直接出栈, 至遇到左括号( 左括号最后也需要出栈) 4. + - * / 若栈顶是左括号,直接入栈 否则, 优先级低于栈顶符号,持续出栈,最后入栈.; 优先级 大于等于栈顶符号,入栈 逆波兰表达式 计算: 1. 数字入栈 2. 遇到符号,出栈两个数字,运算结果入栈. * Created on : 2016年4月22日 * Author : [email protected] * Copyright : 欢迎大家和我一起交流学习,转载请保持源文件的完整性。 任何单位和个人不经本人允许不得用于商业用途 */ #include <cstdio> #include <iostream> using namespace std; typedef char Data_t; typedef struct node { Data_t data; struct node * next; }Node, *pNode; typedef struct { pNode ptop; int count; }Lstack; void Lstack_init(Lstack &LS); void Lstack_push(Lstack &LS, Data_t elem); bool Lstack_pop(Lstack &LS, Data_t &elem); bool Lstack_is_empty(Lstack &LS); int Lstack_get_length(Lstack &LS); bool Lstack_get_elem(Lstack &LS, Data_t &elem); int get_priority(char c); int priority_cmp(char x, char y); void transfer_to_after(char * str , char * one); int calc(char a, char b, char operate); int get_rpn_value(char * str); int main(int argc, char** argv) { char elem='\0'; int k = 0; char *str = "2+(2+1)*2-3"; char target[20]="\0"; cout << "9=" << '9' << endl; Lstack ls; Lstack_init(ls); cout << "original string is : " << str << endl; char *p = str; transfer_to_after(str, target); cout << "Target is: " << target << endl; cout << "value is " << get_rpn_value(target) << endl; return 0; } int calc(char a, char b, char operate) { int ret = 0; int x = a - 48, y=b-48; switch (operate) { case '+' : ret = x + y; break; case '-': ret = x - y; break; case '*': ret = x * y; break; case '/': if (x == 0) cout << "fault, no meanning" << endl; ret = x + y; break; default: break; } return ret; } int get_rpn_value(char * str) { Lstack ls; Lstack_init(ls); char a; char b; char c; int tmp; char *p = str; while (*p != '\0') { switch (*p) { case '+': case '-': case '*': case '/': Lstack_pop(ls, b); Lstack_pop(ls, a); tmp=calc(a, b, *p); c = tmp + 48; Lstack_push(ls,c); break; default: Lstack_push(ls, *p); break; } p++; } Lstack_pop(ls, c); return c-48; } void transfer_to_after(char * str, char * one) { Lstack ls; Lstack_init(ls); char *p = str; int k = 0; char tmp; while (*p != '\0') { switch (*p) { case '(' : Lstack_push(ls, *p); p++; break; case ')': Lstack_get_elem(ls, tmp); while (tmp != '(') { Lstack_pop(ls, tmp); one[k++] = tmp; Lstack_get_elem(ls, tmp); } Lstack_pop(ls, tmp); p++; break; case '+': case '-': case '*': case '/': Lstack_get_elem(ls, tmp); if (tmp == '(') { Lstack_push(ls, *p); } else { while( priority_cmp(*p, tmp) < 0) { Lstack_pop(ls, tmp); one[k++] = tmp; Lstack_get_elem(ls, tmp); } Lstack_push(ls, *p); } p++; break; default: one[k++] = *p; p++; break; } } while (Lstack_is_empty(ls) != true) { char tmp; Lstack_pop(ls, tmp); one[k++] = tmp; } } int get_priority(char c) { switch (c) { case '+': case '-': return 1; break; case '*': case '/': return 2; break; case '(': case ')': return 3; break; default: return 0; break: } } int priority_cmp(char x, char y) { return (get_priority(x) - get_priority(y)); } void Lstack_init(Lstack &LS) { LS.count = 0; LS.ptop = nullptr; } void Lstack_push(Lstack &LS, Data_t elem) { pNode p = LS.ptop; pNode ptmp = nullptr; ptmp = new Node; ptmp->data = elem; ptmp->next = p; LS.ptop = ptmp; LS.count++; } bool Lstack_pop(Lstack &LS, Data_t &elem) { pNode p = LS.ptop; pNode ptmp = nullptr; if (LS.count == 0) { cout << "Lstack is empty,can not pop." << endl; elem = -1; return false; } ptmp = LS.ptop; elem = LS.ptop->data; LS.ptop = ptmp->next; delete ptmp; LS.count--; return true; } bool Lstack_is_empty(Lstack &LS) { return (LS.count == 0) ? true : false; } int Lstack_get_length(Lstack &LS) { return LS.count; } bool Lstack_get_elem(Lstack &LS, Data_t &elem) { if (Lstack_is_empty(LS)) { cout << "Lstack is empty." << endl; return false; } elem = LS.ptop->data; return true; }