Question

In: Computer Science

#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0;...

#include <iostream>
#include <stack>
#include <queue>
using namespace std;


void printFromStack(string expr){
    stack<char> myStack;
    for(int i=0; i<expr.length(); i++){
        //Insert code here to push each character onto the stack
    }
    
    cout << "My stack is popped in this order" << endl;
    while(!myStack.empty()){
        //Insert code here to cout the top of the stack one by one
        //Pop each one after it’s printed out
        
    }
    cout << endl;
    
}


void printFromQueue(string expr){
    queue<char> myQueue;
    
        //Insert code here to push each character onto the queue
    
    cout << "My queue is dequeued in this order" << endl;
    
    //Insert code here to cout the front of the queue one by one
    //Pop (or dequeue) each one after it’s printed out
    
    cout << endl;
    
}

bool isPalindrome(string expr){
    queue<char> myQueue;
    stack<char> myStack;
    bool isPalindrome = true;
    
        //Complete your code here!
    
    while (isPalindrome==true && (!myStack.empty() && !myQueue.empty()))
    {
        
        //Complete your code here!
        
    }
    return isPalindrome;
}

int main(int argc, const char * argv[])
{
    string expression;
    while (expression != "end"){
        cout << "Please enter an expression that you want to store: " << endl;
        cin >> expression;
        
        printFromStack(expression);
        
        printFromQueue(expression);
        
        
        cout << "Your expression: " << expression << " is " << (isPalindrome(expression)? "a palindrome!": "not a palindrome!")  << endl;

        
        
    }
    return 0;
}

the question is inside of coding. needed to complete in c++ form

Solutions

Expert Solution

#include <iostream>

#include <stack>

#include <queue>

using namespace std;

void printFromStack(string expr)

{

    // create a queue

    stack<char> myStack;

    int i;

    // raverse the string

    for( i = 0 ; i < expr.length() ; i++ )

        // push the character into stack

        myStack.push(expr[i]);

    cout<<"My stack is popped in this order"<<endl;

    // loop untill the qaueue is empty

    while( !myStack.empty() )

    {

        // display the element in the top

        cout<<myStack.top()<<" ";

       // pop the element

        myStack.pop();

    }

    cout<<endl;

}

void printFromQueue(string expr)

{

    // create a queue

    queue<char> myQueue;

    int i;

    // raverse the string

    for( i = 0 ; i < expr.size() ; i++ )

        // push the character into queue

        myQueue.push(expr[i]);

    cout<<"My queue is dequeued in this order"<<endl;

    // loop untill the queue is empty

    while( !myQueue.empty() )

    {

        // display the element in the front

        cout<<myQueue.front()<<" ";

        // pop the element

        myQueue.pop();

    }

    cout<<endl;

}

bool isPalindrome(string expr)

{

    queue<char> myQueue;

    stack<char> myStack;

    bool isPalindrome = true;

    int i;

    // raverse the string

    for( i = 0 ; i < expr.size() ; i++ )

        // push the character into queue

        myQueue.push(expr[i]);

    // raverse the string

    for( i = 0 ; i < expr.length() ; i++ )

        // push the character into stack

        myStack.push(expr[i]);

    while( isPalindrome == true && ( !myStack.empty() && !myQueue.empty() ) )

    {

        // if the elements are not same

        if( myStack.top() != myQueue.front() )

        {

            isPalindrome = false;

        }

       

        myStack.pop();

        myQueue.pop();

    }

    return isPalindrome;

}

int main(int argc, const char * argv[])

{

    string expression;

    while (expression != "end")

    {

        cout << "Please enter an expression that you want to store: " << endl;

        cin >> expression;

       

        printFromStack(expression);

       

        printFromQueue(expression);

       

        cout << "Your expression: " << expression << " is " << (isPalindrome(expression)? "a palindrome!": "not a palindrome!") << endl;

    }

    return 0;

}


Sample output


Related Solutions

#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start,...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat = 'y'; for (;repeat == 'y';){ char emplyeename[35]; float basic_Salary,EPF, Dearness_Allow, tax, Net_Salary , emplyee_id; cout << "Enter Basic Salary : "; cin >> basic_Salary; Dearness_Allow = 0.40 * basic_Salary; switch (01) {case 1: if (basic_Salary <= 2,20,00) EPF = 0; case 2: if (basic_Salary > 28000 && basic_Salary <= 60000) EPF = 0.08*basic_Salary; case 3: if (basic_Salary > 60000 && basic_Salary <= 200000)...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4]...
#include <iostream> using namespace std; void count( int begin[], int end[] ) { int index, current[4] = { begin[0], begin[1], begin[2], begin[3] }; add: goto print; carry: if ( current[index] < end[index] - 1 ) { current[index]++; goto add; } else if ( index > 0 ) { current[index] = begin[index]; index--; goto carry; } else return; print: cout << current[0] << current[1] << current[2] << current[3] << endl; index = 3; goto carry; } int main( ) { int...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function prototype int main() { int num; for (num = 0; num < 10; num++) showDouble(num); return 0; } // Definition of function showDouble void showDouble(int value) { cout << value << ‘\t’ << (value * 2) << endl; } Please do the following Program and hand in the code and sample runs. Write a program using the following function prototypes: double getLength(); double getWidth();...
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput);...
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput); // Declaring base int N = 30; if (userInput.length() > 10) { cout << 0 << endl; } else { int finalTotal = 0; //Iterates through userInput for(int i = 0; i < 10; i++){ char convertedInput = userInput[i]; // ASCII decimal value of each character int asciiDec = int(convertedInput); //Casts char value from input to int value stringstream chr; chr << convertedInput; int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT