SlideShare a Scribd company logo
1 of 22
Download to read offline
Data structures:
****Using java language and develop a prototype for a demo program that reads in strings that
look like:
Using the infix to postfix code below:
A = 3; // your code should store A and 3 as a key-value pair
B = 4; // your code should store B and 4 as a key-value pair
A; // your code should now look-up A and then display its value
later we (you) will add the ability to write
Y = A * B + 3;
Y; // your code should show 16
Using this code
// infix.java
// converts infix arithmetic expressions to postfix
// to run this program: C>java InfixApp
import java.io.*; // for I/O
////////////////////////////////////////////////////////////////
class StackX {
private int maxSize;
private char[] stackArray;
private int top;
// --------------------------------------------------------------
public StackX(int s) // constructor
{
maxSize = s;
stackArray = new char[maxSize];
top = -1;
}
// --------------------------------------------------------------
public void push(char j) // put item on top of stack
{
stackArray[++top] = j;
}
// --------------------------------------------------------------
public char pop() // take item from top of stack
{
return stackArray[top--];
}
// --------------------------------------------------------------
public char peek() // peek at top of stack
{
return stackArray[top];
}
// --------------------------------------------------------------
public boolean isEmpty() // true if stack is empty
{
return (top == -1);
}
// -------------------------------------------------------------
public int size() // return size
{
return top + 1;
}
// --------------------------------------------------------------
public char peekN(int n) // return item at index n
{
return stackArray[n];
}
// --------------------------------------------------------------
public void displayStack(String s) {
System.out.print(s);
System.out.print("Stack (bottom-->top): ");
for (int j = 0; j < size(); j++) {
System.out.print(peekN(j));
System.out.print(' ');
}
System.out.println(" ");
}
// --------------------------------------------------------------
} // end class StackX
////////////////////////////////////////////////////////////////
class InToPost // infix to postfix conversion
{
private StackX theStack;
private String input;
private String output = "";
// --------------------------------------------------------------
public InToPost(String in) // constructor
{
input = in;
int stackSize = input.length();
theStack = new StackX(stackSize);
}
// --------------------------------------------------------------
public String doTrans() // do translation to postfix
{
for (int j = 0; j < input.length(); j++) {
char ch = input.charAt(j);
theStack.displayStack("For " + ch + " "); // *diagnostic*
switch (ch) {
case '+': // it’s + or -
case '-':
gotOper(ch, 1); // go pop operators
break; // (precedence 1)
case '*': // it’s * or /
case '/':
gotOper(ch, 2); // go pop operators
break; // (precedence 2)
case '(': // it’s a left paren
theStack.push(ch); // push it
break;
case ')': // it’s a right paren
gotParen(ch); // go pop operators
break;
default: // must be an operand
output = output + ch; // write it to output
break;
} // end switch
} // end for
while (!theStack.isEmpty()) // pop remaining opers
{
theStack.displayStack("While "); // *diagnostic*
output = output + theStack.pop(); // write to output
}
theStack.displayStack("End "); // *diagnostic*
return output; // return postfix
} // end doTrans()
// --------------------------------------------------------------
public void gotOper(char opThis, int prec1) { // got operator from input
while (!theStack.isEmpty()) {
char opTop = theStack.pop();
if (opTop == '(') // if it’s a ‘(‘
{
theStack.push(opTop); // restore ‘(‘
break;
} else // it’s an operator
{
int prec2; // precedence of new op
if (opTop == '+' || opTop == '-') // find new op prec
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1) // if prec of new op less
{ // than prec of old
theStack.push(opTop); // save newly-popped op
break;
} else // prec of new not less
output = output + opTop; // than prec of old
} // end else (it’s an operator)
} // end while
theStack.push(opThis); // push new operator
} // end gotOp()
// --------------------------------------------------------------
public void gotParen(char ch) { // got right paren from input
while (!theStack.isEmpty()) {
char chx = theStack.pop();
if (chx == '(') // if popped ‘
break; // we’re done
else // if popped operator
output = output + chx; // output it
} // end while
} // end popOps()
// --------------------------------------------------------------
} // end class InToPost
////////////////////////////////////////////////////////////////
class InfixApp {
public static void main(String[] args) throws IOException {
String input, output;
while (true) {
System.out.print("Enter infix: ");
System.out.flush();
input = getString(); // read a string from kbd
if (input.equals("")) // quit if [Enter]
break;
// make a translator
InToPost theTrans = new InToPost(input);
output = theTrans.doTrans(); // do the translation
System.out.println("Postfix is " + output + ' ');
} // end while
} // end main()
// --------------------------------------------------------------
public static String getString() throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
// --------------------------------------------------------------
} // end class InfixApp
////////////////////////////////////////////////////////////////
Data structures:
****Using java language and develop a prototype for a demo program that reads in strings that
look like:
Using the infix to postfix code below:
A = 3; // your code should store A and 3 as a key-value pair
B = 4; // your code should store B and 4 as a key-value pair
A; // your code should now look-up A and then display its value
later we (you) will add the ability to write
Y = A * B + 3;
Y; // your code should show 16
Using this code
// infix.java
// converts infix arithmetic expressions to postfix
// to run this program: C>java InfixApp
import java.io.*; // for I/O
////////////////////////////////////////////////////////////////
class StackX {
private int maxSize;
private char[] stackArray;
private int top;
// --------------------------------------------------------------
public StackX(int s) // constructor
{
maxSize = s;
stackArray = new char[maxSize];
top = -1;
}
// --------------------------------------------------------------
public void push(char j) // put item on top of stack
{
stackArray[++top] = j;
}
// --------------------------------------------------------------
public char pop() // take item from top of stack
{
return stackArray[top--];
}
// --------------------------------------------------------------
public char peek() // peek at top of stack
{
return stackArray[top];
}
// --------------------------------------------------------------
public boolean isEmpty() // true if stack is empty
{
return (top == -1);
}
// -------------------------------------------------------------
public int size() // return size
{
return top + 1;
}
// --------------------------------------------------------------
public char peekN(int n) // return item at index n
{
return stackArray[n];
}
// --------------------------------------------------------------
public void displayStack(String s) {
System.out.print(s);
System.out.print("Stack (bottom-->top): ");
for (int j = 0; j < size(); j++) {
System.out.print(peekN(j));
System.out.print(' ');
}
System.out.println(" ");
}
// --------------------------------------------------------------
} // end class StackX
////////////////////////////////////////////////////////////////
class InToPost // infix to postfix conversion
{
private StackX theStack;
private String input;
private String output = "";
// --------------------------------------------------------------
public InToPost(String in) // constructor
{
input = in;
int stackSize = input.length();
theStack = new StackX(stackSize);
}
// --------------------------------------------------------------
public String doTrans() // do translation to postfix
{
for (int j = 0; j < input.length(); j++) {
char ch = input.charAt(j);
theStack.displayStack("For " + ch + " "); // *diagnostic*
switch (ch) {
case '+': // it’s + or -
case '-':
gotOper(ch, 1); // go pop operators
break; // (precedence 1)
case '*': // it’s * or /
case '/':
gotOper(ch, 2); // go pop operators
break; // (precedence 2)
case '(': // it’s a left paren
theStack.push(ch); // push it
break;
case ')': // it’s a right paren
gotParen(ch); // go pop operators
break;
default: // must be an operand
output = output + ch; // write it to output
break;
} // end switch
} // end for
while (!theStack.isEmpty()) // pop remaining opers
{
theStack.displayStack("While "); // *diagnostic*
output = output + theStack.pop(); // write to output
}
theStack.displayStack("End "); // *diagnostic*
return output; // return postfix
} // end doTrans()
// --------------------------------------------------------------
public void gotOper(char opThis, int prec1) { // got operator from input
while (!theStack.isEmpty()) {
char opTop = theStack.pop();
if (opTop == '(') // if it’s a ‘(‘
{
theStack.push(opTop); // restore ‘(‘
break;
} else // it’s an operator
{
int prec2; // precedence of new op
if (opTop == '+' || opTop == '-') // find new op prec
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1) // if prec of new op less
{ // than prec of old
theStack.push(opTop); // save newly-popped op
break;
} else // prec of new not less
output = output + opTop; // than prec of old
} // end else (it’s an operator)
} // end while
theStack.push(opThis); // push new operator
} // end gotOp()
// --------------------------------------------------------------
public void gotParen(char ch) { // got right paren from input
while (!theStack.isEmpty()) {
char chx = theStack.pop();
if (chx == '(') // if popped ‘
break; // we’re done
else // if popped operator
output = output + chx; // output it
} // end while
} // end popOps()
// --------------------------------------------------------------
} // end class InToPost
////////////////////////////////////////////////////////////////
class InfixApp {
public static void main(String[] args) throws IOException {
String input, output;
while (true) {
System.out.print("Enter infix: ");
System.out.flush();
input = getString(); // read a string from kbd
if (input.equals("")) // quit if [Enter]
break;
// make a translator
InToPost theTrans = new InToPost(input);
output = theTrans.doTrans(); // do the translation
System.out.println("Postfix is " + output + ' ');
} // end while
} // end main()
// --------------------------------------------------------------
public static String getString() throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
// --------------------------------------------------------------
} // end class InfixApp
////////////////////////////////////////////////////////////////
Data structures:
****Using java language and develop a prototype for a demo program that reads in strings that
look like:
Using the infix to postfix code below:
A = 3; // your code should store A and 3 as a key-value pair
B = 4; // your code should store B and 4 as a key-value pair
A; // your code should now look-up A and then display its value
later we (you) will add the ability to write
Y = A * B + 3;
Y; // your code should show 16
Using this code
// infix.java
// converts infix arithmetic expressions to postfix
// to run this program: C>java InfixApp
import java.io.*; // for I/O
////////////////////////////////////////////////////////////////
class StackX {
private int maxSize;
private char[] stackArray;
private int top;
// --------------------------------------------------------------
public StackX(int s) // constructor
{
maxSize = s;
stackArray = new char[maxSize];
top = -1;
}
// --------------------------------------------------------------
public void push(char j) // put item on top of stack
{
stackArray[++top] = j;
}
// --------------------------------------------------------------
public char pop() // take item from top of stack
{
return stackArray[top--];
}
// --------------------------------------------------------------
public char peek() // peek at top of stack
{
return stackArray[top];
}
// --------------------------------------------------------------
public boolean isEmpty() // true if stack is empty
{
return (top == -1);
}
// -------------------------------------------------------------
public int size() // return size
{
return top + 1;
}
// --------------------------------------------------------------
public char peekN(int n) // return item at index n
{
return stackArray[n];
}
// --------------------------------------------------------------
public void displayStack(String s) {
System.out.print(s);
System.out.print("Stack (bottom-->top): ");
for (int j = 0; j < size(); j++) {
System.out.print(peekN(j));
System.out.print(' ');
}
System.out.println(" ");
}
// --------------------------------------------------------------
} // end class StackX
////////////////////////////////////////////////////////////////
class InToPost // infix to postfix conversion
{
private StackX theStack;
private String input;
private String output = "";
// --------------------------------------------------------------
public InToPost(String in) // constructor
{
input = in;
int stackSize = input.length();
theStack = new StackX(stackSize);
}
// --------------------------------------------------------------
public String doTrans() // do translation to postfix
{
for (int j = 0; j < input.length(); j++) {
char ch = input.charAt(j);
theStack.displayStack("For " + ch + " "); // *diagnostic*
switch (ch) {
case '+': // it’s + or -
case '-':
gotOper(ch, 1); // go pop operators
break; // (precedence 1)
case '*': // it’s * or /
case '/':
gotOper(ch, 2); // go pop operators
break; // (precedence 2)
case '(': // it’s a left paren
theStack.push(ch); // push it
break;
case ')': // it’s a right paren
gotParen(ch); // go pop operators
break;
default: // must be an operand
output = output + ch; // write it to output
break;
} // end switch
} // end for
while (!theStack.isEmpty()) // pop remaining opers
{
theStack.displayStack("While "); // *diagnostic*
output = output + theStack.pop(); // write to output
}
theStack.displayStack("End "); // *diagnostic*
return output; // return postfix
} // end doTrans()
// --------------------------------------------------------------
public void gotOper(char opThis, int prec1) { // got operator from input
while (!theStack.isEmpty()) {
char opTop = theStack.pop();
if (opTop == '(') // if it’s a ‘(‘
{
theStack.push(opTop); // restore ‘(‘
break;
} else // it’s an operator
{
int prec2; // precedence of new op
if (opTop == '+' || opTop == '-') // find new op prec
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1) // if prec of new op less
{ // than prec of old
theStack.push(opTop); // save newly-popped op
break;
} else // prec of new not less
output = output + opTop; // than prec of old
} // end else (it’s an operator)
} // end while
theStack.push(opThis); // push new operator
} // end gotOp()
// --------------------------------------------------------------
public void gotParen(char ch) { // got right paren from input
while (!theStack.isEmpty()) {
char chx = theStack.pop();
if (chx == '(') // if popped ‘
break; // we’re done
else // if popped operator
output = output + chx; // output it
} // end while
} // end popOps()
// --------------------------------------------------------------
} // end class InToPost
////////////////////////////////////////////////////////////////
class InfixApp {
public static void main(String[] args) throws IOException {
String input, output;
while (true) {
System.out.print("Enter infix: ");
System.out.flush();
input = getString(); // read a string from kbd
if (input.equals("")) // quit if [Enter]
break;
// make a translator
InToPost theTrans = new InToPost(input);
output = theTrans.doTrans(); // do the translation
System.out.println("Postfix is " + output + ' ');
} // end while
} // end main()
// --------------------------------------------------------------
public static String getString() throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
// --------------------------------------------------------------
} // end class InfixApp
////////////////////////////////////////////////////////////////
Solution
#include
char stack[20];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
}
main()
{
char exp[20];
char *e, x;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c",pop());
}
}

