求表达式的值

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

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

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

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

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

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

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

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

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函数中调用与前面一样。。。不过这次用到了链栈。。。

原文地址:https://www.cnblogs.com/ccmfc/p/1723293.html