-
qSort with objects
There is a lot of code...so i removed the parts that aren't important...unless of course i should post over 550 lines of code ;).
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <sys\timeb.h>
using namespace std;
int nMAXAccountNum = 100;
class cACCOUNT {
private: int nAccountNum;
string sAccountName;
float fTotalAmount;
public: cACCOUNT() { nAccountNum = 0; sAccountName = ""; fTotalAmount = 0; }
cACCOUNT(int Num, string Name, float Tot) {
nAccountNum = Num; sAccountName = Name; fTotalAmount = Tot;
}
bool operator < (cACCOUNT & rhs) {
return nAccountNum < rhs.nAccountNum;
}
bool operator == (cACCOUNT & rhs) {
return nAccountNum == rhs.nAccountNum;
}
/* all values are read in within here from file or user imput*/
};
int compare(const void *var1, const void *var2)
{
if ( (cACCOUNT* )var1 < (cACCOUNT* )var2 ) return -1;
if ( (cACCOUNT* )var1 == (cACCOUNT* )var2 ) return 0;
else return 1;
}
class cTRANS {
private: short nTransYear;
short nTransMonth;
short nTransDay;
float fTransAmount;
int nTransAccountNum;
char * psTransDescription;
public: cTRANS () { nTransYear = 2006; nTransMonth = 1; nTransDay = 1;
fTransAmount = 0; nTransAccountNum = 0; psTransDescription = NULL;
}
cTRANS(cTRANS & obj) {
nTransAccountNum = obj.nTransAccountNum;
nTransYear = obj.nTransYear;
nTransMonth = obj.nTransMonth;
nTransDay = obj.nTransDay;
fTransAmount = obj.fTransAmount;
psTransDescription = new char [strlen(obj.psTransDescription) + 1];
strcpy(psTransDescription, obj.psTransDescription);
}
~cTRANS () { if (psTransDescription != NULL) delete [] psTransDescription; }
bool operator = (cTRANS & obj) {
nTransAccountNum = obj.nTransAccountNum;
nTransYear = obj.nTransYear;
nTransMonth = obj.nTransMonth;
nTransDay = obj.nTransDay;
fTransAmount = obj.fTransAmount;
if (psTransDescription != NULL) delete [] psTransDescription;
psTransDescription = new char [strlen(obj.psTransDescription) + 1];
strcpy(psTransDescription, obj.psTransDescription);
return true;
}
bool operator > (cTRANS & obj) {
if (nTransAccountNum > obj.nTransAccountNum)
return true;
else
if (nTransAccountNum < obj.nTransAccountNum)
return false;
else
//They're equal go to Year
if (nTransYear > obj.nTransYear)
return true;
else
if (nTransYear < obj.nTransYear)
return false;
else
//They're equal go to Month
if (nTransMonth > obj.nTransMonth)
return true;
else
if (nTransMonth < obj.nTransMonth)
return false;
else
//They're equal go to Day
if (nTransDay > obj.nTransDay)
return true;
else
return false;
}
bool operator < (cTRANS &obj)
{
if (nTransAccountNum < obj.nTransAccountNum)
return true;
else
if (nTransAccountNum > obj.nTransAccountNum)
return false;
else
//They're equal go to Year
if (nTransYear < obj.nTransYear)
return true;
else
if (nTransYear > obj.nTransYear)
return false;
else
//They're equal go to Month
if (nTransMonth < obj.nTransMonth)
return true;
else
if (nTransMonth > obj.nTransMonth)
return false;
else
//They're equal go to Day
if (nTransDay < obj.nTransDay)
return true;
else
return false;
}
bool operator == (cTRANS & obj)
{
if ((nTransAccountNum == obj.nTransAccountNum) &&
(nTransYear == obj.nTransYear) &&
(nTransMonth == obj.nTransMonth) && (nTransDay == obj.nTransDay))
return true;
else
return false;
}
bool ReadFromFile (ifstream & fIn) {
char temp[1000];
fIn >> nTransAccountNum >> nTransYear >> nTransMonth >> nTransDay
>> fTransAmount >> temp;
if (psTransDescription != NULL)
delete [] psTransDescription;
psTransDescription = new char[strlen(temp) + 1];
strcpy (psTransDescription, temp);
if (fIn.fail() || nTransAccountNum < 0 ||
nTransAccountNum > nMAXAccountNum ||
nTransMonth < 1 || nTransMonth > 12 ||
nTransDay < 1 || nTransDay > 31)
return false;
else return true;
}
};
class cCOMPANY {
private: string sCompanyName;
cACCOUNT arAccounts[100];
cTRANS * parTransactions;
int nNumAccounts;
int nNumTransactions;
float fBankBalance;
public: cCOMPANY () { sCompanyName = ""; parTransactions = NULL;
fBankBalance = 0.0f; nNumAccounts = 0; nNumTransactions = 0;
}
~cCOMPANY () { if (parTransactions != NULL) delete [] parTransactions;
}
void SortAccounts() {
qsort(arAccounts,nNumAccounts,sizeof(cACCOUNT), compare);
}
};
From what I can gather...the program seems to know the operators are overloaded in the cACCOUNT class...but i must have typed something wrong within the compare method.
I just can't seem to get the qsort to work. I've been searching on the net for ways to solve it but i have been unable to.
Thanks for any help you all can give :),
Dan
-
Um, have you considered using std::sort()?
-
Well for my class the teacher wants us to practice using different types of sorts. This is one of the ones we have to do, so I'm trying to get it to work...but I'm having troubles with it.
Dan
-
Your code formatting could do with a examination, looks very out of sorts... I hope it doesn't look like that on your editior. Make sure you indent to 3-4 spaces. Do not use tabs, they are a real pain in my oppionon
-
Well I wasnt asking for my code formatting to be examined...but it's just from cutting and pasting that everything got messed up.
Dan