More Related Content

Similar to Data structuresUsing java language and develop a prot.pdf

Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdfarshiartpalace
 
I need help with implementing the priority queue data structure with a.docx
I need help with implementing the priority queue data structure with a.docxI need help with implementing the priority queue data structure with a.docx
I need help with implementing the priority queue data structure with a.docxhendriciraida
 
Write a simple program in C (which comiles in gcc a unix environment ).docx
Write a simple program in C (which comiles in gcc a unix environment ).docxWrite a simple program in C (which comiles in gcc a unix environment ).docx
Write a simple program in C (which comiles in gcc a unix environment ).docxnoreendchesterton753
 
Gift-VT Tools Development Overview
Gift-VT Tools Development OverviewGift-VT Tools Development Overview
Gift-VT Tools Development Overviewstn_tkiller
 
Introduction to Dart
Introduction to DartIntroduction to Dart
Introduction to DartRamesh Nair
 
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxcurwenmichaela
 
#include avrinterrupt.h The global interrupt flag is maintained.pdf
#include avrinterrupt.h The global interrupt flag is maintained.pdf#include avrinterrupt.h The global interrupt flag is maintained.pdf
#include avrinterrupt.h The global interrupt flag is maintained.pdfarasanlethers
 
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdfWrite message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdfeyeonsecuritysystems
 
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdf
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdfplease send edited code of RCBug.javaRCBUG.javaimport java.util..pdf
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdfFashionBoutiquedelhi
 
