我一直在尝试完成这段代码(函数)一段时间,但被困在最后一部分。在这段代码中,我提示用户选择多个整数和任意位数,然后在这些数字中找到最小值和最大值。在下一部分中,我应该确定最小和最大的给定数字中的哪一个,以便输出应该是:
数字 _ 可以在整数中找到:_, _
如果我的代码草率,我提前道歉;我刚开始学习 C++,还没有完全掌握这门语言。
int digitSizeLoca() {
int userNumInteger;
int* iPtr;
int* iPtr2;
int* iPtr3;
int value;
int value2;
int value3;
std::cout << "\nHow many integers? ";
std::cin >> userNumInteger;
iPtr = new int[userNumInteger];
iPtr2 = new int[userNumInteger];
iPtr3 = new int[userNumInteger];
for (int i = 0; i < userNumInteger; i++) {
*(iPtr3 + 1) = *(iPtr2 + 1) = *(iPtr + 1);
std::cout << "\nEnter digit #" << i + 1 << ": ";
std::cin >> *(iPtr + 1);
}
value = *(iPtr + 1);
value2 = *(iPtr2 + 1);
value3 = *(iPtr3 + 1);
if (value != 0, value2 != 0, value3 != 0) {
if (value <= 0)
value = -value;
if (value2 <= 0)
value2 = -value2;
if (value3 <= 0)
value3 = -value3;
int lDigit;
int sDigit;
int curDigit;
int pot = 10;
lDigit = sDigit = value % pot;
while (value, value2, value3) {
if (value / pot == 0, value2 / pot == 0, value3 / pot == 0) break;
curDigit = (value / pot, value2 / pot, value3 / pot) % 10;
if (curDigit < sDigit)
sDigit = curDigit;
if (curDigit > lDigit)
lDigit = curDigit;
pot*=10;
}
std::cout << "\nThe smallest digit: " << sDigit << std::endl
<< "\n Digit " << sDigit
<< " can be found in integer number(s): ";
std::cout << "\nThe largest digit: " << lDigit << std::endl
<< "\n Digit " << lDigit
<< " can be found in integer number(s): ";
}
return 0;
}
应该给用户输入什么输出的例子:
如果用户为 userNumInteger 选择 2,并输入数字值 1234 和 -1578,
我的问题的输出应该是:
最小数字:1 数字 1 可以在整数中找到: 1, 2 。. .
谢谢!