Anonymous

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

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

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(←執行者輸入此數字)

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

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(←執行者輸入此數字)

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

1001

862

862

724

565

346

207

207

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

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

Update:

Rating
• gary
Lv 4

#include <stdio.h>

#include <conio.h>

#define MAX_SIZE 10

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':

break;

case '2':

print(scoreColl, count);

break;

case '3':

break;

default:

}

} while (op != '3');

return 0;

}

{

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