public void turnRight(double degrees) {rotationInDegrees + - = deg.pdf
public void turnRight(double degrees) {rotationInDegrees + - = deg.pdfpublic void turnRight(double degrees) {rotationInDegrees + - = deg.pdf
public void turnRight(double degrees) {rotationInDegrees + - = deg.pdfisenbergwarne4100
 
forwarder.java.txt java forwarder class waits for an in.docx
forwarder.java.txt java forwarder class waits for an in.docxforwarder.java.txt java forwarder class waits for an in.docx
forwarder.java.txt java forwarder class waits for an in.docxbudbarber38650
 
void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch);.pdf
void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch);.pdfvoid ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch);.pdf
void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch);.pdfarihantmobilepoint15
 
PQTimer.java A simple driver program to run timing t.docx
  PQTimer.java     A simple driver program to run timing t.docx  PQTimer.java     A simple driver program to run timing t.docx
PQTimer.java A simple driver program to run timing t.docxjoyjonna282
 
Lab08Lab08.cppLab08Lab08.cpp.docx
Lab08Lab08.cppLab08Lab08.cpp.docxLab08Lab08.cppLab08Lab08.cpp.docx
Lab08Lab08.cppLab08Lab08.cpp.docxDIPESH30
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionMohammad Imam Hossain
 
Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeCory Forsyth
 
