(C) 尽管值比变量大小低一个数量级,但我似乎溢出了

(C) i'm seemingly overflowing despite values being an order of magnitude below the size of the variable

所以我正在为 C 的介绍性 class 中的项目编写代码(将小数转换为二进制),并决定将二进制存储为小数而不是使用数字数组。该程序 运行 对于 1023 以内的数字没问题,而对于 1024 及以后的任何数字我都一团糟

这里是完整代码(调用 bclMain 运行):

void bclMain(void){
    int d,in,in2;

    do{
        in=bclMenu();
        switch(in){
        case 1://dec to bin
            do{
                printf("\tMENU CONVERSÃO DE DECIMAL PARA BINÁRIO\n\n");
                printf("1 - 8 bits sem sinal\n");
                printf("2 - 8 bits sinal e módulo\n");
                printf("3 - 8 bits complemento de 1\n");
                printf("4 - 8 bits complemento de 2\n");
                printf("5 - 16 bits sem sinal\n");
                printf("6 - 16 bits complemento de 2\n");
                printf("0 - Voltar ao menu anterior\n");
                in2 = selectOp();
                system("cls");
                switch(in2){
                case 1:
                    printf("8 bits\tsem sinal\n");
                    printf("Gama de valores [0,255]\n\n");
                    printf("valor a converter: ");
                    d=getNum(0,255);
                    printf("valor em binário: %d\n",decToBin(d,0,8));
                    break;
                case 2:
                    printf("8 bits\tsinal e módulo\n");
                    printf("Gama de valores: [-127,127]\n\n");
                    printf("Valor a converter: ");
                    d=getNum(-127,127);
                    printf("Valor em binário: %d",decToBin(d,3,8));
                    break;
                case 3:
                    printf("8 bits\tcomplemento de 1\n");
                    printf("Gama de valores: [-127,127]\n\n");
                    printf("Valor a converter: ");
                    d=getNum(-127,127);
                    printf("Valor em binário: %d",decToBin(d,2,8));
                    break;
                case 4:
                    printf("8 bits\tcomplemento de dois\n");
                    printf("Gama de valores: [-128,127]\n\n");
                    printf("Valor a converter: ");
                    d=getNum(-128,127);
                    printf("Valor em binário: %d",decToBin(d,1,8));
                    break;
                case 5:
                    printf("16 bits\tsem sinal\n");
                    printf("Gama de valores: [0,65535]\n\n");
                    printf("Valor a converter: ");
                    d=getNum(0,65535);
                    printf("Valor em binário: %llu",decToBin(d,0,16));
                    break;
                case 6:
                    printf("16 bits\tcomplemento de dois\n");
                    printf("Gama de valores: [-32768,32767]\n\n");
                    printf("Valor a converter: ");
                    d=getNum(-32768,32767);
                    printf("Valor em binário: %llu",decToBin(d,1,16));
                    break;
                }
                if(in2!=0){
                    pause("\nprima ENTER para continuar\n");
                    system("cls");
                }
            }while(in2!=0);
            break;
        case 2:
            do{
                break;
            }while(in2!=0);
        case 0:
            break;
        default:
            pause("Não implementado - prima ENTER para continuar");
            break;
        }
    }while(in!=0);
}

int bclMenu(void){
    int in;
    system("cls");
    printf("\tCONVERSOR DE BASES\n");
    printf("1 - Decimal\t-> Binário\n");
    printf("2 - Decimal\t-> Hexadecimal\n");
    printf("3 - Binário\t-> Decimal\n");
    printf("4 - Binário\t-> Hexadecimal\n");
    printf("5 - Hexadecimal\t-> Decimal\n");
    printf("6 - Hexadecimal\t-> Binário\n");
    printf("0 - Voltar ao menu anterior\n");
    in = selectOp();
    system("cls");
    return in;
}

unsigned long long int decToBin(int d,unsigned char mode,int digitNr){
    //d is decimal value, mode indicates binary format to output in
    unsigned long long int b=0;//b stands for binary, biggest int possible, should be able to store 19 digits (we needed at least 16)
    unsigned char digits[16]={0},tf1; //tf1 stands for true/false 1
    if(mode==0){//takes positive int and returns standard binary for it.
    for(int cc=0;d>0;cc++){
        //printf("%d\n",(d%2) * intPow(10,cc));
        b+=(d%2) * intPow(10,cc);
        d= d/2;
    }}
    if(mode==1){//two's complement
        if(d>=0)
            b=decToBin(d,0,digitNr);
        else{
            b=decToBin(-d,0,digitNr);
            for(int cc=0;b>0;cc++){
                digits[cc]=b%10;
                b=b/10;
            }//standard binary separated into digits
            //note - digits vector is backwards
            tf1=0;
            for(int cc=0;cc<digitNr;cc++){
                    //finding the first '1' and inverting everything after it
                    if(tf1){
                        if(digits[cc])
                            digits[cc]=0;
                        else
                            digits[cc]=1;
                    }
                    else if(digits[cc]==1)
                        tf1=1;
            }
            for(int cc=0;cc<digitNr;cc++){
                b+= digits[cc] * intPow(10,cc);
                //printf("digit - %d, multiplied - %d\n",digits[cc],digits[cc] * intPow(10,cc));
            }
        }
    }
    if(mode==2){
        if(d>=0)
            b=decToBin(b,0,digitNr);
        else{
            b=decToBin(-b,0,digitNr);
            for(int cc=0;b>0;cc++){
                digits[cc]=b%10;
                b=b/10;
            }//standard binary separated into digits
            //note - digits vector is backwards
            for(int cc=0;cc<digitNr;cc++){
                //invert all digits
                if(digits[cc])
                    digits[cc]=0;
                else
                    digits[cc]=1;
            }
            for(int cc=0;cc<digitNr;cc++){
                //put digits back into a number
                b+= digits[cc] * intPow(10,cc);
            }
        }
    }
    if(mode==3){
        if(d>=0)
            b=decToBin(d,0,digitNr);
        else
            b=decToBin(-d,0,digitNr)+intPow(10,digitNr);
    }
    return b;
}

这是实用程序库

void pause(char msg[]){
printf("%s",msg);
fflush(stdin);
getchar();
fflush(stdin);
}

unsigned long long int intPow(int a,unsigned int b){
    int cc;
    if(b==0)
        return 1;
    unsigned long long int c=a;
    for (cc=1;cc<b;cc++)
        a=a*c;//continuously multiply by base.
    return a;
}

int selectOp(void){
    int in;
    fflush(stdin);
    printf("Qual a sua opção?\n");
    scanf("%d",&in);
    return in;
}

int getNum(int min,int max){
    int in;
    do{
        scanf("%d",&in);
        if((min<=in)&&(in<=max))
            return in;
        printf("valor inválido\n\nInsira novo valor: ");
    }while((min>in)||(in>max));
}

如果代码太多或者有什么不清楚的地方,我深表歉意,这是我的第一个 post。

任何您需要的信息都可以问

ps今天到期23:50伦敦时间,我正在尽快完成这个

错误是整数intPow:

unsigned long long int c=a;
for (cc=1;cc<b;cc++)
    a=a*c;//continuously multiply by base.
return a;

传入的参数 a 的类型为 int,您要 return 的是 a 的值。所以对于任何大于 10 的 b 你都会溢出。您希望 c 成为您累积的变量并且 return:

for (cc=1;cc<b;cc++)
    c=c*a;
return c;