zoukankan      html  css  js  c++  java
  • 支持+-*/()int 型数据的计算机c++实现

      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 };
     36 float caculator::trans(const char *p)//底层const,对象为常量
     37 {
     38     float n;
     39     n = *p - '' - 48;//确保转化成int后数值不变
     40     int i = strlen(p);
     41     while (--i)
     42     {
     43         *p++;
     44         n = n * 10 + (*p - '' - 48);
     45     }
     46     return n;
     47 }
     48 void  caculator::readnum()
     49 {
     50     str = ss.str();
     51     if (!str.empty())//str中存放数字串
     52     {
     53         ss.str("");//清空流
     54         num.push(trans(str.c_str()));
     55     }
     56 }
     57 void caculator::caucEveTime()//由符号栈弹出符号决定调用
     58 {
     59     y = num.top();
     60     num.pop();
     61     x = num.top();
     62     num.pop();
     63     switch (op.top())
     64     {
     65     case'+':num.push(x + y); break;
     66     case'-':num.push(x - y); break;
     67     case'*':num.push(x*y); break;
     68     case'/':num.push(x / y); break;
     69     default:break;
     70     }
     71 }
     72 float caculator::antipoland()
     73 {
     74     for (int i = 0; i < s.size(); i++)
     75     switch (s[i])
     76     {
     77     case '(':op.push(s[i]);readnum(); break;
     78     case '+':
     79     case '-':
     80         readnum();
     81     if (op.top() == '(')
     82     op.push(s[i]);
     83     else if(op.empty())
     84         op.push(s[i]);
     85     else
     86     {
     87     while (!op.empty())
     88     {
     89         if (op.top() != '('&&op.top() != ')')
     90         {
     91             sb << op.top();
     92             caucEveTime();
     93         }
     94         op.pop();
     95     }
     96     op.push(s[i]);
     97     }
     98     break;
     99     case ')':
    100         readnum();
    101     while (op.top() != '(')
    102     {
    103        sb << op.top();
    104        caucEveTime();
    105        op.pop();
    106     }op.pop(); break;
    107     case '*':
    108     case'/':
    109         readnum();
    110     while (op.top() == '*' || op.top() == '/')
    111     {
    112        sb << op.top();
    113        caucEveTime();
    114        op.pop();
    115     }op.push(s[i]); break;
    116     default:
    117     sb << s[i];
    118     ss <<s[i];
    119     break;
    120     }
    121     str = ss.str();
    122     num.push(trans(str.c_str()));
    123     while (!op.empty())
    124     {
    125         if (op.top() != '('&&op.top() != ')')
    126         {
    127             sb<< op.top();
    128             caucEveTime();
    129         }
    130          op.pop();
    131     }    
    132     
    133     strs = sb.str();
    134     return num.top();
    135 }
    136 void main()
    137 {
    138     char ch[40];
    139     char *p=ch;
    140     cin >> p;
    141     caculator a(p);
    142     //a.antipoland();//两次重复调用改变数字栈中的数字!
    143     // a.show();
    144      cout <<"="<<a.antipoland()<<endl;
    145     // cout << endl;
    146     //a.shownum();
    147     //a.showop();
    148 } 
  • 相关阅读:
    JavascriptCore中扩展自定义函数
    Qt for Symbian应用的开发转载
    Qt Symbian 开发环境安装转载
    qt屏幕抓图
    为JavascriptCore添加自定义对象
    Tesseract OCR开源项目转载
    Joomla, Wordpress, Drupal 全面详细Pk比较转载
    Drupal,Joomla,Wordpress在内置应用功能方面的区别和比较转载
    Docker:第五章:基于centos7的docker安装配置部署教程以及基于docker Tomcat镜像使用的项目部署教程
    【JavaP6大纲】Redis篇:redis集群工作原理,协议,分布式寻址算法
  • 原文地址:https://www.cnblogs.com/yuelien/p/5557286.html
Copyright © 2011-2022 走看看