Guest User

Untitled

a guest
Jan 13th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. /* 程式範例: Ch2-3-4e.c */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. struct test { /* 考試成績的結構 */
  6. int math;
  7. int english;
  8. int computer;
  9. };
  10. struct phone /* phone結構 */
  11. {
  12. char phone1[15];
  13. char phone2[15];
  14. };
  15.  
  16. struct student { /* 學生資料的結構 */
  17. int id;
  18. char name[20];
  19. struct test score;
  20. struct phone callno; /* 結構變數 */
  21. };
  22. /* 主程式 */
  23. int main() {
  24. /* 結構變數的宣告 */
  25. struct student std1;
  26. struct student std2 = {2, "江小魚", {45, 78, 66},{"0988123456","0987333666"}};
  27. int total;
  28. std1.id = 1; /* 指定結構變數的值 */
  29. strcpy(std1.name, "陳會安");
  30. std1.score.math = 78;
  31. std1.score.english = 65;
  32. std1.score.computer = 90;
  33. strcpy(std1.callno.phone1,"0987987987");
  34. strcpy(std1.callno.phone2,"0987999666");
  35. /* 顯示學生資料 */
  36. printf("學號: %d\n", std1.id);
  37. printf("姓名: %s\n", std1.name);
  38. printf("電話: %s\n", std1.callno.phone1);
  39. printf("電話: %s\n", std1.callno.phone2);
  40. total = std1.score.math + std1.score.english +
  41. std1.score.computer;
  42. printf("成績總分: %d\n", total);
  43. printf("--------------------\n");
  44. printf("學號: %d\n", std2.id);
  45. printf("姓名: %s\n", std2.name);
  46. printf("電話: %s\n", std2.callno.phone1);
  47. printf("電話: %s\n", std2.callno.phone2);
  48. total = std2.score.math + std2.score.english +
  49. std2.score.computer;
  50. printf("成績總分: %d\n", total);
  51. system("PAUSE");
  52. return 0;
  53. }
Add Comment
Please, Sign In to add comment