我在計算RPN計算器時遇到了困難,我們必須使用鏈接列表來執行此任務。 (我已經完成了使用stack<double>
方法分配,但它使用鏈表是使用鏈接列表的RPN
我有一些麻煩的錯誤檢查:
太多的運營商(+ -/*) 太多。操作數(雙精度) 除零除
程序應該繼續執行和計算表達式,直到用戶在一行中輸入一個零(0),然後是一個新行,我也遇到了一些麻煩這是因爲我把錯誤檢查放在「太多操作數」中,它不會允許我做第二個計算,OU TPUT將「操作數過多」
我似乎無法得到一個錯誤檢查爲「太多的運營商」
我嘗試了好幾種不同的東西需要的錯誤檢查,看着許多其他工作RPN問題在不同的網站上,你可以看到我嘗試和評論過的一些東西。 任何幫助將不勝感激! 謝謝
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
struct NODE
{
float num;
NODE *next;
};
class stack
{
private:
NODE *head;
public:
stack();
void push(float);
float pop();
int nElements();
float display();
};
class RPN: public stack
{
public:
void add();
void subtract();
void multiply();
void divide();
};
stack::stack()
{
head = NULL;
}
void stack::push(float a_number)
{
NODE *temp = new NODE;
if (temp)
{
temp->num = a_number;
temp->next = head;
head = temp;
}
}
float stack::pop()
{
float number = 0;
if (!head)
{
return 0;
}
else
{
NODE *temp = head;
number = head->num;
head = temp->next;
delete temp;
}
return number;
}
int stack::nElements()
{
int counter=0;
for (NODE *node = head; node; node=node->next)
{
counter++;
}
return counter;
}
float stack::display()
{
//ERROR CHECKING TOO MANY OPERANDS??? PRINTING TOO MANY OPERANDS FOR : 100 10 50 25/* - -2/= , but still giving correct output && WILL NOT ALLOW ME TO DO A SECOND CALCULATAION , OUTPUT WILL BE TOO MANY OPERANDS
if(nElements() !=1)
{
cout << "Error: too many operands" << endl;
return 1;
}
/*
// if(nElements() < 2)
{
cout << "Error: too many operators" << endl;
return 1;
}
*/
else //(nElements() > 0)
{
float temp = pop();
cout << temp << endl;
push(temp);
return temp;
}
}
void RPN::add()
{
if (nElements()>=2)
{
push(pop() + pop());
}
}
void RPN::subtract()
{
if (nElements()>=2)
{
push(0 - pop() + pop());
}
}
void RPN::multiply()
{
if (nElements()>=2)
{
push(pop() * pop());
}
}
int RPN::divide()
{
double op2;
op2 = pop();
if(op2 != 0.0)
{
push(pop()/op2);
}
else
{
cout << "Error: Division by zero.\n"; //??? Still printing output
return -1;
}
}
//Function prototype for isOperator
bool isOperator(const string& input);
//Function prototype for perforOperation
int performOperation(const string& input, RPN& calculator);
Int main(){
RPN calculator;
string input;
float num;
cout << "RPN Calculator: " << endl;
cout << "Input\n";
while(input != "0")
{
//Terminate program when 0 is entered by user
while(true)
{
// get input
cin >> input;
// check for being numeric value
if(istringstream(input) >> num)
{
//use push function
calculator.push(num);
}
// check for operator
else if(isOperator(input))
{
performOperation(input, calculator);
}
// If user enters 0 on a line followed by a new line, the program exits ????????????
else if(input == "0\n")
{
return -1;
}
}
}
}
bool isOperator(const string& input)
{
static const string operators ="-+*/";
if (input.length() == 1) // right size to be an operator.
{
return operators.find_first_of(input[0]) != string::npos;
// look in the operator string for the first (and only) character in input
}
return false;
}
int performOperation(const string& input, RPN& calculator)
{
//double firstOperand = 0;
//double secondOperand = 0;
//double result;
/*
if(calculator.size() > 2) //Error check gives a false error for last input ???
{
cout << "Error: too many operands" << endl;
return 1;
}
//Error chceck for too many operators ////STILL PRINT OUTPUT???
if(calculator.size() < 2)
{
cout << "Error: too many operators" << endl;
return 1;
}
*/
switch (input[0])
{
case '+': calculator.add();
calculator.display();
break;
case '-': calculator.subtract();
calculator.display();
break;
case '*': calculator.multiply();
calculator.display();
break;
case '/': calculator.divide();
//if (firstOperand/0)
//{
// cout << "Error: Divide by 0.\n";
//}
calculator.display();
break;
}
/*
if (secondOperand == 0)
{ // moved this test to here because it's the only place it matters.
cout << "Error: Division by 0.\n";
return -1;
}
*/
return 0;
}
This is the sample input and output
Input Output
10 15 + = 25
10 15 - = -5
2.5 3.5 + = 6 (or 6.0)
10 0/= Error: Division by zero
10 20 */= Error: Too many operators
12 20 30/= Error: Too many operands
-10 -30 - = 20
100 10 50 25/* - -2/= -40
I got the error division by 0 check to work, It just keeps printing the output in addition to the error...How could I fix this?
int RPN::divide()
{
double op2;
op2 = pop();
if(op2 != 0.0)
{
push(pop()/op2);
}
else
{
cout << "Error: Division by zero.\n";
return -1;
}
}
我不確定它是否解決了這個問題,但是嘗試在循環中移動'RPN計算器;'。我認爲最後一次迭代的結果正在絆倒它。 (另外,'>>'跳過空格,所以你將永遠不會讀'0 \ n「')。 – molbdnilo