SlideShare a Scribd company logo
Write a method named convertInfixToPostfix that accepts an Infix expression as a parameter and
converts it to its Postfix equivalent by implementing the algorithm given in Figure 6.10 of the
required text book. Note that the Infix expression does not need to be a fully parenthesized
expression.
Your method should have the following signature and should be included in a class called
ExpressionConversion.
public String convertInfixToPostfix(String infixExp) { /* your implementation here*/ }
Solution
import java.util.Scanner; //for user input
public class ExpressionConversion
{
public static void main(String[] args) //main function
{
String infixExp,postfixExp;
Scanner in = new Scanner(System.in);
System.out.println("Enter Infix Expression : ");
infixExp = in.next();
postfixExp = convertInfixToPostfix(infixExp);
System.out.println("Postfix Expression : " + postfixExp);
}
public static String convertInfixToPostfix(String infixExp) //function to convert infix to
postfix
{
int len = infixExp.length();
Stack s = new Stack(len);
int i=0;
char op;
String postfixExp = "";
for(i=0; i < len; i++)
{
if(infixExp.charAt(i) == '+' || infixExp.charAt(i) == '-' || infixExp.charAt(i) == '*' ||
infixExp.charAt(i) == '/' || infixExp.charAt(i) == '(' || infixExp.charAt(i) == ')')
{
switch(infixExp.charAt(i))
{
case '(' :
s.push(infixExp.charAt(i));
break;
case ')' :
op = s.pop();
while(op != '(')
{
postfixExp = postfixExp + op;
op = s.pop();
}
break;
default:
while(preceed(s.peek()) >= preceed(infixExp.charAt(i)))
postfixExp = postfixExp + s.pop();
s.push(infixExp.charAt(i));
}
}
else
postfixExp = postfixExp + infixExp.charAt(i);
}
while(!s.isEmpty())
postfixExp = postfixExp + s.pop();
return postfixExp;
}
static int preceed(char st) //function to check the precedenceof operators
{
switch(st)
{
case '*' :
case '/' :
return 3;
case '+' :
case '-' :
return 2;
default :
return 1;
}
}
}
class Stack //creating stack to implement infix to postfix conversion
{
int maxSize;
char[] stackArray;
int top;
public Stack(int max)
{
maxSize = max;
stackArray = new char[maxSize];
top = -1;
}
public void push(char item)
{
stackArray[++top] = item;
}
public char pop()
{
return stackArray[top--];
}
public char peek()
{
if(!isEmpty())
return stackArray[top];
else
return 0;
}
public boolean isEmpty()
{
return (top == -1);
}
}

More Related Content

Similar to Write a method named convertInfixToPostfix that accepts an Infix exp.pdf

Function C++
Function C++ Function C++
Function C++
Shahzad Afridi
 
You can look at the Java programs in the text book to see how commen
You can look at the Java programs in the text book to see how commenYou can look at the Java programs in the text book to see how commen
You can look at the Java programs in the text book to see how commen
anitramcroberts
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
Sampath Kumar
 
C function
C functionC function
C function
thirumalaikumar3
 
How to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescriptHow to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescript
Katy Slemon
 
Description of programYou are to write a program name InfixToPost.pdf
Description of programYou are to write a program name InfixToPost.pdfDescription of programYou are to write a program name InfixToPost.pdf
Description of programYou are to write a program name InfixToPost.pdf
arihantelehyb
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
Chandrakant Divate
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
Function in c
Function in cFunction in c
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
ssuser823678
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
ssuser2076d9
 