start_printf: dev/ic/com.c comstart()
start_printf: dev/ic/com.c comstart()start_printf: dev/ic/com.c comstart()
start_printf: dev/ic/com.c comstart()Kiwamu Okabe
 

Similar to Data structuresUsing java language and develop a prot.pdf (20)

Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
 
I need help with implementing the priority queue data structure with a.docx
I need help with implementing the priority queue data structure with a.docxI need help with implementing the priority queue data structure with a.docx
I need help with implementing the priority queue data structure with a.docx
 
Write a simple program in C (which comiles in gcc a unix environment ).docx
Write a simple program in C (which comiles in gcc a unix environment ).docxWrite a simple program in C (which comiles in gcc a unix environment ).docx
Write a simple program in C (which comiles in gcc a unix environment ).docx
 
Gift-VT Tools Development Overview
Gift-VT Tools Development OverviewGift-VT Tools Development Overview
Gift-VT Tools Development Overview
 
Introduction to Dart
Introduction to DartIntroduction to Dart
Introduction to Dart
 
Stack prgs
Stack prgsStack prgs
Stack prgs
 
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
#include avrinterrupt.h The global interrupt flag is maintained.pdf
#include avrinterrupt.h The global interrupt flag is maintained.pdf#include avrinterrupt.h The global interrupt flag is maintained.pdf
#include avrinterrupt.h The global interrupt flag is maintained.pdf
 
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdfWrite message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf
 
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdf
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdfplease send edited code of RCBug.javaRCBUG.javaimport java.util..pdf
please send edited code of RCBug.javaRCBUG.javaimport java.util..pdf
 
public void turnRight(double degrees) {rotationInDegrees + - = deg.pdf
public void turnRight(double degrees) {rotationInDegrees + - = deg.pdfpublic void turnRight(double degrees) {rotationInDegrees + - = deg.pdf
public void turnRight(double degrees) {rotationInDegrees + - = deg.pdf
 
forwarder.java.txt java forwarder class waits for an in.docx
forwarder.java.txt java forwarder class waits for an in.docxforwarder.java.txt java forwarder class waits for an in.docx
forwarder.java.txt java forwarder class waits for an in.docx
 
void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch);.pdf
void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch);.pdfvoid ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch);.pdf
void ReplaceCharInBuffer(bufferADT buffer, char oldch, char newch);.pdf
 
PQTimer.java A simple driver program to run timing t.docx
  PQTimer.java     A simple driver program to run timing t.docx  PQTimer.java     A simple driver program to run timing t.docx
PQTimer.java A simple driver program to run timing t.docx
 
Lab08Lab08.cppLab08Lab08.cpp.docx
Lab08Lab08.cppLab08Lab08.cpp.docxLab08Lab08.cppLab08Lab08.cpp.docx
Lab08Lab08.cppLab08Lab08.cpp.docx
 
Sbaw091006
Sbaw091006Sbaw091006
Sbaw091006
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
 
Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to Come
 
start_printf: dev/ic/com.c comstart()
start_printf: dev/ic/com.c comstart()start_printf: dev/ic/com.c comstart()
start_printf: dev/ic/com.c comstart()
 

More from armyshoes

Foofy obtained a significant Fobt from an experiment with five level.pdf
Foofy obtained a significant Fobt from an experiment with five level.pdfFoofy obtained a significant Fobt from an experiment with five level.pdf
Foofy obtained a significant Fobt from an experiment with five level.pdfarmyshoes
 
Explain how water interacts with the following types of compounds to.pdf
Explain how water interacts with the following types of compounds to.pdfExplain how water interacts with the following types of compounds to.pdf
Explain how water interacts with the following types of compounds to.pdfarmyshoes
 
Explain how the financial reporting of gains and losses on assets.pdf
Explain how the financial reporting of gains and losses on assets.pdfExplain how the financial reporting of gains and losses on assets.pdf
Explain how the financial reporting of gains and losses on assets.pdfarmyshoes
 
Explain two ways in which microbiologists have overcome the difficul.pdf
Explain two ways in which microbiologists have overcome the difficul.pdfExplain two ways in which microbiologists have overcome the difficul.pdf
Explain two ways in which microbiologists have overcome the difficul.pdfarmyshoes
 
During the preparation of the bank reconciliation for Building Conce.pdf
During the preparation of the bank reconciliation for Building Conce.pdfDuring the preparation of the bank reconciliation for Building Conce.pdf
During the preparation of the bank reconciliation for Building Conce.pdfarmyshoes
 
C++ CodeConsider the LinkedList class and the Node class that we s.pdf
C++ CodeConsider the LinkedList class and the Node class that we s.pdfC++ CodeConsider the LinkedList class and the Node class that we s.pdf
C++ CodeConsider the LinkedList class and the Node class that we s.pdfarmyshoes
 
DNA Paternity Testing Simulation Post-lab QuestionsA)Why do differ.pdf
DNA Paternity Testing Simulation Post-lab QuestionsA)Why do differ.pdfDNA Paternity Testing Simulation Post-lab QuestionsA)Why do differ.pdf
DNA Paternity Testing Simulation Post-lab QuestionsA)Why do differ.pdfarmyshoes
 
Describe how the lung cancer travels to the first site of metastasis.pdf
Describe how the lung cancer travels to the first site of metastasis.pdfDescribe how the lung cancer travels to the first site of metastasis.pdf
Describe how the lung cancer travels to the first site of metastasis.pdfarmyshoes
 
Define the following engineering terms1. engineering ethics2. W.pdf
Define the following engineering terms1. engineering ethics2. W.pdfDefine the following engineering terms1. engineering ethics2. W.pdf
Define the following engineering terms1. engineering ethics2. W.pdfarmyshoes
 
