在C中转换为base [英] Convert to base in C

查看:61
本文介绍了在C中转换为base的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我是该领域的新手。不幸的是我不明白这个程序中的第三个功能我想知道

Hello,

I'm new in the field. Unfortunately I don't understand the third function in this program I want to know what do

for(--digit; digit >= 0; --digit )




and

baseDigits[nextDigit]





谢谢。



我尝试过:



.

Thank you.

What I have tried:

#include <stdio.h>
int convertedNumber[64];
long int numberToConvert;
int base;
int digit = 0;
void getNumberAndBase (void)

{
        printf ("Number to be converted? ");
        scanf ("%li", &numberToConvert); printf ("Base? "); scanf ("%i", &base);
        if ( base < 2 || base > 16 ) {
          printf ("Bad base - must be between 2 and 16\n"); base = 10; }
}
void convertNumber (void) {
        do { convertedNumber[digit] = numberToConvert % base;
             ++digit;
             numberToConvert /= base;
           }
              while ( numberToConvert != 0 );


}
void displayConvertedNumber (void) {
        const char baseDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        int nextDigit;
        printf ("Converted number = ");
        for (--digit; digit >= 0; --digit )
        {
          nextDigit = convertedNumber[digit];


          printf ("%c", baseDigits[nextDigit]);
        }
        printf ("\n");

}
int main (void) {
        void getNumberAndBase (void), convertNumber (void), displayConvertedNumber (void);
        getNumberAndBase ();
        convertNumber ();
        displayConvertedNumber ();
        return 0;
}

推荐答案

语句有四个部分:

A for statement has four parts:
for (a ; b ; c )
   d;

其中:

a 在循环开始之前执行

c 循环运行后执行

b 在c执行后进行测试

每次循环运行时都会执行d

在伪代码中它看起来像这样:



1)执行 a

2)如果(不是 b )goto(3)

2.1)执行 d

2.2)执行 c

2.3)转到(2)

3)完成。



所以在你的情况下:



1)从数字减去一个

2)if(digit< 0)退出循环。

2.1)执行以下行:

Where:
a is executed before the loop starts
c is executed after the loop has run
b is tested after c has executed
d is executed each time the loop runs.
In pseudo code it looks like this:

1) Execute a
2) If ( not b ) goto (3)
2.1) Execute d
2.2) Execute c
2.3) Goto (2)
3) done.

So in your case:

1) Subtract one from digit
2) if ( digit < 0 ) exit the loop.
2.1) Execute these lines:

nextDigit = convertedNumber[digit];
printf ("%c", baseDigits[nextDigit]);

2.2)从数字减去一个

2.3)循环到(2)并再次检查。



其他代码片段很简单: baseDigits 是一个数组,你的代码是

2.2) Subtract one from digit
2.3) Loop round to (2) and check again.

The other code fragment is simple: baseDigits is an array, and your code

baseDigits[nextDigit]

nextDigit 中的索引值给出的位置提取元素 - 所以如果索引为零,则获取第一个字符'0',如果它是1,它取'1',依此类推,最多14个'E'和15个'F'





你为家庭作业写了这段代码,所以你应该真正理解它的作用......

extracts the element at the position given by the index value in nextDigit - so if the index is zero, it fetches the first character '0', if it's one it fetches '1', and so on up to fourteen 'E' and fifteen 'F'


You wrote this code for your homework, so you should really understand what it does...


这篇关于在C中转换为base的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