gpt4 book ai didi

c - 按名称对队列进行排序不起作用

转载 作者:行者123 更新时间:2023-11-30 17:23:08 25 4
gpt4 key购买 nike

我应该编辑程序,以便它按某种顺序对队列进行排序。我已经弄清楚如何对评级(数字)进行升序和降序排列,但我无法按艺术家(名称)进行排序。不知怎的,它就是没有像我想要的那样工作。 “le_item_artist”应该使输入的名称按应有的方式从 a 到 z 排序,但事实并非如此。你能告诉我出了什么问题吗?代码如下:

#include <stdio.h>
#include <stdlib.h>
#include "pqueue.h"

struct track
{
char artist[20];
char title[20];
int rating;
};

void print_item(any i)
{
printf("(%i,'%s','%s')",
((struct track*)i)->rating,
((struct track*)i)->artist,
((struct track*)i)->title);
}

int le_item_desc(any i, any j)
{
return ((struct track*)i)->rating <= ((struct track*)j)->rating;
}

int le_item_asc(any i, any j)
{
return ((struct track*)i)->rating >= ((struct track*)j)->rating;
}

int le_item_artist(any i, any j)
{
return ((struct track*)i)->artist >= ((struct track*)j)->artist;
}


int main(int argc, char **argv)
{
char order;

//Select sorting menu
printf("Please select one of the following\n");
printf("(a)scanding\n");
printf("(d)escending\n");
printf("Your option: ");

scanf("%c", &order);

pqueue *pq = new_bounded_pqueue(5,le_item_desc);
pqueue *pq2 = new_bounded_pqueue(5,le_item_asc);
pqueue *pq3 = new_bounded_pqueue(5,le_item_artist);

struct track *it;
int i;

switch(order){
case 'D':
case 'd': for(i=0; i<3; i++) {
it = (struct track*)malloc(sizeof(struct track));
printf("rating artist title? ");scanf(" %i %s %s", &(it->rating), it->artist, it->title);
pqueue_insert(pq,it);
pqueue_print(pq,print_item);
printf("\n\n");
}
for(i=0; i<3; i++) {
pqueue_dequeue(pq);
pqueue_print(pq,print_item);
printf("\n\n");
}
break;
case 'A':
case 'a': for(i=0; i<3; i++) {
it = (struct track*)malloc(sizeof(struct track));
printf("rating artist title? ");scanf(" %i %s %s", &(it->rating), it->artist, it->title);
pqueue_insert(pq2,it);
pqueue_print(pq2,print_item);
printf("\n\n");
}
for(i=0; i<3; i++) {
pqueue_dequeue(pq2);
pqueue_print(pq2,print_item);
printf("\n\n");
}
break;
case 'N':
case 'n': for(i=0; i<3; i++) {
it = (struct track*)malloc(sizeof(struct track));
printf("rating artist title? ");scanf(" %i %s %s", &(it->rating), it->artist, it->title);
pqueue_insert(pq3,it);
pqueue_print(pq3,print_item);
printf("\n\n");
}
for(i=0; i<3; i++) {
pqueue_dequeue(pq3);
pqueue_print(pq3,print_item);
printf("\n\n");
}
break;
default: printf("Error, wrong character!\n");
}

}

最佳答案

le_item_artist需要使用strcmp来比较两个字符串。当前的代码只是比较两个指针值,这不会执行您想要的操作。 – user3386109

关于c - 按名称对队列进行排序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27696123/

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