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

《算法与数据结构---C语言描述》里的求多项达式值

2016-12-31 09:52 323 查看
在栈的应用里,作者写了将多项式值 ,但是描述的不是很清楚,露了一个说明 。
从左到右读取中缀表达式,依次一个操作项.

如果是操作数直接进入输出队列.

读到左括号时总是将它压入栈中.

读到右括号, 将最近栈顶的第一个左括号上面的操作符全部依次弹出, 送至输出队列后, 再丢弃左括号.

当读到操作符时,将栈中所有优先级高于或等于当前操作符的操作符弹出,送到输出队列中.

中缀表达式全部读完后,若栈中仍然有运算符,将其送到输出队列中

少了5,6的说明,但是从例子中也能看出来。一定要注意,遇到优先级高于当前处理的操作符时,栈内高于或等于此优先级的操作符逐一出栈,直到遇到小于此操作符的。

 

我写了多项式求值的示例代码。为了处理方便,将char** 用string[]来处理了。

栈数据结构的代码




栈的实现

1 #ifndef LINKSTACK_H
2 #define LINKSTACK_H
3
4 template<class T>
5 class Node
6 {
7 public:
8     T m_Data;
9     Node* m_Link;
10 public:
11     Node(){
12         m_Link = NULL;
13     }
14 };
15 template<class T>
16 class LinkStack
17 {
18 public:
19     LinkStack(){
20         mTop = NULL;
21     }
22     ~LinkStack(){
23
24     }
25     bool isEmptyStack_link(){
26         if (mTop == NULL)
27         {
28             return true;
29         }
30         return false;
31     }
32     bool push_link(T data){
33         Node<T>* newnode = new Node<T>();
34         newnode->m_Data = data;
35         if(!newnode){
36             return false;
37         }
38         newnode->m_Link = mTop;
39         mTop = newnode;
40
41         return true;
42     }
43     T pop_link(){
44         if (!isEmptyStack_link())
45         {
46             T data = (mTop->m_Data);
47             Node<T>* p = mTop->m_Link;
48             if(mTop){
49                 delete mTop;
50             }
51             mTop = p;
52
53             return data;
54         }
55     }
56     T top_link(){
57         if (!isEmptyStack_link())
58         {
59             T data = (mTop->m_Data);
60
61             return data;
62         }
63     }
64 private:
65     Node<T>* mTop;
66 };
67 #endif


多项式求值的代码




多项式求值

1 // stack.cpp : 定义控制台应用程序的入口点。
2 //
3
4 #include "stdafx.h"
5 #include "string.h"
6 #include <stdlib.h>
7 #include "seqstack.h"
8 #include "linkstack.h"
9 #include <string>
10 #include <iostream>
11 using namespace std;
12 /*
13 * 前缀表达式转后缀表达式
14 */
15 int  fixpost(char* src,string& ret,int len);
16 bool isOp(const char* var);                    //是否是操作符
17 int  result(string* des,int len);            //求后缀表达式的值(参数为已经转成后缀表达式的字符串)
18 int getPriority(const char* s);                //求某操作符对应的优先级
19 char g_op[]= {'+','-','*','/','(',')'};        //存储表达式
20 int  g_priority[]={1,1,2,2,3,3};            //各个表达式的优先级
21 int divtoken(const char* src,string* ret);    //将表达式以空格分隔
22 int _tmain(int argc, _TCHAR* argv[])
23 {
24     LinkStack<int> stack;
25
26     //     for (int i = 0 ; i < 10 ; i++)
27 //     {
28 //         stack.push_link(i);
29 //     }
30 //     for (int i = 0 ; i < 10 ; i++)
31 //     {
32 //         printf("%d\n",stack.pop_link());
33 //     }
34     char* src = "31 * ( 5 - 22 ) + 70";
35     string ret ;
36     fixpost(src,ret,strlen(src));
37     cout<<ret<<endl;
38     string ss[10];
39     int n = divtoken(ret.c_str(),ss);
40     cout<<result(ss,n)<<endl;
41     system("pause");
42     return 0;
43 }
44 bool isOp(const char* var)
45 {
46
47     if (strstr(g_op,var) == 0)
48     {
49         return false;
50     }
51     return true;
52 }
53 int divtoken(const char* src,string* ret)
54 {
55     int len = strlen(src);
56     if( src[len-1] == ''){
57         len --;
58     }
59     int j = 0;
60     for (int i = 0 ; i < len; i++)
61     {
62         char a = src[i];
63         if(src[i] == ''){
64             j++;
65             continue;
66         }
67         ret[j] += src[i];
68     }
69
70     return j+1;
71 }
72 int fixpost(char* src,string& ret ,int len)
73 {
74     if( len <= 0 ){
75         return 0;
76     }
77
78     string str[30];
79     int l = divtoken(src,str);
80
81     LinkStack<string> stack;
82     int j = 0;
83
84     string temp = "";
85     for (int i = 0; i < l; i++)
86     {
87         if (!isOp(str[i].c_str()))
88         {
89             ret += str[i];
90             ret+="";
91         }
92         else{
93             if (str[i] ==")")
94             {
95                 while (!stack.isEmptyStack_link() && stack.top_link() != "(")
96                 {
97
98                     temp = stack.pop_link();
99                     if(temp!= "("){
100                         ret += temp;
101                         ret+="";
102                     }
103
104                 }
105                 stack.pop_link();
106             }
107             else{
108                 int pri = getPriority(str[i].c_str());
109                 while (!stack.isEmptyStack_link() &&
110                     stack.top_link() != "(" &&
111                     stack.top_link() != ")" &&
112                     str[i] != "("
113                     )
114                 {
115                     if ( getPriority(stack.top_link().c_str()) >= pri )
116                     {
117                         temp = stack.pop_link();
118                             ret += temp;
119                             ret+="";
120                     }
121                 }
122                 stack.push_link(str[i]);
123             }
124         }
125     }
126     while (!stack.isEmptyStack_link())
127     {
128         ret += stack.pop_link();
129         ret+="";
130     }
131
132     return 0;
133 }
134 int getPriority(const char* s)
135 {
136     int index = -1;
137     int i = 0;
138     for (;i < sizeof(g_op)/sizeof(char);i++)
139     {
140         if(g_op[i] == *s){
141             index = i;
142             break;
143         }
144     }
145     if(index == -1){
146         return -1;
147     }
148     return g_priority[i];
149 }
150 int  result(string* des,int len)
151 {
152
153     LinkStack<int> stack;
154     int res = 0;
155     for (int i = 0; i < len; i++)
156     {
157         if (isOp(des[i].c_str()))
158         {
159             int num1 = 0;
160             int num2 = 0;
161             if (!stack.isEmptyStack_link())
162             {
163                 num2 = stack.pop_link();
164                 num1 = stack.pop_link();
165             }
166             int res = 0;
167             //如果有反射就好了
168             if(des[i] == "+"){
169                 res = num1+num2;
170             }
171             if(des[i] == "-"){
172                 res = num1-num2;
173             }
174             if(des[i] == "*"){
175                 res = num1*num2;
176             }
177             if(des[i] == "/"){
178                 if( num2 == 0 ){res = 0;}
179                 else
180                 res = num1/num2;
181             }
182             stack.push_link(res);
183         }
184         else{
185             stack.push_link(atoi(des[i].c_str()));
186         }
187     }
188     res = stack.pop_link();
189     return res;
190 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: