[英]not using strcmp properly in C
我編寫了此方法,以找到適合給定十進制數的2的最大冪。 小數點采用char數組格式,以避免在存儲數字時出現輸入溢出錯誤。 2的冪是使用float的pow(2,power)格式計算的。 8.000000然后將這個數字發送到一種方法來刪除句點和0的那個尾跡。 即。 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 ...
也許這個輸出有幫助
此外,由於strcmp的返回值僅取決於不同的FIRST字符,因此您找到數字最大2的冪的方法存在缺陷。 因此strcmp(“ 2”,“ 16”)和strcmp(“ 200000000”,“ 16”)總是會返回相同的內容。
此代碼的清理版本產生的輸出如下所示:
$ ./xx 20
strcmp(1, 20) = -1
strcmp(2, 20) = -48
strcmp(4, 20) = 2
strcmp(8, 20) = 6
strcmp(16, 20) = -1
strcmp(32, 20) = 1
strcmp(64, 20) = 4
strcmp(128, 20) = -1
strcmp(256, 20) = 5
strcmp(512, 20) = 3
strcmp(1024, 20) = -1
strcmp(2048, 20) = 52
strcmp(4096, 20) = 2
strcmp(8192, 20) = 6
strcmp(16384, 20) = -1
strcmp(32768, 20) = 1
strcmp(65536, 20) = 4
strcmp(131072, 20) = -1
strcmp(262144, 20) = 6
strcmp(524288, 20) = 3
strcmp(1048576, 20) = -1
strcmp(2097152, 20) = 57
strcmp(4194304, 20) = 2
strcmp(8388608, 20) = 6
strcmp(16777216, 20) = -1
strcmp(33554432, 20) = 1
strcmp(67108864, 20) = 4
strcmp(134217728, 20) = -1
strcmp(268435456, 20) = 6
strcmp(536870912, 20) = 3
strcmp(1073741824, 20) = -1
$
可以說,這里有很多小的變化。
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
static void removeFloatZeros(char *floatvalue)
{
char *ptr = strchr(floatvalue, '.');
*ptr = '\0';
}
static void decimalToBinary(char *decimal)
{
int x;
double power = 0;
char *twosPower = malloc(sizeof(char *) * 1024);
/* What is the greatest power of 2 that will fit into the decimal? */
for(x = 0; x <= 30; x++)
{
power = pow(2.0, x);
snprintf(twosPower, 1023, "%f", power);
removeFloatZeros(twosPower);
printf("strcmp(%s, %s) = %d\n", twosPower, decimal, strcmp(twosPower, decimal));
//printf("strcmp(decimal, twosPower) = %d\n", strcmp(twosPower, decimal));
memset(twosPower, '\0', 1023);
}
free(twosPower);
}
int main(int argc, char*argv[])
{
for (int i = 1; i < argc; i++)
decimalToBinary(argv[i]);
return 0;
}
顯示比較值使事情更容易理解。 您需要釋放內存(或使用自動數組)。 您需要使用標題。 static
聲明不是100%必需的,但是意味着在über-fussy編譯器設置下進行編譯時,我不會收到任何警告。
但是關鍵的變化是打印出要比較的值-它使strcmp()
中的數字有意義。
(在進行比較之前,請考慮添加適當數量的前導零。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.