strcmp ()
私の意見では、数値変換は必要ありません。ただし、この場合、そのうちの 1 つに数字のみを含む文字列が格納されていることを確認する必要があります。
memcmp ()
また、文字列で行うこともできます
編集1
先頭のゼロについて他の人が指摘したように、先頭のゼロを手動でスキャンして呼び出すstrcmp ()
かmemcmp ()
、ゼロ以外の最初の数字へのポインターを渡すことができます。
EDIT2
以下のコードは、私が言おうとしていることを示しています。これは浮動小数点数ではなく、整数専用です。
int main (void)
{
char s1[128], s2[128];
char *p1 = s1, *p2 = s2;
/* populate s1, s2 */
while (*p1 && (*p1 == '0'))
p1++;
while (*p2 && (*p2 == '0'))
p2++;
if (strcmp (p1, p2) == 0)
printf ("\nEqual");
else
printf ("\nNot equal");
printf ("\n");
return 0;
}
浮動小数点数の場合、小数点以下の末尾のゼロは手動で切り取る必要があります。
または、すべてを手動で行います。
EDIT4
また、この浮動小数点のコードもご覧ください。これにより、小数点の前の先頭のゼロと小数点の後の後続のゼロが検出されます。例えば
00000000000001.10000000000000
そして、以下のコードになり1.1
ますEqual
int main (void)
{
char s1[128], s2[128];
char *p1, *p2, *p1b, *p2b;
printf ("\nEnter 1: ");
scanf ("%s", s1);
printf ("\nEnter 2: ");
scanf ("%s", s2);
p1 = s1;
p2 = s2;
/* used for counting backwards to trim trailing zeros
* in case of floating point
*/
p1b = s1 + strlen (s1) - 1;
p2b = s2 + strlen (s2) - 1;
/* Eliminate Leading Zeros */
while (*p1 && (*p1 == '0'))
p1++;
while (*p2 && (*p2 == '0'))
p2++;
/* Match upto decimal point */
while (((*p1 && *p2) && ((*p1 != '.') && (*p2 != '.'))) && (*p1 == *p2))
{
p1++;
p2++;
}
/* if a decimal point was found, then eliminate trailing zeros */
if ((*p1 == '.') && (*p2 == '.'))
{
/* Eliminate trailing zeros (from back) */
while (*p1b == '0')
p1b--;
while (*p2b == '0')
p2b--;
/* match string forward, only upto the remaining portion after
* discarding of the trailing zero after decimal
*/
while (((p1 != p1b) && (p2 != p2b)) && (*p1 == *p2))
{
p1++;
p2++;
}
}
/* First condition on the LHS of || will be true for decimal portion
* for float the RHS will be . If not equal then none will be equal
*/
if (((*p1 == '\0') && (*p2 == '\0')) || ((p1 == p1b) && (p2 == p2b)))
printf ("\nEqual");
else
printf ("\nNot equal");
printf ("\n");
return 0;
}
使用前にテストが必要です。