2014-12-07 75 views
-2

我应该写一个函数不使用其他库,但是从用户获取字符串(最多100个字符)。字符串将是这种形式:A,B,C,d,X,Y,Z等...从我应该在逗号之间仅提取第一整数到不同的阵列。 因此,例如,比方说,我得到这个作为输入:ABCD,32,23.5,4,6,hf3,45g,2个 新数组中的数字应该是:32,4,6,2,因为这是第一次你可以在输入中看到4个数字。如果没有发现号默认值为0如何检查字符串中某个位置的数字并在c中提取其值?

这是我到目前为止一直在做,但不知何故,似乎并不是正确的, 想法是seperately检查每个字符,并除非看到逗号并且字符在'0'和'9'的ASCII值之间进行求和。如果在逗号前面出现了不同的字符,那么“跳过”将得到值1,这样该函数将继续寻找另一个数字。

感谢您的帮助。

int getParameters() 
{ 
    char input[100]; 
    int parameters[4]={0}; 
    int indexInput=0, indexParameters; 
    int charValue=0; 
    int skip=1; 
    scanf("%s", input); 

    for (indexParameters=0; indexParameters<4;skip=0) 
    { 
    if (input[indexInput]=='\0') 
     break; 
    else 
    { 
     for (;input[indexInput]!=','; ++indexInput) 
     { 
     printf("%c\n", input[indexInput]); 
     if(input[indexInput]=='\0') 
      break; 
     else if (input[indexInput]<'9' && 
       input[indexInput]>'0') 
     { 
      charValue=input[indexInput]-'0'; 
      parameters[indexParameters]*=10; 
      parameters[indexParameters]+=charValue; 
     } 
     else 
     { 
      skip=1; 
     } 
     } 
    } 
    indexInput++; 
    if (input[indexInput]==",") 
     skip==1; 
    if (skip==1) 
    { 
     parameters[indexParameters]=0; 
    } 
    else 
     indexParameters++; 
    } 
    return 0; 
    } 
+1

_but不知它似乎并没有被right._究竟如何?预期的输出是32,4,6,2。你得到的输出是什么? – 2014-12-07 18:34:30

+0

线'如果(输入[indexInput] == “”),则跳过== 1;'当然应该是'如果(输入[indexInput] == '')跳过= 1;':您分配1〜'跳过'并且测试一个cahacter(用单引号)。 – 2014-12-07 18:35:22

+0

另外,您从数字中排除0和9。 – 2014-12-07 18:37:24

回答

2

样品通过strtokstrtol

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

int main(){ 
    char input[100] = "abcd,32,23.5,4,6,hf3,45g,2"; 
    char *token = strtok(input, ","); 

    while(token){ 
     char *endp; 
     int num = strtol(token, &endp, 10); 
     if(*endp == '\0') 
      printf("%d\n", num); 
     //else //not integer. 

     token = strtok(NULL, ","); 
    } 
    return 0; 
} 

strtok没有

#include <stdio.h> 
#include <stdlib.h> 

int main(){ 
    char input[100] = "abcd,32,23.5,4,6,hf3,45g,2"; 
    char *p = input; 

    while(*p){ 
     char *endp; 
     int num = strtol(p, &endp, 10); 
     if(*endp == ',' || *endp == '\0') 
      printf("%d\n", num); 
     //else //not integer. 
     //skip to next read point  
     p = endp; 
     while(*p != ',' && *p) 
      ++p; 
     if(*p) 
      ++p; 
    } 
    return 0; 
} 
+0

好办法,但这个时候,我没有被允许使用 Ryan 2014-12-07 19:33:22

+0

@Ryan样品添加无'' BLUEPIXY 2014-12-07 19:44:21

1

解决了这个问题,尝试了不同的算法只是一个环:

int getParameters() 
{ 
    char input[100]; 
    int parameters[4]={0}; 
    int indexInput=0, indexParameters=0; 
    int skip=0, numberSeen=0; 
    scanf("%s", input); 

for (;input[indexInput]!='\0' && indexParameters<4;++indexInput) 
{ 
    if (input[indexInput]==',' && skip==1) 
    { 
    parameters[indexParameters]=0; 
    skip=0; 
    } 
    else if (input[indexInput]==','&& numberSeen==1) 
    { 
    numberSeen=0; 
    indexParameters++; 
    } 
    else if (input[indexInput]==',') 
    continue; 
    else if (input[indexInput]<='9' && input[indexInput]>='0') 
    { 
    parameters[indexParameters]*=10; 
    parameters[indexParameters]+=input[indexInput]-'0'; 
    numberSeen=1; 
    } 
    else 
    skip=1; 
} 

return 0; 
} 
相关问题