简体   繁体   中英

How can I store a digit of an Integer I'm trying to separating?

The issue I'm having is, I want to take an integer and separate it. For example: The user enters: 23432. The console should print" 2 3 4 3 2. The issue I'm having is storing that digits. For example,

  User Input : 2020
  assign input to num.
  digit = 2020 % 10 = 0 <--- 1st Digit
  num = num / 10 = 202
  digit2 = num % 10 = 2 <--- 2nd Digit
  num = num / 100 = 20.2 
  temp = round(num) = 20
  digit3 = num % 10 = 0 <--- 3rd Digit
  digit4 = num / 10 = 2 <---- 4th Digit

The problem with this approach is that its dependent on the user input, I'm working with the range 1-32767, so I wont know how many digit variables to create. Using the structure I've created can someone assist in making it run in a way the no matter what the number is, the digit is saved and printed in the way I've described?

int Rem(int num);
  int Div(int num);

  int main() {
      int num;
      printf("Enter an integer between 1 and 32767: ");
      scanf("%d", &num);
      Rem(num);
      Div(num);
      printf("%d","The digits in the number are: ");

  }


      int Rem(int num) {
          int rem = num % 10;
          return rem;
      }

      int Div(int num){
          int div = num / 10;
          return div;
      }

The problem with this approach is that its dependent on the user input, I'm working with the range 1-32767, so I wont know how many digit variables to create.

So calculate it. You can do this by increasing a variable by a factor of 10 each time until increasing it one more time would make it larger than your input number:

int num;
printf("Enter an integer between 1 and 32767: ");
scanf("%d", &num);
int div = 1;
while(div * 10 <= num)
    div *= 10;

Then you can repeatedly divide your number by this divisor to get each of the digits, dividing the divisor by 10 each time to shift one place at a time:

printf("The digits in the number are: ");
while(div > 0)
{
    printf("%d ", (num / div) % 10);
    div /= 10;
}

That's a really complicated approach. Why not read the string, and parse the string out like this:

int main(void) {
  char buf[256]; // should be big enough, right? 10^256-1
  memset(buf, 0, 256];
  puts("enter something : ");
  if( NULL == fgets(STDIN, 255, buf)) {
    perror("Egad! Something went wrong!");
    exit(1);
  }
  // at this point, you already have all the input in the buf variable
  for(int i=0; buf[i]; i++) {
    putchar( buf[i] ); // put out the number
    putchar( ' ' ); // put out a space
  } 
  putchar( '\n' ); // make a nice newline
}

As written above, it allows any character, not just numbers. If you want to restrict to numbers, you could filter the input, or put a check in the for loop ... depending on what you were trying to accomplish.

One way that C allows you do deal with problems this extremely elegantly is via recursion.

Consider a routine that only knows how to print the very last digit of a number, with a preceding space if needed.

void printWithSpaces(int neblod)
{
    // Take everything except the last digit.
    int mene = neblod / 10;
    // Now extract the last digit
    int fhtagn = neblod % 10;

    // Check if there are leading digits
    if (mene != 0)
    {
        // There are, so do some magic to deal with the leading digits
        // And print the intervening space.
        putchar(' ');
    }
    putchar(fhtagn + '0');
}

OK. So that's well and good, except what can we use to "Do some magic to deal with the leading digits"?

Don't we want to just print them as a sequence of digits, with suitable intervening spaces?

Isn't that exactly what void printWithSpaces(int neblod) does?

So we make one change:

void printWithSpaces(int neblod)
{
    // Take everything except the last digit.
    int mene = neblod / 10;
    // Now extract the last digit
    int fhtagn = neblod % 10;

    // Check if there are leading digits
    if (mene != 0)
    {
        // There are, so print them out
        printWithSpaces(mene);
        // And print the intervening space.
        putchar(' ');
    }
    putchar(fhtagn + '0');
}

And you're done.

For the curious, the following article on C recursion may provide both an amusing read, and a little insight into my slightly unusual choice of variable names. ;) http://www.bobhobbs.com/files/kr_lovecraft.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM