gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:32:28 24 4
gpt4 key购买 nike

所以我正在为 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;
}

这是 utils 库

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));
}

如果代码太多或者有些内容不清楚,我深表歉意,这是我的第一篇文章。

您需要的任何信息只需询问

ps 该截止日期为伦敦时间今天 23:50,我正在尽快完成此任务

最佳答案

错误是 int intPow:

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

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

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

关于c - (C) 尽管值比变量的大小低一个数量级,但我似乎溢出了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59792263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com