#define PASS_MARK1 35
#define PASS_MARK2 60
Trial *addTrial(char *name, int mark1, int mark2) {
Trial *trial;
if((trial = (Trial*)malloc(sizeof(Trial))) == NULL) {
return NULL;
}
strcpy(trial->name, name);
trial->mark1 = mark1;
trial->mark2 = mark2;
return trial;
}
int result(Trial *trial) {
return trial->mark1 + trial->mark2;
}
int isPassed(Trial *trial) {
return result(trial) >= PASS_MARK;
}
int isPassedLite(Trial *trial) {
return trial->mark1 >= PASS_MARK1 &&
trial->mark2 >= PASS_MARK2;
}
char *toString(Trial *trial, char *str) {
if(trial == NULL) {
strcpy(str, "null");
} else {
sprintf(str, "%s;%d;%d;%d;",
trial->name, trial->mark1, trial->mark2);
strcat(str, isPassed(trial)? "true": "false");
}
return str;
}
char *toStringLite(Trial *trial, char *str) {
if(trial == NULL) {
strcpy(str, "null");
} else {
sprintf(str, "%s;%d;%d;%d;",
trial->name, trial->mark1, trial->mark2);
strcat(str, isPassedLite(trial)? "true": "false");
}
return str;
}
//Runner2
void main(void) {
Trial *trials[] = {
addTrial("Sakovich", 45, 93),
addTrial("Zhylinsky", 51, 35),
addTrial("Astapenka", 90, 57)
};
char str[256];
for (int i=0; i < sizeof(trials)/sizeof(Trial*); i++) {
printf("%s\n", toString(trials[i], str));
}
Trial *liteTrials[] = {
addTrial("Barkou", 29, 95),
addTrial("Lis", 35, 68),
};
for (int i=0; i < sizeof(liteTrials)/sizeof(Trial*); i++) {
printf("%s\n", toStringLite(liteTrials[i], str));
}
}
//код добавляется, однако в «большом» количестве
//новый недостаток – наличие нескольких однотипных массивов
//with lite trial 3 (model of oop)
typedef struct TrialStruct {
char name[80];
int mark1;
int mark2;
int (*pIsPassed)(TrialStruct *trial);
} Trial;
#define PASS_MARK 120
#define PASS_MARK1 35
#define PASS_MARK2 60
Trial *addTrial(char *name, int mark1, int mark2) {
Trial *trial;
int isPassed(Trial *trial);
if((trial = (Trial*)malloc(sizeof(Trial))) == NULL) {
return NULL;
}
strcpy(trial->name, name);
trial->mark1 = mark1;
trial->mark2 = mark2;
trial->pIsPassed = isPassed;
return trial;
}
Trial *addLiteTrial(char *name, int mark1, int mark2) {
Trial *trial;
if((trial = addTrial(name, mark1, mark2)) == NULL) {
return NULL;
}
trial->pIsPassed = isPassedLite;
return trial;
}
int result(Trial *trial) {
return trial->mark1 + trial->mark2;
}
int isPassed(Trial *trial) {
return result(trial) >= PASS_MARK;
}
int isPassedLite(Trial *trial) {
return trial->mark1 >= PASS_MARK1 &&
trial->mark2 >= PASS_MARK2;
}
char *toString(Trial *trial, char *str) {
if(trial == NULL) {
strcpy(str, "null");
} else {
sprintf(str, "%s;%d;%d;%d;",
trial->name, trial->mark1, trial->mark2);
strcat(str, (*trial->pIsPassed)(trial)? "true": "false");
}
return str;
}
//Runner2
void main(void) {
Trial *trials[] = {
addTrial("Sakovich", 45, 93),
Уважаемый посетитель!
Чтобы распечатать файл, скачайте его (в формате Word).
Ссылка на скачивание - внизу страницы.