[英]How to return an array of ints to a dynamic array of ints using a member function
我正在編寫的程序使用表示分數的類。 我有一個類成員函數,該函數從類成員變量中獲取兩個整數(分子和分母),並對它們進行比較以找出是否存在任何不常見的數字。
(分子123和分母110035將具有3個不常見的數字,分別是2和0和5,因為這些數字對於它們的int是唯一的)。
我的成員函數必須輸出一個數組,其中包含以下數據:有多少個不常見的數字,具體是哪些數字,以及該數字在其各自的int(分子或分母)中出現了多少次。
我在名為uncommonDigitArray的成員函數中有一個數組,其格式如下:
索引0包含一個整數,表示有多少個不尋常的數字
索引1-10代表數字0-9,並且它們包含整數,它們代表該數字出現在分子中的次數。
索引11-20代表數字0-9,並且它們包含整數,它們代表該數字出現在分母中的次數。
我需要將該數組傳遞給另一個函數,然后對其進行迭代以正確輸出信息。
這是創建數組的成員函數:
int Fraction::extractUncommonDigit() {
int numDigitCounterArray[10] = { 0 };
int denomDigitCounterArray[10] = { 0 };
int digitCounterArray[20] = { 0 };
int uncommonDigitArray[21] = { 0 };
int tempDigit;
int numInt{ num };
int denomInt{ denom };
int numLength{ 0 };
int denomLength{ 0 };
int uncommonDigitCounter = 0;
do {
tempDigit = numInt % 10;
digitCounterArray[tempDigit] += 1;
numInt /= 10;
numLength++;
} while (numInt != 0);
do {
tempDigit = denomInt % 10;
digitCounterArray[tempDigit + 10] += 1;
denomInt /= 10;
denomLength++;
} while (denomInt != 0);
for (int i = 0; i < 10; i++) {
if ((digitCounterArray[i] == 0 && digitCounterArray[i + 10] > 0) ||
(digitCounterArray[i] > 0 && digitCounterArray[i + 10] == 0)) {
uncommonDigitCounter++;
}
}
uncommonDigitArray[0] = uncommonDigitCounter;
for (int i = 0; i < 10; i++) {
if (digitCounterArray[i] > 0 && digitCounterArray[i + 10] == 0) {
uncommonDigitArray[i+1] = digitCounterArray[i];
}
if (digitCounterArray[i] == 0 && digitCounterArray[i + 10] > 0) {
uncommonDigitArray[i + 11] = digitCounterArray[i + 10];
}
}
cout << "\nuncommonDigitsArray" << endl;
for (int i = 0; i < 21; i++) {
cout << uncommonDigitArray[i];
}
cout << "\n\ndigitCounterArray\n";
for (int i = 0; i < 20; i++) {
cout << digitCounterArray[i];;
}
return (*uncommonDigitArray);
}
這是我要獲取該數組並對其進行迭代的地方。 在情況3中,我調用成員函數提取不常見的數字:
void runMenu3(void) {
int option;
int n;
int d;
Fraction* frPtr{ nullptr };
int *uncommonDigitArray = new int[21];
do {
cout <<
"*****************************************\n"
"* Menu - HW #1 *\n"
"* 1. Creating/Updating Fraction *\n"
"* 2. Displaying the Fraction *\n"
"* 3. Displaying Uncommon Digit(s) *\n"
"* 4. Quit *\n"
"*****************************************\n"
"Enter your option (1 through 4): ";
cin >> option;
switch (option) {
case 1:
if (frPtr == nullptr) {
cout << "\nEnter an int for num: ";
cin >> n;
cout << "\nEnter an int for denom: ";
cin >> d;
frPtr = new FractionGeorgeMS(n, d);
}
else if (frPtr != nullptr) {
runHWMenu4(frPtr);
}
break;
case 2:
frPtr->print();
break;
case 3:
frPtr->print();
*uncommonDigitArray = frPtr->extractUncommonDigit();
cout << "\n\nuncommonDigitArray after copying" << endl;
for (int i = 0; i < 21; i++) {
cout << uncommonDigitArray[i] << endl;
}
cout << "Using the results from extractUncommonDigit() -\n"
<< " The uncommon digit array has " <<
uncommonDigitArray[0] << " uncommon digit(s) of\n"
<< " from num --\n";
for (int i = 0; i < 10; i++) {
if (uncommonDigitArray[i] > 0) {
cout << i << " (occured " << uncommonDigitArray[i + 1]
<< " time(s))" << endl;
}
}
for (int i = 10; i < 20; i++) {
if (uncommonDigitArray[i] > 0) {
cout << i << " (occured " << uncommonDigitArray[i + 11]
<< " time(s))" << endl;
}
}
break;
}
} while (option != 4);
}
這是我的輸出:
*****************************************
* Menu - HW #1 *
* 1. Creating/Updating Fraction *
* 2. Displaying the Fraction *
* 3. Displaying Uncommon Digit(s) *
* 4. Quit *
*****************************************
Enter your option (1 through 4): 1
Enter an int for num: 123
Enter an int for denom: 110035
123 / 110035
A call to Fraction(int, int) was made to build a Fraction!
num : 123
denom : 110035
*****************************************
* Menu - HW #1 *
* 1. Creating/Updating Fraction *
* 2. Displaying the Fraction *
* 3. Displaying Uncommon Digit(s) *
* 4. Quit *
*****************************************
Enter your option (1 through 4): 3
The current Fraction object has
num : 123
denom : 110035
uncommonDigitsArray
300100000002000010000
digitCounterArray
01110000002201010000
uncommonDigitArray after copying
3
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
-842150451
Using the results from extractUncommonDigit() -
The uncommon digit array has 3 uncommon digit(s) of
from num --
0 (occured -842150451 time(s))
*****************************************
* Menu - HW #1 *
* 1. Creating/Updating Fraction *
* 2. Displaying the Fraction *
* 3. Displaying Uncommon Digit(s) *
* 4. Quit *
*****************************************
Enter your option (1 through 4):
如您所見,當我遍歷成員函數中的數組時,我得到了很好的結果。 當我返回數組的副本后遍歷數組時,我什么也沒得到。
任何關於我做錯事的建議將不勝感激。
我的直接想法是返回一個結構而不是單個整數。 像這樣:
struct UncommonDigits {
int uncommon_count;
int numerator_freq[10];
int denominator_freq[10];
};
這樣,您無需解釋數組的索引0意味着什么,因為“數組索引”現在具有名稱。
當您將結構作為參數傳遞或從函數返回時,將對其進行復制。 這正是您所需要的。 另一方面,在這些非常常見的情況下,數組的行為會很奇怪。 因此,將它們包裝在結構中通常是一個好主意。
UncommonDigits Fraction::extractUncommonDigit() {
UncommonDigits result = {0, {0}, {0}};
result.uncommon_count = ...;
result.nominator_freq[3] = ...;
return result;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.