Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define STR_SIZE 30
- #define STRING_SIZE 130
- #define MIN_COUNT 40
- #define MAX_COUNT 100
- #define ERROR_REGISTR 1
- #define ERROR_SYMBOL 2
- #define SUCCESS 0
- /*Фамилия Имя Отчество Факультет Форма обучения(бюджетная(проживание в общежитии, размер стипендии)
- * /платная(стоимость обучения в год в рублях, средний балл за последнюю сессию))
- */
- #define STR_SIZE 30
- typedef struct
- {
- char dormitory_accommodation[4]; // Проживает в общежитии (Да / Нет)
- char scholarships[4]; // Получает стипендию (Да / Нет)
- }budgetary_form;
- typedef struct
- {
- float average_mark;
- }paid_form;
- typedef union
- {
- budgetary_form budgetary;
- paid_form paid;
- }forms;
- typedef struct
- {
- char surname[STR_SIZE]; // Фамилия
- char name[STR_SIZE]; // Имя
- char patronymic[STR_SIZE]; // Отчество
- char faculty[STR_SIZE]; // факультет
- char form[STR_SIZE];
- forms form_of_education; // форма обучения
- }students;
- int fill_str(char *p_string, char *string, int first_is_upper)
- {
- int j = 0; int k = 0;
- for(j = 0; string[j] != ' ' && string[j] != '\n'; j++)
- {
- if (j == 0 && first_is_upper)
- {
- if (string[j] >= 'A' && string[j] <= 'Z')
- {
- p_string[j] = string[j];
- continue;
- }
- if (string[j] >= 'a' && string[j] <= 'z')
- {
- printf("Not correct registr of char.");
- return ERROR_REGISTR;
- }
- else
- {
- printf("Not correct symbol of char.");
- return ERROR_SYMBOL;
- }
- }
- if ((j != 0 || !first_is_upper) && (string[j] >= 'a' && string[j] <= 'z'))
- {
- p_string[j] = string[j];
- }
- else
- {
- printf("Not correct symbol of char.");
- return ERROR_SYMBOL;
- }
- }
- p_string[j] = '\0';
- j += 1;
- for (k = 0; k < strlen(string) - j; k++)
- {
- string[k] = string[k + j];
- }
- return SUCCESS;
- }
- int fill_faculty(char* p_string, char* string)
- {
- int j = 0; int k = 0;
- for(j = 0; string[j] != ' ' && string[j] != '\n'; j++)
- {
- if (string[j] >= 'A' && string[j] <= 'Z')
- {
- p_string[j] = string[j];
- }
- else
- {
- printf("Not correct symbol\n");
- return ERROR_SYMBOL;
- }
- }
- p_string[j] = '\0';
- j += 1;
- for (k = 0; k < strlen(string) - j; k++)
- {
- string[k] = string[k + j];
- }
- return SUCCESS;
- }
- int fill_mark(float* mark, char* string)
- {
- *mark = 0;
- if (string[0] >= '0' && string[0] <= '5' && string[1] == '.' && string[2] >= '0'
- && string[2] <= '9' && string[3] == '\n')
- *mark += (string[0] - '0') * 1.0 + (string[2] - '0') * 0.1;
- else
- {
- printf("Averange mark is in wrong format\n");
- return ERROR_SYMBOL;
- }
- return SUCCESS;
- }
- int string_parse(char* string, students* table)
- {
- int temp;
- temp = fill_str(table->surname, string, 1);
- if (temp != SUCCESS)
- return temp;
- temp = fill_str(table->name, string, 1);
- if (temp != SUCCESS)
- return temp;
- temp = fill_str(table->patronymic, string, 1);
- if (temp != SUCCESS)
- return temp;
- temp = fill_faculty(table->faculty, string);
- if (temp != SUCCESS)
- return temp;
- temp =fill_str(table->form, string, 0);
- if (temp != SUCCESS)
- return temp;
- if (strcmp(table->form, "paid") == SUCCESS)
- {
- fill_mark(&table->form_of_education.paid.average_mark, string);
- }
- else if (strcmp(table->form, "budgetary") == SUCCESS)
- {
- fill_str(table->form_of_education.budgetary.dormitory_accommodation, string, 1);
- fill_str(table->form_of_education.budgetary.scholarships, string, 1);
- }
- else
- {
- printf("Not correct form of education");
- return ERROR_SYMBOL;
- }
- return SUCCESS;
- }
- int filling_table_from_file(students* table) // Считывание в таблицу данных из файла
- {
- int count = 0;
- FILE *f = fopen("table_student.txt", "rt");
- rewind(f);
- while(1)
- {
- if (feof(f))
- {
- break;
- }
- char string[STRING_SIZE] = "";
- fgets(string, 250, f);
- if(strcmp("end\n", string) != SUCCESS)
- {
- int rc = string_parse(string, &table[count]);
- if (rc != SUCCESS)
- {
- fclose(f);
- return rc;
- }
- }
- count++;
- }
- fclose(f);
- return SUCCESS;
- }
- void print_tab(students* table, const int temp_table_size)
- {
- printf("\nTOTAL: %d\n\n", temp_table_size);
- printf("┌-------------------------------------------------------------------------------------------------------------------------------------------------┐\n");
- printf("| № | Surname | Name | Patronymic | Faculty | Form | Dormitory accommodation | Scholarships | Averange mark |\n");
- printf("|----|--------------------|---------------|---------------------|-----------|------------|-------------------------|--------------|---------------|\n");
- for (int i = 0; i < temp_table_size; i++)
- {
- printf("| %d", i + 1);
- if (i < 9)
- printf(" |");
- if (i >= 9 && i < 99)
- printf(" |");
- if (i == 99)
- printf("|");
- printf(" %s", table[i].surname);
- for(int j = 0; j < 19 - strlen(table[i].surname); j++)
- {
- printf(" ");
- }
- printf("|");
- printf(" %s", table[i].name);
- for(int j = 0; j < 14 - strlen(table[i].name); j++)
- {
- printf(" ");
- }
- printf("|");
- printf(" %s", table[i].patronymic);
- for(int j = 0; j < 20 - strlen(table[i].patronymic); j++)
- {
- printf(" ");
- }
- printf("|");
- printf(" %s", table[i].faculty);
- for(int j = 0; j < 10 - strlen(table[i].faculty); j++)
- {
- printf(" ");
- }
- printf("|");
- printf(" %s", table[i].form);
- for(int j = 0; j < 11 - strlen(table[i].form); j++)
- {
- printf(" ");
- }
- printf("|");
- if (strcmp(table[i].form, "paid") == SUCCESS)
- {
- for (int j = 0; j < 25; j++)
- printf(" ");
- printf("|");
- for (int j = 0; j < 14; j++)
- printf(" ");
- printf("| ");
- printf("%.1f", table[i].form_of_education.paid.average_mark);
- for(int j = 0; j < 11; j++)
- printf(" ");
- printf("|\n");
- }
- else
- {
- printf( " %s", table[i].form_of_education.budgetary.dormitory_accommodation);
- for (int j = 0; j < 24 - strlen(table[i].form_of_education.budgetary.dormitory_accommodation); j++)
- printf(" ");
- printf("|");
- printf( " %s", table[i].form_of_education.budgetary.scholarships);
- for (int j = 0; j < 13 - strlen(table[i].form_of_education.budgetary.scholarships); j++)
- printf(" ");
- printf("|");
- for (int j = 0; j < 15; j++)
- printf(" ");
- printf("|\n");
- }
- }
- printf("└-------------------------------------------------------------------------------------------------------------------------------------------------┘\n");
- }
- int add_new_student()
- int main(void)
- {
- students *table_students = (students*)malloc(MIN_COUNT * sizeof(students));
- //*copy_table = (students*)malloc(MIN_COUNT * sizeof(students));
- printf("This program works with table, containing information about from 40 to 100 students of BMSTU. "
- "It has information about its: surname, name, patronymic, faculty"
- "\nif the student is on budgetary form of education, here is information about dormitory accommodation(YES/NO), scholarships(YES/NO)\n"
- "if the staudent is on paid form of education, here is information about average mark\n"
- "You can do several things with the table entering the special key of the task.\n");
- int temp_table_size = MIN_COUNT;
- filling_table_from_file(table_students);
- free(table_students);
- for (; ;)
- {
- printf("LIST OF TASKS\n"
- "enter 1 to show the table\n"
- "press 2 to add new student\n"
- "press 3 to delete the special student\n"
- "enter 4 to show sorted key table\n"
- "press 5 to show sorted table\n"
- "enter 6 to show sorted by the key table (insert sort)\n"
- "enter 7 to show sorted by the key table (bubble sort)\n"
- "enter 8 to show efficiency of different sorts\n"
- "enter 9 to find student on the paid form with spaecial average mark\n"
- "enter 10 to close the program\n\n");
- int s;
- scanf("%d", &s);
- switch (s)
- {
- case 1:
- print_tab(table_students, temp_table_size);
- }
- break;
- }
- free(table_students);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement