2017-02-14 88 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