冒泡排序函数后跳过循环
For loop being skipped after bubble sort function
我正在为期末作业制作分类程序。它获取帐户的帐号、姓氏和余额,并根据帐号按升序对其进行排序。我的程序遇到的问题是,一旦进入 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 . . .
感谢您的帮助以及您抽出时间来提供帮助!
首先如评论中所述,在 while-loop
末尾添加 x++
以扫描数据。
您还应该知道,通过将结构 (number
) 的一个成员发送到您的排序函数并对其进行排序,其他成员将不会被排序(只有另一个数字将被替换为他们的)。
这里还要注意 sort(person[x].number, count);
您正在将成员编号的一个元素从 struct 发送到 function 。在函数声明中,你说你将向它发送一个整数数组。
这应该是 sort(person, count);
并且在函数减速中你应该
使用 sort(struct account p[],int count);
您还必须使用 p[x].number
.
更改功能
另请注意,要在 while-loop
的末尾添加 x++
,您必须删除那些 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';
}
}
}
}
我正在为期末作业制作分类程序。它获取帐户的帐号、姓氏和余额,并根据帐号按升序对其进行排序。我的程序遇到的问题是,一旦进入 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 . . .
感谢您的帮助以及您抽出时间来提供帮助!
首先如评论中所述,在 while-loop
末尾添加 x++
以扫描数据。
您还应该知道,通过将结构 (number
) 的一个成员发送到您的排序函数并对其进行排序,其他成员将不会被排序(只有另一个数字将被替换为他们的)。
这里还要注意 sort(person[x].number, count);
您正在将成员编号的一个元素从 struct 发送到 function 。在函数声明中,你说你将向它发送一个整数数组。
这应该是 sort(person, count);
并且在函数减速中你应该
使用 sort(struct account p[],int count);
您还必须使用 p[x].number
.
另请注意,要在 while-loop
的末尾添加 x++
,您必须删除那些 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';
}
}
}
}