码迷,mamicode.com
首页 > 其他好文 > 详细

【升级版】支持浮点型+-*/()运算的计算器

时间:2016-06-03 21:21:33      阅读:515      评论:0      收藏:0      [点我收藏+]

标签:

  1 #include <iostream>
  2 #include<sstream>
  3 using namespace std;
  4 template<typename T>
  5 class stack
  6 {
  7     T p[40];
  8     int toop;
  9 public:
 10     stack() { toop = -1; }
 11     void push(T t) { toop++; p[toop] = t; }
 12     T top() { return p[toop]; }
 13     bool empty() { if (toop == -1)return true; return false; }
 14     void pop() { toop--; }
 15 };
 16 class caculator
 17 {
 18     string s;//原波兰式的容器
 19     stack<char>op;
 20     stack<float>num;
 21     stringstream ss;//用于转换的流
 22     stringstream sb;//插入逆波兰式的流
 23     string str;//存放数字的容器,每次更新
 24     string strs;//存放逆波兰式的容器
 25     float x, y;
 26 public:
 27     caculator(char *p) { s = p; }
 28     float trans(const char *p);
 29     float antipoland();
 30     void show() { cout << strs; }
 31     void readnum();
 32     void caucEveTime();
 33     void shownum() { while (!num.empty()) { cout << num.top() << endl; num.pop(); } }
 34     void showop() { while (!op.empty()) { cout << op.top() << endl; op.pop(); } }
 35     bool checkpoint(const char *p);
 36 };
 37 bool caculator::checkpoint(const char *p)
 38 {
 39     int i = strlen(p);
 40     while (i--)
 41     {
 42         if (*p == .)
 43             return true;
 44         *p++;
 45     }
 46     return false;
 47 }
 48 float caculator::trans(const char *p)//底层const,对象为常量
 49 {
 50     float n = 0; float m = 0;
 51     int i = strlen(p); int j;//记录小数点后有几位
 52     if (checkpoint(p))
 53     {
 54         while (--i && (*p != .))
 55         {
 56             n = n * 10 + (*p - \0 - 48);
 57             *p++;
 58         }--i; *p++;//跳过小数点
 59         j = i;
 60         m = *p - \0 - 48;//确保转化成int后数值不变,*p指向第一位
 61         while (i--)
 62         {
 63             *p++;
 64             m = m * 10 + (*p - \0 - 48);
 65         }
 66         return n + m*pow(0.1, j + 1);
 67     }
 68     else
 69     {
 70         while (i--)
 71         {
 72             n = n * 10 + (*p - \0 - 48);
 73             *p++;
 74         }
 75         return n;
 76     }
 77 }
 78 void  caculator::readnum()
 79 {
 80     str = ss.str();
 81     if (!str.empty())//str中存放数字串
 82     {
 83         ss.str("");//清空流
 84         num.push(trans(str.c_str()));
 85     }
 86 }
 87 void caculator::caucEveTime()//由符号栈弹出符号决定调用
 88 {
 89     y = num.top();
 90     num.pop();
 91     x = num.top();
 92     num.pop();
 93     switch (op.top())
 94     {
 95     case+:num.push(x + y); break;
 96     case-:num.push(x - y); break;
 97     case*:num.push(x*y); break;
 98     case/:num.push(x / y); break;
 99     default:break;
100     }
101 }
102 float caculator::antipoland()
103 {
104     for (int i = 0; i < s.size(); i++)
105         switch (s[i])
106         {
107         case (:op.push(s[i]); readnum(); break;
108         case +:
109         case -:
110             readnum();
111             if (op.top() == ()
112                 op.push(s[i]);
113             else if (op.empty())
114                 op.push(s[i]);
115             else
116             {
117                 while (!op.empty())
118                 {
119                     if (op.top() != (&&op.top() != ))
120                     {
121                         sb << op.top();
122                         caucEveTime();
123                     }
124                     op.pop();
125                 }
126                 op.push(s[i]);
127             }
128             break;
129         case ):
130             readnum();
131             while (op.top() != ()
132             {
133                 sb << op.top();
134                 caucEveTime();
135                 op.pop();
136             }op.pop(); break;
137         case *:
138         case/:
139             readnum();
140             while (op.top() == * || op.top() == /)
141             {
142                 sb << op.top();
143                 caucEveTime();
144                 op.pop();
145             }op.push(s[i]); break;
146         default:
147             sb << s[i];
148             ss << s[i];
149             break;
150         }
151     str = ss.str();
152     num.push(trans(str.c_str()));
153     while (!op.empty())
154     {
155         if (op.top() != (&&op.top() != ))
156         {
157             sb << op.top();
158             caucEveTime();
159         }
160         op.pop();
161     }
162 
163     strs = sb.str();
164     return num.top();
165 }
166 void main()
167 {
168     char ch[40];
169     char *p = ch;
170     cin >> p;
171     caculator a(p);
172     //a.antipoland();//两次重复调用改变数字栈中的数字!
173     // a.show();
174     cout << "=" << a.antipoland() << endl;
175     // cout << endl;
176     //a.shownum();
177     //a.showop();
178 }

 

【升级版】支持浮点型+-*/()运算的计算器

标签:

原文地址:http://www.cnblogs.com/yuelien/p/5557437.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!