[英]In C How to use leftmost digit from an integer
I was wondering how to reverse my output to match entered number. 我想知道如何反转我的输出以匹配输入的数字。 Example if user entered
543210
, I want the output to be: Five Four Three Two One Zero
. 例如,如果用户输入
543210
,我希望输出为: 543210
Five Four Three Two One Zero
。 But instead it's reversed and I can't figure out how to reverse it. 但是相反,它是反向的,我不知道该如何反向。
I can't use loops or anything else.
我不能使用循环或其他任何东西。
Code: 码:
int main(void){
int value;
int digit;
printf("enter:");
scanf("%i", &value);
while(value)
{
digit = value % 10;
value = value / 10;
if(digit != 0)
{
switch(digit)
{
case 0:
printf("zero ");
break;
case 1:
printf("one ");
break;
case 2:
printf("two ");
break;
case 3:
printf("three ");
break;
case 4:
printf("four ");
break;
case 5:
printf("five ");
break;
case 6:
printf("six ");
break;
case 7:
printf("seven ");
break;
case 8:
printf("eight ");
break;
case 9:
printf("nine ");
break;
}
}
}
return 0;
}
Exmaple: If user entered 1234 示例:如果用户输入1234
Output would be: four three two one. 输出将是:四三二一。
How would I fix it to be: One Two Three Four. 我要如何解决它:一二三四。
Since you've said that you aren't allowed to use loops, then recursion really is the thing that you are probably being expected to use. 既然您已经说过不允许使用循环,那么递归实际上就是您可能希望使用的东西。 I personally am not sure if it would be right to not consider a recursion as a loop, but whatever.
我个人不确定将递归视为循环而不是循环是否正确。
You are using a while
there, which also is a loop. 您
while
那里使用了一段while
,这也是一个循环。 If you are allowed to use loops, then you could just do the following, easy-to-understand modification in your code, and get the output you desire: 如果你被允许使用的循环,那么你可以只做到以下几点,易于理解你的代码修改,并得到你想要的输出:
...
int input; // <-- added this
int value;
int digit;
printf( "enter:" );
scanf( "%i", &input ); // <-- notice the change in variable usage
value = 0;
while ( input ) {
value = 10 * value + input % 10; // adds the last digit of input to value from right
input /= 10;
}
while ( value ) { ... }
...
If you aren't allowed to use loops, then you probably are expected to use a special function, a function which outputs a specific value for a single case, and returns back to itself in any other case. 如果不允许使用循环,则可能需要使用一个特殊的函数,该函数针对单个情况输出特定的值,并在其他情况下返回其自身。 You need a recursive function.
您需要一个递归函数。 Examine this simple example:
检查这个简单的例子:
// This is in maths, not C
f(x) = 2x + 1 for all integer x >= 0
Out of many ways, this one way to describe the function which maps 0
to 1
, then 1
to 3
, then n
to 2n + 1
. 在许多方法中,这种描述函数的方法是将
0
映射为1
,然后将1
映射为3
,然后将n
映射为2n + 1
。 If we wanted to define the exact same function recursively : 如果我们想递归定义完全相同的函数:
// In maths
f(x = 0) = 1 for x = 0
f(x > 0) = f(x-1) + 2 for integer x > 0
You see what's going on in there? 您知道那里发生了什么吗? It's saying that each subsequent
f(x)
is 2 greater than the previous one f(x-1)
. 也就是说,每个后续
f(x)
都比前一个f(x-1)
。 But more importantly, the function is calling itself! 但更重要的是,该函数正在调用自身! If you look closer, the called function
f(x-1)
will also call itself: 如果仔细看,被调用的函数
f(x-1)
也将自己调用:
f(x) = f(x-1) + 2
f(x) = f(x-2) + 2 + 2
f(x) = f(x-3) + 2 + 2 + 2
...
// all these are the same
All this calling deeper and deeper has to end somewhere, and that somewhere is when f(x-...)
is f(0)
, which has been explicitly defined to be 1
. 所有这些越来越深的调用必须在某个地方结束,并且那个地方是当
f(x-...)
是f(0)
,它已明确定义为1
。
This is what recursion is all about. 这就是递归的全部内容。 Let me write out the examples I gave above in C:
让我写出我上面在C语言中给出的示例:
// non-recursive version
int fnonrec( int x ){
return 2 * x + 1;
}
// recursive version
int frec( int x ){
if ( x == 0 )
return 1; // explicit return value for f(0)
else // redundant else, hehe
return frec( x - 1 ) + 2;
}
Definitions of the functions really look similar to how they were defined in maths, don't they? 函数的定义看起来确实类似于数学中的定义,不是吗? Yeah, well, I don't think giving you the answer for your question would be nice of me.
是的,我认为给您答案的答案对我来说不是很好。 All I can say is that you can print things in reverse really nicely with recursive functions.
我只能说,您可以使用递归函数很好地反向打印内容。
//store user input to int
variable " value
" //将用户输入存储到
int
变量“ value
”
char str[15];
sprintf(str, "%d", value);
You can then use the strrev
function to reverse the string array. 然后,您可以使用
strrev
函数反转字符串数组。 Manipulate it from there. 从那里操纵它。
#include <stdio.h>
void print(int v){
static char *numbers[] = {
"zero","one","two","three","four",
"five","six","seven","eight","nine"
};
int digit = v % 10;
int value = v / 10;
if(value){
print(value);
printf(" %s", numbers[digit]);
} else
printf("%s", numbers[digit]);
}
int main(void){
int value;
printf("enter:");
scanf("%i", &value);
print(value);
return 0;
}
Example using recursive function and numbers from the parameters : 使用递归函数和参数中的数字的示例:
#include <stdio.h>
void display(char c)
{
char *numbers[] = {
"zero","one","two","three","four",
"five","six","seven","eight","nine "
};
printf("%s ", numbers[c]);
}
int aff_num(char *c)
{
if (*c == '\0')
return (0);
display(*c-48);
aff_num(++c);
return (1);
}
int main(int argc, char **argv)
{
if (argc < 2)
{
printf("Need numbers\n");
return (-1);
}
aff_num(argv[1]);
return (0);
}
I'm a python hacker and I almost never program in C. that being said: 我是一个Python黑客,我几乎从不使用C编程。
#include <stdlib.h>
#include <stdio.h>
int highest_power_of_ten(int value){
int exponent = 0;
int tens = 1;
while(value > tens){
tens *= 10;
exponent += 1;
}
return exponent-1;
}
int pow(int base, int exponent){
if (exponent == 0)
return 1;
int temp = base;
while(exponent > 1){
base *= temp;
exponent -= 1;
}
return base;
}
int main(int argc, char** argv){
char* digits[] =
{"zero","one","two","three","four","five","six","seven","eight","nine"};
int value, n, exp, x;
scanf("%i", &value);
while(highest_power_of_ten(value)>0){
exp = highest_power_of_ten(value);
x = pow(10, exp);
n = value/x;
printf("%s ",digits[n]);
value -= n*x;
}
printf("%s\n", digits[value]);
//system("PAUSE"); for windows i guess
return 0;
}
Another method to get the digits in the right order: 另一种以正确顺序获取数字的方法:
Eg To get the digit at 1st position in 123 divide 123 by 100, to get 2nd - 123 / 10, to get 3rd 123 / 1. That equals: value / 10^(index of desired digit) So what we have to do is 例如,要获得123中第一位的数字,将123除以100,得到第二位-123/10,得到第三位123 /1。这等于:value / 10 ^(所需数字的索引)所以我们要做的是
calculate value := value - 10^length and start from 1, unless the result is 0 (mind handeling numbers that end on 0). 计算value:= value-10 ^ length并从1开始,除非结果为0(介意的数字以0结尾)。
while (value) { len = log10(value); digit = (int) value / pow(10, len); value -= pow(10, len); }
And your code does never enter case 0. To fix that just leave the if(digit != 0) - that's what I meant when I wrote "mind the 0"). 而且您的代码永远不会输入大小写0。要解决该问题,只需保留if(digit!= 0)-这就是我写“注意0”的意思。
if(digit != 0) // enters if digit is not 0
{
switch(digit)
{
case 0: // enters if digit is 0
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.