2014-10-03 182 views
-1

在我的代碼中,我想使用表示Will,Bob,Billy,Ryan或Ed而不是int的字符串。具體來說,使用N個字符串S [1],S [2] ... S [N],其中S [I]是上面的名稱。如果你想比較字符串,這不是要走的路(1≤N≤100,000)C:將int轉換爲字符串

#include <stdio.h> 


int main(void) 

{ 

int input_num=0; 
int isWill = 0; 
int isBob = 0; 
int isBilly = 0; 
int isRyan = 0; 
int isEd = 0; 
int n=0; 

scanf("%d", input_num); 
printf("%d", input_num); 

for(n;n<input_num;n++) 
{ 
    char tmp[100,000]; 
    scanf("%s", tmp); 

    if(tmp == "Will") 
     isWill = 1; 
    else if(tmp == "Bob") 
     isBob = 1; 
    else if(tmp == "Billy") 
     isBlue = 1; 
    else if(tmp == "Ryan") 
     isRyan = 1; 
    else if(tmp == "Ed") 
     isEd = 1; 

} 
//end of input 

if(isWill == 0) 
    printf("Will\n"); 
if(isBob == 0) 
    printf("Bob\n"); 
if(isBilly == 0) 
    printf("Billy\n"); 
if(isRyan == 0) 
    printf("Ryan\n"); 
if(isEd == 0) 
    printf("Ed\n"); 

return 0; 

}

+1

那麼,最新的問題是什麼? – Borna 2014-10-03 21:57:07

+0

您需要更新isWhite to isWill等等...還可以嘗試使用枚舉,因爲它們可以幫助在int和字符串之間切換 – Honorificabilitudinitas 2014-10-03 21:57:21

+0

您告訴我們您想使用什麼,但實際上並未提出問題。你的代碼不能編譯?什麼是錯誤信息?還是它沒有做你想做的事?它有什麼作用?你想要它做什麼?你在哪裏定義「isWhite」等?你爲什麼要設置「isWhite」,但測試「isWill」?如果你設置了'isWhite = 1',你爲什麼要測試'if(isWill == 0)'?那些測試相反的事情。 – 2014-10-03 21:57:26

回答

1

比較字符串,您需要使用strcmp庫函數,如果兩個參數字符串相等,返回0:

if (strcmp(tmp, "Will") == 0) 
    // tmp contains the string "Will" 

如果你想要一個字符串數組,你可以做一個二方法。你可以聲明一個二維數組,其中第一維是名字的TNE數,第二個維度是出了名的最大長度:

char names[NUMBER_OF_NAMES][MAX_NAME_LENGTH+1]; 

要複製一個字符串,使用strcmp

strcmp(names[0], "Will"); 
strcmp(names[1], "Bob"); 
// etc. 

或者,你可以聲明一維數組,其中每個元素是指針字符串:

char *names[NUMBER_OF_NAMES]; 

names[0] = "Will"; 
names[1] = "Bob"; 

注意,你分配一個指針的值爲names[i];您不會將字符串內容複製到names[i]

最後,C不識別整數文字,如100,000;你要申報你的tmp緩衝區

char tmp[100000]; 

雖然這是一個字符串緩衝區有點大。

0

而且還輸入N的限制如下。
使用string.h庫可以使用函數strcmp
要使用此函數類型strcmp(string1, string2)
請記住,如果字符串相同,strcmp將返回0。
所以你想要做的是使用if(!strcmp("Will", tmp))輸入字符串是否相同。

你也需要改變焦炭tmp[100,000]to char tmp[100000],因爲這是在C

希望這有助於支持的格式。

1

幾件事情不像我會寫他們。 我有幾分鐘的時間,寫了一個編譯示例。 也許你的問題可以通過這個來解決。

char * colortext[] = 
{ 
    "unknown", 
    "white", 
    "black", 
    "blue", 
    "red", 
    "yellow" 
}; 

typedef enum color { 
    unknown = 0, 
    white, 
    black, 
    blue, 
    red, 
    yellow 
}; 

typedef struct 
{ 
    int number; 
    char name[200]; 
    color team; 
}Player; 

typedef struct 
{ 
    char * name; 
    color team; 
}Autoassign; 

Autoassign autoassign[] = 
{ 
    {"Will", white}, 
    {"Bob", black}, 
    {"Billy", blue}, 
    {"Ryan", red}, 
    {"Ed", yellow} 
}; 

int autoassignLength = sizeof(autoassign)/sizeof(autoassign[0]); 

int Player_print(Player * player) 
{ 
    int e = 0; 
    printf("\n"); 
    printf("Player %i Profile:\n", player->number); 
    printf("Name: '%s'\n", player->name); 
    printf("Team: '%s'\n", colortext[player->team]); 
    printf("\n"); 
    return e; 
} 

int Player_scan(Player * player, int number) 
{ 
    int e = 0; 
    int i; 
    printf("Input Player Name: "); 
    scanf("%s", player->name); 
    player->number = number; 
    player->team = unknown; 
    for(i = 0; i < autoassignLength; ++i) 
    { 
     if(0 == strcmp(player->name,autoassign[i].name)) 
     { 
      player->team = autoassign[i].team; 
      break; 
     } 
    } 
    return e; 
} 

int main(int argc, char ** argv) 
{ 
    int i; 
    int input_num, n; 
    Player players[10]; 

    printf("How many Players? "); 
    scanf("%d", &input_num); 

    for(n = 0; n < input_num ; n++) 
    { 
     Player_scan(&players[n], n+1); 
    } 

    for(n = 0; n < input_num ; n++) 
    { 
     Player_print(&players[n]); 
    } 

    return 0; 
}