zoukankan      html  css  js  c++  java
  • 求表达式的值

    晚上赶快改了下上午写的代码。。。。只是加了一个小数点的的处理。。。。

    没对负数进行处理了。。。

    汗。。。。我更加无语了。。。。

    看来以后做题的时候要多想想才去写。。。要不然到时候又得改动。。。。还好不要改得太多了。

    要是大项目。。。那有点改了。。。

    吸取教训吧。。。。。。/

    贴下改了小数点的部分代码

    转换成后缀表达式那的代码

    else if(::isdigit(*tChar))
      {
       while((*tChar >= '0') && (*tChar <= '9'))
       {
        tSave[i] = *tChar;
        ++i;
        ++tChar;
       }
       if(*tChar == '.')
       {
        tSave[i] = *tChar;
        ++i;
        ++tChar;
        while((*tChar >= '0') && (*tChar <= '9'))
        {
         tSave[i] = *tChar;
         ++i;
         ++tChar;
        }
       // --tChar;
       }
       --tChar;
       tSave[i] = ' ';
       ++i;

      }

    //由后缀表达式得到的结果

    int GetResult(char *tPtr,double  *tResult)
    {
     Stack<double> s;
     char *tChar = tPtr;
     double  tInt;
     double  tDec;
     double  one, two;
     while(*tChar != '#')
     {
      if(*tChar == ' ')
      {
       ++tChar;
       continue;
      }
      else if(isdigit(*tChar))
      {
       tInt = 0.0;
       tDec = 0.1;
       while((*tChar >= '0') && (*tChar <= '9'))
       {
        tInt = tInt*10 + *tChar - '0';
        ++tChar;
       }
       if(*tChar == '.')
       {
        ++tChar;
        while((*tChar >= '0') && (*tChar <= '9'))
        {
         tInt += tDec*(*tChar - '0');
         tDec = tDec*0.1;
         ++tChar;
        }
       }
       --tChar;
       s.Push(tInt);
      }
      else if((*tChar == '+') || (*tChar == '-') || (*tChar == '*') || (*tChar == '/'))
      {
       if(s.IsEmpty())
        s.Push(*tChar);
       else
       {
        one = s.Top();
        s.Pop();
        two = s.Top();
        s.Pop();
        switch(*tChar)
        {
        case '+':s.Push(two+one);
         break;
        case '-':s.Push(two-one);
         break;
        case '*':s.Push(two*one);
         break;
        case '/':s.Push(two/one);
         break;
        default:
         return -1;
        }

       }
      }
      else
       return -1;
      ++tChar;
     }
     //结果
     *tResult = s.Top();
     s.Pop();
     return 1;
    }

    main函数中调用与前面一样。。。不过这次用到了链栈。。。

  • 相关阅读:
    NPM 与 left-pad 事件:我们是不是早已忘记该如何好好地编程?
    Groovy split竖杆注意
    使用Flask-Migrate进行管理数据库升级
    Fabric自动部署太方便了
    程序员的复仇:11行代码如何让Node.js社区鸡飞狗跳
    grails 私有库相关设置
    A successful Git branching model
    String to Date 多种格式转换
    C#搭建CEF(CEFGLUE) 环境。
    使用Win PE修改其他硬盘中的系统注册表
  • 原文地址:https://www.cnblogs.com/ccmfc/p/1723293.html
Copyright © 2011-2022 走看看