Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm struggling trying to convert this program into a template using C++. I have the code, but getting syntax errors. Can anyone help? I'd really

I'm struggling trying to convert this program into a template using C++. I have the code, but getting syntax errors. Can anyone help? I'd really appreciate it.

#include #include #include #include using namespace std; ofstream outfile;

template class list { private: static const size_t CAP = 65; T data[CAP]; size_t pos, used; public: //Constructors list() { for (int i = 0; i < CAP; i++) data[i] = 0; pos = 0; used = 0; } list(const list& other) { for (int i = 0; i < CAP; i++) data[i] = other.data[i]; pos = other.pos; used = other.used; } friend ostream& operator <<(ostream& out, const list& l); bool empty() { if (used == 0) return true; else return false; } void first() { pos = 0; } void last() { pos = used - 1; } void prev() { if (pos > 0) pos--; } void next() { if (pos < used - 1) pos++; } T getPos() { return pos; } void setPos(size_t val) { pos = val; } void insertBefore(T val) { if (size() < CAP) { if (used == 0) { data[used] = val; used++; pos = 0; } else if (pos == 0) { for (int k = used; k > pos; k--) data[k] = data[k - 1]; data[pos] = val; used++; } else if (used<= pos) { data[used] = val; used++; pos = used - 1; } else if (pos != 0) { for (int i = used; i > (pos - 1); i--) data[i] = data[i - 1]; data[pos] = val; used++; pos--; } } } void insertAfter(T val) { if (used == 0) { data[used] = val; used++; pos = 0; } else if (used <= pos) { data[used] = val; used++; pos = used - 1; } else if (pos != CAP) { for (int i = used; i > (pos + 1); i--) data[i] = data[i - 1]; data[used] = val; used++; pos++; } } T getElement() { return data[pos]; } T size() { return used; } void replace(T val) { if (val > 0) data[pos] = val; } void erase() { if (used == 0) return; for (int i = pos; i < used; i++) data[i] = data[i + 1]; used--; } void clear() { for (int i = 0; i < CAP; i++) data[i] = 0; used = 0; pos = 0; } int get_CAP() { return CAP; } bool operator ==(const list& other) { int x = 0, i = 0; for (i = 0; i < CAP; i++) { if (data[i] == other.data[i]) x++; } if (i == 20) return true; else return false; } void operator +(const list& other) { int x = 0, p = used; while (other.data[x] != 0&& used < 20) { data[p] = other.data[x]; x++; used++; } } void operator =(const list& other) { for (int i = 0; i < CAP; i++) { data[i] = other.data[i]; } pos = other.pos; used = other.used; }

}; template ostream& operator <<(ostream& out, const list& l) { if (l.used == 0) return out; for (int p = 0; p < l.used; p++) { if (p >= l.CAP) { out << endl; return out; } out << l.data[p] << " "; } return out; } template class stack { private: list myStk; public: void push(char x) { myStk.last(); myStk.insertAfter(x); } void pop() { myStk.erase(); myStk.prev(); }

char top() { return myStk.getElement(); }

bool full() { return (myStk.size() == myStk.get_CAP()); }

bool empty() { return myStk.empty(); }

int size() { return myStk.size(); } };

void print_result(bool x) { if (x) { outfile << endl << "The string is matched." << endl << endl; } else { outfile << endl << "The string is mismatched." << endl << endl; } }

void handle(char x, bool& match, stack& stk) { outfile << x; if ((x == '(') || (x == '[') || (x == '{') || (x == '<')) { stk.push(x); } else if ((x == ')') || (x == ']') || (x == '}') || (x == '>')) { if (!(stk.top())) { match = false; } else if ((stk.top() == '(') && (x == ')')) { stk.pop(); } else if ((stk.top() == '[') && (x == ']')) { stk.pop(); } else if ((stk.top() == '{') && (x == '}')) { stk.pop(); } else if ((stk.top() == '<') && (x == '>')) { stk.pop(); } else { match = false; } } }

int main() { outfile.open("Results.txt"); test.open("Brackets.txt"); data1.open("LispClassData.txt"); stack3.open("Stack3.txt");

stack Stack1, Stack2, Stack3, Stack4; stack T; char x; bool match = true;

while (test >> x) // File 1 { handle(x, match, Stack1); } if ((Stack1.top())) { match = false; } print_result(match);

match = true;

while (data1 >> x) // File 2 { handle(x, match, Stack2); } if ((Stack2.top())) { match = false; } print_result(match);

match = true;

while (stack3 >> x) // File 3 { handle(x, match, Stack4); } if ((Stack4.top())) { match = false; } print_result(match);

stack3.close(); data2.close(); data1.close(); test.close(); outfile.close(); system("Pause"); return EXIT_SUCCESS; }

Here is a test file called brackets.txt:

( ) [ ] ( ) { } [ { ( ) } ] < > <<< >>> [ [ ) ) { ( ) [ ( ) ] }

The other two files I have will work if this one does. Once again, thank you.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

9th Edition

0135188148, 978-0135188149, 9781642087611

More Books

Students also viewed these Databases questions

Question

Question Can employees make contributions to a profit sharing plan?

Answered: 1 week ago