Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Machine Problem 4 - Stacks [1] A stack can be used to recognize certain types of

ID: 3783301 • Letter: M

Question

Machine Problem 4 - Stacks

[1] A stack can be used to recognize certain types of patterns. Consider the pattern STRING1#STRING2, where neither string contains "#". The STRING2 must be the reverse of STRING1. Write a program that reads strings until the user enters an empty string. The program should indicate whether each string matches the pattern.

Run:

Input a string: 1&A#A&1
1&A#A&1 matches the pattern

Input a string: 1&A#1&A
1&A#1&A does not match the pattern

Input a string: madamimadam#madamimadam
madamimadam#madamimadam matches the pattern

Input a string:

[2] Write a program that prompts the user to enter a non-negative decimal number and a base in the range 2 <= base <= 16. Write a function multibaseOutput() that displays the number in the specified base. The program terminates when the user enters a number of 0 and a base 0.

Run:
Enter a non-negative decimal number and base (2 <= B <= 16) or 0 0 to terminate: 155 16
    155 base 16 is 9B
Enter a non-negative decimal number and base (2 <= B <= 16) or 0 0 to terminate: 3553 8
    3553 base 8 is 6741
Enter a non-negative decimal number and base (2 <= B <= 16) or 0 0 to terminate: 0 0

[3] The program prompts for a filename and then reads the file to check for balanced curly braces, {}; parentheses, (); and square brackets, []. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that the proper nesting is required.

Assume File "balance1.txt" has
((a+b))[{{c}}](){([])} * c[i]
(welcome to C++)
{while (i = 5) ;}
[z]
Run 1:
Enter the file name: balance1.txt
The symbols in balance1.txt are balanced.

Assume File "balance2.txt" has [a(b]c)
Run 2:
Enter the file name: balance2.txt
The symbols in balance2.txt are not balanced.

Please upload the following:

The class .cpp file

The main program

The class .h file

Output File

Explanation / Answer

// Iterative C++ program to check if a string is subsequence of another string
#include<iostream>
#include<cstring>
using namespace std;

// Returns true if str1[] is a subsequence of str2[]. m is
// length of str1 and n is length of str2
bool isSubSequence(char str1[], char str2[], int m, int n)
{
int j = 0; // For index of str1 (or subsequence

// Traverse str2 and str1, and compare current character
// of str2 with first unmatched char of str1, if matched
// then move ahead in str1
for (int i=0; i<n&&j<m; i++)
if (str1[j] == str2[i])
j++;

// If all characters of str1 were found in str2
return (j==m);
}

// Driver program to test methods of graph class
int main()
{
char str1[] = "madamimadam";
char str2[] = "madamimadam";
int m = strlen(str1);
int n = strlen(str2);
isSubSequence(str1, str2, m, n)? cout << "Yes ":
cout << "No";
return 0;
}

2)

3)

#include<stdio.h>

#include<stdlib.h>

#define bool int

/* structure of a stack node */

struct sNode

{

   char data;

   struct sNode *next;

};

/* Function to push an item to stack*/

void push(struct sNode** top_ref, int new_data);

/* Function to pop an item from stack*/

int pop(struct sNode** top_ref);

/* Returns 1 if character1 and character2 are matching left

   and right Parenthesis */

bool isMatchingPair(char character1, char character2)

{

   if (character1 == '(' && character2 == ')')

     return 1;

   else if (character1 == '{' && character2 == '}')

     return 1;

   else if (character1 == '[' && character2 == ']')

     return 1;

   else

     return 0;

}

/*Return 1 if expression has balanced Parenthesis */

bool areParenthesisBalanced(char exp[])

{

   int i = 0;

   /* Declare an empty character stack */

   struct sNode *stack = NULL;

   /* Traverse the given expression to check matching parenthesis */

   while (exp[i])

   {

      /*If the exp[i] is a starting parenthesis then push it*/

      if (exp[i] == '{' || exp[i] == '(' || exp[i] == '[')

        push(&stack, exp[i]);

      /* If exp[i] is a ending parenthesis then pop from stack and

          check if the popped parenthesis is a matching pair*/

      if (exp[i] == '}' || exp[i] == ')' || exp[i] == ']')

      {

             

          /*If we see an ending parenthesis without a pair then return false*/

         if (stack == NULL)

           return 0;

         /* Pop the top element from stack, if it is not a pair

            parenthesis of character then there is a mismatch.

            This happens for expressions like {(}) */

         else if ( !isMatchingPair(pop(&stack), exp[i]) )

           return 0;

      }

      i++;

   }

    

   /* If there is something left in expression then there is a starting

      parenthesis without a closing parenthesis */

   if (stack == NULL)

     return 1; /*balanced*/

   else

     return 0; /*not balanced*/

}

