识别数字

/识别数字(10分) 题目内容:编写函数,输出字符串中的所有整数。要考虑正、负数。编写主函数,输入带空格的字符串,调用函数输出其中的整数。输入格式:一行,表示一个句子,中间可能有空格,有若干整数。输出格式:一行,若干整数,用一个空格隔开,末尾无空格。注意:单独符号不算数字,但-0为0,+0也为0。输入样例:CHINA DAILY | Updated: 2020-03-21 07:57输出样例:2020 -3 -21 7 57/
#include
using namespace std;
int fun(char str[],int a[])
{
 int i,t=0,count=0,j=0,m=0;
 for(i=0;;i++)
 {
  if(str[i]>=‘0’&&str[i]<=‘9’)
  {
   t=t*10+str[i]-‘0’;
   if(str[i-1]’-’)
   {
    m=1;
   }
   count++;
  }
  else
  {
   if(m
1)
   {
    t=-t;
   }
   if(count!=0)
   {
    a[j]=t;
    j++;
   }
   count=0;
   t=0;
   m=0;
  }
  if(str[i]’\0’) break;
 }
 return j;
}
int main()
{
 char str[101]={0};
 int a[100],m,i;
 cin.getline(str,100);
 m=fun(str,a);
 for(i=0;i<m;i++)
 {
  if(i
m-1) cout<<a[i]<<endl;
  else cout<<a[i]<<" ";
 }
 return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_46226815/article/details/105075109
今日推荐