Question

In: Computer Science

"balancing.cpp " ----------------------------------------------------------- #include "stack.hpp" using namespace std; int main(){ freopen("input_balanced.txt", "r", stdin); string s,r; int...

"balancing.cpp "

-----------------------------------------------------------

#include "stack.hpp"

using namespace std;

int main(){
freopen("input_balanced.txt", "r", stdin);
string s,r;
int line_counter;
while(cin >> r){
    cin>>s;
    Stack<char> stack;
    bool isBalanced = true;


    bool solution;
    if(r[0] == 'Y' || r[0] == 'y'){
      solution = true;
    }else{
      solution = false;
    }

    // The input file is in the format "expected_solution expression"
    // so variable solution tells you whether 'expression' is balanced or not

    for(int i=0; i<s.length(); ++i){
      // WRITE CODE HERE so that isBalanced indicates whether 's' is balanced
    }

    // checking if you stored in isBalanced the correct value
    if(isBalanced == solution){
      cout << "line " << line_counter << ": OK [" << solution << " " << isBalanced << "]" << endl;
    }else{
      cout << "line " << line_counter << ": ERROR [" << solution << " " << isBalanced << "]" << endl;
    }
    line_counter++;
}
}


____________________________________________________________________________

"input_balanced.txt"

yes [(a+b)(b+c)]
no [)(a+b)]
yes [{a+(b+c(3+4))}]

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

"stack.hpp"

#include "stack.hpp"

using namespace std;

template<class T>
void Stack<T>::push(T c){
if(topIndex > MAXSIZE-1){
    cout<<"Stack overflow"<<endl;
    return;
}
arr[topIndex + 1] = c;
topIndex++;
}

template<class T>
T Stack<T>::pop(){
if(topIndex < 0){
    cout<<"Cannot delete. Stack empty."<<endl;
}

return arr[topIndex--];
}

template<class T>
T Stack<T>::peek(){
if(topIndex < 0){
    cout<<"Cannot peek. Stack empty."<<endl;
}
return arr[topIndex];
}

template<class T>
int Stack<T>::size(){
return topIndex+1;
}

template<class T>
void Stack<T>::display(){
for(int i=topIndex; i>=0; --i){
    cout<<arr[i]<<"\t";
}
cout<<endl;
}

template class Stack<char>;
template class Stack<int>;

_______________________________________________________________

can you make the code compile

Solutions

Expert Solution

balancing.cpp

#include "stack.hpp"
using namespace std;

int main() {
freopen("input_balanced.txt", "r", stdin);
string s,r;
int line_counter=1;
while(cin >> r) {
cin>>s;
Stack<char> stack;
bool isBalanced = true;
bool solution;
if(r[0] == 'Y' || r[0] == 'y') {
solution = true;
}else{
solution = false;
}
// Loop to check whether s is balanced or not
for(int i=0; i<s.length(); ++i) {
// If s[i] is opening symbol then push it into the stack
if(s[i] == '[' || s[i] == '(' || s[i] == '{')
stack.push(s[i]);
  
// if it is any of closing symbol then peek of the stack should be
// the opening symbol of same kind
// else make isBalanced false and break the loop
else if (s[i] == ']') {
if(stack.peek() == '[')
stack.pop();
else {
isBalanced = false;
break;
}
}
else if (s[i] == ')') {
if(stack.peek() == '(')
stack.pop();
else {
isBalanced = false;
break;
}
}
else if (s[i] == '}') {
if(stack.peek() == '{')
stack.pop();
else {
isBalanced = false;
break;
}
}
}

if(isBalanced == solution) {
cout << "line " << line_counter << ": OK [" << solution << " " << isBalanced << "]" <<endl;
}else{
cout << "line " << line_counter << ": ERROR [" << solution << " " << isBalanced << "]" <<endl;
}
line_counter++;
}
}

This is the Solution of your Question.


Related Solutions

#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
#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...
#include <string> using namespace std; //using recursion no loops allowed int main() { double nums[] =...
#include <string> using namespace std; //using recursion no loops allowed int main() { double nums[] = { 13.8, 2.14, 51, 82, 3.14, 1.7, 4.89, 18, 5, 23.6, 17, 48, 5.6 };   //Challenge #2: print the list from given range   printList(nums, 0, 12); //13.8 2.14 51 .... 48 5.6   cout << endl;   //Challenge #3: print the list, but backwards   printReverse(nums, 0, 12); //5.6 48 17 ... 2.14 13.8   cout << endl;                  //Challenge #4: reverse order of items in list   reverse(nums,...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
#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 <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; int main() {     int hour;     int min;     for (hour = 1;...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1; hour <= 12; hour++)     {         for (min = 0; min <= 59; min++)         {             cout << hour << ":" << min << "AM" << endl;         }     }       return 0; } 1.      Type in the above program as time.cpp. Add a comment to include your name and date. Compile and run. 2.      What is the bug or logic error in the above program? Add the...
Fill in the code only using pointer variables #include using namespace std; int main() { int...
Fill in the code only using pointer variables #include using namespace std; int main() { int longside; // holds longside (length) int wideside; // holds wideside(width) int total; // holds total (area) int *longsidePointer = nullpointer; // int pointer which will be set to point to length int *widthPointer = nullpointer; // int pointer which will be set to point to width cout << "Please input the longside of the rectangle" << endl; cin >> longside; cout << "Please input...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
#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;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT