1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <fstream>
#include "statsType.h"
using namespace std;
template <class myType>
/*set the filename to the empty string, the setSize to 0, and dataSet pointer to nullptr.*/
statsType<myType>::statsType() {
fileName = "";
setSize = 0;
dataSet = nullptr;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*accept a filename, set setSize to 0 and dataSet to nullptr.*/
template <class myType>
statsType<myType>::statsType(string fileName){
this->fileName = fileName;
setSize = 0;
dataSet = nullptr;
setFileName(fileName);
//setFileName(); ////////////////////////////////////////////////////////////////////////////////////////////////////////
}
/*destructor of the class ,deletes the myType array, set the pointer to nullptr,
the setSize to 0, and the file name to the empty string.*/
template <class myType>
statsType<myType>::~statsType(){
dataSet = nullptr;
setSize = 0;
fileName = "";
delete [] dataSet;
}
/*return the current filename.*/
template <class myType>
string statsType<myType>::getFileName()const{
return fileName;
}
/*set the class variable for the filename to the passed value.*/
template <class myType>
bool statsType<myType>::setFileName(string fileName){
if(fileName.size() > 4 && (fileName.substr(fileName.size() - 3) == "dat" || fileName.substr(fileName.size() - 3) == "txt") /*||(access(fileName.c_st r(), F_OK))*/) {
this->fileName= fileName;
return true;
}
else {
cout << "File does not exist"<< endl;
return false;
}
}
/*prompt for and read a filename from the user*/
template <class myType>
bool statsType<myType>::readFileName() {
cout << "Enter Input File Name: none ";
cin >>fileName;
while (!(fileName.substr(fileName.size() - 3) == "dat" || fileName.substr(fileName.size() - 3) == "txt")){
cout << "Error, invalid file name";
cin >>fileName;
}
setFileName(fileName);
}
template <class myType>
bool statsType<myType>::readDataFile(){
ifstream inFile ("dataFile1.txt");
inFile.ignore(1000, '\n');
inFile.ignore(1000, '\n');
inFile.ignore(1000, '\n');
inFile.ignore(1000, '\n');
inFile >> setSize;
dataSet = new myType[setSize];
for(int i =0; i < setSize; i++) {
inFile >> dataSet[i];
}
delete [] dataSet;
inFile.close();
}
template <class myType>
void statsType<myType>::printDataSet() const{ //printDataSet() function should print data items of type myType with nine (9) values per line.
for (int i = 0; i < setSize; i++) {
cout << dataSet [i] << " ";
}
cout<<endl;
//cout << siz << endl;
}
|