2017-02-14 86 views
-2

我是編程的初學者,這是我第一次來到這裏。我需要你的幫助來解決這個問題。正確的方式來傳遞C中的結構

現在,我想要解決的是寫一個代碼,打印出團隊中的足球運動員的信息和屬性。這裏有一個例子..

enter image description here

這是梅西的著名足球運動員..你可以在右下角有他的屬性看,每一個球員都有3個類別的屬性..技術屬性,心理屬性和身體素質。每個屬性的最大等級是20。

現在我在2種方式編碼這一點,我不知道哪一個是正確的方式,或者兩種方式是錯誤的,不好的編程..

第一種方式:

#include <stdio.h> 
#include <string.h> 
void Messi(); 
int main() 
{ 
    Messi(); 
    return 0; 
} 

void Messi() 
{ 
    struct player10 
    { 
     char technical[150]; 
     char mental[150]; 
     char physical[150]; 
    }; 

    struct player10 messi; 

    strcpy(messi.technical, "Corners: 14\nCrossing: 15\nDribbling: 20\nFinishing: 20\nFirst Touch: 20\nFree Kick: 15\nHeading: 12\nLong shots: 17\nPassing: 19\nPenalty Taking: 18\nTechnique: 20\n"); 

    strcpy(messi.mental, "Agression: 7\nAnticipation: 19\nBravery: 7\nComposure: 19\nConsentration: 14\nDecisions: 19\nDetermination: 20\nFlair: 20\nLeadership: 14\nOff The Ball:  18\nTeamWork: 13\nVision: 20\nWork Rate: 7\n"); 

    printf("Messi Technical:\n%s \t Messi Mental:\n%s",messi.technical,messi.mental); 

    return; 
} 

第二種方式: -

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


struct player10 
{ 
    char technical[150]; 
    char mental[150]; 
    char physical[150]; 
}; 


int main() 
{ 
    struct player10 messi; 
    printf("Messi Attributes: \n");   
    Messi_attr(messi); 
    return 0; 
} 


int Messi_attr (struct player10 messi) 
{ 
    strcpy(messi.technical,"Corners: 14\nCrossing: 15\nDribbling: 20\nFinishing: 20\nFirst Touch: 20\nFree Kick: 15\nHeading: 12\nLong shots: 17\nPassing: 19\nPenalty Taking: 18\nTechnique: 20\n"); 

    printf("\nTechnical Attributes:\n\n%s", messi.technical); 

    strcpy(messi.mental,"Agression: 7\nAnticipation: 19\nBravery: 7\nComposure: 19\nConsentration: 14\nDecisions: 19\nDetermination: 20\nFlair: 20\nLeadership: 14\nOff The Ball: 18\nTeamWork: 13\nVision: 20\nWork Rate: 7\n"); 

    printf("\nMental Attributes:\n\n%s", messi.mental); 

    strcpy(messi.physical,"Acceleration: 18\nAgility: 20\nBalance: 17\nJumping Reach: 6\nNatural Fitness: 14\nPace: 15\nStamina: 13\nStrength: 8\n"); 

    printf("\nPhysical Attributes:\n\n%s", messi.physical); 

    return 0; 
} 

我在做對吧?或者有一種更簡單的方法來對傳遞結構進行編碼? 任何幫助,將不勝感激..

+3