/* UTILITY FUNCTIONS */

/*driver program to test above functions*/

int main()

{

  char exp[100] = "{()}[]";

  if (areParenthesisBalanced(exp))

    printf(" Balanced ");

  else

    printf(" Not Balanced ");

  return 0;

}   

/* Function to push an item to stack*/

void push(struct sNode** top_ref, int new_data)

{

  /* allocate node */

  struct sNode* new_node =

            (struct sNode*) malloc(sizeof(struct sNode));

  if (new_node == NULL)

  {

     printf("Stack overflow ");

     getchar();

     exit(0);

  }          

  /* put in the data */

  new_node->data = new_data;

  /* link the old list off the new node */

  new_node->next = (*top_ref);

  /* move the head to point to the new node */

  (*top_ref)    = new_node;

}

/* Function to pop an item from stack*/

int pop(struct sNode** top_ref)

{

  char res;

  struct sNode *top;

  /*If stack is empty then error */

  if (*top_ref == NULL)

  {

     printf("Stack overflow ");

     getchar();

     exit(0);

  }

  else

  {

     top = *top_ref;

     res = top->data;

     *top_ref = top->next;

     free(top);

     return res;

  }

}

#include<stdio.h>

#include<stdlib.h>

#define bool int

/* structure of a stack node */

struct sNode

{

   char data;

   struct sNode *next;

};

/* Function to push an item to stack*/

void push(struct sNode** top_ref, int new_data);

/* Function to pop an item from stack*/

int pop(struct sNode** top_ref);

/* Returns 1 if character1 and character2 are matching left

   and right Parenthesis */

bool isMatchingPair(char character1, char character2)

{

   if (character1 == '(' && character2 == ')')

     return 1;

   else if (character1 == '{' && character2 == '}')

     return 1;

   else if (character1 == '[' && character2 == ']')

     return 1;

   else

     return 0;

}

/*Return 1 if expression has balanced Parenthesis */

bool areParenthesisBalanced(char exp[])

{

   int i = 0;

   /* Declare an empty character stack */

   struct sNode *stack = NULL;

   /* Traverse the given expression to check matching parenthesis */

   while (exp[i])

   {

      /*If the exp[i] is a starting parenthesis then push it*/

      if (exp[i] == '{' || exp[i] == '(' || exp[i] == '[')

        push(&stack, exp[i]);

      /* If exp[i] is a ending parenthesis then pop from stack and

          check if the popped parenthesis is a matching pair*/

      if (exp[i] == '}' || exp[i] == ')' || exp[i] == ']')

      {

             

          /*If we see an ending parenthesis without a pair then return false*/

         if (stack == NULL)

           return 0;

         /* Pop the top element from stack, if it is not a pair

            parenthesis of character then there is a mismatch.

            This happens for expressions like {(}) */

         else if ( !isMatchingPair(pop(&stack), exp[i]) )

           return 0;

      }

      i++;

   }

    

   /* If there is something left in expression then there is a starting

      parenthesis without a closing parenthesis */

   if (stack == NULL)

     return 1; /*balanced*/

   else

     return 0; /*not balanced*/

}

/* UTILITY FUNCTIONS */

/*driver program to test above functions*/

int main()

{

  char exp[100] = "{()}[]";

  if (areParenthesisBalanced(exp))

    printf(" Balanced ");

  else

    printf(" Not Balanced ");

  return 0;

}   

/* Function to push an item to stack*/

void push(struct sNode** top_ref, int new_data)

{

  /* allocate node */

  struct sNode* new_node =

            (struct sNode*) malloc(sizeof(struct sNode));

  if (new_node == NULL)

  {

     printf("Stack overflow ");

     getchar();

     exit(0);

  }          

  /* put in the data */

  new_node->data = new_data;

  /* link the old list off the new node */

  new_node->next = (*top_ref);

  /* move the head to point to the new node */

  (*top_ref)    = new_node;

}

/* Function to pop an item from stack*/

int pop(struct sNode** top_ref)

{

  char res;

  struct sNode *top;

  /*If stack is empty then error */

  if (*top_ref == NULL)

  {

     printf("Stack overflow ");

     getchar();

     exit(0);

  }

  else

  {

     top = *top_ref;

     res = top->data;

     *top_ref = top->next;

     free(top);

     return res;

  }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote