Conversion of program from JAVA to C++
Here's a program I have written in JAVA and I need to convert it to C++.
This program converts a prefix expression to postfix expression using
STACK. All I need is the equivalent of this program in c++ with same
logic.
package ExpressionConvert;
import java.util.Scanner;
class STACK{
//Private Declarations
private int top, MAX;
private String a[] = new String [1000];
//Public Declarations
public STACK(){
top = -1;
MAX = 1000;
a[0] = "";
}
public void push(String x){
if (top <= MAX-1){
a[++top] = x;
}
else{
System.out.println("Stack overflow");
}
};
public String pop(){
if (top == -1){
System.out.println("\nStack empty!");
return "\n";
}
else{
return a[top--];
}
};
public int getTop(){
return top;
};
}
public class Prefix2Postfix_revised_stack {
static boolean isOperator (char ch){
switch (ch){
case '+':
case '-':
case '*':
case '/':
case '$':
return true;
default :
return false;
}
}
public static void main(String[] args) {
//declarations
Scanner in = new Scanner (System.in);
String exp;
int i, j=-1;
STACK s = new STACK ();
String temp[] = new String[100];
String postfix_exp = "\n";
//input
System.out.println("Enter prefix expression (No spaces or
brackets) : ");
exp = in.next();
//converting string exp into stack of characters converted to strings
for (i=0; i<exp.length(); i++){
s.push(Character.toString(exp.charAt(i)));
}
//Using stack to remove convert and re-insert strings
while(s.getTop() >= 0){
temp[++j] = s.pop();
if (isOperator(temp[j].charAt(0))){
temp[j] = temp[j-1].concat(temp[j-2].concat(temp[j]));
postfix_exp = temp[j];
temp[j-2]=temp[j];
j = j-2;
for(int x=j; j>0; j--)
s.push(temp[x]);
}
}
//Output
System.out.println("After converting to postfix : " + postfix_exp);
in.close();
}
}
Here's what I tried to create but it does not work.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class STACK{
private:
int top, MAX;
char a[100][100] ;
//Public Declarations
public:
STACK(){
top = -1;
MAX = 100;
a[0][0] = ' ';
}
void push(char x[]){
if (top <= MAX-1){
strcpy(a[top++],x);
}
else{
cout << "Stack overflow";
}
};
public char* pop(){
if (top == -1){
cout << "\nStack empty!";
return "\n";
}
else{
return a[top--][];
}
};
public int getTop(){
return top;
};
}
int isOperator (char ch){
switch (ch){
case '+':
case '-':
case '*':
case '/':
case '$':
return 1;
default :
return 0;
}
}
int main() {
//declarations
char exp[100];
int i, j=-1;
STACK s;
char temp[100][100];
char postfix_exp[100] = "\n";
//input
cout << "Enter prefix expression (No spaces or brackets) : ";
cin.getline(exp,100);
//converting string exp into stack of characters converted to strings
for (i=0; i<strlen(exp); i++){
s.push(&exp[i]);
}
//Using stack to remove convert and re-insert strings
while(s.getTop() >= 0){
strcpy(temp[++j],s.pop());
if (isOperator(temp[j].charAt(0))){
strcat (temp[j][],strcat(temp[j-2][].concat(temp[j]));
strcpy(postfix_exp,temp[j]);
strcpy(temp[j-2],temp[j]);
j = j-2;
for(int x=j; j>0; j--)
s.push(temp[x]);
}
}
//Output
cout << "After converting to postfix : " << postfix_exp;
return 0;
}
Please help.
No comments:
Post a Comment