Guest User

Untitled

a guest
Jun 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. /*
  2. * 함수포인터를 사용하여 전략패턴을 구사
  3. * vector 사용
  4. */
  5.  
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <vector>
  11.  
  12. using namespace std;
  13.  
  14. typedef struct _pinfo
  15. {
  16. char name[12];
  17. int math;
  18. int korean;
  19. int eng;
  20. } pinfo;
  21.  
  22. void printmax(vector<pinfo> va, void(*SortFunc)(vector<pinfo>))
  23. {
  24. printf("최고 성적 출력 프로그램\n");
  25. SortFunc(va);
  26. }
  27.  
  28. void engmax(vector<pinfo> va)
  29. {
  30. int max = 0;
  31. int index = 0;
  32. for (int i=0; i<va.size(); i++)
  33. {
  34. if(va[i].eng > max)
  35. {
  36. max = va[i].eng;
  37. index = i;
  38. }
  39. }
  40. printf("영어 최고점수 획득자는 %s: %d\n",
  41. va[index].name,
  42. va[index].eng);
  43. }
  44.  
  45. void avgmax(vector<pinfo> va)
  46. {
  47. int max = 0;
  48. int total;
  49. int index = 0;
  50. for (int i=0; i<va.size(); i++)
  51. {
  52. total = (va[i].eng + va[i].math + va[i].korean);
  53. if(total > max)
  54. {
  55. max = total;
  56. index = i;
  57. }
  58. }
  59.  
  60. printf("최고점자 : \n");
  61. printf("이름 : %s\n", va[index].name);
  62. printf("영어 : %d\n", va[index].eng);
  63. printf("국어 : %d\n", va[index].korean);
  64. printf("수학 : %d\n", va[index].math);
  65. printf("평점 : %.2f\n", (float)max/3.);
  66. }
  67.  
  68. int main()
  69. {
  70. pinfo myinfo;
  71. vector<pinfo> va;
  72.  
  73. myinfo.korean = 80;
  74. myinfo.eng = 65;
  75. myinfo.math = 99;
  76. strncpy(myinfo.name, "jsyoo", 12);
  77. va.push_back(myinfo);
  78.  
  79. myinfo.korean = 90;
  80. myinfo.eng = 65;
  81. myinfo.math = 74;
  82. strncpy(myinfo.name, "kate", 12);
  83. va.push_back(myinfo);
  84.  
  85. myinfo.korean = 63;
  86. myinfo.eng = 88;
  87. myinfo.math = 55;
  88. strncpy(myinfo.name, "jenny", 12);
  89. va.push_back(myinfo);
  90.  
  91. printmax(va, engmax);
  92. printmax(va, avgmax);
  93.  
  94. printf("engmax : %p\n", engmax);
  95. printf("avgmax : %p\n", avgmax);
  96. printf("printmax : %p\n", printmax);
  97. }
Add Comment
Please, Sign In to add comment