This is a kinda long post, and I didn't want to post in beginners as I felt it didn't deserve the attention. This is purely for critiques or advice I guess.
Back at the end of last semester (Dec 02 2009) I was asked to write an assignment to take in a file with random strings, put it in an array of 100 elements, no duplicate strings, sort the array, then output to another file. Program was to make use command line arguments only accepting arguments in the format of: -i inputfile -o outputfile
Question: what if the array fills up.?
Answer: stop processing file and continue.
Anyway my result for that was bad, but due to our teacher leaving halfway through the semester due to a heartattack, getting a new teacher. and the date on the assignment being put back to Feb2010. I actually passed because I was only 1 of 3 who handed in their assignments early to get extra credit. I was dissapointed I did so badly, but in hindsight was doing a java and bourne scripting assignment at the same time. So I didn't quite test the assignment fully.
Anyway after 2 months of posting on these forums the revision only took me close to 40mins :D
All criticism is welcome, I'm curious how others would have gone about this. While still on about the same skill level.
While I would have liked to just stick it in the one file, I kept it in the format the teacher expected just for nostalgia:
SSMain.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
// main for String Sort.
#include "StringSort.h"
int main(int argc,char *argv[])
{
StringSort ss;
ss.checkArgs(argc,argv);
ss.processInFile();
ss.sortStrings();
ss.processOutFile();
return 0;
}
| |
StringSort.h
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
|
#ifndef STRINGSORT_H_
#define STRINGSORT_H_
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
using std::cout;
using std::cerr;
using std::ifstream;
using std::ofstream;
using std::exit;
using std::string;
using std::endl;
class StringSort
{
public:
void checkArgs(int argc,char *argv[]);
string getInFile();
string getOutFile();
void processInFile();
void processOutFile();
void sortStrings();
private:
void setInFile(string inputFileName);
void setOutFile(string outputFileName);
string inFile, outFile;
const static int ARRAY_SIZE=100;
string sSArray[ARRAY_SIZE];
};
#endif /*STRINGSORT_H_*/
| |
StringSort.cpp
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
#include "StringSort.h"
void StringSort::setInFile(string inputFileName)
{
inFile = inputFileName;
}
string StringSort::getInFile()
{
return inFile;
}
void StringSort::setOutFile(string outputFileName)
{
outFile = outputFileName;
}
string StringSort::getOutFile()
{
return outFile;
}
void StringSort::checkArgs(int argc,char *argv[])
{
int row=1;
string sw;
string inputFile="";
string outputFile="";
string usage="Usage: -i input_filename -o output_filename";
while (row < argc)
{
sw = argv[row];
if (sw == "-i")
{
row++;
if (row < argc)
inputFile = argv[row];
}
else if (sw == "-o")
{
row++;
if (row < argc)
outputFile = argv[row];
}
else
{
cerr << "Invalid switch: " << sw << "\n";
cerr << usage << endl;
exit (1);
}
row++;
}
if (inputFile.size() < 1)
{
cerr << "Error input file needs to be given!\n";
cerr << usage << endl;
exit (1);
}
else if (outputFile.size() < 1)
{
cerr << "Error: output file needs to be given!\n";
cerr << usage << endl;
exit (1);
}
if (inputFile == outputFile)
{
cerr << "Error: duplicate filenames, filenames must be unique!\n";
cerr << usage << endl;
exit (1);
}
else
{
setInFile(inputFile);
setOutFile(outputFile);
}
}
void StringSort::processInFile()
{
ifstream ifstr(getInFile().c_str(), ifstream::in);
string line;
bool lineExists=false;
int count=0;
if (! ifstr.is_open())
{
cerr << "Error: unable to open file: " << getInFile() << "\n"
<< "Make sure file names are correct and files exist.\n"
<< "Usage: -i input_filename -o output_filename" << endl;
exit (1);
}
if (! ifstr.good())
{
cerr << "Error: Bad input stream in: " << getInFile() << "\n"
<< "Check file is not corrupted and/or has correct permissions.\n"
<< "Usage: -i input_filename -o output_filename" << endl;
exit (1);
}
while ((!ifstr.eof()) && (count<ARRAY_SIZE))
{
getline(ifstr, line);
// check if line exists already
for (int i=0; i<(count+1); i++)
{
if (line == sSArray[i])
lineExists = true;
}
// if line doesn't exist add it to the array
if (! lineExists)
sSArray[count++] = line;
// reset bool
lineExists = false;
}
ifstr.close();
}
void StringSort::processOutFile()
{
ofstream ofstr(getOutFile().c_str(), ofstream::out);
if (! ofstr.is_open())
{
cerr << "Error: unable to open file: " << getOutFile() << "\n"
<< "Make sure file names are correct and files exist.\n"
<< "Usage: -i input_filename -o output_filename" << endl;
exit (1);
}
if (! ofstr.good())
{
cerr << "Error: Bad input stream in: " << getOutFile() << "\n"
<< "Check file is not corrupted and/or has correct permissions.\n"
<< "Usage: -i input_filename -o output_filename" << endl;
exit (1);
}
for (int i=0; i<ARRAY_SIZE; i++)
{
// don't store null strings
if (! sSArray[i].empty())
ofstr << sSArray[i] << "\n";
}
cout << endl;
ofstr.close();
}
void StringSort::sortStrings()
{
int i, j, min;
for (i=0; i<(ARRAY_SIZE-1); i++)
{
min = i;
for (j=(i+1); j<ARRAY_SIZE;j++)
{
if (sSArray[j] < sSArray[min])
min = j;
}
if (i != min)
{
string swap = sSArray[i];
sSArray[i] = sSArray[min];
sSArray[min] = swap;
}
}
}
| |