Question: T This is part 2 for this assignment, the solution for part 1 was: TextFile.cpp #include #include #include #include TextFile.h #include cstring.h using namespace std;

 T This is part 2 for this assignment, the solution forpart 1 was: TextFile.cpp #include #include #include #include "TextFile.h" #include "cstring.h" usingnamespace std; namespace sdds { Line::operator const char* () const { return(const char*)m_value; } Line& Line::operator=(const char* lineValue) { delete[] m_value; m_value =new char[strLen(lineValue) + 1]; strCpy(m_value, lineValue); return *this; } Line::~Line() { delete[]

Tm_value; } void TextFile::setEmpty(){ delete[] m_textLines; m_textLines = nullptr; delete[] m_filename; m_filename

This is part 2 for this assignment, the solution for part 1 was:

TextFile.cpp

#include  #include  #include  #include "TextFile.h" #include "cstring.h" using namespace std; namespace sdds { Line::operator const char* () const { return (const char*)m_value; } Line& Line::operator=(const char* lineValue) { delete[] m_value; m_value = new char[strLen(lineValue) + 1]; strCpy(m_value, lineValue); return *this; } Line::~Line() { delete[] m_value; } void TextFile::setEmpty(){ delete[] m_textLines; m_textLines = nullptr; delete[] m_filename; m_filename = nullptr; m_noOfLines = 0; } void TextFile::setFilename(const char* fname, bool isCopy){ char name[] = "C_"; if(!isCopy){ delete[] m_filename; m_filename = new char[strLen(fname)+1]; strCpy(m_filename, fname); } else { delete[] m_filename; strCat(name, fname); //strcat(name, fname); m_filename = new char[strLen(name)+1]; strCpy(m_filename, name); } } void TextFile::setNoOfLines(){ char c; std::ifstream f(m_filename); //if(f.is_open()){ while(f){ c = f.get(); if(c == ' '){ m_noOfLines++; } } //} //m_noOfLines++; if(m_noOfLines == 0){ delete[] m_filename; m_filename = nullptr; } m_noOfLines++; } void TextFile::loadText(){ int i = 0; std::string line; if(m_filename != NULL){ delete[] m_textLines; m_textLines = new Line[m_noOfLines]; std::ifstream f(m_filename); std::getline(f, line); while(!f.eof()){ f >> line; m_textLines[i] = line.c_str(); i++; } m_noOfLines = i; } // while(f.is_open() && !f.eof()){ // std::getline(f, line); // for(i = 0; i > line; // m_textLines[i] = line.c_str(); // } // m_noOfLines = i; // } } void TextFile::saveAs(const char *fileName)const{ int i; std::ofstream f(m_filename); if(f.is_open()){ for(i = 0; i > input; if(input == ' '){ for(j = m_pageSize; j > fname; setFilename(fname); setNoOfLines(); loadText(); return istr; } const char* TextFile::operator[](unsigned index)const{ const char* line = NULL; if(*this){ if(index > (m_noOfLines - 1)){ index -= (m_noOfLines - 1); } line = m_textLines[index]; } return line; } TextFile::operator bool()const{ bool flag = false; if(m_textLines != nullptr && m_filename != nullptr && m_noOfLines != 0){ flag = true; } return flag; } const char* TextFile::name()const{ return m_filename; } std::ostream& operator>(std::istream& istr, TextFile& text){ text.getFile(istr); return istr; } }

TextFile.h

#ifndef SDDS_TEXTFILE_H__ #define SDDS_TEXTFILE_H__ #include  namespace sdds { class Text; class Line { char* m_value{nullptr}; // holdes the address of the dynamically allocated Cstring (a line of the txt file) operator const char* ()const; // returns the address in the m_value Line(){}; Line& operator=(const char*); // allocates memory in m_value and copies the Cstring pointed by lineContent into it ~Line(); friend class TextFile; // copy and copy assignment prevention goes here Line& operator=(const Line&) = delete; Line(const Line&) = delete; }; class TextFile { Line* m_textLines = nullptr; char* m_filename = nullptr; unsigned m_noOfLines; // an unsigned int to be set to the number of lines in the file unsigned m_pageSize; // number of lines that should be displayed void setFilename(const char* fname, bool isCopy = false); void setNoOfLines(); void loadText(); void saveAs(const char *fileName)const; void setEmpty(); public: TextFile(unsigned pageSize = 15); TextFile(const char* filename, unsigned pageSize = 15); TextFile(const TextFile&); TextFile& operator=(const TextFile&); ~TextFile(); std::ostream& view(std::ostream& ostr)const; std::istream& getFile(std::istream& istr); operator bool()const; unsigned lines()const; const char* name()const; const char* operator[](unsigned index)const; }; std::ostream& operator>(std::istream& istr, TextFile& text); } #endif // !SDDS_TEXTFILE_H__

cstring.cpp

#include "cstring.h" namespace sdds { void strCpy(char* des, const char* src){ int i = 0; for (i = 0; src[i] != '\0'; i++){ des[i] = src[i]; } des[i] = '\0'; } int strLen(const char* s){ int len = 0, i; for(i = 0; s[i] != '\0'; i++){ len = i+1; } return len; } void strCat(char* des, const char* src){ int length, i = 0; length = strLen(des); while(src[i] != '\0'){ des[length] = src[i]; length++; i++; } des[length] = '\0'; } }

cstring.h

#ifndef SDDS_CSTRING_H_ #define SDDS_CSTRING_H_ namespace sdds { void strCpy(char* des, const char* src); int strLen(const char* s); void strCat(char* des, const char* src); } #endif // !SDDS_CSTRING_H_

w6p1_tester.cpp

#include  #include  #include  #include "TextFile.h" using namespace sdds; using namespace std; void FirstTen(TextFile T); void Copy(const string& dest, const string& source); void Dump(const string& filename); int main() { TextFile Empty; TextFile BadFilename("badFilename"); Copy("echoes.txt", "echoesOriginal.txt"); Copy("seamus.txt", "seamusOriginal.txt"); TextFile E; TextFile S("seamus.txt"); cout " > E; cout >> First ten lines of : " >>"   DIY (50%) Stats Module Create a module called stats that can represent a file on the hard disk containing comma-separated numbers. This module should be able to load the numbers and perform the following tasks.  A Stats object should be able to display the numbers in a tabular format on ostream in a specified number of columns, column width and precision  A Stats object should be able to display the numbers that fall within a specific range and the number of their occurrence.  A Stats object should be able to sort the numbers in an ascending or descending order.  Copying a Stats object should be done safely and doing so it should also copy the data file on the hard disk. The new file name should be the same as the original file with an added "C_" prefix.  Assigning a Stats object to another should be done safely and the content of the target file should also be overwritten by the source file.  A Stats object should also make the numbers available to the user program by indexing like an array.  A Stats object should be able to receive the data file name from the istream and load the data from the file on the hard drive. Required public functionalities construction 3 Argument Constructor Stats(unsigned columnWidth = 15, unsigned noofColumns = 4, unsigned precision = 2);  columnWidth: width of each column in characters  noOfColumns: number of columns to display the numbers in  precision: number of digits after the decimal point for each number In this case, the "Stats" object is not tied to any file on the hard drive and is empty. 4 Argument Constructor Stats(const char* filename, unsigned columnWidth = 15, unsigned noofColumns = 4, unsigned precision = 2); Same as the previous constructor, but in this case, if the filename argument is not null and is successfully opened, the numbers in the file will be loaded into the Stats object. Copy Constructor Copying a Stats object should be done safely and doing so it should also copy the data file on the hard disk. The new file name should be the same as the original file with an added "C_" prefix. Operator overloads Copy assignment Assigning a Stats object to another should be done safely and the content of the target file should also be overwritten by the source file. Index operator overloads double& operator[](unsigned idx); Returns the reference of the number at idx index. If the index exceeds the size of the array, it should loop back to the beginning. For example, if the array size is 10, index 10 will be the reference of the element at index 0 and index 11 will be the reference of the element at index 1. If the Stats object is empty, it should return the reference of a dummy double member variable. double operator[](unsigned idx) const; Returns the value of the number at idx index. If the index exceeds the size of the array, it should loop back to the beginning. For example, if the array size is 10, index 10 will be the value of the element at index 0 and index 11 will be the value of the element at index 1. If the Stats object is empty, it should return zero. Boolean type conversion overload If Stats is casted to boolean, it should return true only if it is not empty. Otherwise, it should return false. Methods (Member variables) sort() void sort(bool ascending); Sorts the numbers in ascending or descending order. size() unsigned size() const; returns the number of numbers in the Stats object. name() const char* name() const; returns the name of the file tied to the Stats object. occurrence) unsigned occurrence (double min, double max, std::ostream& ostr = std::cout); displays the numbers that fall within a specific range and the number of their occurrence on ostream. The format of the printout should be the same as printing all the numbers. The format of the printout should be the same as printing all the numbers. helper implementations ostream insertion operator> Stats object should be able to receive the data file name from ostream (cin) using operator>>. After receiving the name, if the data file is open successfully, the numbers should be loaded into the Stats object. The tester program: Modify the tester program to test all the different circumstances/cases of the application if desired and note that the professor's tester may have many more samples than the tester program here. // Workshop 6-diy: // Version: 0.9 // Date: 2021/02/20 // Author: Fardad Soleimanloo // Description: // This file tests the diy section of your workshop #include  #include  #include  #include "Stats.h" using namespace sdds; using namespace std; void twentyNumbers(Stats T); void badIndex(const Stats& T); void Copy(const string& dest, const string& source); void Dump(const string& filename); Vola Copy(const dest, const string source) void Dump (const string& filename); int main() { Copy("numltl.csv", "numbersLtloriginal.csv"); Copy("numbers.csv", "numbersOriginal.csv"); Stats L(10, 7, 0); Stats S("numLtl.csv"), Empty, BadFilename("badFilename"); cout > L; cout >> 20 numbers of : " >> 20 numbers of : >>" > Stats object should be able to receive the data file name from ostream (cin) using operator>>. After receiving the name, if the data file is open successfully, the numbers should be loaded into the Stats object. The tester program: Modify the tester program to test all the different circumstances/cases of the application if desired and note that the professor's tester may have many more samples than the tester program here. // Workshop 6-diy: // Version: 0.9 // Date: 2021/02/20 // Author: Fardad Soleimanloo // Description: // This file tests the diy section of your workshop #include  #include  #include  #include "Stats.h" using namespace sdds; using namespace std; void twentyNumbers(Stats T); void badIndex(const Stats& T); void Copy(const string& dest, const string& source); void Dump(const string& filename); Vola Copy(const dest, const string source) void Dump (const string& filename); int main() { Copy("numltl.csv", "numbersLtloriginal.csv"); Copy("numbers.csv", "numbersOriginal.csv"); Stats L(10, 7, 0); Stats S("numLtl.csv"), Empty, BadFilename("badFilename"); cout > L; cout >> 20 numbers of : " >> 20 numbers of : >>" 

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!