C++ 实现基本运算+-*/

基本实现思想是通过栈来存储数据与运算符(包括加减乘除小括号)

代码如下

自己实现的一个栈

  1 #include<iostream>
  2 #include<stdlib.h>
  3 
  4 using namespace std;
  5 
  6 #define MAXSIZE 1024
  7 
  8 template<class type>
  9 class my_stack
 10 {
 11     int top;
 12     type* my_s;
 13     int maxsize;
 14 
 15 public:
 16     my_stack():top(-1),maxsize(MAXSIZE)
 17     {
 18         my_s = new type[maxsize];
 19         if(my_s==NULL)
 20         {
 21             cerr<<"动态存储分配失败"<<endl;
 22             exit(0);
 23         }
 24     }
 25     my_stack(int size):top(-1),maxsize(size)
 26     {
 27         my_s = new type[maxsize];
 28         if(my_s==NULL)
 29         {
 30             cerr<<"动态存储分配失败"<<endl;
 31             exit(0);
 32         }
 33     }
 34     ~my_stack()
 35     {
 36         delete[] my_s;
 37     }
 38     bool Empty();
 39 
 40     void Push(type tp);
 41 
 42     type Top();
 43 
 44     void Pop();
 45 
 46     int Size();
 47 
 48 };
 49 
 50 template<class type>
 51 bool my_stack<type>::Empty()
 52 {
 53     if(top == -1)
 54         return true;
 55     else
 56         return false;
 57 }
 58 
 59 template<class type>
 60 void my_stack<type>::Push(type tp)
 61 {
 62     if(top+1<maxsize)
 63     {
 64         top++;
 65         my_s[top] = tp;
 66     }
 67     else
 68     {
 69         cout<<"栈满了"<<endl;
 70         exit(1);
 71     }
 72 }
 73 
 74 template<class type>
 75 type my_stack<type>::Top()
 76 {
 77     if(top < 0)
 78     {
 79         cout<<"栈中没有元素"<<endl;
 80         exit(1);
 81     }
 82     else 
 83         return my_s[top];
 84 }
 85 
 86 template<class type>
 87 void my_stack<type>::Pop()
 88 {
 89     if(top < 0)
 90     {
 91         cout<<"栈中没有元素"<<endl;
 92         exit(1);
 93     }
 94     else 
 95     {
 96         top--;
 97     }
 98 }
 99 
100 template<class type>
101 int my_stack<type>::Size()
102 {
103     if(top<0)
104         return 0;
105     else
106         return top+1;
107 }

