2011-05-24 17 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; 
} 

逆ポーランド記法を計算します。strange function return result

私はCalculate("11+")のようなものを呼び出すと、正しい結果:2を返します。

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"; 

だから、ときI入力文字列:1+111+を返し、私は2番目COUTでいることがわかりGetRPN機能を私はRPN文字列を取得した後、変数を渡す

しかし、。しかし結果は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

+0

'GetRPN'とは何か分かりませんが、最後の' \ n'や '\ r'を文字列outputStrに残すことができます。これは' switch(query [i]) 'どういうわけかnull ... – pascal

+0

あなたの 'Calculate()'はうまく見え、同じ入力を与えても同じ結果が出力されるはずです。あなたは 'GetRPN()'関数も投稿できますか? – uesp

答えて

1

あなたのサイクルは意味をなさない。あなたは明らかに無効なインデックスでベクトルにアクセスしようとしています。 ioperation.size()の場合、operation[i]の要素にアクセスすることは不正です。インデックスが範囲外です。

自己尊重の実装は、この問題をアサーションとともに即座に報告します。いずれにせよ、私がコメントで述べたように、そのような問題はコードをデバッグすることで解決されます。なぜあなたはコードを自分で実行するのではなく、デバッグするように他の人に依頼していますか?

1

あなたの文字列がその中に任意の空白または印刷できない文字がある場合は、あなたのスタックフレーム内の他のものを上書きしますし、何が起こることを引き起こす可能性がある負のインデックス、とstackに格納してしまいます。

あなたはCalculateにチェックいくつかのエラーを追加する必要があります - スイッチが賢明なエラーメッセージを出力defaultを持つべきである、とあなたはスタックがアンダーフローしないことを確認するstack[m]stack[m-2]にアクセスする前にmの値をチェックする必要がありますかオーバーフローすることがあります(そうであれば合理的なエラーを出力する必要があります)。あなたは任意の文字列をCalculateに渡すことができ、それが有効なRPN式ではない理由を伝える必要があります。ここ

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