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

  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 - '' - 48);
 57             *p++;
 58         }--i; *p++;//跳过小数点
 59         j = i;
 60         m = *p - '' - 48;//确保转化成int后数值不变,*p指向第一位
 61         while (i--)
 62         {
 63             *p++;
 64             m = m * 10 + (*p - '' - 48);
 65         }
 66         return n + m*pow(0.1, j + 1);
 67     }
 68     else
 69     {
 70         while (i--)
 71         {
 72             n = n * 10 + (*p - '' - 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             while (op.top() != '(' && !op.empty())
112             {
113                 if (op.top() != '('&&op.top() != ')')
114                 {
115                     sb << op.top();
116                 }
117                 op.pop();
118             }
119             op.push(s[i]);
120 
121             break;
122         case ')':
123                  readnum();
124             while (op.top() != '(')
125             {
126                 sb << op.top();
127                 caucEveTime();
128                 op.pop();
129             }op.pop(); break;
130         case '*':
131         case'/':
132             readnum();
133             while (op.top() == '*' || op.top() == '/')
134             {
135                 sb << op.top();
136                 caucEveTime();
137                 op.pop();
138             }op.push(s[i]); break;
139         default:
140             sb << s[i];
141             ss << s[i];
142             break;
143         }
144     str = ss.str();
145     if (!str.empty())
146         num.push(trans(str.c_str()));
147 
148     while (!op.empty())
149     {
150         if (op.top() != '('&&op.top() != ')')
151         {
152             sb << op.top();
153             caucEveTime();
154         }
155         op.pop();
156     }
157 
158     strs = sb.str();
159     return num.top();
160 }
161 void main()
162 {
163     char ch[40];
164     char *p = ch;
165     cin >> p;
166     caculator a(p);
167     //a.antipoland();//两次重复调用改变数字栈中的数字!
168     //a.show();
169     //cout << endl;
170     cout << "=" << a.antipoland() << endl;
171     // cout << endl;
172     //a.shownum();
173     //a.showop();
174 }
原文地址:https://www.cnblogs.com/yuelien/p/5557437.html