C语言 如何从三位数的整数中提取一位数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17690360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:55:08  来源:igfitidea点击:

How do I extract a single digit from a 3 digit whole number?

cint

提问by Karen

This isn't a homework question, I'm just curious. If I had a program that calculated a 3 digit number, say 123, how can I get just the "1"? I'm trying to print a message at the end that says "(The first digit) tells you...and (the last two digits) tell you..." But I'm not sure HOW to save or get that single digit. Any ideas? Is the a simpler way to do this other than using an array?Thanks.

这不是家庭作业问题,我只是好奇。如果我有一个计算 3 位数字的程序,比如 123,我怎样才能得到“1”?我试图在最后打印一条消息,上面写着“(第一个数字)告诉你......并且(最后两位数字)告诉你......”但我不确定如何保存或获得那个单曲数字。有任何想法吗?除了使用数组之外,还有更简单的方法吗?谢谢。

回答by Shafik Yaghmour

You can use integer division by 100:

您可以使用整数除法100

#include <stdio.h>

int main()
{
  printf( "%d\n", 123/100 ) ;

  return 0 ;
}

A more generalized approach would use subsequent rounds of modulus by 10and integer division by 10to remove the last digit until the number is less than 10:

更通用的方法是使用后续轮模10除法和整数除法10来删除最后一位数字,直到数字小于10

int num = 123 ;
while( num >= 10 )
{
    printf( "%d\n", num % 10 ) ;
    num = num / 10 ;
}
printf( "%d\n", num  ) ;

If you can display your digits in reverse order from last to first this method does not require any additional storage, if not you can store the results in an array.

如果您可以从最后到第一个以相反的顺序显示您的数字,则此方法不需要任何额外的存储空间,否则您可以将结果存储在一个数组中。

回答by loxxy

  • Get the length of the number.
  • Iterate & get each digit.
  • 获取数字的长度。
  • 迭代并获取每个数字。

Here is a sample:

这是一个示例:

#include <stdio.h>
#include <math.h>

int main ()
{
  int n = 123, i; char buffer [33];

  int len = n==0 ? 1 : floor(log10l(abs(n)))+1;

  for(i=n;len--; i=(int)(i/10)) buffer[len] = (i%10);

  printf("%d", buffer[0]);   // First Digit
  printf("%d", buffer[1]);   // Second Digit
  printf("%d", buffer[2]);   // Third Digit... so on

  return 0;
}

回答by 0decimal0

If you want to do it simple way , I mean, if you want to make it program for just one number e.g, 123 then the first example by Shafik is enough.

如果您想以简单的方式进行操作,我的意思是,如果您只想为一个数字编程,例如 123,那么 Shafik 的第一个示例就足够了。

If you would like to take out the digits from the end then the second example of Shafik is good enough.

如果您想从末尾取出数字,那么 Shafik 的第二个示例就足够了。

Suggestions are welcome , if anybody sees improvement , thank you :)

欢迎提出建议,如果有人看到改进,谢谢:)

What about taking the digits out from the beginning, here is my different take on your question , I have taken the digits out from the beginning like this :

从头开始取出数字怎么样,这是我对你的问题的不同看法,我从头开始取出数字是这样的:

#include<stdio.h>
int main()
{
  int di , i , num , pow = 1;
  setbuf ( stdout , NULL);
  printf ("enter the number of digits of the number\n");// specify at run time what will be the number of digits of the array.
  scanf ( "%d" , &di);
  int a[di];

  printf ("enter the %d digit number:\n",di);// 
  scanf ( "%d" , &num);//One thing is to be noted here that user can enter more digits than specified above , It is up to user to enter the specified digits , you can improve this program by making a check whether user enters the specified digits or not , better do it as exercise.
  while( di > 1 )
    {
    pow = pow * 10;
    di--;
    }

  i = 0;
  while ( num > 0)
    {
      a[i]=num/pow;
      num=num%pow;
      pow=pow/10;
      i++;
    }
  printf("the digits from the beginning are :\n"); 
  for(int j = 0 ; j < i ; j++)
    printf("%d\n",a[j]);
  return 0;
}

Important-- When using an array to store the digits if user enter more digits than specified then extra digits will printed as the first element of the array, As I said you can improve this program further if you want and make a check on the user entering the number of digits , good luck :)

重要- 当使用数组存储数字时,如果用户输入的数字多于指定的数字,则额外的数字将打印为数组的第一个元素,正如我所说,如果需要,您可以进一步改进此程序并对用户进行检查输入位数,祝你好运:)

NOTE-- This is just a different way of looking at a problem, both the solutions will eventually yield the same results. I just thought to put it this way.

注意——这只是看待问题的不同方式,两种解决方案最终都会产生相同的结果。我只是想这样说。