Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- #include <stdint.h>
- #include <ctype.h>
- #define MAX 50 //maximum input files
- #define list(Type, Name)\
- typedef struct Name{\
- Type data;\
- int size;\
- struct Name *next;\
- struct Name *last;\
- }Name;\
- Name* Name##_new(){\
- Name* node = (Name*)malloc(sizeof(Name));\
- node->size = 0;\
- node->last=node;\
- node->next = NULL;\
- return node;\
- }\
- void Name##_add(Type t, Name* head){\
- Name* new_node = Name##_new();\
- ++(head->size);\
- head->last->data = t;\
- head->last->next = new_node;\
- head->last=new_node;\
- }
- list(char*, list_char); //inicialization of the string list
- unsigned int overall_point;
- typedef struct {
- char ID[6];
- char name_student[100];
- list_char *chr;
- list_char *not_taken_classes;
- } student;
- student *student_new() {//constructor of the student
- student *st = (student *) malloc(sizeof(student));
- st->chr = list_char_new();//init the list to store classes of student
- st->not_taken_classes = list_char_new();
- return st;
- }
- typedef struct {
- char name_TA[100];
- list_char *chr;
- int8_t runned_classes;
- } TA;
- int chars_read = 0;
- TA *TA_new() {//constructor of the TA
- TA *ta = (TA *) malloc(sizeof(TA));
- ta->chr = list_char_new();//init the list to store classes of TA
- ta->runned_classes = 4;
- return ta;
- }
- typedef struct {
- char name_professor[100];
- list_char *chr;
- int8_t number; //number of running classes
- } professor;
- professor *professor_new() {//constructor of the professor
- professor *prof = (professor *) malloc(sizeof(professor));
- prof->chr = list_char_new();//init the list to store classes of professor
- prof->number = 2;
- return prof;
- }
- list(professor, list_professor);//inicialization of the professor list
- list(TA, list_TA);//inicialization of the TA list
- list(student, list_student);//inicialization of the student list
- typedef struct {
- char name_class[100];
- unsigned int num_students; //at least one
- unsigned int labs_required; //at leaыt one
- professor run_prof;
- list_TA* run_ta;
- list_student* run_st;
- } class;
- class *class_new(){
- class *new = (class *)malloc(sizeof(class));
- new->run_ta = list_TA_new();
- new->run_st = list_student_new();
- }
- list(class, list_class); //inicialization of the classes list
- typedef struct result{
- list_TA *ta;
- list_class *cl;
- list_student *stu;
- list_professor *prof;
- } result;
- typedef struct{
- char name_class[100];
- unsigned int point;
- professor prof;
- list_TA *ta;
- list_student *st;
- }res_course;
- int valid(char str[]) {//check the validity af inputed number
- if(str == NULL) {//if the string is null error
- return 0;
- }
- int i = 0;
- if(str[0] == '0') {//insighnificant zeros
- return 0;
- }
- for(; i < strlen(str); i++) {//check every symbol of the string
- if(!isdigit(str[i])) {//if it is not digit output error
- return 0;
- }
- }
- return 1;
- }
- result *find(result *table){
- char c='0';
- // printf("\n%d",table->cl->size);
- res_course result[table->cl->size];
- res_course res[table->cl->size];
- list_class *class = table->cl;
- list_TA *ta = table->ta;
- TA new_ta[table->cl->size][table->ta->size];
- list_char *st_cl = table->stu->data.chr;
- list_student *st = table->stu;
- int s = 0;
- unsigned int mask = 1ull << (table->cl->size);
- for (int i = 0; i<mask;++i) {
- printf("\n0\n");
- class = table->cl;
- for(int j = 0; j<table->cl->size;++j){
- ta = table->ta;
- printf("1");
- if ((i>>j)&1){
- printf("\n in \n");
- while(ta->next != NULL) {
- list_char *ta_cl = ta->data.chr;
- while(ta_cl->next != NULL){
- if(strcmp(ta_cl->data, class->data.name_class)==0){
- c='1';
- new_ta[j][s] = ta->data;
- printf("%s", new_ta[j][s].name_TA);
- ++s;
- break;
- }
- ta_cl = ta_cl->next;
- }
- ta = ta->next;
- }
- if (c!='1'){
- strcpy(res[i].name_class,class->data.name_class);
- res[i].point = 20;
- printf("\n%d ",res[i].point);
- if (class->next == NULL){break;}
- class = class->next;
- }
- }
- else{
- strcpy(res[i].name_class,class->data.name_class);
- res[i].point = 20;
- printf("\n%d ",res[i].point);
- if (class->next == NULL){break;}
- class = class->next;
- }
- }
- }
- return table;
- }
- void write(int i) {//function to process the input
- char inp[11];//to store path to input file
- char out[19];//to store path to output files
- //variables to store parts of the input
- char str1[100];
- char str2[100];
- char str3[10];
- char c; //to store char get from input and return it back in some cases
- FILE *input;
- FILE *output;
- sprintf(inp, "input%d.txt", i);//write in inp formated string
- sprintf(out, "RavidaSaitovaOutput%d.txt", i);//write in out formated string
- input = fopen("input1.txt", "r"); //open file for reading only
- output = fopen(out, "w"); //open file for writing
- if(input == NULL) { //writes to output if there is no such file in such derictory
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- }
- list_class *listc = list_class_new();//init new list to store inputed classes
- while(true) {
- class new_class = *class_new();//init new class
- chars_read = fscanf(input, "%s", str1);//scan class name
- strcpy(new_class.name_class, str1);//copy to the name field of the new class
- if(strcmp(str1, "P") == 0) { break; }//read file until P
- c = fgetc(input);//get next symbol(first space)
- if(c == '\n') {//if there is new line
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- }
- c = fgetc(input);//get the symbol after space
- if(c == ' ' || c == '\n') {//if double spacing or new line
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- } else {
- ungetc(c, input);//return symbol to the input stream
- }
- printf("%s", str1);
- chars_read = fscanf(input, "%s", str2);//reads the number of required labs
- if(!valid(str2)) { fprintf(output, "Invalid input.%d", __LINE__); } //check the input on correctness
- new_class.labs_required = (int) strtol(str2, NULL, 10);//assign to the new class' field
- printf("%d", new_class.labs_required);
- c = fgetc(input);//get next symbol (first space)
- if(c == '\n') {//if there is new line
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- }
- c = fgetc(input);//get the symbol after space
- if(c == ' ' || c == '\n') {
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- } else {
- ungetc(c, input);//return symbol to the input stream
- }
- chars_read = fscanf(input, "%s", str3);//reads the maximum number of students
- if(!valid(str3)) { fprintf(output, "Invalid input.%d", __LINE__); } //check the input on correctness
- new_class.num_students = (int) strtol(str3, NULL, 10);//assign to the new class' field
- printf("%d\n", new_class.num_students);
- fgets(str1, 100, input); //reads remaining string on the first line
- if(strlen(str1) > 1) {//if there is more than one symbol('\n')
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- }
- list_class_add(new_class, listc);//push new instance to the list
- }
- list_professor *listp = list_professor_new();//list of professors
- while(true) {
- chars_read = fscanf(input, "%s", str1);//scan professors name
- if(strcmp(str1, "T") == 0) { break; }
- professor new_professor = *professor_new();//init new professor
- c = fgetc(input);//get next symbol (first space)
- if(c == '\n') {//if there is new line
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- }
- c = fgetc(input);//get the symbol after space
- if(c == ' ' || c == '\n') {
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- } else {
- ungetc(c, input);//return symbol to the input stream
- }
- chars_read = fscanf(input, "%s", str2);//scan professors surname
- sprintf(str1, "%s %s", str1, str2);//formate string(combine name and surname)
- strcpy(new_professor.name_professor, str1);//assign name to professor
- c = fgetc(input);//get next symbol (first space)
- if(c == '\n') {//if there is new line
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- }
- c = fgetc(input);//get the symbol after space
- if(c == ' ' || c == '\n') {
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- } else {
- ungetc(c, input);//return symbol to the input stream
- }
- printf("\n%s", new_professor.name_professor);
- while(1) {
- char *cls = calloc(100, sizeof(char));
- chars_read = fscanf(input, "%s", cls);//input class
- list_char_add(cls, new_professor.chr);//add it to professor's class list
- c = fgetc(input);//get next symbol (first space)
- if (c=='\n'){ break;}
- c = fgetc(input);//get the symbol after space
- if(c == ' ' || c == '\n') {
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- } else {
- ungetc(c, input);//return symbol to the input stream
- }
- list_char *current = new_professor.chr;//delete it !!!
- while(current->next != NULL) {
- printf("%s ", current->data);
- current = current->next;
- }
- }
- list_professor_add(new_professor, listp);//push new instance to the list
- }
- list_TA *listt = list_TA_new();//list for TA's
- while(true) {
- chars_read = fscanf(input, "%s", str1);//read name
- if(strcmp(str1, "S") == 0) { break; }
- TA new_TA = *TA_new();//init new TA
- c = fgetc(input);//get next symbol (first space)
- if(c == '\n') {//if there is new line
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- }
- c = fgetc(input);//get the symbol after space
- if(c == ' ' || c == '\n') {
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- } else {
- ungetc(c, input);//return symbol to the input stream
- }
- chars_read = fscanf(input, "%s", str2);//read surname
- sprintf(str1, "%s %s", str1, str2);//combine name and surname
- strcpy(new_TA.name_TA, str1);//update the name of new TA
- c = fgetc(input);//get next symbol (first space)
- if(c == '\n') {//if there is new line
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- }
- c = fgetc(input);//get the symbol after space
- if(c == ' ' || c == '\n') {
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- } else {
- ungetc(c, input);//return symbol to the input stream
- }
- printf("\n%s", new_TA.name_TA);//update name of TA
- while(1) {
- char *cls = calloc(100, sizeof(char));
- chars_read = fscanf(input, "%s", cls);
- list_char_add(cls, new_TA.chr);
- c = fgetc(input);//get next symbol (first space)
- if (c=='\n'){ break;}
- c = fgetc(input);//get the symbol after space
- if(c == ' ' || c == '\n') {
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- } else {
- ungetc(c, input);//return symbol to the input stream
- }
- list_char *current = new_TA.chr;//delete it!!!
- while(current->next != NULL) {
- printf("%s ", current->data);
- current = current->next;
- }
- }
- list_TA_add(new_TA, listt);//push new instance to the list
- }
- list_student *lists = list_student_new();//list for students
- while(true) {
- chars_read = fscanf(input, "%s", str1);//scan name
- if(chars_read <= 0) { break; }
- student new_student = *student_new();//init new student
- c = fgetc(input);//get next symbol (first space)
- if(c == '\n') {//if there is new line
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- }
- c = fgetc(input);//get the symbol after space
- if(c == ' ' || c == '\n') {
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- } else {
- ungetc(c, input);//return symbol to the input stream
- }
- chars_read = fscanf(input, "%s", str2);//scan surname
- sprintf(str1, "%s %s", str1, str2);//format by combining
- strcpy(new_student.name_student, str1);
- printf("\n%s ",new_student.name_student);
- c = fgetc(input);//get next symbol (first space)
- if(c == '\n') {//if there is new line
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- }
- c = fgetc(input);//get the symbol after space
- if(c == ' ' || c == '\n') {
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- } else {
- ungetc(c, input);//return symbol to the input stream
- }
- chars_read = fscanf(input, "%s", str3);//scan ID
- if(strlen(str3) != 5) {
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- }
- list_student *current = list_student_new();//list for students
- current = lists;//delete it!!!
- while(current->next != NULL) {
- if(strcmp(str3, current->data.ID) == 0) {
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- }
- current = current->next;
- }
- strcpy(new_student.ID, str3);
- printf("%s ", new_student.ID);
- while(1) {
- char *cls = calloc(100, sizeof(char));
- chars_read = fscanf(input, "%s", cls);
- if(chars_read <= 0) break;
- list_char_add(cls, new_student.chr);
- c = fgetc(input);//get first space
- if (c=='\n'){ break;}
- c = fgetc(input);//get the symbol after space
- if(c == ' ' || c == '\n') {
- fprintf(output, "Invalid input.%d", __LINE__);
- return;
- } else {
- ungetc(c, input);//return symbol to the input stream
- }
- }
- list_char *current1 = new_student.chr;
- while(current1->next != NULL) {
- printf("%s ", current1->data);
- current1 = current1->next;
- }
- list_student_add(new_student, lists);//push new instance to the list
- }
- result *table = (result*)malloc(sizeof(result));
- table->cl=listc;
- table->ta=listt;
- table->stu=lists;
- table->prof=listp;
- result *finish = find(table);
- fclose(input);
- fclose(output);
- }
- /*
- * Check if a file exist using fopen() function
- * return 1 if the file exist otherwise return 0
- */
- int cfileexists(int i) {
- /* try to open file to read */
- char inp[10];
- sprintf(inp, "input%d.txt", i);
- FILE *file;
- if(file = fopen(inp, "r")) {
- fclose(file);
- return 1;
- }
- return 0;
- }
- int main() {
- int max;
- for(int i = 1; i <= MAX; ++i) {
- if(cfileexists(i)) { max = i; }
- }
- for(int i = 1; i <= max; ++i) {
- write(i);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement