Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

(急)請幫我看看這”c語言”程式(很急20點)

請利用陣列撰寫一個程式,

用來儲存學生成績(介於0~100),

輸入後利用氣泡排序法將陣列排序後輸出排序結果與名次;

""禁止宣告全域變數,並請使用副函式""。

執行範例如下

Insert grade (-1 to end): 20(←執行者輸入此數字)

Insert grade (-1 to end): -2(←執行者輸入此數字)

Input error!

Insert grade (-1 to end): 100(←執行者輸入此數字)

Insert grade (-1 to end): 20(←執行者輸入此數字)

Insert grade (-1 to end): 56(←執行者輸入此數字)

Insert grade (-1 to end): 86(←執行者輸入此數字)

Insert grade (-1 to end): 86(←執行者輸入此數字)

Insert grade (-1 to end): -1(←執行者輸入此數字)

Option: 1) Add. 2) Output. 3) Quit: 2(←執行者輸入此數字)

GradeRank

-------------------------------

1001

862

862

564

205

205

Option: 1) Add. 2) Output. 3) Quit: 1(←執行者輸入此數字)

Insert grade (-1 to end): 72(←執行者輸入此數字)

Insert grade (-1 to end): 34(←執行者輸入此數字)

Insert grade (-1 to end): -1(←執行者輸入此數字)

Option: 1) Add. 2) Output. 3) Quit: 2(←執行者輸入此數字)

GradeRank

-------------------------------

1001

862

862

724

565

346

207

207

Option: 1) Add. 2) Output. 3) Quit:

(←執行者輸入此數字)代表跑程式時執行者手動輸入此數字

我是學c語言,不是c++!!!

我是學c語言,不是c++!!!

我是學c語言,不是c++!!!

請問這個c語言程式該怎麼寫呢???(完整版)

並註解喔謝謝=)

Update:

謝謝你!!!

可以...麻煩你加個註解嗎?

因為我初學者太難的東西看不懂對不起ˊˋ

所以想把宣告值或一些其他地方改成簡單一點的...

可是我不知道要怎麼多加點數給你耶>

1 Answer

Rating
  • gary
    Lv 4
    1 decade ago
    Favorite Answer

    #include <stdio.h>

    #include <conio.h>

    #define MAX_SIZE 10

    void add(int scoreColl[], int *pCount);

    void sort(int *pScoreColl, int n);

    void print(int *pScoreColl, int count);

    int main()

    {

    char op;

    int scoreColl[MAX_SIZE];

    int count = 0;

    do

    {

    printf("Option: 1) Add 2) Output 3) Quit: ");

    op = getche();

    printf("\n");

    switch (op)

    {

    case '1':

    add(scoreColl, &count);

    break;

    case '2':

    print(scoreColl, count);

    break;

    case '3':

    break;

    default:

    printf("Invalid input, please try again\n");

    }

    } while (op != '3');

    return 0;

    }

    void add(int scoreColl[], int *pCount)

    {

    int score;

    int sortflag = 0;

    int *pCurrent = &scoreColl[*pCount];

    do

    {

    if (*pCount < MAX_SIZE)

    {

    printf("Enter score (-1 to end): ");

    scanf("%d", &score);

    if (score >= 0 && score <= 100)

    {

    *pCurrent = score;

    pCurrent++;

    (*pCount)++;

    sortflag = 1;

    }

    else if (score < -1 || score > 100)

    {

    printf("Invalid input (score range: 0 - 100)\n");

    }

    }

    else

    {

    printf("Buffer is full, can't accept more scores\n");

    score = -1;

    }

    } while (score != -1);

    if (sortflag)

    sort(scoreColl, *pCount);

    }

    void sort(int *pScoreColl, int n)

    {

    int i, j;

    for (i=0; i<n-1; i++)

    {

    for (j=0; j<n-1-i; j++)

    if (pScoreColl[j+1] > pScoreColl[j]) /* compare the two neighbors */

    {

    /* swap a[j] and a[j+1] */

    pScoreColl[j] ^= pScoreColl[j+1];

    pScoreColl[j+1] ^= pScoreColl[j];

    pScoreColl[j] ^= pScoreColl[j+1];

    }

    }

    }

    void print(int *pScoreColl, int count)

    {

    int i, prev = -1, rank;

    printf("Score Rank\n");

    printf("----------\n");

    for (i =0; i<count; i++)

    {

    printf("%d\t", pScoreColl[i]);

    if (pScoreColl[i] != prev)

    rank = i + 1;

    printf("%d\n", rank);

    prev = pScoreColl[i];

    }

    }

    2008-11-24 21:39:16 補充:

    這個程式沒有用什麼很複雜的機制

    對初學者來說比較復職的可能是 swap 的部份

    那部份也已經幫你註解了

    其他的部份都是很普通的用法

    學過陣列跟指標就應該看得懂了

    你研究看看 把程式弄懂 對你會有幫助

    加油囉

    Source(s): me
Still have questions? Get your answers by asking now.