Программа, которую я пишу, использует класс, который представляет дробь.У меня есть функция-член класса, которая берет два целых числа (числитель и знаменатель) из переменных члена класса и сравнивает их, чтобы выяснить, есть ли какие-либо необычные цифры.
(числитель 123 и знаменатель 110035 будут иметь 3 необычные цифрыбудучи 2 и 0 и 5, потому что эти цифры уникальны для их int).
Моя функция-член должна вывести массив, который содержит данные о том, сколько существует необычных цифр, какие цифры конкретно, и сколько разцифра появляется в соответствующем int (числитель или знаменатель).
У меня есть массив внутри функции-члена uncommonDigitArray, который отформатирован следующим образом:
Индекс 0 содержитint представляет количество необычных цифр
Индексы 1 - 10 представляют цифры 0 - 9, и они содержат целые числа, представляющие, сколько раз эта цифра появляется в числителе.
Индексы 11 - 20 представляют цифры 0 - 9, и они содержат вts, представляющее, сколько раз эта цифра появляется в знаменателе.
Мне нужно передать этот массив другой функции, а затем выполнить итерацию по ней для правильного вывода информации.
Это функция-член, которая создает массив:
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):
Как вы можете видеть, когда я перебираю массив вфункция-член, я получаю хорошие результаты.Когда я перебираю массив после того, как возвращаю его копию, я ничего не получаю.
Буду очень признателен за любые советы о том, что я делаю неправильно.