mara7a ,
ana fe 3ende assignment bel data structurebe lo3;et java o 7let bs ma be3mel run bede 7ada ysa3edn plz

هي السؤال :
Assignment one
Write a full program in java using stack implementation to convert an infix expression to postfix ; your program should evaluate the postfix expression .
The following display the steps for conversion and evaluation .
• Conversion from infix to postfix algorithm :
• 1- create an empty stack called opstack for keeping operators . create an empty list for output .
• 2- convert the input infix string to a list by using the string method split .
• 3- scan the token list from left to right .
• - if the token is an operand , append it to the end of the output list .
• - if the token is a left parenthesis .push it on the stack
• - if the token is a right parenthesis , pop the opstack until the corresponding left parenthesis is removed , append each operator to the end of the output list .
• - if the token is an operator *,/,+,- ,push it on the opstack .
• However ,first remove any operators already on the opstack that have higher or equal precedence and append them to the output list .
• 4- when the input expression has been completely processed .check the opstack . any operators still on the stack can be removed and appended to the end of the output list .


Assume the postfix expression is a string of token delimited by spaces. The operators are *,/,+,- and the operands are assumed to be single-digit integer values . the output will be an integer result .
1- Create an empty stack called operandstack .
2- convert the string to a list by using the string method split
3- Scan the token list from left to right
- If the token is an operand , convert it from a string to an integer and push the value onto the operandstack .
- If the token is an operator *,/,-,+ it will need two operands . pop the operandstack twice . the first pop is the second operand and the second pop is the first operand , perform the arithmetic operation . push the result back on the operandstack .
4- When the input expression has been completely processed , the result is on the stack . pop the operandstack and return the value .
-


وهي الجواب في كلاسين :
import java.util.*;
public class lll {



/**
* @param args
*/
public static void main(String[] args) {


System.out.println("enter your infix expression") ;
Scanner input=new Scanner(System.in);
String y=input.toString ();

staa d=new staa();
d.split(y) ;





}

}



import java.util.Stack;
public class staa {
public char top;
public Stack<Character> opstack ;
public Stack<Integer> operandstack ;
public String output ="" ;
public void split (String s){
char [] a=new char[s.length()] ;
char [] z=new char[s.length()] ;

int r=0 ;
char ll ;
for (int i=0;i<s.length();i++)
{a[i]=s.charAt(i) ;
if (a[i]>='0' && a[i]<='9')
{output+=a[i] ;
ll=a[i] ;
z[r]=ll ;
r+=1 ;
}
else if (a[i]=='(')
{opstack.push(a[i]) ;}
else if(a[i]==')')
{while(opstack.peek()!='(')
{
output+=opstack.pop();
opstack.pop();}
opstack.pop();
}
else if(a[i]=='*' || a[i]=='/')
{while(!opstack.empty() &&((opstack.peek()=='*' ) || (opstack.peek()=='/' ) ))
{output+=opstack.pop();

ll=a[i] ;
z[r]=ll ;
r+=1 ;}
opstack.push(a[i]);}
else if(a[i]=='+' || a[i]=='-')
{while(!opstack.empty() && oo(opstack.peek())==true )
{output+=opstack.pop();
ll=a[i] ;
z[r]=ll ;
r+=1 ;}
opstack.push(a[i]);}}
for(int u=r;u<output.length();u++){
while(!opstack.empty() ){
output+=(opstack.pop()) ;

z[r]=(opstack.pop()) ;
r+=1 ;
}}
for(int k=0;k<output.length();k++)
{System.out.print(output) ;}


char [] t= new char [output.length()] ;
for(int i=0;i<output.length();i++)
{t[i]=z[i];}



for(int j=0;j<t.length;j++)
{
if(t[j]>='1' || t[j]<='9')
{int x =((int)j-'0') ;
operandstack.push(x) ;}
else if ((t[j]=='*') || (t[j]=='/') || (t[j]=='+') || (t[j]=='-')){
operandstack.pop () ;
int x1= operandstack.pop () ; ;
operandstack.pop();
int x2= operandstack.pop () ;
int re =0 ;
if(t[j]=='*')
{re=x2*x1 ;}
else if(t[j]=='/')
{re=x2/x1 ;}
else if(t[j]=='-')
{re=x2-x1 ;}
else if (t[j]=='+')
{re=x2+x1 ;}
operandstack.push((int) re);


}}
System.out.println(operandstack.pop());}

public boolean oo(char q)
{ char c=q ;
if(c=='+')
{return true ;}
else if (c=='-')
{return true ;}
else if(c=='/')
{return true ;}
else if(c=='*')
{return true ;}
else
return false ;
}


}