Question

In: Computer Science

My output is not working correctly its telling me things aren't balanced when it should be...

My output is not working correctly its telling me things aren't balanced when it should be telling me they are balanced. I cannot figure out where I went wrong.

here is the input.txt:

{{[a]b}[]([c])}
[]}b[]([])(}[
[][[{a}{b}{c}]]
({{[x]}([[[[[a]]]b]])c}[])
{{{{{{{{{a}}}}}}}}
{[}]
[[}[abc]{]]
[{{}()()([])}[}]]
{a}{{[a[[{(a)}]]]}}
[[({a[]})b]{}[({c[]}d)]]

#include <iostream>

#include <stack>

#include <fstream>

#include <string>

using namespace std;

bool isbalanced(string exp) {

   stack<char> mystack;

   char x;

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

   {

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

   mystack.push(exp[i]);

   }

   if (mystack.empty())

   return false;

   switch (exp[i]) {

   case ')':

   x = mystack.top();

   mystack.pop();

   if (x == '{' || x == '[')

   return false;

   break;

   case '}':

   x = mystack.top();

   mystack.pop();

   if (x == '(' || x == '[')

   return false;

   break;

   case ']':

   x = mystack.top();

   mystack.pop();

   if (x == '(' || x == '{')

   return false;

   break;

   }

   }

   return (mystack.empty());

}

int main() {

ifstream inputfile;

   string exp;

  

inputfile.open("input.txt");

   while (getline(inputfile, exp)) {

   if (isbalanced(exp)) {

   cout<<exp<<"the expression is balanced. \n";

   }

   else{

   cout<<exp<<"the expression is not balanced. \n";

   }

}

return 0;

}

Solutions

Expert Solution

C++ code

#include <iostream>

#include <stack>

#include <fstream>

#include <string>

using namespace std;

bool isbalanced(string exp) {

   stack<char> mystack;

   char x;

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

   {

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

   mystack.push(exp[i]);
   continue;

   }

   if (mystack.empty())

   return false;

   switch (exp[i]) {

   case ')':

   x = mystack.top();

   mystack.pop();

   if (x == '{' || x == '[')

   return false;

   break;

   case '}':

   x = mystack.top();

   mystack.pop();

   if (x == '(' || x == '[')

   return false;

   break;

   case ']':

   x = mystack.top();

   mystack.pop();

   if (x == '(' || x == '{')

   return false;

   break;

   }

   }

   return (mystack.empty());

}

int main() {

ifstream inputfile;

   string exp;

inputfile.open("input.txt");

   while (getline(inputfile, exp)) {

   if (isbalanced(exp)) {

   cout<<exp<<"the expression is balanced. \n";

   }

   else{

   cout<<exp<<"the expression is not balanced. \n";

   }

}

return 0;

}

sample output


Related Solutions

can someone finish and check my code on main. cpp? Its not working for me even...
can someone finish and check my code on main. cpp? Its not working for me even though im sure my code make sense is it possible to output each function to show they work. this is supposed to be a vector class library made from allocated memory i have included templated functions in the class file to help create the rest of the functions. Thank you so much note: i did not include main.cpp because it  was empty- im hoping someone...
My Teacher keeps telling me I don't have a Thesis. I thought I hada good thesis....
My Teacher keeps telling me I don't have a Thesis. I thought I hada good thesis. Please help! Bud Light in advertising             One of the most effective ways for businesses to thrive is to use effective advertising, whether the ad is on TV, magazines, social media or even movies. The goal is to create enticement for a purchase over the competitor’s product. Budweiser is great example of a billion dollar company that has always maintained a strong record of...
Recommendation Working at Natilapa INC. was a great experience for me. I have learnt many things...
Recommendation Working at Natilapa INC. was a great experience for me. I have learnt many things throughout my internship period. I have to come across a few situations about the project before we finish as an intern. I would prefer and recommended the company should be give any assignment for this semester from the first time that we have met. That intern can prepare any assignments as well as they can. In my opinion I think it not the way...
My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int...
My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int a,b,c,D,n; double x1,x2; double realPart, imagPart; do { // this set of code blocks prompts a message to the user and then read the integer entered and stores it as an integer printf("Enter a value for a:\n"); scanf("%d",&a); printf("Enter a value for b:\n"); scanf("%d",&b); printf("Enter a value for c:\n"); scanf("%d",&c); printf("You entered the Equation \n"); printf("%dx^2%+dx%+d=0\n",a,b,c); D = b*b - 4*a*c;    if (D<0)...
JAVA: This is my code, but when it runs, for the "Average Score" output, it only...
JAVA: This is my code, but when it runs, for the "Average Score" output, it only gives me NaN. How can I fix that? import java.util.Scanner; public class prog4 { public static void main(String[] args) { Scanner reader = new Scanner(System.in); String name; double score; double minScore = 0; double maxScore = 0; int numberOfRecords = 0; double sum = 0; double average = sum / numberOfRecords; System.out.printf("%-15s %-15s %-15s\n", "Student#", "Name", "Score");           while (reader.hasNext()) { name...
Give me a working MATLAB code for the Golden section search method . It should be...
Give me a working MATLAB code for the Golden section search method . It should be working Dont answer if you do not know, the code must work for the golden section method
When working for a major insurance company as an IT Staff Analyst, it was my job...
When working for a major insurance company as an IT Staff Analyst, it was my job to compile information into reports that were given to the management team to interpret. The managers and leaders would make decisions based on these reports. Given that so much information is available for reporting, how can the leadership team confirm accuracy? What techniques do you recommend?
What are some things you need to be aware of when working in a cross-cultural environment?
What are some things you need to be aware of when working in a cross-cultural environment?
My output is incorrect when i put an empty string first_inside_quotes(' "" '). Not sure where...
My output is incorrect when i put an empty string first_inside_quotes(' "" '). Not sure where the issue lies import introcs def first_inside_quotes(s): ''' Returns the first substring of s between two (double) quote characters    Note that the double quotes must be part of the string. So "Hello World" is a precondition violation, since there are no double quotes inside the string.    Example: first_inside_quotes('A "B C" D') returns 'B C' Example: first_inside_quotes('A "B C" D "E F" G')...
holding other things constant if a country working age population decreases and its wetlth increases the...
holding other things constant if a country working age population decreases and its wetlth increases the labor supply cureve 1. shifts to the left If the effect of the change in wealth is bigger than the effect of the change in the working age population 2. shifts to the right if the effect of the change in wealth in magnitude is bigger Than the effect of the change in the working age population 3. shifts to left 4. shifts to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT