Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3b9de2e

Browse files
author
jsquared21
committed
Add Ex 20.16
1 parent3591cc2 commit3b9de2e

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
2.49 KB
Binary file not shown.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*********************************************************************************
2+
* (Convert infix to postfix) Write a method that converts an infix expression *
3+
* into a postfix expression using the following header: *
4+
* *
5+
* public static String infixToPostfix(String expression) *
6+
* For example, the method should convert the infix expression (1 + 2) * 3 to *
7+
* 1 2 + 3 * and 2 * (1 + 3) to 2 1 3 + *. *
8+
*********************************************************************************/
9+
importjava.util.*;
10+
11+
publicclassExercise_20_16 {
12+
publicstaticvoidmain(String[]args) {
13+
// Test method infixToPostfix and display result
14+
System.out.println("Infix Expression Postfix Expression");
15+
System.out.println(" (1 + 2) * 3 "
16+
+infixToPostfix("(1 + 2) * 3"));
17+
System.out.println(" 2 * (1 + 3) "
18+
+infixToPostfix("2 * (1 + 3)"));
19+
}
20+
21+
/** Method converts an infix expression into a postfix expression */
22+
publicstaticStringinfixToPostfix(Stringexpression) {
23+
// Create a Linked list to store the result
24+
LinkedList<String>operatorList =newLinkedList<>();
25+
26+
// Create a Linked list to store operands
27+
LinkedList<String>resultList =newLinkedList<>();
28+
29+
// Create a stack to store '('
30+
Stack<Character>stack =newStack<>();
31+
32+
// Insert blanks around (, ), +, -, /, and *
33+
expression =insertBlanks(expression);
34+
35+
// Extract operands and operators
36+
String[]tokens =expression.split(" ");
37+
38+
// Scan tokens
39+
for (Stringtoken:tokens) {
40+
if (token.length() ==0)// Blank space
41+
continue;// Back to the while loop to extract the next token
42+
elseif (token.charAt(0) =='(')// Push '(' onto the stack
43+
stack.push(token.charAt(0));
44+
elseif (!stack.isEmpty() &&stack.peek() =='(' &&
45+
token.charAt(0) !=')') {
46+
// Place operators within "( )" and the
47+
// front to the front of the operatorList
48+
if (Character.isDigit(token.charAt(0)))
49+
resultList.addLast(token);
50+
elseif (token.charAt(0) =='+' ||token.charAt(0) =='-' ||
51+
token.charAt(0) =='*' ||token.charAt(0) =='/' )
52+
operatorList.addFirst(token);
53+
}
54+
elseif (!stack.isEmpty() &&token.charAt(0) ==')') {
55+
// Add the operatorList to the result
56+
resultList.addAll(operatorList);
57+
operatorList.clear();
58+
stack.pop();
59+
}
60+
elseif (token.charAt(0) =='+' ||token.charAt(0) =='-')
61+
operatorList.addLast(token);// Add +, - to the end of list
62+
elseif (token.charAt(0) =='*' ||token.charAt(0) =='/')
63+
operatorList.addFirst(token);// Add +, - to the front of list
64+
elseif (Character.isDigit(token.charAt(0)))
65+
resultList.addLast(token);// Add digits to result list
66+
}
67+
68+
// Format the result string
69+
Stringresult ="";
70+
resultList.addAll(operatorList);
71+
for (Stringe:resultList) {
72+
result +=e +" ";
73+
}
74+
75+
// return result
76+
returnresult;
77+
}
78+
79+
/** Method Inserts blanks around (, ), +, -, /, and *. */
80+
publicstaticStringinsertBlanks(Strings) {
81+
Stringresult ="";
82+
83+
for (inti =0;i <s.length();i++) {
84+
if (s.charAt(i) =='(' ||s.charAt(i) ==')' ||
85+
s.charAt(i) =='+' ||s.charAt(i) =='-' ||
86+
s.charAt(i) =='*' ||s.charAt(i) =='/')
87+
result +=" " +s.charAt(i) +" ";
88+
else
89+
result +=s.charAt(i);
90+
}
91+
92+
returnresult;
93+
}
94+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp