从字符串中取数字(1-99) 不用正则表达式

时间:2021-08-21 18:49:50
// dwd.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
int get_XYMN_Value(char * buf,int &offset)
{
    unsigned short len=strlen(buf);
    int a=0,b=0;
    bool flag=false;
    while(len)
    {
        if((*buf)>48&&(*buf)<=59) //(0,9]
        {
          if(!flag)
          {
             a=*buf;
             flag=true;
             buf++;
             offset++;
             continue;
          }
          if(flag)
          {
            b=*buf;
            return (a*10+b);
          }
        }
        else
        {
            if(a!=0)
              return a;
            else
              flag=false;
        }
        buf++;
        offset++;
        len=strlen(buf);
    }
    
    if(len==0)
        return a;
    return 0;
}

int main(int argc, char* argv[])
{
    char * buf="91,92,93,94,5,6,7,8,9";
    int len=strlen(buf);
    int offset=1;
    int a=0;
    char b=0;
    while(len)
    {
        a=get_XYMN_Value(buf,offset);
        b=a;
        std::cout<<a<<" "<<b<<std::endl;
        buf=buf+offset;
        offset=1;
        len=strlen(buf);
    }

    std::cout<<"......好戏开场"<<std::endl;
    
    {
      char *content="1,2,3,4,5";
      unsigned short len=strlen(content);
      int x=0,y=0,m=0,n=0;
      char ascii_x=0,ascii_y=0,ascii_m=0,ascii_n=0;
      int step=1;
      int offset=1;
      while(len)
      {
        switch(step)
        {
           case 1:
           {
             ascii_x=get_XYMN_Value(content,offset);
             step=2;
             break;
           }
           case 2:
           {
             ascii_y=get_XYMN_Value(content,offset);
             step=3;
             break;
           }
           case 3:
           {
             ascii_m=get_XYMN_Value(content,offset);
             step=4;
             break;
           }
           case 4:
           {
             ascii_n=get_XYMN_Value(content,offset);
             len=0;
             break;
           }
        }
         content =content + offset;
         offset=1;
         len=strlen(content);
      
      }//end while
      std::cout<<ascii_x<<std::endl;
      std::cout<<ascii_y<<std::endl;
      std::cout<<ascii_m<<std::endl;
      std::cout<<ascii_n<<std::endl;

    }//end {
    return 0;
}