#include #include #include \"DataStore.hpp\" /** * Construct an empty DataStore.
ID: 3746011 • Letter: #
Question
#include
#include
#include "DataStore.hpp"
/**
* Construct an empty DataStore.
*/
DataStore::DataStore() {
// TODO: Handle any default initialization you need here.
}
/**
* Construct a DataStore that was saved to a file.
* @param filename - name of file containing data to load.
*/
DataStore::DataStore(std::string filename) {
// TODO: Read your output file into your program.
// This cannot be done until you decide how you want to
// format your file.
}
/**
* Destructor: cleanup any allocated memory.
*/
DataStore::~DataStore() {
// TODO: Complete the destructor.
// Note: you may remove this entirely (here and in .hpp) if your data is
// stored entirely in automatic variables.
}
Just the Constructor of both data store and the Destructor i need help with
#ifndef _DATA_STORE_HPP_
#define _DATA_STORE_HPP_
#include <string>
class DataStore {
public:
/**
* Construct an empty DataStore.
*/
DataStore();
/**
* Construct a DataStore that was saved to a file.
* @param filename - name of file containing data to load.
*/
DataStore(std::string filename);
/**
* Destructor: cleanup any allocated memory.
*/
~DataStore();
/**
* (disabled) Copy constructor -- create a new object as a copy of another.
*/
DataStore(const DataStore& other) = delete;
/**
* (disabled) Copy assignment -- copy another object into this object.
*/
DataStore& operator=(const DataStore& rhs) = delete;
/**
* (disabled) Move constructor -- create a new object with the data of another.
*/
DataStore(DataStore&& other) = delete;
/**
* (disabled) Move assignment -- move data from other object to this object.
*/
DataStore& operator=(DataStore&& rhs) = delete;
/**
* Add a value to our storage.
* @param value - The value to add.
*/
void add_value(const int& value);
/**
* Remove a value from storage, if it exists.
* @param value - The value to remove.
* @return true if the value was successfully removed and
* false otherwise.
*/
bool remove_value(const int& value);
/**
* Get the number of entries for a particular value.
* @param value - The value to count occurrences of.
* @return The number of times the value was entered.
*/
int get_count(const int& value);
/**
* Create a space separated string of stored data as a single line.
*/
std::string to_string();
/**
* Write out the data stored to a file.
* @param filename - The name of the file to write to.
*/
void write_out(std::string filename);
};//DataStore
#endif
i guess with a vector to store the data,
Problem 2. (80 points) Your task is to build a system to store data as it is provided through a sequence of additions and removals. One feature of this system is to save the data to disk in order to load it at a later point in time. For your designed, you will have to implement the sepcified API in order to provide required functionality, but the internal storage and saved file format are left for you to decide. One thing to note is that you should aim to be efficient in your design To realize this system, you will need to complete the class definition for DataStore by completing the following method definitions . Constructor DataStore: :DataStore perform any default initialization necessary - Note: this is dependant on your implementation. If you are not using anything that needs allo- cation, then this will likely be left empty Constructor DataStore: :DataStore (std::string filename) - initialize the data store to the values stored in the file filename . Destructor DataStore::~DataStore ) - cleanup any allocated memory - Note: this is dependant on your implementation. If you are not using anything that needs deal location, then this will likely be left empty void DataStore::add value (const int&) -record the value in value into your data store - the same value may be added numerous times. bool DataStore:: remove_value (const int&value) - if the value was not previously added, return false, otherwise remove all records of the value in value from your data store and return true - E.g., the following code should print out the value 0: DataStore storage; atorage.add value (5) storage.add value (5) storage.add_ value (5) storage remove value (5) std::coutExplanation / Answer
PROGRAM:
CODE:
#include "DataStore.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <stdlib.h>
using namespace std;
// Default constructor definition
DataStore::DataStore()
{
// Sets the maximum size to 10
maximum = 20;
// Dynamically allocate memory
data = new int[maximum];
// Sets the current position to 0
current = 0;
}// End of default constructor
// Parameterize constructor to read file and store data
DataStore::DataStore(string fileName)
{
// Counter for number of records. Initializes to zero
int counter = 0;
int no;
// Initializes current position to 0
current = 0;
// Declare ifstream objects
ifstream inF;
// Open product file for reading
inF.open(fileName.c_str());
// Checks whether file can be opened or not
if(inF.fail())
{
cout << " ERROR: Unable to open the file "<<fileName<<" for reading." << endl;
exit(0);
}// End of if condition
// Loops till not end of file
while (!inF.eof())
{
// Reads number
inF>>no;
// Increase the record counter by one
counter++;
}// End of while loop
// Close the file
inF.close();
// Sets the maximum size to number of records in file
maximum = counter;
// Dynamically allocates memory of size maximum
data = new int[maximum];
// Re - open product file for reading
inF.open(fileName.c_str());
// Loops till not end of file
while (!inF.eof())
{
// Reads number
inF>>no;
// Check if the number is between -1000 and +1000
if(no >= -1000 && no <= 1000)
{
// Calls the function get the frequency of the parameter number
// Checks if the frequency of the number is greater than 100000 then display error
if(get_count(no) > 100000)
cout<<"ERROR: Number cannot be duplicate more than 100000 times.";
// Otherwise valid
else
{
// Adds the number at current position
data[current] = no;
// Increase the record counter by one
current++;
}// End of else
}// End of if condition
// Otherwise invalid number
else
cout<<" ERROR: Number must be between -1000 and +1000. Your number is: "<<no;
}// End of while loop
}// End of parameterized constructor
// Defines destructor
DataStore::~DataStore()
{
delete data;
}// End of destructor
// Function to add a number in list
void DataStore::add_value(const int& no)
{
// Checks if the current value is equals to maximum then display memory full
if(current == maximum)
cout<<" ERROR: Memory full.";
// Otherwise check if the number is between -1000 and +1000
else if(no >= -1000 && no <= 1000)
{
// Calls the function get the frequency of the parameter number
// Checks if the frequency of the number is greater than 100000 then display error
if(get_count(no) > 100000)
cout<<"ERROR: Number cannot be duplicate more than 100000 times.";
// Otherwise valid
else
{
// Adds the number at current position
data[current] = no;
// Increase the record counter by one
current++;
}// End of else
}// End of else if
// Otherwise invalid number
else
cout<<" ERROR: Number must be between -1000 and +1000. Your number is: "<<no;
}// End of function
// Function to remove a number from list
bool DataStore::remove_value(const int &no)
{
// Sets the found status to -1
int found = -1;
// Checks if current counter is zero then display error message
if(current == 0)
cout<<" ERROR: List is empty.";
// Otherwise list contains data
else
{
// Loops till end of the list
for(int x = 0; x < current;)
{
// Checks if the current number is equals to the parameter number
if(data[x] == no)
{
// Sets the found status to x index position value
found = x;
// Loops from found position to the end of the list
for(int y = found; y <= current; y++)
// Shift data to previous
data[y] = data[y+1];
// Decrease the counter by one
current--;
}// End of if condition
// Otherwise number not matched
else
// Increase the loop counter by one
x++;
}// End of for loop
}// End of else
// Checks if found value is -1 return false
if(found == -1)
return false;
// Otherwise return true
else
return true;
}// End of function
// Function to return number frequency
int DataStore::get_count(const int &no)
{
// Initialize the frequency counter to 0
int counter = 0;
// Checks if the current value is zero then return 0
if(current == 0)
return counter;
// Otherwise loops till end of the list
for(int x = 0; x < current; x++)
// Checks if the current index position number is equals to parameter number
if(data[x] == no)
// Increase the counter by one
counter++;
// Returns the counter
return counter;
}// End of function
// Function to return the concatenates number string format
string DataStore::to_string()
{
string result = "";
// Loops till end of the list
for(int x = 0; x < current; x++)
{
// Declaring output string stream
ostringstream temp;
// Sending a number as a stream into output string
temp << data[x];
// the str() coverts number into string
string conv = temp.str();
// Concatenates the string format of the number with comma
result = result + conv + ", ";
}// End of for loop
// Returns the result string
return result;
}// End of function
// Function to write number to file
void DataStore::write_out(string fileName)
{
// Declare ofstream objects
ofstream outF;
// Open product file for writing
outF.open(fileName.c_str());
// Checks whether file can be opened or not
if(outF.fail())
{
cout << " ERROR: Unable to open the file "<<fileName<<" for writing." << endl;
exit(0);
}// End of if condition
// Loop till current counter
for(int x = 0; x < current; x++)
// Checks for last record
if(x == current-1)
// Writes data without new line
outF<<data[x];
// Otherwise writes data with new line
else
outF<<data[x]<<endl;
}// End of function
// main function definition
int main()
{
// Using parameterized constructor read file contents and store data
DataStore storage("numbers.txt");
// Calls the function displays data
cout<<storage.to_string();
cout<<" Count 2: "<<storage.get_count(2)<<endl;
storage.remove_value(2);
cout<<" After removing 2: "<<storage.to_string();
cout<<" Count 2: "<<storage.get_count(2);
cout<<" Before adding 10";
cout<<" Count 10: "<<storage.get_count(10);
cout<<" Adding 10";
storage.add_value(10);
cout<<" Adding 10";
storage.add_value(10);
cout<<" Adding 10";
storage.add_value(10);
cout<<" Count 10: "<<storage.get_count(10);
storage.remove_value(-15);
cout<<" After removing -15: "<<storage.to_string();
cout<<" Adding -10001";
storage.add_value(-10001);
cout<<" Adding 10001";
storage.add_value(10001);
cout<<" Adding 90";
storage.add_value(90);
cout<<" Adding 90";
storage.add_value(90);
storage.write_out("Frequency.txt");
}// End of main function
Sample Output:
5, 10, -15, 5, 5, 3, 3, 2, 2, 2, 10,
Count 2: 3
After removing 2: 5, 10, -15, 5, 5, 3, 3, 10,
Count 2: 0
Before adding 10
Count 10: 2
Adding 10
Adding 10
Adding 10
Count 10: 5
After removing -15: 5, 10, 5, 5, 3, 3, 10, 10, 10, 10,
Adding -10001
ERROR: Number must be between -1000 and +1000.
Your number is: -10001
Adding 10001
ERROR: Number must be between -1000 and +1000.
Your number is: 10001
Adding 90
Adding 90
ERROR: Memory full.
File numbers.txt contents
5
10
-15
5
5
3
3
2
2
2
10
File Frequency.txt contents
5
10
5
5
3
3
10
10
10
10
90
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.