问题
void print(int num, int digits)
{
double mod, divi;
int mod1, divi1;
mod = pow(10, digits);
divi = pow(10, digits-1);
mod1 = (int)mod;
divi1 = (int)divi;
if(digits > 0)
{
printf("%d\n", ((num%mod1)/divi1));
digits--;
print(num, digits);
}
else
{
return;
}
}
This function is meant to print the digits from num
vertically, however when I run it the only thing it prints are 0's. I know it gets the correct number of digits because it prints the same number of zeroes as there are digits in the number, so something must be wrong with my use of Modulo or Division here, and I can't quite seem to find it. Any help would be appreciated thank you!
The function to get the numbers(maybe the problem is here?):
void getNum()
{
int num, digits=0;
printf("Enter a number to be printed vertically: ");
scanf("%d", &num);
if(num < 0)
{
digits = 1;
}
while(num)
{
num = num/10; //Here was the problem. Num was 0 once i passed it to print()
digits++; //Simply added a new variable to fix it. Thanks all!
}
print(num, digits);
}
回答1:
I don't see anything wrong with your code, except that it's much too complicated and unnecessarily uses floating-point maths.
Here is an alternative:
void print(int num, int digits)
{
if (digits > 0) {
print(num / 10, digits - 1);
printf("%d\n", num % 10);
}
}
The trick is to move the printing after the recursive call. This allows for much simpler logic.
回答2:
This is a fix for my problem. I was setting num
to 0
before passing it to the function. I simply declared another variable num1
to use in the while loop instead of the actual num
which is passed to the function.
Code
void getNum()
{
int num, num1, digits=0;
//get the num
printf("Enter a number to be printed vertically: ");
scanf("%d", &num);
num1 = num;
//Loop to find how many digits are in the number
while(num1)
{
num1 = num1/10;
digits++;
}
//call function to print
print(num, digits);
}
回答3:
int print(int num, int digits)
{
int min = 0;
if (digits > 0) {
min = print(num / 10, digits - 1);
if(num %10)
printf("%s%d\n", num < 0 && !(min++) ? "-" : "", abs(num % 10));
}
return min;
}
int print1(int num, int digits)
{
int min = 0;
if (digits > 0) {
min = print1(num / 10, digits - 1);
if(num %10)
{
printf("%s", num < 0 && !(min++) ? "-\n" : "");
printf("%u\n", abs(num % 10));
}
}
return min;
}
int main()
{
print(-1234, 9);
printf("\n-------------\n\n");
print1(-23456565, 9);
}
回答4:
By little change on NPE answer I reach to version of code that eliminate digits
argument and works for negative numbers.
Code
void print(int num)
{
if (num < 0){
num *=-1;
}
if (num > 0) {
print(num / 10);
printf("%d\n", num % 10);
}
}
[Edit1 and 2]: Correct INT_MIN
overflow
Thanks for chux for his constructive comments I correct potential INT_MIN negation overflow. Another similar topic by chux .
void print(int num)
{
#if INT_MIN < -INT_MAX
///In two's complements systems.
///handle special case
if ( num == INT_MIN ) {
//fprintf(stderr, "Houston, we have a problem\n");
// Maybe return or exit here.
//~ 2147483647 -> 2147483648
print(INT_MAX/10);
print(INT_MIN%10); //portable version ///print(8);
return;
}
if (num < 0){
//num =-((unsigned)num);
num *=-1;
}
#endif
if (num > 0) {
print(num / 10);
printf("%d\n", num%10);
}
}
Now if we call print()
int number = INT_MIN;// a negative number
print(-120);
printf("\n");
print(-321);
print(number);
Output
1
2
0
3
2
1
Houston, we have a problem
[Edit3]
Change code to work for also print(0)
.
int zeroSignal=1;
void print(int num)
{
#if INT_MIN < -INT_MAX
///In two's complements systems.
///handle special case
if ( num == INT_MIN ) {
///fprintf(stderr, "Houston, we have a problem\n");
// Maybe return or exit here.
//~ 2147483647 -> 2147483648
print(INT_MAX/10);
print(INT_MIN%10); //portable version ///print(8);
return;
}
if (num < 0){
//num =-((unsigned)num);
num *=-1;
}
#endif
if (num==0 && zeroSignal){
printf("Only zero: %d\n", 0);
}
if (num > 0) {
zeroSignal = 0;
print(num / 10);
printf("%d\n", num%10);
}
zeroSignal = 1;
}
In above code I use global integer zeroSignal
to implement a way that works for print(0)
.
来源:https://stackoverflow.com/questions/26349387/extracting-digits-from-an-integer-in-c