Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- #define FILENAME_LENGTH 32
- #define VECTOR_CAPACITY 8
- #define VECTOR_GROWTH_RATE 2
- char endl = '\0';
- char space = ' ';
- void die() {
- printf("An internal error occurred");
- exit(1);
- }
- // Vector library for working with dynamic memory allocation
- typedef struct {
- unsigned int length;
- unsigned int capacity;
- unsigned int size;
- unsigned char *data;
- } Vector;
- Vector *vector_init(unsigned int size) {
- Vector *vector;
- unsigned char *tmp;
- vector = malloc(sizeof(Vector));
- if (!vector) return NULL;
- vector->size = size;
- vector->length = 0;
- vector->capacity = VECTOR_CAPACITY;
- tmp = malloc(vector->size * vector->capacity);
- if(!tmp) die();
- vector->data = tmp;
- return vector;
- }
- int vector_len(Vector *vector) {
- return vector->length;
- }
- void* vector_getarr(Vector *vector) {
- return vector->data;
- }
- void* vector_get(Vector *vector, int index) {
- return vector->data + index * vector->size;
- }
- void vector_set(Vector *vector, int index, void *value) {
- memcpy(vector->data + index * vector->size, value, vector->size);
- }
- int vector_push(Vector *vector, void *value) {
- if (vector->length == vector->capacity) {
- unsigned char *tmp;
- tmp = realloc(vector->data, vector->size * vector->capacity);
- if (!tmp) {
- printf("Cannot allocate memory");
- exit(1);
- };
- vector->capacity *= VECTOR_GROWTH_RATE;
- vector->data = tmp;
- }
- vector_set(vector, vector->length++, value);
- return 0;
- }
- void* vector_pop(Vector *vector) {
- return vector->data + --vector->length * vector->size;
- }
- void vector_free(Vector *vector) {
- free(vector->data);
- free(vector);
- }
- // Data structures for storing input information
- Vector *Courses, *Professors, *Teachers, *Students;
- typedef struct {
- char* name;
- Vector trained;
- Vector courses;
- int course_cap;
- } Prof;
- typedef struct {
- char* name;
- Vector trained;
- Vector courses;
- int current_labs;
- } TA;
- typedef struct {
- char* name;
- int lab_max, stud_max;
- int lab_cur, stud_cur;
- Prof professor;
- Vector TAs;
- int score;
- } Course;
- typedef struct {
- char *name;
- char *id;
- Vector classes;
- } Student;
- // Utility functions
- // Initialize the course
- void course_init(Course *course, char *name, int lab_max, int stud_max) {
- strcpy(course->name, name);
- course->lab_cur = 0;
- course->stud_cur = 0;
- course->lab_max = lab_max;
- course->stud_max = stud_max;
- course->score = 0;
- }
- void course_free(Course *course) {
- free(course->name);
- }
- char *make_filename(int n, char type) {
- char *name = calloc(sizeof(char), FILENAME_LENGTH);
- int len;
- if(type == 'i') {
- strcpy(name, "input");
- len = 5;
- }
- else {
- strcpy(name, "YaroslavShumichenkoOutput");
- len = 25;
- }
- char *num = calloc(sizeof(char), 3);
- itoa(n, num, 10);
- strcat(name, num);
- len += strlen(num);
- strcat(name, ".txt");
- len += 4;
- name[len] = endl;
- return name;
- }
- Vector *merge_strings(Vector *a, Vector *b) {
- vector_push(a, &space);
- for (int i = 0; i < vector_len(b); i++)
- vector_push(a, vector_get(b, i));
- vector_free(b);
- return a;
- }
- char *vector_to_string(Vector *s) {
- if(s == NULL) return NULL;
- char *string = malloc((s->length + 1) * sizeof(char));
- for(int i = 0; i < s->length; i++) string[i] = *((char *) vector_get(s, i));
- string[s->length] = '\0';
- vector_free(s);
- return string;
- }
- Vector *read_word(FILE *in, int (*isfunc)(int)) {
- Vector *s = vector_init(sizeof(char));
- int c;
- if (c = fgetc(in), !isfunc(c))
- return NULL;
- vector_push(s, &c);
- while (c = fgetc(in), isfunc(c))
- vector_push(s, &c);
- vector_push(s, &endl);
- ungetc(c, in);
- return s;
- }
- int read_char(FILE *in, int c) {
- if (fgetc(in) == c)
- return 0;
- return 1;
- }
- int read_int(FILE *in, int *out) {
- Vector *s = read_word(in, isdigit);
- char *t = vector_to_string(s);
- if (s == NULL) {
- vector_free(s);
- return 1;
- }
- *out = atoi(t);
- vector_free(s);
- return 0;
- }
- Course *scan_course(FILE *in) {
- Course *course = NULL, c;
- Vector *name;
- while(1) {
- int labs, stud;
- if (!(name = read_word(in, isalpha)))
- return NULL;
- char *name_s = vector_to_string(name);
- if (strcmp(name_s, "P") == 0) {
- if (read_char(in, '\n')) {
- vector_free(name);
- return NULL;
- }
- vector_free(name);
- return course;
- }
- if (read_char(in, ' ')
- || read_int (in, &labs)
- || read_char(in, ' ')
- || read_int (in, &stud)
- || read_char(in, '\n')) {
- vector_free(name);
- return NULL;
- }
- for (int i = 0; i < vector_len(Courses); i++) {
- Course *t = vector_get(Courses, i);
- if (strcmp(t->name, name_s) == 0) {
- vector_free(name);
- return NULL;
- }
- }
- course_init(&c, name_s, labs, stud);
- vector_push(Courses, &c);
- }
- }
- Prof *scan_prof(FILE *in) {
- Prof *prof = NULL, p;
- Vector *name, *surname;
- char *name_s, *surname_s;
- for (;;) {
- int c;
- if (!(name = read_word(in, isalpha)))
- return NULL;
- name_s = vector_to_string(name);
- if (strcmp(name_s, "T") == 0) {
- if (read_char(in, '\n')) {
- vector_free(name);
- return NULL;
- }
- vector_free(name);
- return prof;
- }
- if (read_char(in, ' ')
- || (!(surname = read_word(in, isalpha)))) {
- vector_free(name);
- return NULL;
- }
- name = merge_strings(name, surname);
- name_s = vector_to_string(name);
- for (int i = 0; i < vector_len(Professors); i++) {
- Prof *t = vector_get(Professors, i);
- if (strcmp(t->name, name_s) == 0) {
- vector_free(name);
- return NULL;
- }
- }
- p.name = name_s;
- p.course_cap = 2;
- p.trained = *vector_init(sizeof(Course));
- p.courses = *vector_init(sizeof(Course));
- // PIZDA
- while (c = getc(in), c == ' ') {
- Course *tmp;
- char *s = read_word(in, isalpha);
- if (!s || !(tmp = subj_lookup(subj, s))) {
- vector_push(prof, p);
- vector_free(name);
- return NULL;
- }
- vector_push(p.trained, tmp);
- vector_free(s);
- }
- vector_push(prof, p);
- if (c != '\n')
- goto err0;
- }
- err1: vector_free(fnm);
- return NULL;
- }
- TA *scan_teacher(FILE *in) {
- }
- Student *scan_student(FILE *in) {
- }
- typedef struct {
- char *filename;
- int number;
- int dne;
- int is_invalid;
- } Output_file;
- void file_init(Output_file *file, char *name, int num, int dne) {
- strcpy(file->filename, name);
- file->number = num;
- file->dne = dne;
- }
- int main() {
- Output_file outputs[50];
- char *input_names[50];
- Courses = vector_init(sizeof(Course));
- Professors = vector_init(sizeof(Prof));
- Teachers = vector_init(sizeof(TA));
- Students = vector_init(sizeof(Student));
- int last_existing_file = 0;
- for(int i = 1; i <= 50; i++) {
- input_names[i-1] = make_filename(i, 'i');
- FILE *input;
- if((input = fopen(input_names[i-1], "r")) != NULL) {
- last_existing_file = i;
- file_init(&outputs[i-1], make_filename(i, 'o'), i, 0);
- fclose(input);
- } else file_init(&outputs[i-1], make_filename(i, 'o'), i, 1);
- free(input);
- }
- for(int i = 0; i < last_existing_file; i++) {
- FILE *input;
- if((input = fopen(input_names[i], "r")) != NULL) {
- do {
- } while(!feof(input));
- fclose(input);
- } else {
- FILE *out = fopen(outputs[i].filename, "w");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement