2017-08-10 104 views
1

我有一個格式爲:[email protected] [email protected] ... [email protected]的輸入字符串。它總是由'@'' '分隔。根據條件從字符串中提取子字符串

實施例:

[email protected] [email protected] [email protected] 

它應該顯示具有最小值的人的姓名。
如何解決?

輸出:

driver 
+2

您需要更具體。你知道弦的數量嗎?這會輸入還是在字符串緩衝區中?定界符是否總是@?以前的字符串是否只包含字符? –

+0

其實這個問題沒有指定我們需要計算的輸入數量,下面是問題的輸入,@是上面指定的每種情況的分隔符。 –

+0

將主字符串分隔爲帶有分隔符「'''的子字符串,然後用分隔符」@「進一步分隔這些子字符串,該分隔符給出名稱和數字(在此處使用雙數組)。查找最高數字索引並顯示名稱數組中相應的名稱)。使用你自己的一段代碼來分隔使用分隔符。 此外,你甚至可以解決它,而不需要兩個數組(一個名稱和其他雙打)。這取決於你如何編寫代碼(時間vs空間) –

回答

1

您可以結合使用strtoksscanf函數來解決您的問題。

首先,您必須使用' '(空格)作爲分隔符來標記字符串,然後從每個標記中提取數字以查找具有最小編號的標記。每當您找到一個數字小於當前值的令牌時,從該令牌中提取名稱並存儲新的可能最小的數字。下面是一個例子:

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

int main() { 
    char str[] = "[email protected] [email protected] [email protected]"; 
    char result[256] = { 0 }; 

    char *token, *sep_ptr; 
    float value, min = FLT_MAX; /* set 'min' to the maximum of float */ 

    /* tokenize 'str' and walk through the tokens */ 
    for(token = strtok(str, " "); token != NULL; token = strtok(NULL, " ")) { 
     value = FLT_MAX; 

     /* process the current token if it contains a '@' character */ 
     if(sep_ptr = strchr(token, '@')) { 
      sscanf(sep_ptr, "@%f", &value); /* extract value */ 

      /* check if the new number is smaller than current 'min' */ 
      if(value < min) { 
       strcpy(result, (*sep_ptr = '\0', token)); /* extract name */ 
       min = value; 
      } 
     } 
    } 

    puts(result); 
    return 0; 
} 

代碼的(*sep_ptr = '\0', token)部分上面簡單地替換'@'字符執行復制從tokenresult之前空字符。 (所提到的表達式使用comma operator。)

1

下面的代碼實現一個getSubstrSmallestNumber()函數,它使用strtok()函數而變化的輸入緩衝器。如果你不希望你可以先複製字符串。改變輸入緩衝區的好處是,找到的子字符串不需要內存分配。 strtok()寫入一個空終止符'\0',如果它被調用,則會找到指定的分隔符。

getSubstrSmallestNumber()函數可以用特定的分隔符來調用,這裏是'@'' '。每個號碼將被轉換成雙倍數,並且檢查它是否比以前小。如果它更小,相應的令牌將被保存。 while循環結束後(如果strtok()未找到更多標記),將返回保存的標記(最小雙精度值)。

請注意,代碼沒有錯誤檢查這應該被認爲是實施以及。

的完整代碼

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

char* getSubstrSmallestNumber(char* input, char* numberDelimiter, char* strDelimiter) 
{ 
    char* tokenStr; 
    double minNumber = DBL_MAX; 
    char* minToken = NULL; 

    for (tokenStr = strtok(input, numberDelimiter); 
     tokenStr != NULL; 
     tokenStr = strtok(NULL, numberDelimiter)) 
    { 
     char* numberStr = strtok(NULL, strDelimiter); 
     double number = strtod(numberStr, NULL); 

     if (number < minNumber) 
     { 
     minNumber = number; 
     minToken = tokenStr; 
     } 
    } 

    return minToken; 
} 

int main() 
{ 
    char input[] = "[email protected] [email protected] [email protected]"; 

    printf("<%s>\n", getSubstrSmallestNumber(input, "@", " ")); 

    return 0; 
} 

輸出:

<driver> 

我把'<''>'串圍在printf()調用顯示,getSubstrSmallestNumber()返回的字符串實際上只是driver,沒有什麼比如空間。

0

你可以使用strrok可用的C字符串庫和解析浮點數,你可以使用atof函數但atof函數seems to be unreliable in some cases,不過。在這種情況下,您可以根據自己的需求編寫自己的實現。

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

#define SIZE 1000 

int main() { 
    char input[SIZE], smallest[SIZE], name[SIZE]; 
    const char s[2] = "@"; 
    char *token; 
    float val = FLT_MAX; 

    while(scanf("%s", input) != EOF) { 

     token = strtok(input, s); 
     strcpy(name, token); 

     token = strtok(NULL, s); 
     float curVal = atof(token); 

     if(curVal < val) { 
      val = curVal; 
      strcpy(smallest, name); 
     } 
    } 

    printf("Smallest value : %s\n", smallest); 
    return 0; 
} 

當我測試它似乎表明正確的輸出:

~/Documents/src : $ ./a.out 
[email protected] 
[email protected] 
[email protected] 
Smallest value : driver 

希望幫助!