The problem is in C++ and I am very lost on what to do, any comments in the code
ID: 3740336 • Letter: T
Question
The problem is in C++ and I am very lost on what to do, any comments in the code would be greatly appreciated. Thanks!
circbuf.h
#ifndef CIRCBUF_H
#define CIRCBUF_H
#include<iostream>
using std::ostream;
#include<vector>
using std::vector;
#include<initializer_list>
using std::initializer_list;
class CircBuf{
private:
int sz_;
int cnt_;
vector<long> buf_;
size_t head_;
size_t tail_;
public:
CircBuf(size_t s=10);
CircBuf(initializer_list<long>, size_t);
long front() const;
long back() const;
bool full() const;
bool empty() const;
void add(long);
void remove();;
friend ostream& operator<<(ostream&, const CircBuf &cb);
/*
friend CircBuf operator+(CircBuf &buf, long val);
friend CircBuf operator+(long val, CircBuf &buf);
friend CircBuf operator+(CircBuf &buf2, CircBuf &buf1);
*/
};
ostream& operator<<(ostream&, const CircBuf &cb);
/*
CircBuf operator+(CircBuf &buf, long val);
CircBuf operator+(long val, CircBuf &buf);
CircBuf operator+(CircBuf &buf2, CircBuf &buf1);
*/
#endif
main.cpp
#include<iostream>
using std::cout; using std::cin; using std::endl;
#include<iomanip>
using std::boolalpha;
#include<string>
using std::string;
#include<stdexcept>
using std::runtime_error;
#include "lab10_circbuf.h"
int main(){
cout << boolalpha;
CircBuf cb(4);
cout << boolalpha;
cout << cb << endl;
CircBuf cb_il({2,4,6,8}, 5);
cout << cb_il << endl;
try{
CircBuf bad_il({2,4,6,8}, 4);
}
catch (runtime_error &e){
cout << e.what() << endl;
}
cb.add(11);
cb.add(12);
cout << cb << endl;
cout << "Front:" << cb.front() << endl;
cout << "Back:" << cb.back() << endl;
cb.remove();
cout << "Front:"<<cb.front() <<endl;
cb.remove();
cout << "Empty?:"<< cb.empty() << endl;
cout << "Full?:" << cb.full() << endl;
cout << "Add 4 elements"<<endl;
for(long i=0; i<4;i++)
cb.add(i+27);
cout << cb << endl;
cout << "Remove 4 elements"<<endl;
while(!cb.empty()){
cb.remove();
}
cout << "Fill er up"<<endl;
for(int i=0; !cb.full(); i++)
cb.add(i*i);
cout << "Full?:"<<cb.full() <<endl;
// should throw, it's full
try{
cb.add(1111);
}
catch (runtime_error err){
cout << err.what() << endl;
}
cout << "Drain it"<<endl;
for(int i=0; !cb.empty(); i++)
cb.remove();
cout << "Empty?:"<<cb.empty()<<endl;
// should thow, it's empty
try{
cout << cb.front() << endl;
}
catch (runtime_error err){
cout << err.what() << endl;
}
CircBuf cb2({1,2,3,4}, 5);
cb2.add(20);
cb2.remove();
cb2.add(30);
cb2.remove();
cout << cb2 << endl;
/*
// Extra Credit
cb = cb + 25;
cb = 30 + cb;
cout << cb << endl;
cb = cb + cb;
cout << cb << endl;
*/
}
Explanation / Answer
//main.cpp
#include<iostream>
using std::cout; using std::cin; using std::endl;
#include<iomanip>
using std::boolalpha;
#include<string>
using std::string;
#include<stdexcept>
using std::runtime_error;
#include "lab10_circbuf.h"
int main(){
cout << boolalpha;
CircBuf cb(4);
cout << boolalpha;
cout << cb << endl;
CircBuf cb_il({2,4,6,8}, 5);
cout << cb_il << endl;
try{
CircBuf bad_il({2,4,6,8}, 4);
}
catch (runtime_error &e){
cout << e.what() << endl;
}
cb.add(11);
cb.add(12);
cout << cb << endl;
cout << "Front:" << cb.front() << endl;
cout << "Back:" << cb.back() << endl;
cb.remove();
cout << "Front:"<<cb.front() <<endl;
cb.remove();
cout << "Empty?:"<< cb.empty() << endl;
cout << "Full?:" << cb.full() << endl;
cout << "Add 4 elements"<<endl;
for(long i=0; i<4;i++)
cb.add(i+27);
cout << cb << endl;
cout << "Remove 4 elements"<<endl;
while(!cb.empty()){
cb.remove();
}
cout << "Fill er up"<<endl;
for(int i=0; !cb.full(); i++)
cb.add(i*i);
cout << "Full?:"<<cb.full() <<endl;
// should throw, it's full
try{
cb.add(1111);
}
catch (runtime_error err){
cout << err.what() << endl;
}
cout << "Drain it"<<endl;
for(int i=0; !cb.empty(); i++)
cb.remove();
cout << "Empty?:"<<cb.empty()<<endl;
// should thow, it's empty
try{
cout << cb.front() << endl;
}
catch (runtime_error err){
cout << err.what() << endl;
}
CircBuf cb2({1,2,3,4}, 5);
cb2.add(20);
cb2.remove();
cb2.add(30);
cb2.remove();
cout << cb2 << endl;
// // Extra Credit
// cb = cb + 25;
// cb = 30 + cb;
// cout << cb << endl;
// cb = cb + cb;
// cout << cb << endl;
}
----------------------------------------------------------------------
//lab10_circbuf.cpp
#include<iostream>
using std::cout; using std::cin; using std::endl;
#include<iomanip>
using std::boolalpha;
#include<string>
using std::string;
#include<stdexcept>
using std::runtime_error;
#include <list>
using std::list;
#include<sstream>
using std::ostringstream;
#include "lab10_circbuf.h"
CircBuf::CircBuf(size_t sz)
{
sz_ = sz;
buf_ = vector<long>(sz);
cnt_ = 0;
head_ = 0;
tail_ = 0;
//everything else is set to zero (cnt_, head_, tail_)
}
//if the parameter size is smaller than initializationlist, throw a runtime_errr
CircBuf::CircBuf(initializer_list<long> li, size_t sz)
{
if (sz < li.size()) {
throw runtime_error("runtime error");
}
buf_ = vector<long> (sz);
copy(li.begin(), li.end(), buf_.begin());
cnt_ = li.size();
tail_ = cnt_ + 1;
}
bool CircBuf::full() const
{
if (cnt_ == sz_) {
return true;
}
else {
return false;
}
}
bool CircBuf::empty() const
{
if (cnt_ == 0) {
return true;
}
else {
return false;
}
}
long CircBuf::front() const
{
if (cnt_ == 0) {
throw runtime_error("runtime error");
}
else {
return buf_[head_];
}
}
long CircBuf::back() const
{
if (cnt_ == 0) {
throw runtime_error("runtime error");
}
else {
size_t bufindex = (tail_ -1);
if (bufindex < 0) {
return (buf_[sz_ -1]);
}
else {
return buf_[tail_ -1];
}
}
}
void CircBuf::add(long ele)
{
if (buf_.full()) {
throw runtime_error("runtime error");
}
else {
buf_[tail_] = ele;
}
}
void CircBuf::remove()
{
if (buf_.empty()) {
throw runtime_error("runtime error");
}
else {
head_ = head_ +1;
if (head_ >= sz_) {
head_ = 0;
cnt_ = cnt_ -1;
}
}
}
//Front:3, Back:30, Cnt:4, Sz:5 30, 2, 3, 4, 20
ostream& operator<<(ostream&, const CircBuf &cb)
{
ostringstream oss;
if (cb.empty()) {
cout << ("CircBuf empty") << endl;
}
else {
oss << "Front:" << cb.front() << ", " << "Back:" << cb.back() << ", " << "Cnt:" << cb.cnt_ << ", " << "Sz:" << cb.sz_ << " " << endl;
}
}
-------------------------------------------------------------------------------
//lab10_circbuf.h
#ifndef CIRCBUF_H
#define CIRCBUF_H
#include<iostream>
using std::ostream;
#include<vector>
using std::vector;
#include<initializer_list>
using std::initializer_list;
#include<sstream>
using std::ostringstream;
class CircBuf{
private:
int sz_;
int cnt_;
vector<long> buf_;
size_t head_;
size_t tail_;
public:
CircBuf(size_t s=10);
CircBuf(initializer_list<long>, size_t);
long front() const;
long back() const;
bool full() const;
bool empty() const;
void add(long);
void remove();;
friend ostream& operator<<(ostream&, const CircBuf &cb);
/*
friend CircBuf operator+(CircBuf &buf, long val);
friend CircBuf operator+(long val, CircBuf &buf);
friend CircBuf operator+(CircBuf &buf2, CircBuf &buf1);
*/
};
ostream& operator<<(ostream&, const CircBuf &cb);
/*
CircBuf operator+(CircBuf &buf, long val);
CircBuf operator+(long val, CircBuf &buf);
CircBuf operator+(CircBuf &buf2, CircBuf &buf1);
*/
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.