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++]!='\0');
 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]!='\0')
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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解决这个问题。你可以使用Dev-C++集成开发环境中的图形用户界面库来创建一个简单的计算器程序。以下是一个简单的示例代码,可以实现基本的数学计算和括号运算: ```c++ #include <graphics.h> #include <conio.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <math.h> #define MaxSize 100 int stack[MaxSize]; int top = -1; // 定义计算器按键的位置和尺寸 int button_left = 50; int button_top = 100; int button_width = 50; int button_height = 50; void push(int x) { if (top == MaxSize - 1) { printf("Stack is full!\n"); } else { top++; stack[top] = x; } } int pop() { if (top == -1) { printf("Stack is empty!\n"); return -1; } else { int x = stack[top]; top--; return x; } } void draw_button(char* text, int x, int y) { setcolor(WHITE); rectangle(x, y, x + button_width, y + button_height); outtextxy(x + 10, y + 10, text); } void draw_digit_buttons() { char text[2]; int x = button_left; int y = button_top; for (int i = 0; i <= 9; i++) { text[0] = '0' + i; text[1] = '\0'; draw_button(text, x, y); x += button_width; if ((i + 1) % 3 == 0) { x = button_left; y += button_height; } } } void draw_operator_buttons() { draw_button("+", button_left + button_width * 3, button_top); draw_button("-", button_left + button_width * 3, button_top + button_height); draw_button("*", button_left + button_width * 3, button_top + button_height * 2); draw_button("/", button_left + button_width * 3, button_top + button_height * 3); draw_button("(", button_left + button_width * 4, button_top); draw_button(")", button_left + button_width * 4, button_top + button_height); } void draw_equal_button() { draw_button("=", button_left + button_width * 2, button_top + button_height * 4); } void draw_clear_button() { draw_button("C", button_left + button_width * 3, button_top + button_height * 4); } void draw_all_buttons() { draw_digit_buttons(); draw_operator_buttons(); draw_equal_button(); draw_clear_button(); } void evaluate_expression(char* expression) { int len = strlen(expression); int i = 0; while (i < len) { if (isdigit(expression[i])) { int num = 0; while (i < len && isdigit(expression[i])) { num = num * 10 + (expression[i] - '0'); i++; } push(num); } else if (expression[i] == '+' || expression[i] == '-' || expression[i] == '*' || expression[i] == '/') { int b = pop(); int a = pop(); int result = 0; switch (expression[i]) { case '+': result = a + b; break; case '-': result = a - b; break; case '*': result = a * b; break; case '/': result = a / b; break; } push(result); i++; } else if (expression[i] == '(') { push(-1); i++; } else if (expression[i] == ')') { int result = 0; while (stack[top] != -1) { result += pop(); } pop(); push(result); i++; } else { i++; } } printf("%s = %d\n", expression, pop()); } int main() { int gd = DETECT, gm; initgraph(&gd, &gm, ""); draw_all_buttons(); char expression[MaxSize]; int pos = 0; while (true) { if (kbhit()) { char ch = getch(); if (ch == 'C' || ch == 'c') { // 清空输入 pos = 0; setcolor(BLACK); rectangle(button_left, button_top - 20, button_left + button_width * 5, button_top - 5); continue; } else if (ch == '=') { // 计算表达式 expression[pos] = '\0'; evaluate_expression(expression); pos = 0; setcolor(BLACK); rectangle(button_left, button_top - 20, button_left + button_width * 5, button_top - 5); continue; } // 显示输入的字符 setcolor(WHITE); outtextxy(button_left + pos * button_width, button_top - 20, &ch); // 添加到表达式中 expression[pos++] = ch; } } closegraph(); return 0; } ``` 这个程序使用了 Dev-C++ 集成开发环境中的图形用户界面库,通过鼠标点击计算器按钮来输入数字和运算符,并且支持括号运算。在这个程序中,我们使用一个栈来计算表达式的值。你可以根据自己的需求来修改这个程序,并添加更多的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值