c - 在 C 中没有正确使用 strcmp

标签 c string binary strcmp

我编写此方法是为了找到适合给定小数的 2 的最大幂。小数点为char数组格式,避免数字存储输入溢出错误。 2 的幂是用 float 的 pow(2, power) 格式计算的,即。 8.000000 然后将此数字发送到一种方法以删除句点和尾部的 0。 IE。 8.000000 变成 8

1  #include <string.h>
2  #include <stdio.h>
3  #include <stdlib.h>
4  #include <memory.h>
5  #include <math.h>
6
7   int i;
16
17  void removeFloatZeros(char *floatvalue)
18  {
19      char *ptr = strchr(floatvalue, '.');
20      *ptr = '\0';
21  }
22
45
173 char *decimalToBinary(char *decimal)
174 {
176     int x;
177     double power = 0;
178     char *binary = malloc(sizeof(char *) * 1024);
179     char *twosPower = malloc(sizeof(char *) * 1024);
180
181     /* What is the greatest power of 2 that will fit into the decimal? */
182     for(x = 0; x <= 30; x++)
183     {
184         power = pow(2.0, x);
185         snprintf(twosPower, 1023, "%f", power);
186         removeFloatZeros(twosPower);
189         printf("strcmp(decimal, twosPower) = %d\n", strcmp(twosPower, decimal));  
190         memset(twosPower, '\0', 1023);
191     }
214 }
215
216 int main(int argc, char*argv[])
217 {
218     char *dec1 = argv[1];
219     decimalToBinary(dec1);
220     return 1;
221 }
222

例如,如果我在 argv[1] 中输入 20,它将输出:

strcmp(decimal, twosPower) = -1
strcmp(decimal, twosPower) = -1  
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = -1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = -1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = -1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = -1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = -1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = -1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = -1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = -1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = 1
strcmp(decimal, twosPower) = -1

我哪里出错了?另外,忽略 for 循环的结束条件。它应该在第 6 次迭代之前输出全 1,在第 6 次和第 6 次迭代之后输出所有 -1。

最佳答案

strcmp 返回值:

零值表示两个字符串相等。

大于零的值表示第一个不匹配的字符在 str1 中的值大于在 str2 中的值。

小于零的值表示相反。

您的输入:20 循环的第一次迭代:twosPower = "1" strcmp("20", "1")

第一个字符不匹配并且在 str2 ("1") 中的值小于在 str1 ("2") 中的值 -> 返回负值。

其余的迭代应该自行解释...

此外,编辑:

printf("strcmp(decimal, twosPower) = %d\n", strcmp(twosPower, decimal)); 

您的 printf 格式字符串与您在参数中所做的相反。

编辑:

str1    str2    
1       20  First char that differs is '1' vs. '2'. '1' (ASCII 49) is smaller than '2' (ASCII 50), 49 - 50 = -1 = return value
2       20  First char that differs is '\0' vs. '0'. '\0' (ASCII 0) is smaller than '0' (ASCII 48), 0 - 48 = -48 = return value
4       20  First char that differs is '4' vs. '2'. '4' (ASCII 52) is greather than '2' (ASCII 50), 52 - 50 = 2 = return value
8       20  First char that differs is '8' vs. '2'. '4' (ASCII 56) is greather than '2' (ASCII 50), 56 - 50 = 6 = return value
16      20  First char that differs is '1' vs. '2'. '1' (ASCII 49) is smaller than '2' (ASCII 50), 49 - 50 = -1 = return value

... and so on ...

也许这个输出有更多帮助

此外,您查找数字中 2 的最大幂的方法存在缺陷,因为 strcmp 的返回值仅取决于不同的第一个字符。 所以 strcmp("2", "16") 和 strcmp("200000000", "16") 总是会返回同样的东西。

关于c - 在 C 中没有正确使用 strcmp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9338275/

相关文章:

c - 前序迭代遍历

代码存储器和数据存储器

javascript - 如何使用 Javascript 进行编码和解码(给定的字符串长度应与编码字符串长度匹配)

javascript - 检查字符串是否包含任何没有正则表达式的字符串数组

string - 如何在不使用像 MissingH 这样的外部库的情况下,用 Haskell 中的另一个替换字符串的子字符串?

python - 如何通过 HTTP 发送二进制 post 数据?

python - 为什么二进制表示与 python 编译器不同于我们在纸上所知道的?

c - 通过翻译复制内存的快速方法 - ARGB 到 BGR

c++ - 为什么在比较中将常量放在变量之前?

通过三元条件选择的指针调用函数