You have been cut! While you wage war in the streets of Gotham for y.pdf
You have been cut! While you wage war in the streets of Gotham for y.pdfYou have been cut! While you wage war in the streets of Gotham for y.pdf
You have been cut! While you wage war in the streets of Gotham for y.pdfarmyshoes
 
Why has Mexico encountered such a significant collapse from an econom.pdf
Why has Mexico encountered such a significant collapse from an econom.pdfWhy has Mexico encountered such a significant collapse from an econom.pdf
Why has Mexico encountered such a significant collapse from an econom.pdfarmyshoes
 
Which of the following isare true of BOTH natural selection and gen.pdf
Which of the following isare true of BOTH natural selection and gen.pdfWhich of the following isare true of BOTH natural selection and gen.pdf
Which of the following isare true of BOTH natural selection and gen.pdfarmyshoes
 
Which of the following information-management systems uses artificia.pdf
Which of the following information-management systems uses artificia.pdfWhich of the following information-management systems uses artificia.pdf
Which of the following information-management systems uses artificia.pdfarmyshoes
 
What steps and tools are used for Pen TestingSolutionSteps fo.pdf
What steps and tools are used for Pen TestingSolutionSteps fo.pdfWhat steps and tools are used for Pen TestingSolutionSteps fo.pdf
What steps and tools are used for Pen TestingSolutionSteps fo.pdfarmyshoes
 
which event in this life cycle is comparable to fertilizarion .pdf
which event in this life cycle is comparable to fertilizarion .pdfwhich event in this life cycle is comparable to fertilizarion .pdf
which event in this life cycle is comparable to fertilizarion .pdfarmyshoes
 
what is the value of nursing educationSolutionThe main value o.pdf
what is the value of nursing educationSolutionThe main value o.pdfwhat is the value of nursing educationSolutionThe main value o.pdf
what is the value of nursing educationSolutionThe main value o.pdfarmyshoes
 
What is the exact output of the following code#include stdio.h.pdf
What is the exact output of the following code#include stdio.h.pdfWhat is the exact output of the following code#include stdio.h.pdf
What is the exact output of the following code#include stdio.h.pdfarmyshoes
 
Using a Fabry-Perot interferometer, we can Observe small objects. D.pdf
Using a Fabry-Perot interferometer, we can  Observe small objects.  D.pdfUsing a Fabry-Perot interferometer, we can  Observe small objects.  D.pdf
Using a Fabry-Perot interferometer, we can Observe small objects. D.pdfarmyshoes
 
TRUE of falseA researcher found 95 confident interval for the aver.pdf
TRUE of falseA researcher found 95 confident interval for the aver.pdfTRUE of falseA researcher found 95 confident interval for the aver.pdf
TRUE of falseA researcher found 95 confident interval for the aver.pdfarmyshoes
 
The major type of interactive forces between molecules of HCl are i.pdf
The major type of interactive forces between molecules of HCl are  i.pdfThe major type of interactive forces between molecules of HCl are  i.pdf
The major type of interactive forces between molecules of HCl are i.pdfarmyshoes
 

More from armyshoes (20)

Foofy obtained a significant Fobt from an experiment with five level.pdf
Foofy obtained a significant Fobt from an experiment with five level.pdfFoofy obtained a significant Fobt from an experiment with five level.pdf
Foofy obtained a significant Fobt from an experiment with five level.pdf
 
Explain how water interacts with the following types of compounds to.pdf
Explain how water interacts with the following types of compounds to.pdfExplain how water interacts with the following types of compounds to.pdf
Explain how water interacts with the following types of compounds to.pdf
 
Explain how the financial reporting of gains and losses on assets.pdf
Explain how the financial reporting of gains and losses on assets.pdfExplain how the financial reporting of gains and losses on assets.pdf
Explain how the financial reporting of gains and losses on assets.pdf
 
Explain two ways in which microbiologists have overcome the difficul.pdf
Explain two ways in which microbiologists have overcome the difficul.pdfExplain two ways in which microbiologists have overcome the difficul.pdf
Explain two ways in which microbiologists have overcome the difficul.pdf
 
During the preparation of the bank reconciliation for Building Conce.pdf
During the preparation of the bank reconciliation for Building Conce.pdfDuring the preparation of the bank reconciliation for Building Conce.pdf
During the preparation of the bank reconciliation for Building Conce.pdf
 
C++ CodeConsider the LinkedList class and the Node class that we s.pdf
C++ CodeConsider the LinkedList class and the Node class that we s.pdfC++ CodeConsider the LinkedList class and the Node class that we s.pdf
C++ CodeConsider the LinkedList class and the Node class that we s.pdf
 
DNA Paternity Testing Simulation Post-lab QuestionsA)Why do differ.pdf
DNA Paternity Testing Simulation Post-lab QuestionsA)Why do differ.pdfDNA Paternity Testing Simulation Post-lab QuestionsA)Why do differ.pdf
DNA Paternity Testing Simulation Post-lab QuestionsA)Why do differ.pdf
 
Describe how the lung cancer travels to the first site of metastasis.pdf
Describe how the lung cancer travels to the first site of metastasis.pdfDescribe how the lung cancer travels to the first site of metastasis.pdf
Describe how the lung cancer travels to the first site of metastasis.pdf
 
Define the following engineering terms1. engineering ethics2. W.pdf
Define the following engineering terms1. engineering ethics2. W.pdfDefine the following engineering terms1. engineering ethics2. W.pdf
Define the following engineering terms1. engineering ethics2. W.pdf
 
You have been cut! While you wage war in the streets of Gotham for y.pdf
You have been cut! While you wage war in the streets of Gotham for y.pdfYou have been cut! While you wage war in the streets of Gotham for y.pdf
You have been cut! While you wage war in the streets of Gotham for y.pdf
 
