Проблемы развития проекта при процедурном подходе, страница 4

#define PASS_MARK 120

#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;

      trial->kind = BASIC;

      return trial;

}

Trial *addLiteTrial(char *name, int mark1, int mark2) {

      Trial *trial;

      if((trial = addTrial(name, mark1, mark2)) == NULL) {

            return NULL;

      }

      trial->kind = LITE;

      return trial;

}

ExtraTrial *addExtraTrial(char *name, int mark1, int mark2, int mark3) {

      Trial *trial;

      ExtraTrial *eTrial;

      if((trial = addTrial(name, mark1, mark2)) == NULL) {

            return NULL;

      }

      if((eTrial = (ExtraTrial*)malloc(sizeof(ExtraTrial))) == NULL) {

            return NULL;

      }

      eTrial->trial = *trial;

      eTrial->trial.kind = EXTRA;

      eTrial->mark3 = mark3;

      free(trial);

      return eTrial;

}

int result(Trial *trial) {

      return trial->mark1 + trial->mark2;

}

int isPassed(Trial *trial) {

      int res;

      switch(trial->kind) {

            case BASIC:

                  res = result(trial) >= PASS_MARK;

                  break;

            case LITE:

                  res = trial->mark1 >= PASS_MARK1 && trial->mark2 >= PASS_MARK2;

                  break;

            case EXTRA:

                  ExtraTrial *eTrial = (ExtraTrial*)trial;

                  res = result(&(eTrial->trial)) >= PASS_MARK && eTrial->mark3;

                  break;

      }

      return res;

}

char *toString(Trial *trial, char *str) {

      if(trial == NULL) {

            strcpy(str, "null");

      } else {

            switch(trial->kind) {

                  case EXTRA:

                        Trial *trl = &(((ExtraTrial*)trial)->trial);

                        sprintf(str, "%s;%d;%d;%d;",

                              trl->name, trl->mark1, trl->mark2);

                        strcat(str, ((ExtraTrial*)trial)->mark3? "true;": "false;");

                        strcat(str, isPassed(trl)? "true": "false");

                        break;

                  default:

                        sprintf(str, "%s;%d;%d;%d;",

                              trial->name, trial->mark1, trial->mark2);

                        strcat(str, isPassed(trial)? "true": "false");

            }

      }

      return str;

}

//Runner2

void main(void) {

      Trial *trials[] = {

            addTrial("Sakovich", 45, 93),

            addTrial("Zhylinsky", 51, 35),

            addLiteTrial("Barkou", 29, 95),

            addLiteTrial("Lis", 35, 68),

            addTrial("Astapenka", 90, 57),

            (Trial*)addExtraTrial("Kotau", 73, 60, 1)

      };

      char str[256];

      for (int i=0; i < sizeof(trials)/sizeof(Trial*); i++) {

            printf("%s\n", toString(trials[i], str));

      }

}

//oop model

#include<stdio.h>

#include<string.h>

#include<alloc.h>

typedef struct TrialStruct {

      char name[80];

      int mark1;

      int mark2;

      int (*pIsPassed)(TrialStruct *trial);

      char* (*pToString)(TrialStruct *trial, char*);

} Trial;

typedef struct ExtraTrialStruct {

      Trial trial;

      int mark3;

} ExtraTrial;

#define PASS_MARK 120

#define PASS_MARK1 35

#define PASS_MARK2 60

Trial *addTrial(char *name, int mark1, int mark2) {

      Trial *trial;

      int isPassedBasic(Trial *trial);