Question: URGENT C++ and Ubuntu bash Terminal These are the two links to the questions. The one i've done: http://csundergrad.science.uoit.ca/courses/csci1061u/labs/string-class.html The one I need help in

URGENT C++ and Ubuntu bash Terminal

These are the two links to the questions.

The one i've done: http://csundergrad.science.uoit.ca/courses/csci1061u/labs/string-class.html

The one I need help in : http://csundergrad.science.uoit.ca/courses/csci1061u/labs/queue-class.html

QUESTION

Part 1

Use the string class that you implemented in part 1 and use it to implement a character queue. The Queue should support the following functions.

  • char pop() -- Returns the character at the front of the queue, removing it in the process. This function must raise an assertion if the queue is empty
  • void push(char c) -- Push a character to the end of the queue

The code should work as follows:

class que { protected: str _storage; // This is your string class that // can dynamically grow when // needed. public: que(); // Default constructor. Creates an empty queue. ~que(); // Destructor. char pop(); void push(char c); }

We can use this code as follows:

que q; q.push('h'); q.push('d'); cout << q.pop() << endl; cout << q.pop() << endl;

The output of this program is

h d

Caveat

A push may force the storage to grow. But a pop should be rather efficient.

Part 2

You will notice that as you add (push) and remove (pop) characters into this queue, the underlying storage may continue to grow. Find a way to fix that. Provide the following two functions in the queue class.

  • int size() - Returns the storage needed to store this queue
  • void compress() - Reduces the storage space by discarding the unused space.

See usage below

#include #include #include using namespace std; class que { ... }; char random_char() { char ch = char((rand() % 26) + 65); return ch; } int main() { que q; for (int i=0; i<10; ++i) q.push(random_char()); cout << "size = " << q.size() << endl; for (int i=0; i<4; ++i) cout << q.pop() << endl; cout << "size = " << q.size() << endl; q.compress(); cout << "size = " << q.size() << endl; return 0; }

This is the code implemented in part one (string class.)

// str.h

#ifndef __str_h__ #define __str_h__

#include #include #include

using namespace std;

class str { protected: char* _buf; // pointer to the underlying storage int _n; // size of the buffer

public:

// constructors of various forms str(); str(int n); str(char ch); str(const char* c_str);

// lets not forget the destructor ~str();

// inline functions for finding length info of the str inline const int& length() const { return _n; } inline bool is_empty() const { return length() == 0; }

// index operators char& operator[](int i) { return _buf[i]; } const char& operator[](int i) const { return _buf[i]; }

// TODO 1. You need to implement the assignment operator const str& operator=(const str& s);

// TODO 2. You need to implement the + operator concatenates two str friend str operator+(const str& a, const str& b);

friend ostream& operator<<(ostream& os, const str& s); friend istream& operator>>(istream& is, str& s); };

#endif

// str.cpp

#include "str.h" #include

str::str() : _n(0), _buf(0) {}

str::str(int n) : _n(n) { _buf = new char[_n]; }

str::str(char ch) : _n(1) { _buf = new char[_n]; _buf[0] = ch; }

str::str(const char* c_str) { _n = strlen(c_str); _buf = new char[_n]; for (int i=0; i<_n; ++i) _buf[i]= c_str[i]; }

str::~str() { if (_buf) delete [] _buf; }

ostream& operator<<(ostream& os, const str& s) { if (!s.is_empty()) { for (int i=0; i cout << s[i]; } } else cout << "[null str]";

return os; }

istream& operator>>(istream& is, str& s) { char ch; do { ch = is.get(); if (ch == ' ' || ch == ' ') break; s = s + ch; } while(true);

return is; }

// 1. TODO - assignment operator

const str& str:: operator=(const str& s)

{

this->_n = s._n;

this->_buf = new char[this->_n];

for(int i=0;i

this->_buf[i] = s._buf[i];

return (*this);

}

// 2. TODO - concatenation operator str operator+(const str& a, const str& b)

{

str strNew = str(a._n + b._n);

for(int i=0;i

strNew._buf[i] = a._buf[i];

for(int j=0;j

{

strNew._buf[a.length()+j] = b._buf[j];

}

return strNew;

}

Main.cpp

// main.cpp

#include "str.h"

int main() { str s1; cout << s1 << endl;

str s2("Hello"); cout << s2 << endl;

str s3("World");

str s4 = s2 + " " + s3; cout << s4 << endl;

str s5, s6; cin >> s5 >> s6; cout << s5 << ' ' << s6;

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!