rewrite this code in c++ ---------------------------------------------- //Includ
ID: 3802712 • Letter: R
Question
rewrite this code in c++
----------------------------------------------
//Include library
import java.util.Stack;
import java.util.Scanner;
//Define a class ReversePolishCalculator
public class ReversePolishCalculator
{
//Define boolean method nextIsOperator()
public boolean nextIsOperator(String next)
{
//Return true if expression contains operators
return (next.equals("+") || next.equals("-") || next.equals("*") || next.equals("/"));
}
//Define calculate()
public Integer calculate(String input)
{
//Call trim()
input = input.trim();
//Define next
String next;
//Define a stack of integers
Stack<Integer> valueStack = new Stack<Integer>();
//Define a scanner variable
Scanner lscan = new Scanner(input);
//Loop until end of input
while (lscan.hasNext())
{
//Store value to next
next = lscan.next();
//If operator is present
if (nextIsOperator(next))
{
//If value is greater than 1
if (valueStack.size() > 1)
{
//If operator is +
if (next.equals("+"))
{
//Perform operation
valueStack.push((Integer) valueStack.pop()
+ (Integer) valueStack.pop());
}
//If operator is -
else if (next.equals("-"))
{
//Perform operation
valueStack.push(-(Integer) valueStack.pop()
+ (Integer) valueStack.pop());
}
//If operator is *
else if (next.equals("*"))
{
//Perform operation
valueStack.push((Integer) valueStack.pop()* (Integer) valueStack.pop());
}
//If operator is /
else if (next.equals("/"))
{
//Get operands
Integer lfirst = valueStack.pop();
Integer lsecond = valueStack.pop();
//If denominator is 0
if (lfirst == 0)
{
//Display message
System.out.println("Error!! Divide by zero!!.");
}
//If denominator is non zero
else
{
//Perform operation
valueStack.push(lsecond / lfirst);
}
}
}
//If format is not proper
else
{
//Display message
System.out.println("Format error !!");
}
}
//If error in format
else
{
//Try block
try
{
//Push value to stack
valueStack.push(Integer.parseInt(next));
}
//Catch block
catch (NumberFormatException c)
{
//Display message
System.out.println("This was not properly formatted");
}
}
}
//If value greater than 1
if (valueStack.size() > 1)
{
//Display message
System.out.println("Format error!!");
//Return 0
return 0;
}
//Return value
return valueStack.pop();
}
//Define main method
public static void main(String[] args)
{
//Define scanner variable
Scanner lkeyboard = new Scanner(System.in);
//Define lrpCal
ReversePolishCalculator lrpCal = new ReversePolishCalculator();
//Set value as false
boolean lquit = false;
//Loop until false
while (lquit == false)
{
//Display message
System.out.println("Enter RPN expression or "quit" to quit");
//Receive user input and store value
String input = lkeyboard.nextLine();
//If input is lquit
if (input.equalsIgnoreCase("quit"))
{
//Display message
System.out.println("Goodbye");
//Break
break;
}
//Try block
try
{
//Display result
System.out.println(lrpCal.calculate(input));
}
//Catch block
catch (Exception e)
{
}
}
}
}
Explanation / Answer
#include<conio.h>
#include <iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include <string.h>
#include <cstdlib.h>
#include <cstring.h>
#include <cmath.h>
#include <cctype.h>
using namespace std;
struct node
{
int data;
node *next;
};
void showstack(node *stack);
void push(int value, node *&stack);
int pop(node *&stack);
int value = 40;
int s1, s2, s3, s4;
int num;
string exit_prog = "e";
string user_input;
int main()
{
cout << "Enter an input "<<endl;
cin >> user_input;
while(cin >> user_input)
{
for(int k = 0; k < user_input.length(); k++)
{
if(isdigit(user_input[k]))
{
user_input.c_str();
num = atoi(user_input[k]);
push(num, *&stack);
}
else if(user_input[i] == "+") //if it is this character add by popping s1 and s2
{
s1 = pop(node *&stack);
s2 = pop(node *&stack);
push((s1 + s2), *&stack);
}
else if(user_input[i] == "-")
{
s1 = pop(node *&stack);
s2 = pop(node *&stack);
push((s1 - s2), *&stack);
}
else if(user_input[k] == "*")
{
s1 = pop(node *&stack);
s2 = pop(node *&stack);
push(((s1) * (s2)), *&stack);
}
else if(user_input[i] == "/") //divides
{
s1 = pop(node *&stack);
s2 = pop(node *&stack);
push((s1 / s2), *&stack);
}
else if(user_input[i] == "u")
{
s1 = pop(node *&stack);
s1 = s1 * -1;
push(s1, *&stack);
}
else if(user_input[i] == "p")
{
s1 = pop(node *&stack);
cout << s1 << endl;
}
}
}
return 0;
}
void show_stack (node *stack)
{
cout << "TOP> ";
for ( ; stack != NULL; stack = stack->next)
{
cout << stack->data << ' ';
}
cout << endl;
}
void push (int val, node *&stack)
{
node *p = new node;
p->data = val;
p->next = stack;
stack = p;
}
int pop (node *&stack)
{
node *t = stack;
int val = stack->data;
stack = stack->next;
delete t;
return val;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.