编写程序,把十六进制转换为十进制(可包含0x或0X)整数值。
代码如下:
#include <stdio.h>
#define YES1
#define NO0
char s[1000];
int htoi(char s[]);
int main()
{
printf("input a string:");
gets(s);
printf("The OX number is %d\n", htoi(s));
return 0;
}
int htoi(char s[])
{
int hexdigit, i, inhex, n;
i = 0;
if (s[i] == 0)
{
++i;
if (s[i]=='x' || s[i]=='X')
++i;
}
n = 0;
inhex = YES;
for ( ; inhex==YES; ++i)
{
if (s[i]>='0' && s[i]<='9')
hexdigit = s[i] - '0';
else if (s[i]>='a' && s[i]<='f')
hexdigit = s[i] - 'a' + 10;
else if (s[i]>='A' && s[i]<='a')
hexdigit = s[i] - 'A' + 10;
else
inhex = NO;
if (inhex = YES)
n = 16*n + hexdigit;
}
return n;
}
为何结果运行不了啦?出现“Process exited with return value 3221225477”
无结果。
代码如下:
#include <stdio.h>
#define YES1
#define NO0
char s[1000];
int htoi(char s[]);
int main()
{
printf("input a string:");
gets(s);
printf("The OX number is %d\n", htoi(s));
return 0;
}
int htoi(char s[])
{
int hexdigit, i, inhex, n;
i = 0;
if (s[i] == 0)
{
++i;
if (s[i]=='x' || s[i]=='X')
++i;
}
n = 0;
inhex = YES;
for ( ; inhex==YES; ++i)
{
if (s[i]>='0' && s[i]<='9')
hexdigit = s[i] - '0';
else if (s[i]>='a' && s[i]<='f')
hexdigit = s[i] - 'a' + 10;
else if (s[i]>='A' && s[i]<='a')
hexdigit = s[i] - 'A' + 10;
else
inhex = NO;
if (inhex = YES)
n = 16*n + hexdigit;
}
return n;
}
为何结果运行不了啦?出现“Process exited with return value 3221225477”
无结果。
