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

      char *toStringBasic(Trial *trial, char*);

      if((trial = (Trial*)malloc(sizeof(Trial))) == NULL) {

            return NULL;

      }

      strcpy(trial->name, name);

      trial->mark1 = mark1;

      trial->mark2 = mark2;

      trial->pIsPassed = isPassedBasic;

      trial->pToString = toStringBasic;

      return trial;

}

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

      Trial *trial;

      int isPassedLite(Trial *trial);

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

            return NULL;

      }

      trial->pIsPassed = isPassedLite;

      return trial;

}

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

      Trial *trial;

      ExtraTrial *eTrial;

      int isPassedExtra(Trial *eTrial);

      char *toStringExtra(Trial *eTrial, char*);

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

            return NULL;

      }

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

            return NULL;

      }

      eTrial->trial = *trial;

      eTrial->trial.pIsPassed = isPassedExtra;

      eTrial->trial.pToString = toStringExtra;

      eTrial->mark3 = mark3;

      free(trial);

      return eTrial;

}

int result(Trial *trial) {

      return trial->mark1 + trial->mark2;

}

int isPassedBasic(Trial *trial) {

      return result(trial) >= PASS_MARK;

}

int isPassedLite(Trial *trial) {

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

}

int isPassedExtra(Trial *trial) {

      ExtraTrial *eTrial = (ExtraTrial*)trial;

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

}

int isPassed(Trial *trial) {

      return (*(trial->pIsPassed))(trial);

}

char *toStringBasic(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 *toStringExtra(Trial *trial, char *str) {

      if(trial == NULL) {

            strcpy(str, "null");

      } else {

            ExtraTrial *eTrial = (ExtraTrial*)trial;

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

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

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

            strcat(str, eTrial->mark3? "true;": "false;");

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

      }

      return str;

}

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

      return (*(trial->pToString))(trial, 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));

      }

}


package by.gsu.lecture;

public class Trial {

      protected String name;

      protected int mark1;

      protected int mark2;

      private static final int PASS_MARK = 120;

      public Trial() {

            name="Undefined";

      }

      public Trial(String name, int mark1, int mark2) {

            this.name = name;

            this.mark1 = mark1;

            this.mark2 = mark2;

      }