package layout;
import java.util.Scanner;
/* Class Node */
class Node {
protected int data;
protected Node next, prev;
/* Constructor */
public Node() {
data = 0;
next = null;
}
/* Constructor */
public Node( int d, Node n ) {
data = d;
next = n;
}
/* Function to set link to next node */
public void setLinkNext( Node n ) {
next = n;
}
/* Funtion to get link to next node */
public Node getLinkNext() {
return next;
}
/* Function to set data to node */
public void setData( int d ) {
data = d;
}
/* Function to get data from node */
public int getData() {
return data;
}
}
/* Class linkedList */
class linkedList {
protected Node start, end;
public int size;
/* Constructor */
public linkedList() {
start = null;
end = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty() {
return start == null;
}
/* Function to get size of list */
public int getSize() {
return size;
}
public Node getNodeStart() {
return start;
}
public Node getNodeEnd() {
return end;
}
/* Function to insert element */
public void insertHead( int val ) {
Node p = new Node( val, null );
if ( start == null ) {
start = p;
end = start;
}
else {
p.setLinkNext( start );
start = p;
}
size++;
}
void insertTail( int val ) {
Node p = new Node( val, null );;
if ( start == null ) {
start = p;
end = start;
}
else {
end.setLinkNext( p );
end = p;
}
p.setLinkNext( null );
size++;
}
void Output() {
Node p = start;
while ( p != null ) {
System.out.print( p.getData() + " <-> " );
p = p.getLinkNext();
}
}
/* Function to convert List to a String */
public String display() {
String ListString01 = "";
Node ptr = start;
ListString01 = ListString01 + ptr.getData() + ", ";
if ( size == 0 ) {
ListString01 = "List is empty";
return ListString01;
}
ptr = start.getLinkNext();
while ( ptr.getLinkNext() != null ) {
ListString01 = ListString01 + ptr.getData() + ", ";
ptr = ptr.getLinkNext();
}
return ListString01;
}
void initList( linkedList List ) {
List.start = List.end = null;
}
void removeHead( linkedList list ) {
Node p;
p = list.start;
if ( list.start == null )
return;
list.start = p.getLinkNext();
if ( list.start == null )
list.end = null;
p.setLinkNext( null );
p = null;
}
int getHead( linkedList list ) {
if ( list.start == null )
return 0;
else {
int a;
Node p;
p = list.start;
list.start = p.getLinkNext();
p.setLinkNext( null );
if ( list.start == null )
list.end = null;
a = p.data;
p = null;
return a;
}
}
int pow01( int k1, int k2 ) {
int i = 0;
for ( i = 0; i < k2; i++ ) {
k1 = k1 * k1;
}
return k1;
}
int charToInt( char c[], int h ) {
int s = 0, t = 0;
for ( int i = h; '0' <= c[ i ] && c[ i ] <= '9'; i++ )
s = s + 1;
for ( int j = 0; j < s; j++ )
t = t + ( c[ j + h ] - '0' ) * pow01( 10, s - j - 1 );
h = h + s;
return t;
}
int getElement( char c[], int E, int H, linkedList List ) {
if ( c[ H ] == '*' ) {
H++;
E = 2;
return 1;
}
if ( c[ H ] == '/' ) {
H++;
E = 2;
return 2;
}
if ( c[ H ] == '+' ) {
H++;
E = 3;
return 3;
}
if ( c[ H ] == '-' ) {
H++;
if ( E != 1 && E != 5 )
List.insertHead( 0 );
E = 3;
return 4;
}
if ( c[ H ] == '(' ) {
H++;
E = 4;
return 5;
}
if ( c[ H ] == ')' ) {
H++;
E = 5;
return 6;
}
if ( '0' <= c[ H ] && c[ H ] <= '9' ) {
int a;
a = charToInt( c, H );
E = 1;
return a;
}
else {
System.out.println( "Data is Error !" );
H++;
E = 0;
return 0;
}
}
void Tinh( linkedList List, int b ) {
int c, d, e;
if ( b != 1 && b != 2 && b != 3 && b != 4 )
return;
c = getHead( List );
d = getHead( List );
if ( b == 1 ) {
e = c * d;
List.insertHead( e );
}
if ( b == 2 ) {
e = d / c;
List.insertHead( e );
}
if ( b == 3 ) {
e = c + d;
List.insertHead( e );
}
if ( b == 4 ) {
e = d - c;
List.insertHead( e );
}
}
@SuppressWarnings( "deprecation")
public static void main( String arg[] ) {
int E = 0, H = 0, A;
int i = 0, a = 0, s = 0;
int n = 200;
char c[] = new char[ n ];
String s1;
s1 = "";
System.out.println( "Nhap bieu thuc : " );
Scanner input = new Scanner( System.in );
try {
s1 += input.nextLine();
input.close();
c = s1.toCharArray();
}
catch ( Exception e ) {
System.out.println( e );
}
linkedList list1, list2;
list1 = new linkedList();
list2 = new linkedList();
while ( H != s ) {
/* E = 1: A is numbers. 2: Multiplication and Division. 3: Addition
* and Subtraction. 4: '('. A = 5. 5: ')'. A = 6. A = 1:
* Multiplication. 2: Division. 3: Addition. 4: Subtraction. 5: '('.
* 6: ')'. */
A = list2.getElement( c, E, H, list2 );
if ( E == 1 )
list2.insertHead( A );
if ( E == 4 )
list1.insertHead( A );
if ( E == 5 ) {
while ( list1.start.data != 5 ) {
a = list1.getHead( list1 );
list2.Tinh( list2, a );
}
list1.removeHead( list1 );
}
if ( E == 2 || E == 3 ) {
i = 0;
while ( i != 1 ) {
if ( ( list1.start == null )
|| ( E == 2 && list1.start.data > 2 )
|| ( E == 3 && list1.start.data > 4 ) ) {
i = 1;
list1.insertHead( A );
}
else {
a = list1.getHead( list1 );
list2.Tinh( list2, a );
}
} /* End while (i != 1) */
} /* End if ( E == 2 || E == 3) */
} /* End while (H != s) */
while ( list1.start != null ) {
a = list1.getHead( list1 );
list2.Tinh( list2, a );
}
System.out.println( "Gia tri bieu thuc la: " + list2.start.data );
}
}