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 <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 <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 <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 <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...
#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 <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 =...
I need a Flowgorithm chart of this #include using namespace std; int main() {    float...
I need a Flowgorithm chart of this #include using namespace std; int main() {    float CISBook[] = {55.5,35.8,67.5};    float MATHBook[] = {25.5, 54.5};    float MECBook[] = {65.0,75.5,86.8};    float sumCIS=0, sumMATH = 0, sumMEC = 0;              for (int i=0; i<4; i++)    {sumCIS += CISBook[i];}       for (int i=0; i<3; i++)    {sumMATH += MATHBook[i];}       for (int i=0; i<4; i++)    {sumMEC += MECBook[i];}       cout<<"\nTotal for CIS Books:...
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; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT