Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

transform the expression

$
0
0
import java.io.*;
import java.util.Stack;

class test 
{


    public static void main(String[] args)throws IOException 
    {

        Stack op;

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        op = new Stack();
        int t = Integer.parseInt(br.readLine());
        while(t-->0)
        {
            try
            {
                char inp[] = br.readLine().toCharArray();

                for(int i=0;i<inp.length;i++)
                {
                    char c = inp[i];
                    if(c=='+' || c=='-' || c=='*' || c=='/' || c=='^')
                    {    
                        op.push(c);    
                    }
                    else if(c=='(')
                    {
                        op.push(c);    
                    }
                    else if(c==')')
                    {
                        while(op.lastElement()!='(')       //giving error in this line
                        {
                            System.out.print(op.pop());
                        }
                        if(op.capacity()>1)
                        {
                            op.pop();
                        }
                        else
                        {
                            op.clear();
                        }

                    }

                    else
                    {
                        System.out.print(c);
                    }
                }
            }
            catch(IOException e)
            {
                e.printStackTrace();;
            }
        }
    }
}

this code is running fine in my net beans but giving a compilation error for the usage

while(op.lastElement()!='(')

or while(op.peek()!='(')

plz help...

Viewing all articles
Browse latest Browse all 39796

Trending Articles


Practice Sheet of Right form of verbs for HSC Students


Sarah Samis, Emil Bove III


ZARIA CUMMINGS


Need radio code for IVECO Delphi Aptiv FJ5 RBT M16


Black Angus Grilled Artichokes


Ed Sheeran – Sapphire – Pre-Single [iTunes Plus M4A]


Sunny Garcia’s Ex-Wife Colleen McCullough


99 God Status for Whatsapp, Facebook


Funeral of Sir Warwick Franklin


MHDD



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>