Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5.  
  6. #define FILENAME_LENGTH 32
  7.  
  8. #define VECTOR_CAPACITY 8
  9. #define VECTOR_GROWTH_RATE 2
  10.  
  11. char endl = '\0';
  12. char space = ' ';
  13.  
  14. void die() {
  15. printf("An internal error occurred");
  16. exit(1);
  17. }
  18.  
  19. // Vector library for working with dynamic memory allocation
  20.  
  21. typedef struct {
  22. unsigned int length;
  23. unsigned int capacity;
  24. unsigned int size;
  25. unsigned char *data;
  26. } Vector;
  27.  
  28. Vector *vector_init(unsigned int size) {
  29. Vector *vector;
  30. unsigned char *tmp;
  31.  
  32. vector = malloc(sizeof(Vector));
  33. if (!vector) return NULL;
  34.  
  35. vector->size = size;
  36. vector->length = 0;
  37. vector->capacity = VECTOR_CAPACITY;
  38.  
  39. tmp = malloc(vector->size * vector->capacity);
  40. if(!tmp) die();
  41. vector->data = tmp;
  42. return vector;
  43. }
  44.  
  45. int vector_len(Vector *vector) {
  46. return vector->length;
  47. }
  48.  
  49. void* vector_getarr(Vector *vector) {
  50. return vector->data;
  51. }
  52.  
  53. void* vector_get(Vector *vector, int index) {
  54. return vector->data + index * vector->size;
  55. }
  56.  
  57. void vector_set(Vector *vector, int index, void *value) {
  58. memcpy(vector->data + index * vector->size, value, vector->size);
  59. }
  60.  
  61. int vector_push(Vector *vector, void *value) {
  62. if (vector->length == vector->capacity) {
  63. unsigned char *tmp;
  64. tmp = realloc(vector->data, vector->size * vector->capacity);
  65. if (!tmp) {
  66. printf("Cannot allocate memory");
  67. exit(1);
  68. };
  69. vector->capacity *= VECTOR_GROWTH_RATE;
  70. vector->data = tmp;
  71. }
  72. vector_set(vector, vector->length++, value);
  73. return 0;
  74. }
  75.  
  76. void* vector_pop(Vector *vector) {
  77. return vector->data + --vector->length * vector->size;
  78. }
  79.  
  80. void vector_free(Vector *vector) {
  81. free(vector->data);
  82. free(vector);
  83. }
  84.  
  85. // Data structures for storing input information
  86.  
  87. Vector *Courses, *Professors, *Teachers, *Students;
  88.  
  89. typedef struct {
  90. char* name;
  91. Vector trained;
  92. Vector courses;
  93. int course_cap;
  94. } Prof;
  95.  
  96. typedef struct {
  97. char* name;
  98. Vector trained;
  99. Vector courses;
  100. int current_labs;
  101. } TA;
  102.  
  103. typedef struct {
  104. char* name;
  105. int lab_max, stud_max;
  106. int lab_cur, stud_cur;
  107. Prof professor;
  108. Vector TAs;
  109. int score;
  110. } Course;
  111.  
  112. typedef struct {
  113. char *name;
  114. char *id;
  115. Vector classes;
  116. } Student;
  117.  
  118. // Utility functions
  119.  
  120. // Initialize the course
  121. void course_init(Course *course, char *name, int lab_max, int stud_max) {
  122. strcpy(course->name, name);
  123. course->lab_cur = 0;
  124. course->stud_cur = 0;
  125. course->lab_max = lab_max;
  126. course->stud_max = stud_max;
  127. course->score = 0;
  128. }
  129.  
  130. void course_free(Course *course) {
  131. free(course->name);
  132. }
  133.  
  134. char *make_filename(int n, char type) {
  135. char *name = calloc(sizeof(char), FILENAME_LENGTH);
  136. int len;
  137. if(type == 'i') {
  138. strcpy(name, "input");
  139. len = 5;
  140. }
  141. else {
  142. strcpy(name, "YaroslavShumichenkoOutput");
  143. len = 25;
  144. }
  145. char *num = calloc(sizeof(char), 3);
  146. itoa(n, num, 10);
  147. strcat(name, num);
  148. len += strlen(num);
  149. strcat(name, ".txt");
  150. len += 4;
  151. name[len] = endl;
  152. return name;
  153. }
  154.  
  155. Vector *merge_strings(Vector *a, Vector *b) {
  156. vector_push(a, &space);
  157. for (int i = 0; i < vector_len(b); i++)
  158. vector_push(a, vector_get(b, i));
  159. vector_free(b);
  160. return a;
  161. }
  162.  
  163. char *vector_to_string(Vector *s) {
  164. if(s == NULL) return NULL;
  165. char *string = malloc((s->length + 1) * sizeof(char));
  166. for(int i = 0; i < s->length; i++) string[i] = *((char *) vector_get(s, i));
  167. string[s->length] = '\0';
  168. vector_free(s);
  169. return string;
  170. }
  171.  
  172. Vector *read_word(FILE *in, int (*isfunc)(int)) {
  173. Vector *s = vector_init(sizeof(char));
  174. int c;
  175. if (c = fgetc(in), !isfunc(c))
  176. return NULL;
  177. vector_push(s, &c);
  178. while (c = fgetc(in), isfunc(c))
  179. vector_push(s, &c);
  180. vector_push(s, &endl);
  181. ungetc(c, in);
  182. return s;
  183. }
  184.  
  185. int read_char(FILE *in, int c) {
  186. if (fgetc(in) == c)
  187. return 0;
  188. return 1;
  189. }
  190.  
  191. int read_int(FILE *in, int *out) {
  192. Vector *s = read_word(in, isdigit);
  193. char *t = vector_to_string(s);
  194. if (s == NULL) {
  195. vector_free(s);
  196. return 1;
  197. }
  198. *out = atoi(t);
  199. vector_free(s);
  200. return 0;
  201. }
  202.  
  203. Course *scan_course(FILE *in) {
  204. Course *course = NULL, c;
  205. Vector *name;
  206. while(1) {
  207. int labs, stud;
  208. if (!(name = read_word(in, isalpha)))
  209. return NULL;
  210. char *name_s = vector_to_string(name);
  211. if (strcmp(name_s, "P") == 0) {
  212. if (read_char(in, '\n')) {
  213. vector_free(name);
  214. return NULL;
  215. }
  216. vector_free(name);
  217. return course;
  218. }
  219. if (read_char(in, ' ')
  220. || read_int (in, &labs)
  221. || read_char(in, ' ')
  222. || read_int (in, &stud)
  223. || read_char(in, '\n')) {
  224. vector_free(name);
  225. return NULL;
  226. }
  227. for (int i = 0; i < vector_len(Courses); i++) {
  228. Course *t = vector_get(Courses, i);
  229. if (strcmp(t->name, name_s) == 0) {
  230. vector_free(name);
  231. return NULL;
  232. }
  233. }
  234. course_init(&c, name_s, labs, stud);
  235. vector_push(Courses, &c);
  236. }
  237. }
  238.  
  239. Prof *scan_prof(FILE *in) {
  240. Prof *prof = NULL, p;
  241. Vector *name, *surname;
  242. char *name_s, *surname_s;
  243. for (;;) {
  244. int c;
  245. if (!(name = read_word(in, isalpha)))
  246. return NULL;
  247. name_s = vector_to_string(name);
  248. if (strcmp(name_s, "T") == 0) {
  249. if (read_char(in, '\n')) {
  250. vector_free(name);
  251. return NULL;
  252. }
  253. vector_free(name);
  254. return prof;
  255. }
  256.  
  257. if (read_char(in, ' ')
  258. || (!(surname = read_word(in, isalpha)))) {
  259. vector_free(name);
  260. return NULL;
  261. }
  262.  
  263. name = merge_strings(name, surname);
  264. name_s = vector_to_string(name);
  265.  
  266. for (int i = 0; i < vector_len(Professors); i++) {
  267. Prof *t = vector_get(Professors, i);
  268. if (strcmp(t->name, name_s) == 0) {
  269. vector_free(name);
  270. return NULL;
  271. }
  272. }
  273.  
  274. p.name = name_s;
  275. p.course_cap = 2;
  276. p.trained = *vector_init(sizeof(Course));
  277. p.courses = *vector_init(sizeof(Course));
  278.  
  279. // PIZDA
  280.  
  281. while (c = getc(in), c == ' ') {
  282. Course *tmp;
  283. char *s = read_word(in, isalpha);
  284.  
  285. if (!s || !(tmp = subj_lookup(subj, s))) {
  286. vector_push(prof, p);
  287. vector_free(name);
  288. return NULL;
  289. }
  290.  
  291. vector_push(p.trained, tmp);
  292. vector_free(s);
  293. }
  294.  
  295. vector_push(prof, p);
  296.  
  297. if (c != '\n')
  298. goto err0;
  299. }
  300.  
  301. err1: vector_free(fnm);
  302. return NULL;
  303. }
  304.  
  305. TA *scan_teacher(FILE *in) {
  306.  
  307. }
  308.  
  309. Student *scan_student(FILE *in) {
  310.  
  311. }
  312.  
  313. typedef struct {
  314. char *filename;
  315. int number;
  316. int dne;
  317. int is_invalid;
  318. } Output_file;
  319.  
  320. void file_init(Output_file *file, char *name, int num, int dne) {
  321. strcpy(file->filename, name);
  322. file->number = num;
  323. file->dne = dne;
  324. }
  325.  
  326. int main() {
  327. Output_file outputs[50];
  328. char *input_names[50];
  329. Courses = vector_init(sizeof(Course));
  330. Professors = vector_init(sizeof(Prof));
  331. Teachers = vector_init(sizeof(TA));
  332. Students = vector_init(sizeof(Student));
  333.  
  334. int last_existing_file = 0;
  335. for(int i = 1; i <= 50; i++) {
  336. input_names[i-1] = make_filename(i, 'i');
  337. FILE *input;
  338. if((input = fopen(input_names[i-1], "r")) != NULL) {
  339. last_existing_file = i;
  340. file_init(&outputs[i-1], make_filename(i, 'o'), i, 0);
  341. fclose(input);
  342. } else file_init(&outputs[i-1], make_filename(i, 'o'), i, 1);
  343. free(input);
  344. }
  345.  
  346. for(int i = 0; i < last_existing_file; i++) {
  347. FILE *input;
  348. if((input = fopen(input_names[i], "r")) != NULL) {
  349. do {
  350.  
  351. } while(!feof(input));
  352.  
  353. fclose(input);
  354. } else {
  355. FILE *out = fopen(outputs[i].filename, "w");
  356.  
  357. }
  358. }
  359. return 0;
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement