简体   繁体   English

如何快速分配用户输入

[英]How to quicksort user input

The question is rather simple but the more I search the more confused I get. 这个问题相当简单,但我搜索的越多,我就越困惑。 I'm supposed to quicksort a variable from the struct code I've written. 我应该从我编写的结构代码中快速输入一个变量。 However, I don't understand how I'm supposed to quicksort a variable like em.fName if it's changed every iteration of the loop. 但是,我不明白如果它在循环的每次迭代中都改变了,我应​​该如何快速输入像em.fName这样的变量。 Every example I look up seems to sort int variables or predefined character arrays. 我查找的每个示例似乎都对int变量或预定义的字符数组进行排序。 Could some one point me in the right direction where to start and/or give an example in C? 有人能指出我在哪里开始和/或在C中给出一个例子吗? Thanks, greatly appreciate it. 谢谢,非常感谢。 I've posted the full code here since I didn't want to clutter the 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');

Here are some pointers: 以下是一些指示:

  1. You are only reading one EmployeeRecord and then it gets overwritten, if you want to sort them you have to save them all. 您只是阅读一个EmployeeRecord然后被覆盖,如果要对它们进行排序,则必须将它们全部保存。 You could work with realloc to dynamically change the size of the reserved space or keep it simple: 您可以使用realloc动态更改保留空间的大小或保持简单:

     EmployeeRecord employees[100]; // will fail if more than 100 employees are added ... // after em is complete: employees[counter++] = em; // remove the other counter++ 
  2. Sorting taken from wikibooks.org and replaced the pointers by normal int, working with the index instead of pointer math (easier to understand) and added compare function which you will have to fill: 从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 eg 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); } 
  3. Sort and create the report after the user decided to not continue any more. 在用户决定不再继续之后对报告进行排序和创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM