gpt4 book ai didi

c - 在函数 strcmp 中使用节点

转载 作者:行者123 更新时间:2023-11-30 15:44:45 25 4
gpt4 key购买 nike

我有一个家庭作业问题,如下面的引文所述。这是我必须实现的一个简单功能。网上查了一下,发现有类似实现的函数,但没有解释。

对我的代码进行评分的程序只是说“wordCmp 使用时失败”。那么基本上关于如何以不同的方式处理这个问题有什么建议吗?只是评分者给出了如此广泛的回应,我不确定它可能有什么问题。

int wordCmp (struct inode* n1, struct inode* n2)

Returns an integer indicating the relationship between the strings in two nodes: A value greater than zero indicates that the first character that does not match has a greater value in n1->word than in n2->word; And a value less than zero indicates the opposite. You can use strcmp to implement this function.

struct inode {
char *word;
};

int wordCmp (struct inode* n1, struct inode* n2){
return strcmp(n1->word, n2->word);
}

最佳答案

基于this edit问题作者的解决方案是:

I made pointers that equaled the value of the words in the nodes and then compared them. I don't know why it worked though honestly it seems the same to me.

It said in the handout that we couldn't access the nodes fields directly; not sure why..., so I just implemented the following code so I did not have to access it directly. It does the exact same thing...

int wordCmp (struct inode* n1, struct inode* n2){

char* wordOne = nodeGetWord(n1);
char* wordTwo = nodeGetWord(n2);
return strcmp(wordOne, wordTwo);

}

nodeGetWord(struct lnode *node){
return node->word;
}

That is all I did and it worked... Sorry if this seems dumb but it is what the grader took.

关于c - 在函数 strcmp 中使用节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19340714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com