首页 > 解决方案 > 冒泡排序功能后跳过for循环

问题描述

我正在为最终作业制作一个排序程序。它获取帐户的帐号、姓氏和余额,并根据帐号按升序对其进行排序。我的程序遇到的问题是,一旦它进入 for 循环输出信息,它就会完全跳过它。我相信这可能与我的冒泡排序功能有关,但我不确定如何修复它。我们还没有教过快速排序,所以我没有使用它。我收到一个错误:“sort”的参数 1 的类型不兼容,但我无法将数组更改为 struct 类型,否则会导致更多问题。这是我的代码:

#include <stdio.h>

/* Define structure */

struct account
{
    int number;
    char full_name[30];
    float balance;
};

/* Function prototype */

void sort (int[], int);

/* Begin main function */

int main()
{

    /* Declare variables */

    struct account person[5];
    int x=0, i=0, count=0;

    printf("Enter account number, last name, and balance.\n");
    printf("Enter -999 to end input:\n\n");

    /* Create loop for user information */
    while(1)
    {

            scanf("%i", &person[x].number);
            if(person[x].number == -999) /*Break when -999 is input */
            {
                count = x;
                break;
            }


            if(person[x].number < 1 || person[x].number >1000)
            {
                printf("***Invalid account number. Please enter 1 - 1000 or -999 to exit***\n");
                x--;  
                continue;
            }

            scanf("%s", person[x].full_name);
            scanf("%f", &person[x].balance);

            if(person[x].balance < 0)
            {
            printf("*** Invalid balance amount. Please enter a positive value. ***\n");
            x--;
            continue;
            }
    }/* End of while loop */

    /* Call to sort function to sort information */

    sort(person[x].number, count);

    /* Display the information in ascending order */

    printf("ACCOUNT\tLAST NAME\tBALANCE\n");

    /* Create for loop to output the sorted information */

    for(x=0; x < count; x++)
    {
        printf("%d\t%s\t%f\n",person[x].number,person[x].full_name,person[x].balance);

    } /* End of for loop */
}/* End of main */

/* Start sort function */

void sort(int p[], int count)
{
    /* Declare variables.*/
 char changed = 'T'; /* a "flag" to indicate if a swap was made.*/
 int x, temp;
 while (changed == 'T') 
 {
    changed = 'F';
    /* Create for loop for swaping information */
    for (x = 0; x < count-1; x++)
    {
        if ( p[x] > p[x + 1])
        {
            /* Swap needs to be made.*/
            temp = p[x];
            p[x] = p[x+1];
            p[x+1] = temp;
 /* Set flag indicating that a swap was made. This ensures that */
 /* processing will continue, until nothing needs to be swapped. */
            changed = 'T';
        }
    } /* end for loop */
 } /* end while loop*/
} /* end function sort */

这是我的输出:

Enter account number, last name, and balance.
Enter -999 to end input:

10 Happy 55.00
15 Day 60.00
35 Man 75.00
-999
ACCOUNT LAST NAME       BALANCE
Press any key to continue . . .

感谢您的帮助以及您花时间提供帮助!

标签: cfunctionfor-loopstructurebubble-sort

解决方案


首先如评论中提到的,在您扫描数据x++的末尾添加。while-loop

您还应该知道,通过将 struct ( number) 的一个成员发送到您的排序函数并对其进行排序,其他成员将不会被排序(只会将另一个数字替换为他们的)。

另请注意, sort(person[x].number, count);您正在将成员编号的一个元素从 struct 发送到 function 。在声明函数时,您说您将向其发送一个整数数组。

这应该是sort(person, count);并且在功能减速中你应该使用sort(struct account p[],int count);你也必须通过使用来改变功能p[x].number

还要注意,要x++在你的末尾添加while-loop,你必须删除那些continue,否则当你这样做时x--你将无法到达x++

我写了另一个我认为更合乎逻辑的排序函数。

struct account
{
    int number;
    char full_name[30];
    float balance;
};
void sort(struct account p[], int count);

int main()
{
    struct account person[5];
    int x = 0, i = 0, count = 0;

    printf("Enter account number, last name, and balance.\n");
    printf("Enter -999 to end input:\n\n");
    while (1)
    {
        scanf("%i", &person[x].number);
        if (person[x].number == -999) 
        {
            count = x;
            break;
        }

        if (person[x].number < 1 || person[x].number >1000)
        {
            printf("***Invalid account number. Please enter 1 - 1000 or -999 to exit***\n");
            x--;
            //remove continue
        }

        scanf("%s", person[x].full_name);
        scanf("%f", &person[x].balance);

        if (person[x].balance < 0)
        {
            printf("*** Invalid balance amount. Please enter a positive value. ***\n");
            x--;
            //remove continue
        }
        x++;//add x++ 
    }

    sort(person, count);//new function sort

    printf("ACCOUNT\tLAST NAME\tBALANCE\n");

    for (x = 0; x < count; x++)
    {
        printf("%d\t%s\t%f\n", person[x].number, person[x].full_name, person[x].balance);

    } 
}


void sort(struct account p[], int count)
{
    char changed = 'T'; 
    int x, temp;
    float btemp;
    char ntemp[30];// btemp for balance and ntemp for full_name
    while (changed == 'T')
    {
        changed = 'F';
        for (x = 0; x < count - 1; x++)
        {
            if (p[x].number > p[x + 1].number)
            {
                temp = p[x].number;
                p[x].number = p[x + 1].number;
                p[x + 1].number = temp;

                btemp = p[x].balance;
                p[x].balance = p[x + 1].balance;
                p[x + 1].balance = btemp;

                strcpy(ntemp , p[x].full_name);
                strcpy(p[x].full_name , p[x + 1].full_name);
                strcpy(p[x + 1].full_name , ntemp);
                changed = 'T';

            }
        }
    } 
} 

推荐阅读