Guest User

Untitled

a guest
Feb 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. /* 程式範例: Ch2-1-2.c */
  2. #include <stdio.h>
  3. //#include <stdlib.h>
  4. /* 主程式 */
  5. int main() {
  6. /* 學生成績陣列 */
  7. int scores[10] = {76,85,90,67,59,79,82,95,91,65};
  8. int num; /* 學號 */
  9. int grade; /* 成績 */
  10. int i, choice; /* 選項 */
  11. int num1,temp; /* 對調 */
  12. int doit = 1;
  13. /* 執行操作的主迴圈 */
  14. while ( doit ) {
  15. printf("----選單----\n");
  16. printf("1: 查詢成績\n");
  17. printf("2: 修改成績\n");
  18. printf("3: 成績對調\n");
  19. printf("4: 顯示成績\n");
  20. printf("5: 離開作業\n");
  21. printf("請輸入選項( 1 到 5 ). ==> ");
  22. scanf("%d", &choice);
  23. if (choice < 4 ) {
  24. printf("請輸入學生學號( 0 到 9). ==> ");
  25. scanf("%d", &num); /* 讀入學號 */
  26. }
  27. switch( choice ) {
  28. case 1: /* 查詢成績 */
  29. grade = scores[num]; /* 取得成績 */
  30. printf("學生成績: %d\n", grade);
  31. break;
  32. case 2: /* 修改成績 */
  33. grade = scores[num];
  34. printf("原來學生成績: %d\n", grade);
  35. printf("輸入新成績. ==> ");/* 讀取新成績 */
  36. scanf("%d", &grade);
  37. scores[num] = grade; /* 更新成績 */
  38. break;
  39. case 3: /* 成績對調 */
  40. printf("欲對調成績學生學號:");
  41. scanf("%d",&num1);
  42. temp=scores[num];
  43. scores[num]=scores[num1];
  44. scores[num1]=temp;
  45. break;
  46. case 4: /* 顯示成績 */
  47. printf("學生成績: \n");
  48. for ( i = 0; i < 10; i++ )
  49. printf("%d:%d ", i, scores[i]);
  50. printf("\n");
  51. break;
  52. case 5: /* 結束作業 */
  53. doit = 0;
  54. break;
  55. }
  56. }
  57. system("PAUSE");
  58. return 0;
  59. }
Add Comment
Please, Sign In to add comment