ユーザー入力を受け取り、スタックを使用して優先順位に基づいて中置式を後置式に変換するプログラムを作成しています。オペランドは常に演算子の前に置かれます。たとえば、ユーザーが次のように入力した場合:
(a+b*c)
次に、プログラムは次のように表示されます。
abc*+
これまでのところ、私はこれを持っています:
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main()
{
    stack<char> s;
    char input;
    while (cin.get(input) && input != '\n')
        {
            if (isalnum(input))
                cout << input << "\n";
            else if (input == '(')
                s.push(input);
            else if (input == ')')
            {
        while (!s.empty() && s.top() != '(')
            {
            cout << s.top();
            s.pop();
        }
            if(!s.empty()) 
                    s.pop();
            else
                cout << "ERROR: No Matching ( \n";
        }
     else if (s.empty() && input == '*'||'/'||'+'||'-' && s.top() < input) // Error Begins Here?
     {
         char a = '*';
         char b = '/';
         char c = '+';
         char d = '-';
         bool prec (char a, char b, char c, char d);
             return ('*' > '/' > '+' > '-');
             s.push(input);
     }
         else if (input == '*'||'/'||'+'||'-' && s.top() >= input)
             while (!s.empty()) 
          {
              cout << s.top();
          s.pop();
                  s.push(input);
          }
        }
    while (!s.empty())
    {
        cout << s.top();
        s.pop();
    }
}
コンパイルして実行しますが、正常に機能していません。「ab」のような式を入力すると、本来「ab」と表示されるのですが、「a+b+c」と入力すると「a」しか表示されません。これは、後で表示するためにプログラムが演算子をスタックに配置していないことを意味します。私が助けを必要としているのは、プログラムを変更して、演算子が入力されたときにスタックに追加され、入力が完了したときにオペランドの後に優先順位 (*>/>+>-) に基づいて表示されるようにすることです。
私は一般的にC++とプログラミングにまったく慣れていないので、どんな提案でも素晴らしいでしょう.