急急急 关于一道C语言的编程 全部财富了 谢谢大神

输入一个字符串,内有数字和非数字字符,例如:a123x456 17960?* 302tab5790 将其中连续的数字作为一个整数,依次存放到一数组a中。例如,123放在a[0],456放在a[1]……统计共有多少个整数,并输出这些数。

第1个回答  2016-06-28
#include<stdio.h>
int main(int argc, char ** argv)
{
    char szBuf[1024] = { 0 };
    char *p;
    int a[256], _cnt = 0, _tmp = 0, _flag = 0;

    fgets(szBuf,1000,stdin);
    p = szBuf;
    while (*p)
    {
        if (*p >= 0x30 && *p <= 0x39)
        {
            _tmp *= 10;
            _tmp += (*p) - 0x30;
            _flag = 1;
        }
        else if (_flag)
        {
            a[_cnt++] = _tmp;
            _tmp = 0;
            _flag = 0;
        }
        p++;
    }
    printf("total:%d\n", _cnt);
    while (_cnt--) printf("%d ", a[_cnt]);
    return 0;
}

追问

连续的数字作为一个整数

本回答被网友采纳
第2个回答  2016-06-28
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"

int F(char *s, int n[])
{
int cnt = 0;
int index = 0;
char c, num[20] = "";
while (c = *s++)
{
if (isdigit(c))
{
num[index++] = c;
}
else
{
if (index > 0)
{
n[cnt++] = atoi(num);
index = 0;
}
}
}
//这里有重复代码还不知道如何避免
if (index > 0)
{
n[cnt++] = atoi(num);
index = 0;
}
return cnt;
}

int main()
{
int i;
int n[10];
char *s = "302tab5790";
int cnt = F(s, n);
for (i = 0; i < cnt; i++)
{
printf("%d\n", n[i]);
}
system("pause");
return 0;
}

追问

连续的数字作为一个整数

追答

是的,你运行测试一下。

本回答被提问者采纳
相似回答