2. Stack. Write a program that uses the stack class (you can use.pdf
2. Stack. Write a program that uses the stack class (you can use.pdf2. Stack. Write a program that uses the stack class (you can use.pdf
2. Stack. Write a program that uses the stack class (you can use.pdf
aniarihant
 
Description of a JAVA programYou are to write a program name Infi.pdf
Description of a JAVA programYou are to write a program name Infi.pdfDescription of a JAVA programYou are to write a program name Infi.pdf
Description of a JAVA programYou are to write a program name Infi.pdf
arumugambags
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
Codemotion
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
Mario Fusco
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
Mansi Tyagi
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde3
 

Similar to Write a method named convertInfixToPostfix that accepts an Infix exp.pdf (20)

Function in c
Function in cFunction in c
Function in c
 
Function C++
Function C++ Function C++
Function C++
 
You can look at the Java programs in the text book to see how commen
You can look at the Java programs in the text book to see how commenYou can look at the Java programs in the text book to see how commen
You can look at the Java programs in the text book to see how commen
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
C function
C functionC function
C function
 
How to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescriptHow to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescript
 
Description of programYou are to write a program name InfixToPost.pdf
Description of programYou are to write a program name InfixToPost.pdfDescription of programYou are to write a program name InfixToPost.pdf
Description of programYou are to write a program name InfixToPost.pdf
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Function in c
Function in cFunction in c
Function in c
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
Function
FunctionFunction
Function
 
2. Stack. Write a program that uses the stack class (you can use.pdf
2. Stack. Write a program that uses the stack class (you can use.pdf2. Stack. Write a program that uses the stack class (you can use.pdf
2. Stack. Write a program that uses the stack class (you can use.pdf
 
Description of a JAVA programYou are to write a program name Infi.pdf
Description of a JAVA programYou are to write a program name Infi.pdfDescription of a JAVA programYou are to write a program name Infi.pdf
Description of a JAVA programYou are to write a program name Infi.pdf
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 

More from feelingsboutiques

7. Which statements about financial forecasting is most likely TRUE.pdf
7. Which statements about financial forecasting is most likely TRUE.pdf7. Which statements about financial forecasting is most likely TRUE.pdf
7. Which statements about financial forecasting is most likely TRUE.pdf
feelingsboutiques
 
Inputs in the light reaction of photosynthesis Inputs in the li.pdf
Inputs in the light reaction of photosynthesis Inputs in the li.pdfInputs in the light reaction of photosynthesis Inputs in the li.pdf
Inputs in the light reaction of photosynthesis Inputs in the li.pdf
feelingsboutiques
 
How do I obtain the solution Also, how would I convert the limit .pdf
How do I obtain the solution  Also, how would I convert the limit .pdfHow do I obtain the solution  Also, how would I convert the limit .pdf
How do I obtain the solution Also, how would I convert the limit .pdf
feelingsboutiques
 
If both of Johns parents are heterozygous for sickle cell anemia a.pdf
If both of Johns parents are heterozygous for sickle cell anemia a.pdfIf both of Johns parents are heterozygous for sickle cell anemia a.pdf
If both of Johns parents are heterozygous for sickle cell anemia a.pdf
feelingsboutiques
 
I need help with ALL of the following bacteria -trypanosoma gam.pdf
I need help with ALL of the following bacteria -trypanosoma gam.pdfI need help with ALL of the following bacteria -trypanosoma gam.pdf
I need help with ALL of the following bacteria -trypanosoma gam.pdf
feelingsboutiques
 
I having trouble with this problem. Can someone out there help me J.pdf
I having trouble with this problem. Can someone out there help me J.pdfI having trouble with this problem. Can someone out there help me J.pdf
I having trouble with this problem. Can someone out there help me J.pdf
feelingsboutiques
 
ExplainAnswer these genetic questions please. A pure-breeding line .pdf
ExplainAnswer these genetic questions please. A pure-breeding line .pdfExplainAnswer these genetic questions please. A pure-breeding line .pdf
ExplainAnswer these genetic questions please. A pure-breeding line .pdf
feelingsboutiques
 
Early version of UNIX were available only with a command-driven inte.pdf
Early version of UNIX were available only with a command-driven inte.pdfEarly version of UNIX were available only with a command-driven inte.pdf
Early version of UNIX were available only with a command-driven inte.pdf
feelingsboutiques
 
Environmental sciencePlease answer it ASAP 1. Why do water man.pdf
Environmental sciencePlease answer it ASAP 1. Why do water man.pdfEnvironmental sciencePlease answer it ASAP 1. Why do water man.pdf
Environmental sciencePlease answer it ASAP 1. Why do water man.pdf
feelingsboutiques
 
Discuss a specific public health concern for each of the following g.pdf
Discuss a specific public health concern for each of the following g.pdfDiscuss a specific public health concern for each of the following g.pdf
Discuss a specific public health concern for each of the following g.pdf
feelingsboutiques
 
Due to your interference with organisms on the planet, one species o.pdf
Due to your interference with organisms on the planet, one species o.pdfDue to your interference with organisms on the planet, one species o.pdf
Due to your interference with organisms on the planet, one species o.pdf
feelingsboutiques
 
Complete the following chart, and then circle the layer that is found.pdf
Complete the following chart, and then circle the layer that is found.pdfComplete the following chart, and then circle the layer that is found.pdf
Complete the following chart, and then circle the layer that is found.pdf
feelingsboutiques
 
1a. What kinds of deductions are prohibited as a matter of public po.pdf
1a. What kinds of deductions are prohibited as a matter of public po.pdf1a. What kinds of deductions are prohibited as a matter of public po.pdf
1a. What kinds of deductions are prohibited as a matter of public po.pdf
feelingsboutiques
 
3. IQ tests are given to fifteen pairs of siblings. In each pair, one.pdf
3. IQ tests are given to fifteen pairs of siblings. In each pair, one.pdf3. IQ tests are given to fifteen pairs of siblings. In each pair, one.pdf
3. IQ tests are given to fifteen pairs of siblings. In each pair, one.pdf
feelingsboutiques
 
A rectangular board is 9 by 17 units. A string is connected to the f.pdf
A rectangular board is 9 by 17 units. A string is connected to the f.pdfA rectangular board is 9 by 17 units. A string is connected to the f.pdf
A rectangular board is 9 by 17 units. A string is connected to the f.pdf
feelingsboutiques
 
You have 20 marbles and 20 rocks. Place them into two boxes to incre.pdf
You have 20 marbles and 20 rocks. Place them into two boxes to incre.pdfYou have 20 marbles and 20 rocks. Place them into two boxes to incre.pdf
You have 20 marbles and 20 rocks. Place them into two boxes to incre.pdf
feelingsboutiques
 
X and Y are continuous random variables with joint p.d.f. f(x,y) = c.pdf
X and Y are continuous random variables with joint p.d.f. f(x,y) = c.pdfX and Y are continuous random variables with joint p.d.f. f(x,y) = c.pdf
X and Y are continuous random variables with joint p.d.f. f(x,y) = c.pdf
feelingsboutiques
 
What is meant by cultural emergent and how does this differ from oth.pdf
What is meant by cultural emergent and how does this differ from oth.pdfWhat is meant by cultural emergent and how does this differ from oth.pdf
What is meant by cultural emergent and how does this differ from oth.pdf
feelingsboutiques
 
What are two main causes of recombination Errors in DNA replication.pdf
What are two main causes of recombination  Errors in DNA replication.pdfWhat are two main causes of recombination  Errors in DNA replication.pdf
What are two main causes of recombination Errors in DNA replication.pdf
feelingsboutiques
 
1.Developmental Biology brings together many different areas of stud.pdf
1.Developmental Biology brings together many different areas of stud.pdf1.Developmental Biology brings together many different areas of stud.pdf
1.Developmental Biology brings together many different areas of stud.pdf
feelingsboutiques
 

More from feelingsboutiques (20)

7. Which statements about financial forecasting is most likely TRUE.pdf
7. Which statements about financial forecasting is most likely TRUE.pdf7. Which statements about financial forecasting is most likely TRUE.pdf
7. Which statements about financial forecasting is most likely TRUE.pdf
 
Inputs in the light reaction of photosynthesis Inputs in the li.pdf
Inputs in the light reaction of photosynthesis Inputs in the li.pdfInputs in the light reaction of photosynthesis Inputs in the li.pdf
Inputs in the light reaction of photosynthesis Inputs in the li.pdf
 
How do I obtain the solution Also, how would I convert the limit .pdf
How do I obtain the solution  Also, how would I convert the limit .pdfHow do I obtain the solution  Also, how would I convert the limit .pdf
How do I obtain the solution Also, how would I convert the limit .pdf
 
If both of Johns parents are heterozygous for sickle cell anemia a.pdf
If both of Johns parents are heterozygous for sickle cell anemia a.pdfIf both of Johns parents are heterozygous for sickle cell anemia a.pdf
If both of Johns parents are heterozygous for sickle cell anemia a.pdf
 
I need help with ALL of the following bacteria -trypanosoma gam.pdf
I need help with ALL of the following bacteria -trypanosoma gam.pdfI need help with ALL of the following bacteria -trypanosoma gam.pdf
I need help with ALL of the following bacteria -trypanosoma gam.pdf
 
I having trouble with this problem. Can someone out there help me J.pdf
I having trouble with this problem. Can someone out there help me J.pdfI having trouble with this problem. Can someone out there help me J.pdf
I having trouble with this problem. Can someone out there help me J.pdf
 
ExplainAnswer these genetic questions please. A pure-breeding line .pdf
ExplainAnswer these genetic questions please. A pure-breeding line .pdfExplainAnswer these genetic questions please. A pure-breeding line .pdf
ExplainAnswer these genetic questions please. A pure-breeding line .pdf
 
Early version of UNIX were available only with a command-driven inte.pdf
Early version of UNIX were available only with a command-driven inte.pdfEarly version of UNIX were available only with a command-driven inte.pdf
Early version of UNIX were available only with a command-driven inte.pdf
 
Environmental sciencePlease answer it ASAP 1. Why do water man.pdf
Environmental sciencePlease answer it ASAP 1. Why do water man.pdfEnvironmental sciencePlease answer it ASAP 1. Why do water man.pdf
Environmental sciencePlease answer it ASAP 1. Why do water man.pdf
 
Discuss a specific public health concern for each of the following g.pdf
Discuss a specific public health concern for each of the following g.pdfDiscuss a specific public health concern for each of the following g.pdf
Discuss a specific public health concern for each of the following g.pdf
 
Due to your interference with organisms on the planet, one species o.pdf
Due to your interference with organisms on the planet, one species o.pdfDue to your interference with organisms on the planet, one species o.pdf
Due to your interference with organisms on the planet, one species o.pdf
 
Complete the following chart, and then circle the layer that is found.pdf
Complete the following chart, and then circle the layer that is found.pdfComplete the following chart, and then circle the layer that is found.pdf
Complete the following chart, and then circle the layer that is found.pdf
 
1a. What kinds of deductions are prohibited as a matter of public po.pdf
1a. What kinds of deductions are prohibited as a matter of public po.pdf1a. What kinds of deductions are prohibited as a matter of public po.pdf
1a. What kinds of deductions are prohibited as a matter of public po.pdf
 
3. IQ tests are given to fifteen pairs of siblings. In each pair, one.pdf
3. IQ tests are given to fifteen pairs of siblings. In each pair, one.pdf3. IQ tests are given to fifteen pairs of siblings. In each pair, one.pdf
3. IQ tests are given to fifteen pairs of siblings. In each pair, one.pdf
 
A rectangular board is 9 by 17 units. A string is connected to the f.pdf
A rectangular board is 9 by 17 units. A string is connected to the f.pdfA rectangular board is 9 by 17 units. A string is connected to the f.pdf
A rectangular board is 9 by 17 units. A string is connected to the f.pdf
 
You have 20 marbles and 20 rocks. Place them into two boxes to incre.pdf
You have 20 marbles and 20 rocks. Place them into two boxes to incre.pdfYou have 20 marbles and 20 rocks. Place them into two boxes to incre.pdf
You have 20 marbles and 20 rocks. Place them into two boxes to incre.pdf
 
X and Y are continuous random variables with joint p.d.f. f(x,y) = c.pdf
X and Y are continuous random variables with joint p.d.f. f(x,y) = c.pdfX and Y are continuous random variables with joint p.d.f. f(x,y) = c.pdf
X and Y are continuous random variables with joint p.d.f. f(x,y) = c.pdf
 
What is meant by cultural emergent and how does this differ from oth.pdf
What is meant by cultural emergent and how does this differ from oth.pdfWhat is meant by cultural emergent and how does this differ from oth.pdf
What is meant by cultural emergent and how does this differ from oth.pdf
 
What are two main causes of recombination Errors in DNA replication.pdf
What are two main causes of recombination  Errors in DNA replication.pdfWhat are two main causes of recombination  Errors in DNA replication.pdf
What are two main causes of recombination Errors in DNA replication.pdf
 
1.Developmental Biology brings together many different areas of stud.pdf
1.Developmental Biology brings together many different areas of stud.pdf1.Developmental Biology brings together many different areas of stud.pdf
1.Developmental Biology brings together many different areas of stud.pdf
 

Recently uploaded

BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 

Recently uploaded (20)

BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 

Write a method named convertInfixToPostfix that accepts an Infix exp.pdf

  • 1. Write a method named convertInfixToPostfix that accepts an Infix expression as a parameter and converts it to its Postfix equivalent by implementing the algorithm given in Figure 6.10 of the required text book. Note that the Infix expression does not need to be a fully parenthesized expression. Your method should have the following signature and should be included in a class called ExpressionConversion. public String convertInfixToPostfix(String infixExp) { /* your implementation here*/ } Solution import java.util.Scanner; //for user input public class ExpressionConversion { public static void main(String[] args) //main function { String infixExp,postfixExp; Scanner in = new Scanner(System.in); System.out.println("Enter Infix Expression : "); infixExp = in.next(); postfixExp = convertInfixToPostfix(infixExp); System.out.println("Postfix Expression : " + postfixExp); } public static String convertInfixToPostfix(String infixExp) //function to convert infix to postfix { int len = infixExp.length(); Stack s = new Stack(len); int i=0; char op; String postfixExp = ""; for(i=0; i < len; i++) { if(infixExp.charAt(i) == '+' || infixExp.charAt(i) == '-' || infixExp.charAt(i) == '*' || infixExp.charAt(i) == '/' || infixExp.charAt(i) == '(' || infixExp.charAt(i) == ')')
  • 2. { switch(infixExp.charAt(i)) { case '(' : s.push(infixExp.charAt(i)); break; case ')' : op = s.pop(); while(op != '(') { postfixExp = postfixExp + op; op = s.pop(); } break; default: while(preceed(s.peek()) >= preceed(infixExp.charAt(i))) postfixExp = postfixExp + s.pop(); s.push(infixExp.charAt(i)); } } else postfixExp = postfixExp + infixExp.charAt(i); } while(!s.isEmpty()) postfixExp = postfixExp + s.pop(); return postfixExp; } static int preceed(char st) //function to check the precedenceof operators { switch(st) { case '*' : case '/' : return 3; case '+' : case '-' :
  • 3. return 2; default : return 1; } } } class Stack //creating stack to implement infix to postfix conversion { int maxSize; char[] stackArray; int top; public Stack(int max) { maxSize = max; stackArray = new char[maxSize]; top = -1; } public void push(char item) { stackArray[++top] = item; } public char pop() { return stackArray[top--]; } public char peek() { if(!isEmpty()) return stackArray[top]; else return 0; }