SlideShare une entreprise Scribd logo






•
•
•

•

•
•

•
Design interpreter pattern
Design interpreter pattern
Design interpreter pattern
Design interpreter pattern

Contenu connexe

Tendances

Socioeconomía
SocioeconomíaSocioeconomía
Socioeconomía
Sara de Cifuentes
 
презентфз2014
презентфз2014презентфз2014
презентфз2014
showprom324
 
Audience feedback
Audience feedbackAudience feedback
Audience feedback
Matteo Rimini
 
İçeriğin önemi
İçeriğin önemiİçeriğin önemi
İçeriğin önemiCemil Gunel
 

Tendances (9)

Socioeconomía
SocioeconomíaSocioeconomía
Socioeconomía
 
презентфз2014
презентфз2014презентфз2014
презентфз2014
 
foc.pptx
foc.pptxfoc.pptx
foc.pptx
 
La Web Cam
La Web CamLa Web Cam
La Web Cam
 
Audience feedback
Audience feedbackAudience feedback
Audience feedback
 
EPC Presentation
EPC PresentationEPC Presentation
EPC Presentation
 
بلغة 3
بلغة 3بلغة 3
بلغة 3
 
İçeriğin önemi
İçeriğin önemiİçeriğin önemi
İçeriğin önemi
 
بلغة 3
بلغة 3بلغة 3
بلغة 3
 

En vedette

Interpreter Design Pattern in Javascript
Interpreter Design Pattern in JavascriptInterpreter Design Pattern in Javascript
Interpreter Design Pattern in Javascript
Dmytro Verbovyi
 
Architectural styles 2
Architectural styles   2Architectural styles   2
Architectural styles 2
Dr Reeja S R
 
Case study 1
Case study 1Case study 1
Case study 1
Dr Reeja S R
 
Ch2
Ch2Ch2
Architectural styles and patterns
Architectural styles and patternsArchitectural styles and patterns
Architectural styles and patterns
Himanshu
 
Three Software Architecture Styles
Three Software Architecture StylesThree Software Architecture Styles
Three Software Architecture Styles
Jorgen Thelin
 
ARCHITECTURAL STYLES
ARCHITECTURAL STYLESARCHITECTURAL STYLES
ARCHITECTURAL STYLES
Architecture Faculty
 
Architecture design in software engineering
Architecture design in software engineeringArchitecture design in software engineering
Architecture design in software engineering
Preeti Mishra
 

En vedette (8)

Interpreter Design Pattern in Javascript
Interpreter Design Pattern in JavascriptInterpreter Design Pattern in Javascript
Interpreter Design Pattern in Javascript
 
Architectural styles 2
Architectural styles   2Architectural styles   2
Architectural styles 2
 
Case study 1
Case study 1Case study 1
Case study 1
 
Ch2
Ch2Ch2
Ch2
 
Architectural styles and patterns
Architectural styles and patternsArchitectural styles and patterns
Architectural styles and patterns
 
Three Software Architecture Styles
Three Software Architecture StylesThree Software Architecture Styles
Three Software Architecture Styles
 
ARCHITECTURAL STYLES
ARCHITECTURAL STYLESARCHITECTURAL STYLES
ARCHITECTURAL STYLES
 
Architecture design in software engineering
Architecture design in software engineeringArchitecture design in software engineering
Architecture design in software engineering
 

Plus de valeri kopaleishvili

Georgia(格鲁吉亚)
Georgia(格鲁吉亚)Georgia(格鲁吉亚)
Georgia(格鲁吉亚)
valeri kopaleishvili
 
Run wordcount job (hadoop)
Run wordcount job (hadoop)Run wordcount job (hadoop)
Run wordcount job (hadoop)
valeri kopaleishvili
 
Staruml
StarumlStaruml
Software specification for
Software specification forSoftware specification for
Software specification for
valeri kopaleishvili
 
Erp (sap report)
Erp (sap report)Erp (sap report)
Erp (sap report)
valeri kopaleishvili
 
