Call or Whatsapp +27682021794
www.myassignments.co.za
COS3711
Assignment 02
2023
Due date
20 July 2023 ,
11:00 PM
Unique Number:732808
There is a link to download the zipped file at the end of the
document.Please DO NOT submit that zipped file as it is, use
it as reference to correct your code and see what needs to be
done. All programs are running correctly with the instruction
on the questions, no errors when building the code, check the
images.
,QUESTION 1
// COUNT.H
#ifndef COUNT_H
#define COUNT_H
#include <QStringList>
class Count
{
public:
Count();
Count(QStringList args);
QString doCount();
private:
bool aFlag, bFlag, cFlag, dFlag;
QStringList filenames;
QString processFile(QString f);
QString process(char flag, QString contents);
};
#endif // COUNT_H
// COUNT.CPP
#include "count.h"
#include <QRegularExpression>
#include <QFile>
#include <QTextStream>
Count::Count()
{
aFlag = false;
bFlag = false;
cFlag = false;
dFlag = false;
}
Count::Count(QStringList args)
{
aFlag = false;
bFlag = false;
cFlag = false;
dFlag = false;
foreach (QString s, args)
{
if (s.at(0) == '-')
{
s.remove(0, 1); //remove -
while (s.length()>0)
{
switch (s.at(0).toLatin1())
{
case 'a': aFlag = true; break;
case 'b': bFlag = true; break;
case 'c': cFlag = true; break;
case 'd': dFlag = true;
}
s.remove(0, 1);
}
, }
else
filenames.append(s);
}
if (!aFlag && !bFlag && !cFlag && !dFlag) // no flags passed
{
aFlag = true;
bFlag = true;
cFlag = true;
dFlag = true;
}
}
QString Count::doCount()
{
QString result;
if (filenames.size() > 0)
{
foreach (QString filename, filenames)
{
result.append(processFile(filename));
result.append("\n");
}
}
else
result = QString("No files to process");
return result;
}
QString Count::processFile(QString f)
{
QString result;
QFile file(f);
if (!file.open(QIODevice::ReadOnly))
{
result = QString("%1 did not open sucessfully").arg(f);
}
else
{
QTextStream in(&file);
QString contents = in.readAll();
file.close();
result.append(QString("***" + f + "***\n"));
QString str = contents.trimmed();
QRegExp rem("[.,?!;:]");
str.remove(rem);
if (aFlag) result.append(process('a', str));
if (bFlag) result.append(process('b', str));
if (cFlag) result.append(process('c', str));
if (dFlag) result.append(process('d', str));
}
return result;
}
QString Count::process(char flag, QString contents)
{