C中的八进制到十进制

时间:2012-03-27 18:42:14

标签: c

我已经修改了一下代码并且具有以下内容。输出的数字现在更加真实,但它仍然不正确(例如,输入54#时输出为36,应该输出44)

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

main()
{
    printf("Please enter an octal number ending with # \n");
    int nextNum = getchar();
    int number[100];
    int numberOfSlots = 0; //Records how many digits are entered
    int power = 0; //Sets power of 8 during conversion
    int decimalNumber = 0;
    int i=0;

    while(nextNum != '#') //reads in the whole number, putting the characters together to form one Octal number.
        {
             if(nextNum >='0' && nextNum <='9')
                nextNum = (nextNum - '0');
             else{printf("Oops! That's not a valid number!");}

             number['i'] = (nextNum);
             //numberOfSlots++;
             i++;
             nextNum = getchar();
printf("%d\n", number['i']);

    }


    //Begin converson from Octal to Decimal

    for(i; i > 0; i--)
    {
        decimalNumber = decimalNumber + (number['i'] * pow(8,power));
        power++;
    }

    printf("%d", decimalNumber);

}

6 个答案:

答案 0 :(得分:2)

由于这是作业,我会给出一个提示而不是答案:

函数getchar()返回一个字符而不是一个数字。字符具有基于系统字符编码的值(请参阅ASCII)。

查看实际分配给数字的值,并将您从获得的值转换为实际数值应该是什么。

答案 1 :(得分:1)

埃里克已经确定了原因。这是你需要做的事情

int nextNum= getchar();
if ( nextNum >='0' && nextNum <='9' )
    nextNum = nextNum -'0';

number[numberOfSlots] = nextNum;

在声明时取消初始getchar()并在循环中使其成为通用

答案 2 :(得分:0)

printf("%d", number[numberOfSlots]);中您正在访问尚未初始化的索引(计数器numberOfSlots已预先递增。)

printf之后移动增量或通过numberOfSlots-1

中的printf访问元素

以上也是转化循环中的问题,因为numberOfSlots上的最后一个增量指向number中的最后一个有效元素。

另外,正如Eric J.所说,getchar()返回字符的整数代码,而不是字符所代表的数字(通常是ASCII值,但它取决于平台)。

答案 3 :(得分:0)

decimalNumber = (decimalNumber + (number[numberOfSlots] * (pow(8, power))));

使用pow()功能乞求麻烦。对于大整数,浮点值将不再包含整数值,并且您将具有舍入错误。在这种情况下,你不应该混淆使用浮点数学。你应该使用整数变量。

答案 4 :(得分:0)

#include <stdio.h>

main(){
    printf("Please enter an octal number ending with # \n");
    int number[21];
    int numberOfSlots = 0;
    int decimalNumber = 0;
    int nextNum, i;

    for(nextNum=getchar();nextNum != '#'; nextNum=getchar()){
        number[numberOfSlots++] = nextNum - '0';
    }

    for(i=0; i< numberOfSlots ; ++i){
        decimalNumber = decimalNumber * 8 + number[i];
    }

    printf("%d", decimalNumber);

}
/*
17777777777#
2147483647
*/
#if 0
#include <stdio.h>

int main(){
    int n, ch;
    printf("Please enter an octal number ending with # \n");

    for(n=0 ; '#' != (ch = getchar()); ){
        n = n * 8 + ch - '0';
    }

    printf("%d", n);
    return 0;
}
#endif

答案 5 :(得分:0)

固定

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

main()
{
    printf("Please enter an octal number ending with # \n");
    int nextNum = getchar();
    int number[100];
    int numberOfSlots = 0; //Records how many digits are entered
    int power = 0; //Sets power of 8 during conversion
    int decimalNumber = 0;
    int i=0;

    while(nextNum != '#') //reads in the whole number, putting the characters together to form one Octal number.
        {
             if(nextNum >='0' && nextNum <='9'){
                nextNum = (nextNum - '0');
                number[numberOfSlots] = nextNum;
                numberOfSlots++;

             } else{
                printf("Oops! That's not a valid number!");
             }

             nextNum = getchar();

    }


    //Begin converson from Octal to Decimal

    for(i=numberOfSlots-1; i >= 0; i--)
    {
        decimalNumber = decimalNumber + (number[i] * pow(8,power));
        power++;
    }

    printf("%d", decimalNumber);

}