Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2538975
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2010-08-25 09:41:28

    输入一个字符串,内有数字和非数字字符。如:asdf345;45;345\odsaf;将其中连续的数字作为一个整数,依次存放到一数组a中。例如345放在a[0],45放在a[1],345放在a[2]...统计一共有多少个整数,并输出这些数。
    我的思路是使用指针,如果在字符串中碰到数字,则开始统计字符的个数,并把数字字符放入一个字符数组中,当遇到非数字字符时,则判断放数字字符的的个数是否大于1,如果大于1,则将这个数字字符数组转化为int类型。代码如下:
 

#include <stdio.h>
#include <math.h>
#define N 100

int getnum(char *);
int ch2i(char);
void print_number(int *,int);
int main(int argc, char *argv[])
{
    char str[N],*p;
    char ch_num[10],*p_char;
    int a[N],n,count = 0,num = 0,*int_p;
    printf("please input a string:\n");
    gets(str);
    p = str;
    int_p = a;
    while(1)
    {
             if (*p >= '0' && *p <= '9')
             {
                if (0 == num)
                {
                   p_char = ch_num; //指向字符数组的首地址

                   *p_char++ = *p;
                }
                else
                {
                    *p_char++ = *p;
                }
                num++;
             }
             else
             {
                 if (num > 1)
                 {
                     *p_char = '\0'; //给字符数组的最后一个单元添加'\0'

                     num = 0;
                     
                     *int_p++ = getnum(ch_num);
                     count++;
                                    
                 }
             }
             
             if (*p == '\0')
             {
                    break;
             }
             p++;
    }
    printf("the string has %d number is:\n",count);
    int_p = a; //数组指针指向首地址。

    print_number(int_p,count);
    system("pause");
    return 0;
}

int getnum(char *ch)
{
    char *head;
    int result = 0,i = 0;
    head = ch;
    while(*ch)
    {
               ch++;
    }
    ch--; //指针指向数组的最后一个元素。

    while (head <= ch)
    {
          result += pow(10,i++) * ch2i(*ch--);
    }
    return result;
}

int ch2i(char c)
{
    int result;
    switch(c)
    {
          case '0':
               result = 0;
               break;
          case '1':
               result = 1;
               break;
          case '2':
               result = 2;
               break;
          case '3':
               result = 3;
               break;
          case '4':
               result = 4;
               break;
          case '5':
               result = 5;
               break;
          case '6':
               result = 6;
               break;
          case '7':
               result = 7;
               break;
          case '8':
               result = 8;
               break;
          case '9':
               result = 9;
               break;
          default:
               result = -1;
               break;
    }
    return result;
}

void print_number(int *p,int n)
{
     int i;
     for (i = 0; i < n;i++)
     {
         printf("%d ",*p++);
     }
}


阅读(2115) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~