Question

In: Computer Science

INFIX2POSTFIX.CPP #include "stack.hpp" using namespace std; // Auxiliary method, you probably find it useful // Operands...

INFIX2POSTFIX.CPP

#include "stack.hpp"

using namespace std;

// Auxiliary method, you probably find it useful

// Operands are all lower case and upper case characters

bool isOperand(char c){

  return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');

}

// Auxiliary method, you probably find it useful

int precedence(char c)

{

  if(c == '+' || c == '-'){

    return 0;

  }

  if(c == '*' || c == '/'){

    return 1;

  }

  if(c == '^'){

    return 2;

  }

  return -1;

}

int main(){

  freopen("input_infix2postfix.txt", "r", stdin);

  string input;

  string solution;

  int line_counter = 0;

  while(cin >> solution){

    cin >> input;

    Stack<char> stack;

    string result;

     //The input file is in the format "expected_solution infix_expression",

     //where expected_solution is the infix_expression in postfix format

    for(int i=0; i<input.length(); ++i){

      // WRITE CODE HERE to store in 'result' the postfix transformation of 'input'

    }

    

    // You need to do some extra stuff here to store in 'result' the postfix transformation of 'input'

    

    // Checking whether the result you got is correct

    if(solution == result){

      cout << "line " << line_counter << ": OK [" << solution << " " << result << "]" << endl;

    }else{

      cout << "line " << line_counter << ": ERROR [" << solution << " " << result << "]" << endl;

    }

    line_counter++;

  }

}

Implement a stack and solutions to the following problems: balancing parenthesis, evaluating postfix expressions and transforming infix expressions into postfix expressions.

infix2postfix.cpp

- create main method to transform an infix expression into postfix

Solutions

Expert Solution

 INFIX2POSTFIX.CPP #include "stack.hpp" using namespace std; // Auxiliary method, you probably find it useful // Operands are all lower case and upper case characters bool isOperand(char c) {     return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } // Auxiliary method, you probably find it useful int precedence(char c) {     if(c == '+' || c == '-') {     return 0;   }   if(c == '*' || c == '/') {     return 1;   }   if(c == '^') {     return 2;   }   return -1; } int main() {   freopen("input_infix2postfix.txt", "r", stdin);   string input;    string output; //output string   string solution;   string dummy;   string here;     int j = 0;     int con;   int line_counter = 0;   while(cin >> solution) {     cin >> input;     Stack<char> stack;     string result;   //The input file is in the format "expected_solution infix_expression",    //where expected_solution is the infix_expression in postfix format int x = 0; for(int i=0; i<input.length(); ++i) {   if (isOperand(input[i]) ) {     output[j++] = input[i];         cout << "output: " << output[i] << endl;     } else if (input[i] == '(' )  {     stack.push(input[i]); }     else if (input[i] == ')' ) {     while (stack.peek() != '(' )    {         output[j++] = stack.peek();         stack.pop();     }     stack.pop();         } else if (input[i] == '*' || input[i] == '/' || input[i] == '+' || input[i] == '-' || input[i] == '^') {                     stack.push(input[i]);     // cout << "input: " << stack.peek() << endl;         if (precedence(stack.peek()) >= precedence(input[i]) )         {                 output[j++] = stack.peek();              stack.pop();              }        else              { break; } }             } 

Related Solutions

--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std; float series(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum += r[i];    return sum; } float parallel(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum = sum + (1 / r[i]);...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; //...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; // Structure for storing the process data struct procData { char pname[5]; // Name of a process int arrivt; //Arrival time of a process int pburst; // Burst time of a process int endtime; // Exit time/ Leaving time of a process int remburst; // Remaining burst time of a process int readyFlag; // boolean, Flag for maintaining the process status }; // Global variable...
using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...
using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std; "Write a program that asks the user to enter an odd positive integer. The program reads a value (n) entered by the user and prints an n x n grid displaying a large letter X. The left half should be made up of pluses (+) and the right half should be made with the character "x" and the very center should be an asteric...
Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...
Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std; "Write a program that asks the user to enter an integer between 1 and 20. If the user enters an illegal number, the program repeatedly asks the user to enter the correct one. If the user has not entered a correct number after 10 attempts, the program chooses the number 10 as the user's number. The program prints the cube of the user's number.
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int SIZE = 20;     char str[SIZE];     char str1[SIZE];     int n;     int k =1;        printf("Enter a word: \n");     fgets(str,SIZE,stdin);     printf("Enter another word: \n");     fgets(str1,SIZE,stdin);        if (str1[strlen(str1) - 1] == '\n')     {         str1[strlen(str1)-1] = '\0';     }     if (str[strlen(str) - 1] == '\n')     {         str[strlen(str)-1] = '\0';     }      ...
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string...
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string itemname;    int id;    string itemcolor;    double cost; }; void fillProduct(Product[10], int& );//read data from a file and store in an array void writeProduct(Product[10], int);//write the array into a file void writeBinary(Product[10], int);//write the array into a file in binary mode void printbinary(Product[10], int);//read data from the binary file and print int main() {    Product myList[10];    int numItems = 0;...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool a = true; bool b= true;    //Print the Conjunction function cout<<"\n\nConjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(P∧Q)" <<endl; cout<< a <<"\t"<< b <<"\t"<< conjunction(a,b) <<endl; cout<< a <<"\t"<< !b <<"\t"<< conjunction(a,!b) <<endl; cout<< !a <<"\t"<< b <<"\t"<< conjunction(!a,b) <<endl; cout<< !a <<"\t"<< !b <<"\t"<< conjunction(!a,!b)<<endl;    //Print the Disjunction function cout<<"\n\nDisjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(PVQ)" <<endl; cout<< a <<"\t"<< b <<"\t"<< disjunction(a,b) <<endl;...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool a = true; bool b= true;    //Print the Conjunction function cout<<"\n\nConjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(P∧Q)" <<endl; cout<< a <<"\t"<< b <<"\t"<< conjunction(a,b) <<endl; cout<< a <<"\t"<< !b <<"\t"<< conjunction(a,!b) <<endl; cout<< !a <<"\t"<< b <<"\t"<< conjunction(!a,b) <<endl; cout<< !a <<"\t"<< !b <<"\t"<< conjunction(!a,!b)<<endl;    //Print the Disjunction function cout<<"\n\nDisjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(PVQ)" <<endl; cout<< a <<"\t"<< b <<"\t"<< disjunction(a,b) <<endl;...
Use while loop for the num inputs #include #include using namespace std; int main() {   ...
Use while loop for the num inputs #include #include using namespace std; int main() {    long long int digit;    long long int num1, num2, num3, num4, num5;    int ave;       cout << "Enter a 10 digit number: ";    cin >> digit;       num5 = digit %100;    digit = digit / 100;       num4 = digit %100;    digit = digit / 100;    num3 = digit %100;    digit = digit /...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT