C++——输入一个字符串,内有数字和非数字字符,如a123x456_ 17960?302tab5876将其中连续的数字作为一个整数, 依次存放到一数组a中。用指针或引用方法处理。

没注释的源代码

#include <iostream>
#include<stdio.h>
using namespace std;

int main()
{
    char str[50],*pstr;
    int i,j,k,m,e10,digit,ndigit,a[10],*pa;
    cout<<"please input string:"<<endl;
    gets(str);
    pstr=&str[0];
    pa=&a[0];
    ndigit=0;
    i=0;
    j=0;
    while(*(pstr+i)!='\0')
    {
        if((*(pstr+i)>='0')&&(*(pstr+i)<='9'))
            j++;
        else
        {
            if(j>0)
            {
                digit=*(pstr+i-1)-48;
                k=1;
                while(k<j)
                {
                    e10=1;
                    for(m=1;m<=k;m++)
                        e10*=10;
                    digit+=(*(pstr+i-1+k)-48)*e10;
                    k++;
                }
                *pa=digit;
                ndigit++;
                pa++;
                j=0;
            }
        }
        i++;
    }
    if(j>0)
    {
        digit=*(pstr+i-1)-48;
        k=1;
        while(k<j)
        {
            e10=1;
            for(m=1;m<=k;m++)
                e10*=10;
            digit+=(*(pstr+i-1-k)-48)*e10;
            k++;
        }
        *pa=digit;
        ndigit++;
        j=0;
    }
    cout<<"there are numbers in this line:"<<endl;
    j=0;
    pa=&a[0];
    for(j=0;j<ndigit;j++)
        cout<<*(pa+j)<<endl;
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/2303_80770781/article/details/143472311