如何快速排序用户输入
How to quicksort user input
问题很简单,但越查越糊涂。我应该从我编写的结构代码中快速排序一个变量。但是,我不明白我应该如何对 em.fName 这样的变量进行快速排序,如果它在循环的每次迭代中都发生了变化。我查找的每个示例似乎都对 int 变量或预定义的字符数组进行排序。有人能给我指明从哪里开始的正确方向 and/or 举一个 C 语言的例子吗?谢谢,非常感谢。我 posted the full code here 因为我不想弄乱 post。
typedef struct EmployeeRecord
{
STR21 lName;
STR11 fName;
char fullName[33];
float hrs;
float pay;
float dfr;
float gross;
float ft;
float st;
float ssit;
float net;
} EmployeeRecord;
int main(void)
{
EmployeeRecord em;
do
{
counter++;
em.dfr = em.ft = em.st = em.ssit = em.gross = em.hrs = em.pay = em.net = ovrtHrs = 0; //initalized to 0 and then changed by function if greater than 0
printf("Please enter your FIRST name: ");
scanf("%s",fName);
printf("Please enter your LAST name: ");
scanf("%s",lName);
printf("Enter your payrate: ");
scanf("%f",&em.pay);
printf("Please enter your total hours: ");
scanf("%f",&em.hrs);
printf("Please enter the amount of deferred earnings: ");
scanf("%f",&em.dfr);
strcpy(fullName,lName); // combine last + ", " + first into full
strcat(fullName,", ");
strcat(fullName,fName);
payTotal = payTotal + em.pay;
dfrTotal = dfrTotal + em.dfr;
em.gross = grossModule(&em.hrs,em.pay,&ovrtHrs); // call 3.4
CalculateTaxes(&em.gross,&em.dfr,&em.ft,&em.st,&em.ssit); // call 3.0
em.net = netModule(&em.gross,&em.ft,&em.st,&em.ssit); // cal 3.5
hoursTotal = hoursTotal + em.hrs;
overtimeTotal = overtimeTotal + ovrtHrs; // TOTALS
grossTotal = grossTotal + em.gross;
stateTotal = stateTotal + em.st;
ssiTotal = ssiTotal + em.ssit;
netTotal = netTotal + em.net;
fedTotal = fedTotal + em.ft;
fprintf(myreport,REPORTCOLUMNFORMATS,em.fullName,em.pay,em.hrs,em.gross,em.ft,em.ssit,em.net);
fprintf(myreport,REPORTCOLUMNFORMATS3,ovrtHrs,em.st,em.dfr);
printf("Would you like to continue, y/n? \n");
scanf("%s",&ask);
}while(ask == 'y' || ask == 'Y');
这里有一些建议:
你只读了一个EmployeeRecord,然后它被覆盖了,如果你想对它们进行排序,你必须把它们全部保存下来。您可以使用 realloc
动态更改保留 space 的大小或保持简单:
EmployeeRecord employees[100]; // will fail if more than 100 employees are added
...
// after em is complete:
employees[counter++] = em; // remove the other counter++
排序取自 wikibooks.org 并用普通 int 替换指针,使用索引而不是指针数学(更容易理解)并添加您必须填写的比较函数:
void swap(int a, int b)
{
EmployeeRecord tmp = employees[a];
employees[a] = employees[b];
employees[b] = tmp;
}
int compare(int a, int b)
{
// return -1 if employees[a] comes before employees[b]
// return 1 if employees[a] comes after employees[b]
// return 0 "if they have the same order"
// (stable algorithms e.g. mergesort won't change the order of those)
}
void quicksort(int begin, int end)
{
int ptr;
int split;
if (end - begin <= 1)
return;
ptr = begin;
split = begin + 1;
while (++ptr <= end) {
if (compare(ptr, split) > 0) { // sorts ascending
swap(ptr, split);
++split;
}
}
swap(begin, split - 1);
quicksort(begin, split - 1);
quicksort(split, end);
}
在用户决定不再继续后排序并创建报告。
问题很简单,但越查越糊涂。我应该从我编写的结构代码中快速排序一个变量。但是,我不明白我应该如何对 em.fName 这样的变量进行快速排序,如果它在循环的每次迭代中都发生了变化。我查找的每个示例似乎都对 int 变量或预定义的字符数组进行排序。有人能给我指明从哪里开始的正确方向 and/or 举一个 C 语言的例子吗?谢谢,非常感谢。我 posted the full code here 因为我不想弄乱 post。
typedef struct EmployeeRecord
{
STR21 lName;
STR11 fName;
char fullName[33];
float hrs;
float pay;
float dfr;
float gross;
float ft;
float st;
float ssit;
float net;
} EmployeeRecord;
int main(void)
{
EmployeeRecord em;
do
{
counter++;
em.dfr = em.ft = em.st = em.ssit = em.gross = em.hrs = em.pay = em.net = ovrtHrs = 0; //initalized to 0 and then changed by function if greater than 0
printf("Please enter your FIRST name: ");
scanf("%s",fName);
printf("Please enter your LAST name: ");
scanf("%s",lName);
printf("Enter your payrate: ");
scanf("%f",&em.pay);
printf("Please enter your total hours: ");
scanf("%f",&em.hrs);
printf("Please enter the amount of deferred earnings: ");
scanf("%f",&em.dfr);
strcpy(fullName,lName); // combine last + ", " + first into full
strcat(fullName,", ");
strcat(fullName,fName);
payTotal = payTotal + em.pay;
dfrTotal = dfrTotal + em.dfr;
em.gross = grossModule(&em.hrs,em.pay,&ovrtHrs); // call 3.4
CalculateTaxes(&em.gross,&em.dfr,&em.ft,&em.st,&em.ssit); // call 3.0
em.net = netModule(&em.gross,&em.ft,&em.st,&em.ssit); // cal 3.5
hoursTotal = hoursTotal + em.hrs;
overtimeTotal = overtimeTotal + ovrtHrs; // TOTALS
grossTotal = grossTotal + em.gross;
stateTotal = stateTotal + em.st;
ssiTotal = ssiTotal + em.ssit;
netTotal = netTotal + em.net;
fedTotal = fedTotal + em.ft;
fprintf(myreport,REPORTCOLUMNFORMATS,em.fullName,em.pay,em.hrs,em.gross,em.ft,em.ssit,em.net);
fprintf(myreport,REPORTCOLUMNFORMATS3,ovrtHrs,em.st,em.dfr);
printf("Would you like to continue, y/n? \n");
scanf("%s",&ask);
}while(ask == 'y' || ask == 'Y');
这里有一些建议:
你只读了一个EmployeeRecord,然后它被覆盖了,如果你想对它们进行排序,你必须把它们全部保存下来。您可以使用
realloc
动态更改保留 space 的大小或保持简单:EmployeeRecord employees[100]; // will fail if more than 100 employees are added ... // after em is complete: employees[counter++] = em; // remove the other counter++
排序取自 wikibooks.org 并用普通 int 替换指针,使用索引而不是指针数学(更容易理解)并添加您必须填写的比较函数:
void swap(int a, int b) { EmployeeRecord tmp = employees[a]; employees[a] = employees[b]; employees[b] = tmp; } int compare(int a, int b) { // return -1 if employees[a] comes before employees[b] // return 1 if employees[a] comes after employees[b] // return 0 "if they have the same order" // (stable algorithms e.g. mergesort won't change the order of those) } void quicksort(int begin, int end) { int ptr; int split; if (end - begin <= 1) return; ptr = begin; split = begin + 1; while (++ptr <= end) { if (compare(ptr, split) > 0) { // sorts ascending swap(ptr, split); ++split; } } swap(begin, split - 1); quicksort(begin, split - 1); quicksort(split, end); }
在用户决定不再继续后排序并创建报告。