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 =...
#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 =...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y);...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y); int main(){ char s1[] = "zoey"; char s2[] = "zoey"; string s3 = s1; string s4 = s2; cout << "1. sizeof(s1) = " << sizeof(s1) << endl; if(s1 == s2) cout << "2. s1 == s2 is true" << endl; else cout << "2. s1 == s2 is false" << endl; if(s3 == s4) cout << "3. s3 == s4 is true" <<...
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; }...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char**...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char** argv) { int number, sum, count; // Write a while loop that reads a number from the user and loop // until the number is divisible by 7 cout << "What is the number? "; cin >> number; while ( ... ) { ... } cout << number << " is divisible by 7!! << endl << endl; // Write a for loop that...
#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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT