簡體   English   中英

如何快速分配用戶輸入

[英]How to quicksort user input

這個問題相當簡單,但我搜索的越多,我就越困惑。 我應該從我編寫的結構代碼中快速輸入一個變量。 但是,我不明白如果它在循環的每次迭代中都改變了,我應​​該如何快速輸入像em.fName這樣的變量。 我查找的每個示例似乎都對int變量或預定義的字符數組進行排序。 有人能指出我在哪里開始和/或在C中給出一個例子嗎? 謝謝,非常感謝。 我已經在這里發布了完整的代碼,因為我不想讓帖子混亂。

    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');

以下是一些指示:

  1. 您只是閱讀一個EmployeeRecord然后被覆蓋,如果要對它們進行排序,則必須將它們全部保存。 您可以使用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. 從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. 在用戶決定不再繼續之后對報告進行排序和創建。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM