输入一个字符串内有数字和非数字字符,将连续的数字作为一个整数,存到数组a中,统计整数的个数,并输出这些数

#include <iostream>
using namespace std;
int main()
{
 int statistic(char *,int *);
 char str[50],*pstr;
 int a[10],*pa,len;
 int m=0;
 pstr=str;
 pa=a;
 cout<<"please input a string:";
 gets(str);
 cout<<endl;
 len=strlen(str);
 cout<<"the length of the string is:"<<len<<endl;
 m=statistic(pstr,pa);
 cout<<"the number of the integer is:"<<m<<endl;
 cout<<"the integer are(is): "<<endl;
 for(int n=0;n<m;n++)
 {
  cout<<*(pa+n)<<" "; 
 }
 cout<<endl;
 return 0;
}
int statistic(char *pstr,int *pa)
{
 int i=0;          //i代表字符串中的第几个字符
 int j=0;    //j代表连续数字的个数
 int count=0;   //count代表有多少个整数
 int digit,k,e10,m;
 while(*(pstr+i)!='\0')
 {
  if(*(pstr+i)>='0' && *(pstr+i)<='9')
  { 
   j++;
  }
  else 
  { 
   if(j>0)
   {
    digit=*(pstr+i-1)-48;   //将个数位赋予digit
    k=1;
    while(k<j)                      //将含有两位以上数的其它位的数值累计于digit
    {
     e10=1;
     for(m=0;m<k;m++)
     {
      e10=e10*10;          //e10代表该位数所应乘的因子
     }
     digit=digit+(*(pstr+i-1-k)-48)*e10;   //将该位数的数值累加于digit
     k++;                                                     //位数k自增
    }
    *pa=digit;                  //将数值放在数组a中
    count++;
    pa++;                        //指针pa指向a数组下一元素
    j=0;
   }
  }
   i++;
 }
 if(j>0)                     //以数字结尾字符串的最后一个数据
 {
  digit=*(pstr+i-1)-48;
  k=1;
  while(k<j)
  {
   e10=1;
   for(m=0;m<k;m++)
   {
    e10=e10*10;
   }
   digit=digit+(*(pstr+i-1-k)-48)*e10;
   k++;
  }
 }
 *pa=digit;
 count++;
 j=0;
 return count;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值