Big data
Big dataBig data

Plus de valeri kopaleishvili (6)

Georgia(格鲁吉亚)
Georgia(格鲁吉亚)Georgia(格鲁吉亚)
Georgia(格鲁吉亚)
 
Run wordcount job (hadoop)
Run wordcount job (hadoop)Run wordcount job (hadoop)
Run wordcount job (hadoop)
 
Staruml
StarumlStaruml
Staruml
 
Software specification for
Software specification forSoftware specification for
Software specification for
 
Erp (sap report)
Erp (sap report)Erp (sap report)
Erp (sap report)
 
Big data
Big dataBig data
Big data
 

Design interpreter pattern

Notes de l'éditeur

  1. Interpret() - An abstract base class specifies the method interpret(). Each concrete subclass implements interpret() by accepting (as an argument) the current state of the language stream, and adding its contribution to the problem solving process.
  2. public class InterpreterDemo { public static boolean precedence( char a, char b ) { String high = "*/", low = "+-"; if (a == '(') return false; // if (a == '(' && b == ')') return false; if (a == ')' && b == '(') { System.out.println( ")-(" ); return false; } if (b == '(') return false; if (b == ')') return true; if (high.indexOf( a ) > -1 && low.indexOf( b ) > -1) return true; if (high.indexOf( a ) > -1 && high.indexOf( b ) > -1) return true; if (low.indexOf( a ) > -1 && low.indexOf( b ) > -1) return true; return false; } public static String convertToPostfix( String in ) { StkChar opstk = new StkChar(); StringBuffer out = new StringBuffer(); String opers = "+-*/()"; char topsym = '+'; boolean empty; for (int i = 0; i < in.length(); i++) if (opers.indexOf( in.charAt(i) ) == -1) out.append( in.charAt(i) ); else { while ( ! (empty = opstk.isEmpty()) && precedence( topsym = opstk.pop(), in.charAt(i) )) out.append( topsym ); if ( ! empty) opstk.push( topsym ); if (empty || in.charAt(i) != ')') opstk.push( in.charAt(i) ); else topsym = opstk.pop(); } while ( ! opstk.isEmpty()) out.append( opstk.pop() ); return out.toString(); } public static int evaluate( String in ) { StkInt stack = new StkInt(); String opers = "+-*/"; for (int a, b, i=0; i < in.length(); i++) if (opers.indexOf( in.charAt(i) ) == -1) stack.push( in.charAt(i)-48 ); else { b = stack.pop(); a = stack.pop(); if (in.charAt(i) == '+') a = a + b; else if (in.charAt(i) == '-') a = a - b; else if (in.charAt(i) == '*') a = a * b; else if (in.charAt(i) == '/') a = a / b; stack.push( a ); } return stack.pop(); } public static void main( String[] args ) { System.out.print( args[0] ); String postfix = convertToPostfix( args[0] ); System.out.print( " -- " + postfix ); System.out.println( " -- " + evaluate( postfix ) ); } class StkChar { private char[] arr = new char[9]; private int sp = -1; void push( char ch ) { if ( ! isFull()) arr[++sp] = ch; } char pop() { if (isEmpty()) return '\0'; return arr[sp--]; } boolean isFull() { return sp == arr.length-1; } boolean isEmpty() { return sp == -1; } } class StkInt { private int[] arr = new int[9]; private int sp = -1; void push( int ch ) { if ( ! isFull()) arr[++sp] = ch; } int pop() { if (isEmpty()) return 0; return arr[sp--]; } boolean isFull() { return sp == arr.length-1; } boolean isEmpty() { return sp == -1; } }} 2+3*4-5+6 -- 234*+5-6+ -- 15 (2+3)*4-5+6 -- 23+4*5-6+ -- 21 2+3*(4-5)+6 -- 2345-*+6+ -- 5 2+3*((4-5)+6) -- 2345-6+*+ -- 17 (3-(4*(5+6))/(7-8))*9/4 -- 3456+*78-/-9*4/ -- 105