就像現在一樣,最簡單的編碼方式是printf( 「\ n技術屬性:\ n \ n角色:14 \ n交叉:15 \ n' ...依此類推 – immibis

+0

#1毫無意義#2更好但仍然錯誤 – John3136

+0

寫一個函數來創建一個player10 struct和一個來打印它,把它們混合起來沒有任何意義,也可以使用struct作爲類型,'typedef struct {...} player10;'在一個haeder文件中或者在代碼的開始處會很好。 – kaetzacoatl

回答

2

您可以通過指針傳遞player10結構到一個函數與所需的字符串進行填充。在c中,傳入的對象是可選的,這是一個相當常見的模式,所以它不會強制程序使用堆。如果你傳遞NULL,那麼它會分配它返回的內容,結果你需要釋放結果。

(如果你想,我沒有得到的10意義或player10_),這對玩家操作的,有點封裝它可以創建一組與player_前綴的函數。下面我顯示player_create這是有效的構造函數和player_print來打印所有的玩家。

這裏的例子有希望幫助你。我還展示瞭如何在沒有現有球員struct的情況下使用它。

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

#define FIELD_SIZE 150 

typedef struct 
{ 
    char technical[FIELD_SIZE]; 
    char mental[FIELD_SIZE]; 
    char physical[FIELD_SIZE]; 
} player10; 

char* sstrncpy(char* dest, char* src, size_t destsize){ 

    dest[destsize - 1] = '\0'; 
    return strncpy(dest, src, destsize - 1); 
} 

player10* player_create(player10* p, char* tech, char* mental, char* physical) 
{ 
    if(!p){ 
     p = malloc(sizeof(player10)); 
     assert(p); 
    } 

    sstrncpy(p->technical, tech, FIELD_SIZE); 
    sstrncpy(p->mental, mental, FIELD_SIZE); 
    sstrncpy(p->physical, physical, FIELD_SIZE); 

    return p; 
} 

void player_print(player10* p) 
{ 
    printf("technical: %s\n", p->technical); 
    printf("mental: %s\n", p->mental); 
    printf("physical: %s\n", p->physical); 
} 

int main() 
{ 
    player10 messi; 

    // some like to reset their structs 
    //memset(&messi, 0, sizeof(player10)); 

    player_create(&messi, 
     "Corners: 14\nCrossing: 15\nDribbling: 20\nFinishing: 20\nFirst Touch: 20\nFree Kick: 15\nHeading: 12\nLong shots: 17\nPassing: 19\nPenalty Taking: 18\nTechnique: 20\n", 
     "Agression: 7\nAnticipation: 19\nBravery: 7\nComposure: 19\nConsentration: 14\nDecisions: 19\nDetermination: 20\nFlair: 20\nLeadership: 14\nOff The Ball: 18\nTeamWork: 13\nVision: 20\nWork Rate: 7\n", 
     "Acceleration: 18\nAgility: 20\nBalance: 17\nJumping Reach: 6\nNatural Fitness: 14\nPace: 15\nStamina: 13\nStrength: 8\n"); 

    printf("Messi Attributes: \n"); 
    player_print(&messi); 

    player10* ronaldo = player_create(NULL, "stuff", "stuff", "stuff"); 

    printf("Ronaldo Attributes: \n"); 
    player_print(ronaldo); 
    free(ronaldo); 

    return 0; 
} 

如果您計劃雖然這些player10結構多操作,我建議不同的存儲您的參數。你也許應該有子結構technicalmental & physical其中包含數字字段。這樣你的數據可以查詢得多,佔用的空間也更少,你可以避免字符串緩衝區大小和複製字符串等問題。

+0

非常感謝您的幫助。這是有幫助的,當我完成所有章節的學習時,我還沒有學習malloc課程以及像'typedef','dest','src','sizeof''assert','\ 0 」。我還不熟悉。所以也許更多的學習後,我會更好地理解你的代碼。至於'struct player10',10只是玩家的號碼。就像'player10 messi','player9 ronaldo'等。 – whiteknights

+0

不客氣。如果你願意,我可以把這個malloc放出來嗎? src和dest只是變量名稱,沒有特定的語言。我必須這樣做才能確保字符串複製是安全的。回答在這裏有很多審查。 –

+0

明天我要去學習malloc,也許在學習之後,我會了解更多。你認爲我自己在解決這樣的問題時感到困惑嗎?我應該先完成所有的課程?因爲我研究自己的每一堂課只是想出了這樣的複雜練習的例子。我只是在腦海中發現了這個問題,並試圖解決它。 – whiteknights