代码如下:
#include <stdio.h>
#include <math.h>
int main(int argc, const char *argv[])
{
int sum = 0;
int m = 0;
int n = 0;
int i = 10;
int temp = 0;
scanf("%d",&sum);
temp = sum;
/*if(sum < -2147483648 || sum > 2147483647){
printf("输入数字错误,请重新输入\n");
return -1;
}*/
if((int)fabs(sum) > (int)pow(10,9)){
if(sum>=0){
while(i>0){
i--;
if(sum%10 > ((int)pow(2,31) - 1)/((int)pow(10,i))){
printf("输入数字倒叙错误,请重新输入\n");
return -1;
}
sum = sum/10;
}
}else if(sum<0){
while(i>0){
i--;
if(sum%10 < (-(int)pow(2,31))/((int)pow(10,i))){
printf("输入数字倒叙错误,请重新输入\n");
return -1;
}
sum = sum/10;
}
}
}else{
while(temp != 0){
m = temp%10;
temp = temp/10;
n = (m + n)*10;
}
n = n/10;
printf("%d\n",n);
}
return 0;
}
终端执行结果:
ubuntu@ubuntu:C_base$ ./a.out
-35664
-46653
ubuntu@ubuntu:C_base$ ./a.out
1864858
8584681
ubuntu@ubuntu:C_base$ ./a.out
-1111142753
输入数字倒叙错误,请重新输入
ubuntu@ubuntu:C_base$ ./a.out
2437752533
输入数字倒叙错误,请重新输入
ubuntu@ubuntu:C_base$ gcc 18_test.c -lm