Description C语言中的10进制整数常量定义如下(不是严格的C定义,有简化):1) 常量的开头可以有“+”或“-”号,也可以没有;2) 至少有一位数字位,除非数字为0,否则第一个数字必须为1到9的数

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/10 07:05:30
Description C语言中的10进制整数常量定义如下(不是严格的C定义,有简化):1) 常量的开头可以有“+”或“-”号,也可以没有;2) 至少有一位数字位,除非数字为0,否则第一个数字必须为1到9的数

Description C语言中的10进制整数常量定义如下(不是严格的C定义,有简化):1) 常量的开头可以有“+”或“-”号,也可以没有;2) 至少有一位数字位,除非数字为0,否则第一个数字必须为1到9的数
Description
C语言中的10进制整数常量定义如下(不是严格的C定义,有简化):
1) 常量的开头可以有“+”或“-”号,也可以没有;
2) 至少有一位数字位,除非数字为0,否则第一个数字必须为1到9的数字;
3) 如果有符号的话,符号与数字之间可以有0个或多个空格.
4) 常量的前后可能会有若干空格.
请写一个程序判断给定的整数是否符合这个定义.
输入
每个样例一行,每行表示一个整数,字符长度不超过20;
输出
每行输出一个样例的结果,如果符合定义输出“Yes”,否则输出“No”
Sample Input
-123
5F
Sample Output
Yes
No
+++++++++++++++++++++++++++++++++++++++++++++++++++++
下面是我写的
#include
using namespace std;
int main()
{
\x05
\x05char a[20];
\x05while(1)
\x05{
\x05\x05int di=0;
\x05\x05int len;
\x05\x05gets(a);
\x05\x05int i=0;//记录位置
\x05\x05len=strlen(a);//获取a的长度
if(a[i]==' ')
\x05\x05{
\x05\x05\x05while(a[i]==' ')
\x05\x05\x05\x05i++;
\x05\x05}//有空格,i++,跳过空格
\x05\x05
\x05\x05\x05 if(a[i]=='+'||a[i]=='-')
\x05\x05\x05 {
\x05\x05\x05\x05 i++;
\x05\x05\x05 }//有符号,跳过符号.
\x05\x05\x05
\x05\x05\x05\x05 if(a[i]==' ')
\x05\x05\x05\x05\x05 {
\x05\x05\x05\x05\x05\x05 while(a[i]==' ')
\x05\x05\x05\x05\x05\x05\x05 i++;
\x05\x05\x05\x05\x05 }//有空格,跳过空格.
\x05\x05\x05\x05\x05
\x05\x05\x05\x05\x05\x05 while(a[i]!=' '&&a[i]!='\0')
\x05\x05\x05\x05\x05\x05 {
\x05\x05\x05\x05\x05\x05\x05 if(a[i]'9')
\x05\x05\x05\x05\x05\x05\x05 {
\x05\x05\x05\x05\x05\x05\x05\x05 di++;
\x05\x05\x05\x05\x05\x05\x05\x05 break;
\x05\x05\x05\x05\x05\x05\x05 }//排除非数字
\x05\x05\x05\x05\x05\x05\x05 if(a[i]=='0')
\x05\x05\x05\x05\x05\x05\x05 {
\x05\x05\x05\x05\x05\x05\x05\x05 if(a[++i]==' ')
\x05\x05\x05\x05\x05\x05\x05\x05 {
\x05\x05\x05\x05\x05\x05\x05\x05\x05 while(a[i]==' ')
\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05 i++;
\x05\x05\x05\x05\x05\x05\x05\x05 }
break;
\x05\x05\x05\x05\x05\x05\x05 }//等于0的特殊情况;
\x05\x05\x05\x05\x05\x05\x05\x05
\x05\x05\x05\x05\x05\x05\x05
\x05\x05\x05\x05\x05\x05\x05 i++;
\x05\x05\x05\x05\x05\x05 }//检验数字部分,DI为标志位
\x05\x05
\x05\x05\x05\x05\x05\x05 if(a[i]==' ')
\x05\x05\x05\x05\x05\x05 {
\x05\x05\x05\x05\x05\x05\x05 while(a[i]==' ')
\x05\x05\x05\x05\x05\x05\x05\x05 i++;
\x05\x05\x05\x05\x05\x05 }//i应该到达底部
\x05\x05\x05 if(di!=0)
\x05\x05\x05\x05 {
\x05\x05\x05\x05\x05 cout

Description C语言中的10进制整数常量定义如下(不是严格的C定义,有简化):1) 常量的开头可以有“+”或“-”号,也可以没有;2) 至少有一位数字位,除非数字为0,否则第一个数字必须为1到9的数
#include
using namespace std;
int main()
{
char a[20];
while(1)
{
int di=0;
int len;
gets(a);
int i=0;//记录位置
len=strlen(a);//获取a的长度
if(a[i]==' ')
{
while(a[i]==' ')
i++;
}//有空格,i++,跳过空格
if(a[i]=='+'||a[i]=='-')
{
i++;
}//有符号,跳过符号.
if(a[i]==' ')
{
while(a[i]==' ')
i++;
}//有空格,跳过空格.
bool leadingzero = true;
int legalzero = 0;
while(a[i]!=' '&&a[i]!='\0')
{
if(a[i]'9')
{
di++;
break;
}//排除非数字
if(a[i]=='0'&&leadingzero)
{
legalzero = 1;
}//等于0的特殊情况;
else if(legalzero == 1)
{
legalzero = 2;
}
leadingzero = false;
i++;
}//检验数字部分,DI为标志位
if(a[i]==' ')
{
while(a[i]==' ')
i++;
}//i应该到达底部
if(di!=0)
{
cout