Skip to content

Instantly share code, notes, and snippets.

@sumnjc
Created June 20, 2018 02:14
Show Gist options
  • Save sumnjc/a4f3031fe49890cf27e3d4ea9eef54c8 to your computer and use it in GitHub Desktop.
Save sumnjc/a4f3031fe49890cf27e3d4ea9eef54c8 to your computer and use it in GitHub Desktop.
/*
* 함수포인터를 사용하여 전략패턴을 구사
* vector 사용
*/
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <vector>
using namespace std;
typedef struct _pinfo
{
char name[12];
int math;
int korean;
int eng;
} pinfo;
void printmax(vector<pinfo> va, void(*SortFunc)(vector<pinfo>))
{
printf("최고 성적 출력 프로그램\n");
SortFunc(va);
}
void engmax(vector<pinfo> va)
{
int max = 0;
int index = 0;
for (int i=0; i<va.size(); i++)
{
if(va[i].eng > max)
{
max = va[i].eng;
index = i;
}
}
printf("영어 최고점수 획득자는 %s: %d\n",
va[index].name,
va[index].eng);
}
void avgmax(vector<pinfo> va)
{
int max = 0;
int total;
int index = 0;
for (int i=0; i<va.size(); i++)
{
total = (va[i].eng + va[i].math + va[i].korean);
if(total > max)
{
max = total;
index = i;
}
}
printf("최고점자 : \n");
printf("이름 : %s\n", va[index].name);
printf("영어 : %d\n", va[index].eng);
printf("국어 : %d\n", va[index].korean);
printf("수학 : %d\n", va[index].math);
printf("평점 : %.2f\n", (float)max/3.);
}
int main()
{
pinfo myinfo;
vector<pinfo> va;
myinfo.korean = 80;
myinfo.eng = 65;
myinfo.math = 99;
strncpy(myinfo.name, "jsyoo", 12);
va.push_back(myinfo);
myinfo.korean = 90;
myinfo.eng = 65;
myinfo.math = 74;
strncpy(myinfo.name, "kate", 12);
va.push_back(myinfo);
myinfo.korean = 63;
myinfo.eng = 88;
myinfo.math = 55;
strncpy(myinfo.name, "jenny", 12);
va.push_back(myinfo);
printmax(va, engmax);
printmax(va, avgmax);
printf("engmax : %p\n", engmax);
printf("avgmax : %p\n", avgmax);
printf("printmax : %p\n", printmax);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment