如何根据C中的价格对指定等级的输入酒店进行排序?

时间:2018-10-15 18:59:04

标签: c

我大约一个月前才开始学习C,这时我很尴尬。

我编写了用于输入任何酒店数量的名称,地址,价格,等级和房间数的代码。但是现在我陷入了一个困境,即必须从用户那里输入等级的信息,并按价格的升序显示酒店。我可以成功显示给定等级的酒店,但是按价格对酒店进行排序时遇到了麻烦,因为这是我的代码第一次变得如此冗长和混乱。

到目前为止,这是我的代码:

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

//Structure definition starts
struct hotels
{
    char name[25];
    char address[300];
    int grade, avgCharge, rooms;
};
//Structure definition over

 //Main function starts
main()
{
    int i, j, k, hotels, choice, grade, temp;
    struct hotels a[50];
    //Declaration part over---------------------------------------------------------------------------

    printf("Enter the number of hotels\n");
    scanf("%d", &hotels);
    printf("\n\n");

    //Taken number of hotels for upcoming loops-------------------------------------------------------

    //for Loop for input of hotels and their details starts-------------------------------------------

    for (i = 0; i < hotels; i++)
    {
        fflush(stdin);
        printf("Enter the name of hotel %d\n", i + 1);
        gets(a[i].name);
        printf("\n");

        printf("Enter the address of %s\n", a[i].name);
        gets(a[i].address);
        printf("\n");

        printf("Enter the grade of the hotel from 1 to 5. (5 being the best)\n");
        scanf("%d", &a[i].grade);
        printf("\n");

        printf("Enter the average charge per night of the hotel.\n");
        scanf("%d", &a[i].avgCharge);
        printf("\n");

        printf("Enter the number of available rooms of the hotel.\n");
        scanf("%d", &a[i].rooms);
        printf("\n\n");
    }
    //Input part over, switch part starts-------------------------------------------------------------


    printf("\n\n");
    printf("Choose the task to perform-\n");
    printf("1. Show hotels of a particular grade from low to high price.\n");
    printf("2. Filter and show hotels under a particular tariff only.\n\n");

    scanf("%d", &choice);
    switch (choice)
    {
    case 1:
        printf("Enter the grade of the hotel to apply the list filter.\n");
        scanf("%d", &grade);
        printf("The following hotels were found- \n\n");
        for (i = 0; i < hotels; i++)
        {
            for (j = i + 1; j < hotels; j++) // Sorting according to price- Low to High
            {
                if (a[i].avgCharge > a[j].avgCharge)
                {
                    temp = a[i].avgCharge;
                    a[i].avgCharge = a[j].avgCharge;
                    a[j].avgCharge = temp;
                }
            }

            if (a[i].grade == grade) //Comparison for displaying the hotels of selected grade only.
            {
                printf("Hotel %d= %s\n", i + 1, a[i].name);
            }

        }
    }

}

现在,到目前为止,这可以按预期工作,我只是想不出如何按价格升序显示酒店。任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:1)

问题是,当您发现需要交换的两个物品时,您只是在更改它们的费用,这是不正确的。相反,您应该直接交换整个项目。

类似这样的东西(仅感兴趣的部分):

if (a[i].avgCharge > a[j].avgCharge)
{
   hotels temp = a[i];
   a[i] = a[j];
   a[j] = temp;
}

旁注:我建议您将结构命名为hotel,因为这确实是,不是吗?之后,当您声明hotel结构的数组时,可以将该数组命名为 hotels

答案 1 :(得分:0)

尝试将if (a[i].avgCharge > a[j].avgCharge)替换为if ((a[i].avgCharge > a[j].avgCharge) || (a[i].avgCharge == a[j].avgCharge && a[i].grade > a[j].grade))

通常来说:如果需要,您需要在排序过程中更改两个酒店的顺序:

  • 一个更贵
  • 它们的价格都一样,但是评分较高

编辑:排序完成后,您需要在第二次for循环中进行打印。 编辑2:正如@NiVeR所说,您需要交换整个酒店:

                temp = a[i];
                a[i] = a[j];
                a[j] = temp;

还有一种新的温度类型。