新修改的代码如下,需要解决的问题同上
/*例如:-3.5转为-3、-5->-5、-0.3->0、0->0、0.56->0、8->8其它形式的都是错误*/
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
main()
{
char x[20],ch,*str;
int b,i,j,k,n,sign;
do
{
i=0,sign=0;
printf("Please input a integer:\n");
scanf("%s",&x);
if (x[i]=='e'||x[i]=='E') {printf("Exit!");break;} /*按E键退出!*/
if(x[i])
{for(j=58;j<=255;j++)
{
ch=j;
str=memchr(x,ch,strlen(x)); /*在数组X的前N个字节中检测CH字符*/
if(str){printf("include character(ASCII58-255)->%c\n",ch);sign=1;break;}
/*检测58-255之间的非法字符*/
}
}
if(x[i])
{
for(k=0;k<43;k++)
{
ch=k;
str=memchr(x,ch,strlen(x));
if(str){printf("include character(ASCII 0-42)->%c\n",ch);sign=1;break;}
}
}
if(x[i])
{
for (n=44;n<=47;n+=3)
{
ch=n;
str=memchr(x,ch,strlen(x));
if(str){printf("Include character(ASCII44&&47) ->%c\n",ch);sign=1;break;}
}
}
}while(sign==1) ;
b=atoi(x); /*字符转换为整形数字*/
if(b==0)
{
if(x[i]>='0'&&x[i]<'1') /*此外过滤输入的是0<=X<1的情况,因为0.5等数字atoi()后也是0*/
{printf("I0nt=%d",b);}
else
printf("Input error!");
}
else
{
printf("Intb=%d",b);
}
getch();
}