我应该编辑程序,以便按某种顺序对队列进行排序。我已经弄清楚如何在评级(数字)上进行升序和降序但我不能按艺术家(姓名)进行排序。不知怎的,它根本不像我想要的那样工作。 " 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");
}
}
答案 0 :(得分:0)
le_item_artist
需要使用strcmp
来比较两个字符串。当前代码只是比较两个指针值,这不会做你想要的。
- user3386109