统计共有多少个整数,并输出这些整数。

5、小作业:
输入一个字符串,例如:
a123x456__17960?302ab5876
将其中连续的数字作为一个整数,依次存放到一个数组中a中,例如:123放在a[0]中,456放在a[1]中。统计共有多少个整数,并输出这些整数。

#include <iostream>
using namespace std;

void huan(char* x, int* y, int* t)
{
    char* str = x;
    int* a = y;
    int num = 0;
    while (*str)
    {
        if (*str >= '0' && *str <= '9')
        {
            num = num * 10 + *str -48; // 1的ASCLL码是49,2是50依次往后所以1的ASCLL码-48 = 1

            if (!(*(str+1) >= '0' && *(str+1) <= '9'))
            {
                a[(*t)++] = num;
                num = 0;
            }
        }
        str++;
    }
}
int main()
{
    char c_str[] = "a123x456__17960?302ab5876,8888";
    int a[60] = { 0 };
    int temp = 0;
    huan(c_str, a, &temp);
    cout  << c_str <<  "<<<中" << endl;
    cout << "共有" <<temp << "个整数" << endl;
    for (int i = 0; i < temp; i++)
    {
        cout << "分别是>" << a[i] << endl;
    }
    
    return 0;
}

 

posted on 2021-01-03 16:46  SakuraQAQ  阅读(150)  评论(0)    收藏  举报

导航