2011-05-24 140 views
2
float Calculate(const string &query) 
{ 
     std::cout << "Query: " << query << "\n"; 
     unsigned int size = query.length(); 
     char stack[70]; 
     float res; 
     int m = 0; 

     for (int i = 0; i < size; i++) 
     { 
       if (query[i] >= '0' && query[i] <= '9') 
       { 
         stack[m] = query[i] - '0'; 
         m++; 
         continue; 
       } 

       switch (query[i]) 
       { 
         case '+': 
         { 
           res = stack[m - 2] + stack[m - 1]; 
           break; 
         } 
         case '-': 
         { 
           res = stack[m - 2] - stack[m - 1]; 
           break; 
         } 
         case '*': 
         { 
           res = stack[m - 2] * stack[m - 1]; 
           break; 
         } 
         case '/': 
         { 
           res = stack[m - 2]/stack[m - 1]; 
           break; 
         } 
       } 

        stack[m - 2] = res; 
       m--; 
       cout << "RES: " << res << "\n"; 
     } 

     return res; 
} 

它计算逆波兰表示法。奇怪的函数返回结果

当我打电话给:Calculate("11+")时,它会返回正确的结果:2

但是,当我通过获取RPN串的后一个变量:

string inputStr; 
string outputStr; 

cout << "Put exercise\n"; 
getline(std::cin, inputStr); 

outputStr = GetRPN(inputStr); 
cout << "Output str :" << outputStr << ":\n"; 

float res = Calculate(outputStr); 
std::cout << res << "\n"; 

所以,当我输入字符串:1+1,功能GetRPN回报11+,我看到,在第二COUT。但结果是0

它可能是什么?


string GetRPN(string input) 
{ 
    vector <char> operation; 
    string outputStr;  //output string, keep RPN 
    int stack_count = 0; 

    for(int i = 0; i < input.length(); i++) 
    { 
     if(input[i] >= '0' && input[i] <= '9') 
     { 
      outputStr += input[i]; 
     } 
     else 
     { 
      if(operation.empty()) 
      { 
       operation.push_back(input[i]); 
       stack_count++; 
      } 
      else if(operation[stack_count - 1] == '+' || operation[stack_count - 1] == '-') 
      { 
       operation.push_back(input[i]); 
       stack_count++; 
      } 
      else if ((operation[stack_count - 1] == '*' || operation[stack_count - 1] == '/') && (input[i] == '*' || input[i] == '/')) 
      { 
       outputStr += operation[stack_count - 1]; // move mark of operation to output str 
       operation.pop_back(); // delet last element from vector 
       operation.push_back(input[i]);// plus new operation mark to vector 
       stack_count++; 
      } 
      else if (operation[stack_count - 1] == '*' || operation[stack_count - 1] == '/') 
      { 
       outputStr += input[i]; 
      } 
     } 
    } 

    for(int i = operation.size(); i >= 0; i--) 
    { 
     outputStr += operation[i]; // move all operation marks to otput str 
    } 

    return outputStr; 
} 
+2

确保您没有任何尾随空格或GetRPN返回的其他“垃圾”空格字符 - 您可能会打印出长度。 – holtavolt 2011-05-24 16:03:37

+0

不知道'GetRPN'是什么,但它可能在字符串outputStr中留下最后的'\ n'或'\ r',这在'switch(query [i])'中不能匹配,只剩下'res'某种程度上为空... – pascal 2011-05-24 16:05:02

+0

你的'Calculate()'看起来不错,并且给定相同的输入应该输出相同的结果。你可以发布你的'GetRPN()'函数吗? – uesp 2011-05-24 16:07:13

回答

1

你的周期这里

for(int i = operation.size(); i >= 0; i--) 
{ 
    outputStr += operation[i]; // move all operation marks to otput str 
} 

没有任何意义。显然你试图访问无效索引处的向量。当i等于operation.size()时,访问operation[i]处的元素是非法的。该指数超出范围。

任何自我尊重的实现都会立即用这个断言报告这个问题。无论如何,正如我在评论中所说的,类似的问题可以通过调试代码来解决。你为什么要求其他人调试你的代码而不是自己做?

1

如果字符串中有任何空格或不可打印的字符,你最终会存储到stack具有负折射率,这将覆盖其他的东西在你的栈帧,并可能导致发生什么事。

你应该添加一些错误检查Calculate - 开关应该有一个default,打印一个有意义的错误消息,并且您访问stack[m]stack[m-2]以确保栈不会发生下溢前,应检查m值或溢出(如果是的话,你应该打印一个明显的错误)。你应该能够传递任意一个随机字符串来计算并让它告诉你为什么它不是一个有效的RPN表达式。