Why has Mexico encountered such a significant collapse from an econom.pdf
Why has Mexico encountered such a significant collapse from an econom.pdfWhy has Mexico encountered such a significant collapse from an econom.pdf
Why has Mexico encountered such a significant collapse from an econom.pdf
 
Which of the following isare true of BOTH natural selection and gen.pdf
Which of the following isare true of BOTH natural selection and gen.pdfWhich of the following isare true of BOTH natural selection and gen.pdf
Which of the following isare true of BOTH natural selection and gen.pdf
 
Which of the following information-management systems uses artificia.pdf
Which of the following information-management systems uses artificia.pdfWhich of the following information-management systems uses artificia.pdf
Which of the following information-management systems uses artificia.pdf
 
What steps and tools are used for Pen TestingSolutionSteps fo.pdf
What steps and tools are used for Pen TestingSolutionSteps fo.pdfWhat steps and tools are used for Pen TestingSolutionSteps fo.pdf
What steps and tools are used for Pen TestingSolutionSteps fo.pdf
 
which event in this life cycle is comparable to fertilizarion .pdf
which event in this life cycle is comparable to fertilizarion .pdfwhich event in this life cycle is comparable to fertilizarion .pdf
which event in this life cycle is comparable to fertilizarion .pdf
 
what is the value of nursing educationSolutionThe main value o.pdf
what is the value of nursing educationSolutionThe main value o.pdfwhat is the value of nursing educationSolutionThe main value o.pdf
what is the value of nursing educationSolutionThe main value o.pdf
 
What is the exact output of the following code#include stdio.h.pdf
What is the exact output of the following code#include stdio.h.pdfWhat is the exact output of the following code#include stdio.h.pdf
What is the exact output of the following code#include stdio.h.pdf
 
Using a Fabry-Perot interferometer, we can Observe small objects. D.pdf
Using a Fabry-Perot interferometer, we can  Observe small objects.  D.pdfUsing a Fabry-Perot interferometer, we can  Observe small objects.  D.pdf
Using a Fabry-Perot interferometer, we can Observe small objects. D.pdf
 
TRUE of falseA researcher found 95 confident interval for the aver.pdf
TRUE of falseA researcher found 95 confident interval for the aver.pdfTRUE of falseA researcher found 95 confident interval for the aver.pdf
TRUE of falseA researcher found 95 confident interval for the aver.pdf
 
The major type of interactive forces between molecules of HCl are i.pdf
The major type of interactive forces between molecules of HCl are  i.pdfThe major type of interactive forces between molecules of HCl are  i.pdf
The major type of interactive forces between molecules of HCl are i.pdf
 

Recently uploaded

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 

Recently uploaded (20)

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 

