本帖最后由 大马强 于 2021-8-4 23:32 编辑 #include <stdio.h>
#include <math.h>
#define NUMBER ch - 48 + sum * b
int main()
{
int ch;
int b = 10;
double sum = -1;//long long int
int max = pow(2,31) - 1;
int min = pow(2,31) * (-1);
int d = 0;
printf("请输入待转换的字符串:");
while((ch = getchar()) != '\n')
{
while(ch >= '0' && ch <= '9')
{
// NUMBER = ch - 48 + sum * b;
if(sum == -1)
{
sum = 0;
}if(NUMBER > max || NUMBER < min)
{
d = 1;
break;
}
sum = NUMBER;
break;
}
if(!(ch >= '0' && ch <= '9'))
{
if(sum != -1)
{
break;
}
}
}
if(sum == -1)
{
printf("并未找到任何数值!\n");
}
else if(d)
{
printf("数值超出范围,结果未定义!\n");
}
else
{
printf("结果是:%d\n",sum);
}
return 0;
}
|