gpt4 book ai didi

c - 二进制 % 的操作数无效?

转载 作者:太空宇宙 更新时间:2023-11-04 02:44:43 27 4
gpt4 key购买 nike

我不知道出了什么问题,也不知道为什么会出现此错误。我四处搜索,但我终究无法弄明白。

void print_arb_base(unsigned int n, unsigned int b) {

char output[36] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

int t = n; //Keep track of the number

int s = 0; //The space we are currently on
while(pow(b, s) < t) {

s++;

}

s--;

for(int i = s; i >= 0; i--) {

int r = t % pow(b, i); //Gets number that goes into this part of base number
t = t - r * pow(b, i);

printf("%c", output[r]);

}

}

最佳答案

模数运算符 (%) 仅适用于整数操作数,而 pow() 返回 double 值。您需要将其结果转换为整数类型,或者更安全地使用 lrint() 之类的调用来舍入到最接近的整数结果。也许你的意思是:

int r = t % lrint(pow(b, i));

如果您尝试以任意基数打印数字,那么您可能是指:

unsigned long div = b;

while (div <= t)
div *= b;

div /= b;

for(; 0 != div; div /= b) {
unsigned long d = t / div;

t -= d * div;

printf("%c", output[d]);
}

关于c - 二进制 % 的操作数无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28574159/

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