Hi c++ experts, I need some help this project I have been stuck on for a bit. An
ID: 3581987 • Letter: H
Question
Hi c++ experts, I need some help this project I have been stuck on for a bit. Any help would be appreciated.
For this project, create a dynamic container class so your program can be used for any sized Apache weblog. Each line of the file will be a single string that will be stored in a dynamic array (do not use vector for this assignment!). The dynamic array should be declared in the private section of your container class. Feel free to use your Project 1 code for this assignment, but be sure to modify it to use an array, not a vector. Make a backup copy of your project 1 code first, just in case.
The container class should include the following functions to work with the weblog.CPP file (see below).
void add(); a function to open the weblog.txt file and add a line from the file as a string to the private array. If the array is filled to capacity, a private function should be called to grow the array before the add is performed.
int size(); a function to return the total number of lines in the container.
void lineNum( int index); a function to display a line from the container when a line number is provided. Note: This is the line number, which is the array's index number + 1.
For testing purposes, you can use the following larger versions of the original weblog.txt from project 1. Right click the files below to save the files to your computer.
weblog_small.txt
weblog_medium.txt
Requirements
Class private data member that is a dynamic array to hold strings and all required functions. (5 points)
Class private function to grow the array when it is filled to capacity. (5 points)
Create another function that adds to the array. If the array is filled to capacity, the private function should be called to grow the array before the add is performed. (5 points)
Make sure you class implementation is in a separate .cpp file and not part of your header file. Remember to #include the header file in your implementation. (3 points)
Follow Style Guidelines. (2 points)
Explanation / Answer
//operating system linux, ubuntu, Mac
#include "weblog1.h"
/*This function reads file*/
Weblog::Weblog(){
capacity=100;
line Store=new string[capacity];//initalizing array size
}
/* default destructor*/
Weblog::~Weblog() {
delete[] line Store;
}
void Weblog:: add(){
std::string line;
//Creates an instance of stream, and opens example.txt
if stream file("weblog.txt");
if ( !file.is_open() ) {
std::cout<<"File could not be opened"<<std::endl;
}
else {
if( file.good() ) {
int i=0;
/*Copying content into array line by line*/
while (file.good())
{
if(i>capacity-2){
resize(capacity+200);
}
get line(file,lineStore[i]);//copy content into string array at particular index
i++;
}
}
}
}
/*Resize array with new Size*/
void Weblog::resize(int new Size) {
string *temp;
temp = new string[new Size];//dynamically array creation
for (int i = 0; i < capacity; i++) {
temp[i]=line Store[i];//copying content to temporary string array
}
delete[] line Store;//deleting old array content
line Store = temp;//assign to old array
capacity = new Size;//updating capacity value with new size
}
/* Returning size of file*/
int Weblog:: size(){
/*Returning size of vector*/
return capacity;
}
/*printing line number at particular index*/
void Weblog::lineNum(int index){
std::cout<<line Store[index+1]<<std::endl;
}
int main(){
int index;
/*Creating object of Weblog class*/
Weblog weblog;
/*Calling open WebLog function for reading file*/
weblog.add();
/*Getting size of file*/
std::cout<<"Size of file "<<weblog.size()<<std::endl;
/*Getting line at particular index*/
std::cout<<"please enter the index "<<std::endl;
std::cin>>index;
std::cout<<"Line at index "<<index<<std::endl;
weblog.line Num(index);
return 0;
}
/*************weblog1.h****************/
# include <iostream>
# include <fstream>
# include <vector>
# include <cstring>
# include <string>
using name space std;
class Weblog{
private:
int capacity;
/*vector object declaration*/
string *line Store;
public:
//default constructure declaration
Weblog();
//destructore
~Weblog();
/*function declarations*/
void resize(int new Size);
void add();// for reading file
int size();//calculating size of file
void line Num( int index);//printing line at particular index
};
/***************output********************/
sekhar@sekhar:~/Desktop/chegg$ g++ Weblog1.cpp
sekhar@sekhar:~/Desktop/chegg$ ./a.out
Size of file 3100
please enter the index
0
Line at index 0
172.138.80.174 - - [05/Aug/2001:21:06:28 -0300] "GET /~csc226/ HTTP/1.0" 200 3387 "http://www.goto.com/d/search/?Keywords=stringVar+%2B+savitch&view=2+80+0&did=" "Mozilla/4.61 [en] (Win98; I)"
sekhar@sekhar:~/Desktop/chegg$ ./a.out
Size of file 3100
please enter the index
1
Line at index 1
172.138.80.174 - - [05/Aug/2001:21:06:37 -0300] "GET /~csc226/class HTTP/1.0" 301 309 "http://cs.stmarys.ca/~csc226/" "Mozilla/4.61 [en] (Win98; I)"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.