主函数:

  1 /**
  2 *achive calcutor
  3 *
  4 *收到字符串表达式进行处理
  5 *
  6 */
  7 #include"mystack.h"
  8 #include<string>
  9 #define type1 float
 10 
 11 my_stack<type1> num(100);
 12 my_stack<char> sym(20);
 13 
 14 
 15 bool isdigist(char c)
 16 {
 17     if(c>='0'&& c<='9'||c =='.')
 18         return true;
 19     else
 20         return false;
 21 }
 22 
 23 int calcSize(char *calc)
 24 {
 25     int i = 0;
 26     while(calc[i++]!='');
 27     return i;
 28 }
 29 void processA(char c)
 30 {
 31     if(sym.Empty()||sym.Top() =='(')
 32     {
 33         sym.Push(c);
 34     }
 35     else if(sym.Top() == '+'||sym.Top() == '-')
 36     {
 37         type1 temp=0;//存储计算结果
 38         type1 temp1 = num.Top();
 39         num.Pop();
 40         type1 temp2 = num.Top();
 41         num.Pop();
 42         if(sym.Top()=='+')
 43             temp = temp1+temp2;
 44         else
 45             temp = temp2-temp1;
 46 
 47         sym.Pop();
 48         sym.Push(c);
 49         num.Push(temp);
 50     }
 51     else if(sym.Top() == '*'||sym.Top() == '/')
 52     {
 53         type1 temp=0;//存储计算结果
 54         type1 temp1 = num.Top();
 55         num.Pop();
 56         type1 temp2 = num.Top();
 57         num.Pop();
 58         if(sym.Top()=='*')
 59             temp = temp1*temp2;
 60         else
 61             temp = temp2/temp1;
 62 
 63         sym.Pop();
 64         sym.Push(c);
 65         num.Push(temp);
 66     }
 67 }
 68 void processB(char c)
 69 {
 70     if(sym.Empty()||sym.Top() =='('||sym.Top() == '+'||sym.Top() == '-')
 71     {
 72         sym.Push(c);
 73     }
 74     else if(sym.Top() == '*'||sym.Top() == '/')
 75     {
 76         type1 temp=0;//存储计算结果
 77         type1 temp1 = num.Top();
 78         num.Pop();
 79         type1 temp2 = num.Top();
 80         num.Pop();
 81         if(sym.Top()=='*')
 82             temp = temp1*temp2;
 83         else
 84             temp = temp2/temp1;
 85 
 86         sym.Pop();
 87         sym.Push(c);
 88         num.Push(temp);
 89     }
 90 }
 91 
 92 void processC(char c)
 93 {
 94     while(sym.Top()!='(')
 95     {
 96         if(sym.Top() == '+'||sym.Top() == '-'||sym.Top() == '*'||sym.Top() == '/')
 97         {
 98             if(sym.Top() == '+'||sym.Top() == '-')
 99             {
100                 type1 temp=0;//存储计算结果
101                 type1 temp1 = num.Top();
102                 num.Pop();
103                 type1 temp2 = num.Top();
104                 num.Pop();
105                 if(sym.Top()=='+')
106                     temp = temp1+temp2;
107                 else
108                     temp = temp2-temp1;
109 
110                 sym.Pop();
111                 //sym.Push(c);
112                 num.Push(temp);
113             }
114             else if(sym.Top() == '*'||sym.Top() == '/')
115             {
116                 type1 temp=0;//存储计算结果
117                 type1 temp1 = num.Top();
118                 num.Pop();
119                 type1 temp2 = num.Top();
120                 num.Pop();
121                 if(sym.Top()=='*')
122                     temp = temp1*temp2;
123                 else
124                     temp = temp2/temp1;
125 
126                 sym.Pop();
127                 //sym.Push(c);
128                 num.Push(temp);
129             }
130         }
131     }
132     sym.Pop();
133 }
134 void processD()
135 {
136     while(!sym.Empty())
137     {
138         if(sym.Top() == '+'||sym.Top() == '-'||sym.Top() == '*'||sym.Top() == '/')
139         {
140             if(sym.Top() == '+'||sym.Top() == '-')
141             {
142                 type1 temp=0;//存储计算结果
143                 type1 temp1 = num.Top();
144                 num.Pop();
145                 type1 temp2 = num.Top();
146                 num.Pop();
147                 if(sym.Top()=='+')
148                     temp = temp1+temp2;
149                 else
150                     temp = temp2-temp1;
151 
152                 sym.Pop();
153                 //sym.Push(c);
154                 num.Push(temp);
155             }
156             else if(sym.Top() == '*'||sym.Top() == '/')
157             {
158                 type1 temp=0;//存储计算结果
159                 type1 temp1 = num.Top();
160                 num.Pop();
161                 type1 temp2 = num.Top();
162                 num.Pop();
163                 if(sym.Top()=='*')
164                     temp = temp1*temp2;
165                 else
166                     temp = temp2/temp1;
167 
168                 sym.Pop();
169                 //sym.Push(c);
170                 num.Push(temp);
171             }
172         }
173     }
174 }
175 type1 process(char* calc)
176 {
177     int i;
178     int size;
179     char *temp;
180     temp = calc;
181     static int flag=0;//设置标志位..continue
182     type1 result;
183     int flag1=0;//设置标志位..处理负数
184 
185     size = calcSize(calc);
186     for(i = 0;i < size;i++)
187     {
188         if(isdigist(calc[i]))
189         {
190             flag1=0;
191             if(flag == 0)
192             {
193                type1 numTemp=atof(temp);
194                numTemp = type1(numTemp);
195                num.Push(numTemp);
196                flag = 1;
197             }
198             temp++;
199             continue;
200         }
201 
202         temp++;
203         flag = 0;
204         flag1++;
205         switch(calc[i])
206         {
207             case '(':
208                 sym.Push(calc[i]);
209                 break;
210             case '+':
211             case '-':
212                 if(flag1>1&&num.Top()=='(')
213                     num.Push(0);
214                 processA(calc[i]);
215                 break;
216             case '*':
217             case '/':
218                 processB(calc[i]);
219                 break;
220             case ')':
221                 processC(calc[i]);
222                 break;
223             default:
224                 if(calc[i]!='')
225                 {
226                     cout<<"输入有误"<<endl;
227                     exit(0);
228                 }
229                 processD();
230                 break;
231         }
232             
233     }
234     if(!num.Empty())
235     {
236         result = num.Top();
237         num.Pop();
238     }
239     else
240         result = 0;
241     return result;
242 }
243 
244 
245 int main(int argc,char* argv[])
246 {
247     char *a=NULL;
248     char *cals=NULL;
249 
250 
251     a = (char*)malloc(100*sizeof(char));
252     memset(a,0,sizeof(a));
253     cout<<"请输入表达式:(如(-2*(5+6))/2)注意括号英文"<<endl;
254     gets(a);
255     cals =a;
256     cout<<"结果:"<<endl;
257     cout <<a<<" = "<<process(cals)<<endl;
258 
259     free(a);
260 
261     system("PAUSE");
262     return 0;
263 }

执行如下

原文地址:https://www.cnblogs.com/lyf-sunicey/p/8134196.html