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

            addTrial("Zhylinsky", 51, 35),

            addLiteTrial("Barkou", 29, 95),

            addLiteTrial("Lis", 35, 68),

            addTrial("Astapenka", 90, 57)

      };

      char str[256];

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

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

      }

}

//код «умеренно» как переписывается, так и добавляется

//решение компромиссное, но развитие проблематично из-за отсутствия

//подходящих концептуальных (и языковых) средств

//добавляется новый вид испытания – экстра (с дополнительным зачетом)

//common struct & new enum item

#include<stdio.h>

#include<string.h>

#include<alloc.h>

typedef enum {

      BASIC, LITE, EXTRA

} TrialKind;

typedef struct TrialStruct {

      char name[80];

      int mark1;

      int mark2;

      int mark3;

      TrialKind kind;

} Trial;

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

}

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

      Trial *trial;

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

            return NULL;

      }

      trial->kind = EXTRA;

      trial->mark3 = mark3;

      return trial;

}

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: res = result(trial) >= PASS_MARK && trial->mark3;

                        break;

      }

      return res;

}

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

            if(trial->kind == EXTRA) {

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

            }

            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),

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

      }

}

//new struct & new enum item

#include<stdio.h>

#include<string.h>

#include<alloc.h>

typedef enum {

      BASIC, LITE, EXTRA

} TrialKind;

typedef struct TrialStruct {

      char name[80];

      int mark1;

      int mark2;

      TrialKind kind;

} Trial;

typedef struct ExtraTrialStruct {

      Trial trial;

      int mark3;

} ExtraTrial;