strcmp()
私の意見では、数値の変換は必要ないため、しかし、この場合は、そのうちの1つに数字だけの文字列が格納されていることを確認する必要があります。あなたは
EDIT1
としては、先行ゼロについて他の人が指摘した文字列にmemcmp()
を行うことができます。また
、手動で先行ゼロをスキャンして、ポインタを渡すことによってstrcmp()
またはmemcmp()
を呼び出すことができます最初のゼロ以外の数字に変更します。
EDIT2
以下のコードは、私が言うことを試みているものを伝えます。これは整数のみであり、浮動小数点数ではありません。
浮動小数点数の場合、小数点以下の後続ゼロは手動で切り捨てる必要があります。
また、手作業全体を手動で行うこともできます。
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;
}
になります。
ありがとうございます。これは、関数のstr *ファミリに多く依存しているので、私が常に使用するメソッドです。実際には標準のように見えます。知っておいてよかった! – Valdogg21
@ Valdogg21:私が更新したコードを見てください。これで、設定したプロトコルに従って浮動小数点数を比較できます。 – phoxis