[英]Reverse the output of printf function in while loop
I wrote a code for a b-adic representation of a chosen number.我为所选数字的 b-adic 表示写了一个代码。
#include <stdio.h>
int b_adisch (int a, int b)
{
int x, y, mod, mod1;
x = a / b;
mod1 = a % b;
printf("%i\n", mod1);
do {
y = x / b;
mod = x % b;
x = y;
printf("%i\n", mod);
} while(x != 0);
return a ;
}
int main (void)
{
int a, b;
printf("pls input a ");
scanf("%i", &a);
printf("pls input b ");
scanf("%i", &b);
b_adisch(a, b);
return 0;
}
The output order will be reversed since the printf has to be put into the while loop and the calculation starts with the last number of the representation.由于必须将 printf 放入 while 循环并且计算从表示的最后一个数字开始,因此输出顺序将被颠倒。
Example if a = 10 and b = 2示例如果 a = 10 且 b = 2
The output is 0101输出为 0101
but it should be 1010但应该是 1010
How can I change my code to make this happen?如何更改我的代码以实现此目的?
How can i change my code to make this happen?我怎样才能改变我的代码来实现这一点?
2 approaches: 2种方法:
Compute the digits from least to most significant and save in a adequate sized buffer.计算从最低到最高有效的数字并保存在足够大小的缓冲区中。 This is similar to OP's approach yet saves the results of each digit's computation for later printing.这类似于 OP 的方法,但会保存每个数字的计算结果以供以后打印。
#include <assert.h>
#include <limits.h>
void b_adisch(int value, int base) {
// Let us work with simple cases first.
assert(value >= 0);
assert(base >= 2 && base <= 10);
// Adequate sized buffer
char buffer[sizeof value * CHAR_BIT + 1];
// Start at end
char *end = &buffer[sizeof buffer - 1];
*end = '\0';
do {
end--;
int digit = value%base; // Find least digit
value /= base;
*end = digit + '0'; // save the digit as text
} while (value);
printf("<%s>\n", end); // print it as a string
}
Use recursion.使用递归。 A more radical change;更彻底的改变; This computes and prints the output of the more significant digits first.这首先计算并打印更有效数字的输出。
void b_adischR_helper(int value, int base) {
// If the value is at least 2 digits, print the most significant digits first
if (value >= base) {
b_adischR_helper(value/base, base);
}
putchar(value % base + '0'); // Print 1 digit as text
}
void b_adischR(int value, int base) {
// Let us work with simple cases first.
assert(value >= 0);
assert(base >= 2 && base <= 10);
printf("<");
b_adischR_helper(value, base);
printf(">\n");
}
Test测试
int main() {
b_adisch(10, 2);
b_adischR(10, 2);
b_adisch(INT_MAX, 10);
b_adischR(INT_MAX, 10);
b_adisch(INT_MAX, 2);
b_adischR(INT_MAX, 2);
}
Output输出
<1010>
<1010>
<2147483647>
<2147483647>
<1111111111111111111111111111111>
<1111111111111111111111111111111>
You can store the output in an array as here it is stored in "arr" and later print the output in reverse order (from end to start).您可以将输出存储在一个数组中,因为它存储在“arr”中,然后以相反的顺序(从结束到开始)打印输出。
#include <stdio.h>
int arr[10000]={0};
void b_adisch (int a, int b)
{
int x, y, mod, mod1,i=0,j;
x = a / b;
mod1 = a % b;
arr[i++]=mod1;
do {
y = x / b;
mod = x % b;
x = y;
arr[i++]=mod;
} while(x != 0);
for(j=i-1;j>=0;j--)
printf("%i\n",arr[j]);
}
int main (void)
{
int a, b;
printf("pls input a ");
scanf("%i", &a);
printf("pls input b ");
scanf("%i", &b);
b_adisch(a, b);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.