リンクされたリストを使用してスタックを維持しながら、単純な操作 (+、-、、/) を行う RPN (後置表記) 電卓を作成する必要があります。大部分は完了しましたが、いくつかの問題が発生しています。1 つのオペランドで任意の 2 つの数値を計算できますが (例: 5 5 + = 10)、それ以上のことはできません。私はオンラインでいくつかの調査を行い、いくつかの YouTube ビデオを見て現在の状況を把握しましたが、ほとんどの場合、スタック参照を使用してそれを実行しています。独自のスタックを作成する方法とともに、チュートリアルを組み合わせようとしました。
私はこれにまったく慣れておらず、より大きな式(例:5 5 5 + + = 15)を計算する方法についてかなり迷っています。また、エラーをチェックする必要もあります。エラーはいくつか完了しましたが、苦労するのは「演算子が多すぎる」と「オペランドが多すぎる」です。演算子が多すぎると、そこに値がないために値をポップできないことに関係があると思いますが、それは私が得ることができる限りです(正しい場合、実装方法はまだよくわかりません)それ)。これらの 3 つのことのいずれか、またはここで確認できるその他のことについて、何かお役に立てば幸いです。
#include<iostream>
#include<string>
#include<sstream>
#include<iomanip>
using namespace std;
class SLLNode
{
double data;
SLLNode *top;
SLLNode *ptr;
public:
SLLNode()
{
top = NULL;
ptr = NULL;
}
void pushVal(double val)
{
SLLNode *next = new SLLNode;
next -> data = val;
next -> ptr = top;
top = next;
}
double popVal()
{
SLLNode *next = new SLLNode;
next = top;
top = top -> ptr;
next -> ptr = NULL;
return next -> data;
delete next;
}
void print()
{
SLLNode *next = new SLLNode;
next = top;
cout << "= " << next -> data << endl << ">>";
next = next -> ptr;
delete next;
}
};
bool isOperator(const string& input)
{
string ops[] = {"+", "-", "*", "/"};
for(int i = 0; i < 4; i++)
{
if(input == ops[i])
{
return true;
}
}
return false;
}
void performOp(const string& input, SLLNode& stack)
{
double fVal, sVal;
int result = 0;
sVal = stack.popVal();
fVal = stack.popVal();
if(input == "+")
{
stack.pushVal(fVal + sVal);
}
else if(input == "-")
{
stack.pushVal(fVal - sVal);
}
else if(input == "*")
{
stack.pushVal(fVal*+ sVal);
}
else if(input == "/" && sVal != 0)
{
stack.pushVal(fVal / sVal);
}
if(input == "/" && sVal == 0)
{
cout << "Error: Division by zero" << endl;
result = 1;
}
if(result == 0)
{
stack.print();
}
}
int main()
{
string input;
SLLNode stack;
cout << "::::::::::::::::RPN CALCULATOR:::::::::::::::::" << endl;
cout << "::TYPE IN A POSTFIX EXPRESSION OR 'q' TO QUIT::" << endl;
cout << ":::::::::::::::::::::::::::::::::::::::::::::::" << endl << endl;
cout << ">>";
while(true)
{
cin >> input;
double num;
if(istringstream(input) >> num)
{
stack.pushVal(num);
}
else if (isOperator(input))
{
performOp(input, stack);
}
else if (input == "q")
{
return 0;
}
else
{
cout << "Error: Invalid input" << endl;
}
}
}