オブジェクトの配列tArrayには、購入者の名前と購入のnumsharesが含まれ、各購入者はオブジェクトの配列に複数回存在する可能性があります。私は5人の最大のバイヤーの名前を配列で返さなければなりません。
購入者の名前と並行して2つのアレイを実行しようとしましたが、別のアレイに合計ボリュームがあります。
間違った結果が得られるため、私の方法は一般的に欠陥があります。どうすればこの問題を解決できますか。
ありがとう
ntransactions=配列内のトランザクションの数
string* Analyser::topFiveBuyers()
{
//set size and add buyer names for comparison.
const int sSize = 5;
string *calcString = new string[sSize];
calcString[0] = tArray[0].buyerName;
calcString[1] = tArray[1].buyerName;
calcString[2] = tArray[2].buyerName;
calcString[3] = tArray[3].buyerName;
calcString[4] = tArray[4].buyerName;
int calcTotal[sSize] = {INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN};
//checks transactions
for (int i = 0; i<nTransactions; i++)
{
//compares with arrays
for(int j =0; j<sSize; j++)
{
//checks if the same buyer and then increase his total
if(tArray[i].buyerName == calcString[j])
{
calcTotal[j] += tArray[i].numShares;
break;
}
//checks if shares is great then current total then replaces
if(tArray[i].numShares > calcTotal[j])
{
calcTotal[j] = tArray[i].numShares;
calcString[j] = tArray[i].buyerName;
break;
}
}
}
return calcString;
}