Data structuresUsing java language and develop a prot.pdf

  • 1. Data structures: ****Using java language and develop a prototype for a demo program that reads in strings that look like: Using the infix to postfix code below: A = 3; // your code should store A and 3 as a key-value pair B = 4; // your code should store B and 4 as a key-value pair A; // your code should now look-up A and then display its value later we (you) will add the ability to write Y = A * B + 3; Y; // your code should show 16 Using this code
  • 2. // infix.java // converts infix arithmetic expressions to postfix // to run this program: C>java InfixApp import java.io.*; // for I/O //////////////////////////////////////////////////////////////// class StackX { private int maxSize; private char[] stackArray; private int top; // -------------------------------------------------------------- public StackX(int s) // constructor { maxSize = s; stackArray = new char[maxSize]; top = -1; } // -------------------------------------------------------------- public void push(char j) // put item on top of stack { stackArray[++top] = j; } // -------------------------------------------------------------- public char pop() // take item from top of stack { return stackArray[top--]; }
  • 3. // -------------------------------------------------------------- public char peek() // peek at top of stack { return stackArray[top]; } // -------------------------------------------------------------- public boolean isEmpty() // true if stack is empty { return (top == -1); } // ------------------------------------------------------------- public int size() // return size { return top + 1; } // -------------------------------------------------------------- public char peekN(int n) // return item at index n { return stackArray[n]; } // -------------------------------------------------------------- public void displayStack(String s) { System.out.print(s); System.out.print("Stack (bottom-->top): "); for (int j = 0; j < size(); j++) { System.out.print(peekN(j));
  • 4. System.out.print(' '); } System.out.println(" "); } // -------------------------------------------------------------- } // end class StackX //////////////////////////////////////////////////////////////// class InToPost // infix to postfix conversion { private StackX theStack; private String input; private String output = ""; // -------------------------------------------------------------- public InToPost(String in) // constructor { input = in; int stackSize = input.length(); theStack = new StackX(stackSize); } // -------------------------------------------------------------- public String doTrans() // do translation to postfix { for (int j = 0; j < input.length(); j++) { char ch = input.charAt(j); theStack.displayStack("For " + ch + " "); // *diagnostic* switch (ch) { case '+': // it’s + or - case '-': gotOper(ch, 1); // go pop operators break; // (precedence 1)
  • 5. case '*': // it’s * or / case '/': gotOper(ch, 2); // go pop operators break; // (precedence 2) case '(': // it’s a left paren theStack.push(ch); // push it break; case ')': // it’s a right paren gotParen(ch); // go pop operators break; default: // must be an operand output = output + ch; // write it to output break; } // end switch } // end for while (!theStack.isEmpty()) // pop remaining opers { theStack.displayStack("While "); // *diagnostic* output = output + theStack.pop(); // write to output } theStack.displayStack("End "); // *diagnostic* return output; // return postfix } // end doTrans() // -------------------------------------------------------------- public void gotOper(char opThis, int prec1) { // got operator from input while (!theStack.isEmpty()) { char opTop = theStack.pop(); if (opTop == '(') // if it’s a ‘(‘ { theStack.push(opTop); // restore ‘(‘ break; } else // it’s an operator { int prec2; // precedence of new op
  • 6. if (opTop == '+' || opTop == '-') // find new op prec prec2 = 1; else prec2 = 2; if (prec2 < prec1) // if prec of new op less { // than prec of old theStack.push(opTop); // save newly-popped op break; } else // prec of new not less output = output + opTop; // than prec of old } // end else (it’s an operator) } // end while theStack.push(opThis); // push new operator } // end gotOp() // -------------------------------------------------------------- public void gotParen(char ch) { // got right paren from input while (!theStack.isEmpty()) { char chx = theStack.pop(); if (chx == '(') // if popped ‘ break; // we’re done else // if popped operator output = output + chx; // output it } // end while } // end popOps() // -------------------------------------------------------------- } // end class InToPost //////////////////////////////////////////////////////////////// class InfixApp { public static void main(String[] args) throws IOException { String input, output; while (true) { System.out.print("Enter infix: ");
  • 7. System.out.flush(); input = getString(); // read a string from kbd if (input.equals("")) // quit if [Enter] break; // make a translator InToPost theTrans = new InToPost(input); output = theTrans.doTrans(); // do the translation System.out.println("Postfix is " + output + ' '); } // end while } // end main() // -------------------------------------------------------------- public static String getString() throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } // -------------------------------------------------------------- } // end class InfixApp //////////////////////////////////////////////////////////////// Data structures: ****Using java language and develop a prototype for a demo program that reads in strings that look like: Using the infix to postfix code below: A = 3; // your code should store A and 3 as a key-value pair
  • 8. B = 4; // your code should store B and 4 as a key-value pair A; // your code should now look-up A and then display its value later we (you) will add the ability to write Y = A * B + 3; Y; // your code should show 16 Using this code // infix.java // converts infix arithmetic expressions to postfix // to run this program: C>java InfixApp import java.io.*; // for I/O //////////////////////////////////////////////////////////////// class StackX { private int maxSize; private char[] stackArray; private int top; // --------------------------------------------------------------
  • 9. public StackX(int s) // constructor { maxSize = s; stackArray = new char[maxSize]; top = -1; } // -------------------------------------------------------------- public void push(char j) // put item on top of stack { stackArray[++top] = j; } // -------------------------------------------------------------- public char pop() // take item from top of stack { return stackArray[top--]; } // -------------------------------------------------------------- public char peek() // peek at top of stack { return stackArray[top]; } // -------------------------------------------------------------- public boolean isEmpty() // true if stack is empty { return (top == -1); }
  • 10. // ------------------------------------------------------------- public int size() // return size { return top + 1; } // -------------------------------------------------------------- public char peekN(int n) // return item at index n { return stackArray[n]; } // -------------------------------------------------------------- public void displayStack(String s) { System.out.print(s); System.out.print("Stack (bottom-->top): "); for (int j = 0; j < size(); j++) { System.out.print(peekN(j)); System.out.print(' '); } System.out.println(" "); } // -------------------------------------------------------------- } // end class StackX //////////////////////////////////////////////////////////////// class InToPost // infix to postfix conversion { private StackX theStack; private String input; private String output = "";
  • 11. // -------------------------------------------------------------- public InToPost(String in) // constructor { input = in; int stackSize = input.length(); theStack = new StackX(stackSize); } // -------------------------------------------------------------- public String doTrans() // do translation to postfix { for (int j = 0; j < input.length(); j++) { char ch = input.charAt(j); theStack.displayStack("For " + ch + " "); // *diagnostic* switch (ch) { case '+': // it’s + or - case '-': gotOper(ch, 1); // go pop operators break; // (precedence 1) case '*': // it’s * or / case '/': gotOper(ch, 2); // go pop operators break; // (precedence 2) case '(': // it’s a left paren theStack.push(ch); // push it break; case ')': // it’s a right paren gotParen(ch); // go pop operators break; default: // must be an operand output = output + ch; // write it to output break; } // end switch
  • 12. } // end for while (!theStack.isEmpty()) // pop remaining opers { theStack.displayStack("While "); // *diagnostic* output = output + theStack.pop(); // write to output } theStack.displayStack("End "); // *diagnostic* return output; // return postfix } // end doTrans() // -------------------------------------------------------------- public void gotOper(char opThis, int prec1) { // got operator from input while (!theStack.isEmpty()) { char opTop = theStack.pop(); if (opTop == '(') // if it’s a ‘(‘ { theStack.push(opTop); // restore ‘(‘ break; } else // it’s an operator { int prec2; // precedence of new op if (opTop == '+' || opTop == '-') // find new op prec prec2 = 1; else prec2 = 2; if (prec2 < prec1) // if prec of new op less { // than prec of old theStack.push(opTop); // save newly-popped op break; } else // prec of new not less output = output + opTop; // than prec of old } // end else (it’s an operator) } // end while theStack.push(opThis); // push new operator } // end gotOp()
  • 13. // -------------------------------------------------------------- public void gotParen(char ch) { // got right paren from input while (!theStack.isEmpty()) { char chx = theStack.pop(); if (chx == '(') // if popped ‘ break; // we’re done else // if popped operator output = output + chx; // output it } // end while } // end popOps() // -------------------------------------------------------------- } // end class InToPost //////////////////////////////////////////////////////////////// class InfixApp { public static void main(String[] args) throws IOException { String input, output; while (true) { System.out.print("Enter infix: "); System.out.flush(); input = getString(); // read a string from kbd if (input.equals("")) // quit if [Enter] break; // make a translator InToPost theTrans = new InToPost(input); output = theTrans.doTrans(); // do the translation System.out.println("Postfix is " + output + ' '); } // end while } // end main() // -------------------------------------------------------------- public static String getString() throws IOException {
  • 14. InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } // -------------------------------------------------------------- } // end class InfixApp //////////////////////////////////////////////////////////////// Data structures: ****Using java language and develop a prototype for a demo program that reads in strings that look like: Using the infix to postfix code below: A = 3; // your code should store A and 3 as a key-value pair B = 4; // your code should store B and 4 as a key-value pair A; // your code should now look-up A and then display its value later we (you) will add the ability to write Y = A * B + 3; Y; // your code should show 16
  • 15. Using this code // infix.java // converts infix arithmetic expressions to postfix // to run this program: C>java InfixApp import java.io.*; // for I/O //////////////////////////////////////////////////////////////// class StackX { private int maxSize; private char[] stackArray; private int top; // -------------------------------------------------------------- public StackX(int s) // constructor { maxSize = s; stackArray = new char[maxSize]; top = -1; } // -------------------------------------------------------------- public void push(char j) // put item on top of stack { stackArray[++top] = j; }
  • 16. // -------------------------------------------------------------- public char pop() // take item from top of stack { return stackArray[top--]; } // -------------------------------------------------------------- public char peek() // peek at top of stack { return stackArray[top]; } // -------------------------------------------------------------- public boolean isEmpty() // true if stack is empty { return (top == -1); } // ------------------------------------------------------------- public int size() // return size { return top + 1; } // -------------------------------------------------------------- public char peekN(int n) // return item at index n { return stackArray[n]; }
  • 17. // -------------------------------------------------------------- public void displayStack(String s) { System.out.print(s); System.out.print("Stack (bottom-->top): "); for (int j = 0; j < size(); j++) { System.out.print(peekN(j)); System.out.print(' '); } System.out.println(" "); } // -------------------------------------------------------------- } // end class StackX //////////////////////////////////////////////////////////////// class InToPost // infix to postfix conversion { private StackX theStack; private String input; private String output = ""; // -------------------------------------------------------------- public InToPost(String in) // constructor { input = in; int stackSize = input.length(); theStack = new StackX(stackSize); } // -------------------------------------------------------------- public String doTrans() // do translation to postfix { for (int j = 0; j < input.length(); j++) {
  • 18. char ch = input.charAt(j); theStack.displayStack("For " + ch + " "); // *diagnostic* switch (ch) { case '+': // it’s + or - case '-': gotOper(ch, 1); // go pop operators break; // (precedence 1) case '*': // it’s * or / case '/': gotOper(ch, 2); // go pop operators break; // (precedence 2) case '(': // it’s a left paren theStack.push(ch); // push it break; case ')': // it’s a right paren gotParen(ch); // go pop operators break; default: // must be an operand output = output + ch; // write it to output break; } // end switch } // end for while (!theStack.isEmpty()) // pop remaining opers { theStack.displayStack("While "); // *diagnostic* output = output + theStack.pop(); // write to output } theStack.displayStack("End "); // *diagnostic* return output; // return postfix } // end doTrans() // -------------------------------------------------------------- public void gotOper(char opThis, int prec1) { // got operator from input while (!theStack.isEmpty()) { char opTop = theStack.pop();
  • 19. if (opTop == '(') // if it’s a ‘(‘ { theStack.push(opTop); // restore ‘(‘ break; } else // it’s an operator { int prec2; // precedence of new op if (opTop == '+' || opTop == '-') // find new op prec prec2 = 1; else prec2 = 2; if (prec2 < prec1) // if prec of new op less { // than prec of old theStack.push(opTop); // save newly-popped op break; } else // prec of new not less output = output + opTop; // than prec of old } // end else (it’s an operator) } // end while theStack.push(opThis); // push new operator } // end gotOp() // -------------------------------------------------------------- public void gotParen(char ch) { // got right paren from input while (!theStack.isEmpty()) { char chx = theStack.pop(); if (chx == '(') // if popped ‘ break; // we’re done else // if popped operator output = output + chx; // output it } // end while } // end popOps() // -------------------------------------------------------------- } // end class InToPost ////////////////////////////////////////////////////////////////
  • 20. class InfixApp { public static void main(String[] args) throws IOException { String input, output; while (true) { System.out.print("Enter infix: "); System.out.flush(); input = getString(); // read a string from kbd if (input.equals("")) // quit if [Enter] break; // make a translator InToPost theTrans = new InToPost(input); output = theTrans.doTrans(); // do the translation System.out.println("Postfix is " + output + ' '); } // end while } // end main() // -------------------------------------------------------------- public static String getString() throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } // -------------------------------------------------------------- } // end class InfixApp //////////////////////////////////////////////////////////////// Solution #include char stack[20];
  • 21. int top = -1; void push(char x) { stack[++top] = x; } char pop() { if(top == -1) return -1; else return stack[top--]; } int priority(char x) { if(x == '(') return 0; if(x == '+' || x == '-') return 1; if(x == '*' || x == '/') return 2; } main() { char exp[20]; char *e, x; printf("Enter the expression :: "); scanf("%s",exp); e = exp; while(*e != '0') { if(isalnum(*e)) printf("%c",*e); else if(*e == '(') push(*e); else if(*e == ')') {
  • 22. while((x = pop()) != '(') printf("%c", x); } else { while(priority(stack[top]) >= priority(*e)) printf("%c",pop()); push(*e); } e++; } while(top != -1) { printf("%c",pop()); } }