Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include<stdio.h>
- #include<string.h>
- #include<conio.h>
- #include<math.h>
- /*structions decleration*/
- typedef struct Student
- {
- char *name;
- long id;
- int exam_grade;
- char program;
- char labs[5];
- char hw_grade;
- }Student;
- typedef struct University
- {
- Student *arr;
- int amount;
- }University;
- /*functions decleration*/
- void Error_Msg(char* str);
- void input_university(FILE *input, University* un);
- void output_university(FILE *output, University* un);
- void hw_grades(University* un);
- void final_grade(FILE *output, University* un);
- void uni_stats(FILE *output, University* un);
- void above_avg(FILE *output, University* un);
- void main()
- {
- University Braude;
- FILE *input, *output;
- int option, i;
- Braude.arr = (Student*)malloc(sizeof(Student));
- if (Braude.arr == NULL)
- Error_Msg("Allocation failed");
- /* initializing "input" to point to the txt file we created, as well as checking for faliure*/
- if (!(input = fopen("input.txt", "r")))
- Error_Msg("Error searching for file");
- printf("Enter option (1-6), You must start with option 1: ");
- scanf("%d", &option);
- while (option != 1)
- {
- printf("You must start with option 1, try again: ");
- scanf("%d", &option);
- }
- while (option!=6)
- {
- /* Creating a new empty txt file called "output" /writing over the last one*/
- if (!(output = fopen("output.txt", "w")))
- Error_Msg("Error! unable to create file");
- switch (option)
- {
- case 1:
- input_university(input, &Braude);
- hw_grades(&Braude);
- break;
- case 2:
- output_university(output, &Braude);
- break;
- case 3:
- final_grade(output, &Braude);
- break;
- case 4:
- uni_stats(output, &Braude);
- break;
- case 5:;
- above_avg(output, &Braude);
- break;
- default:
- printf("Error, Wrong input\n ");
- }
- fclose(output);
- printf("What would you like to do next? (2-6): ");
- scanf("%d", &option);
- }
- if (!(output = fopen("output.txt", "w")))
- Error_Msg("Error! unable to create file");
- fprintf(output, "%s", "End Of Program");
- printf("\nGood Bye!\n");
- /*Closing files*/
- fclose(output);
- fclose(input);
- for (i = 0; i < Braude.amount; i++)
- free(Braude.arr[i].name);
- free(Braude.arr);
- getch();
- }
- void Error_Msg(char* str)
- {
- printf("\n%s", str);
- exit(1);
- }
- void input_university(FILE *input, University* un)
- {
- int i = 0;
- char tmp_name[99];
- char tmp_labs[6];
- while (fscanf(input, "%s %ld %d %s %c", tmp_name, &un->arr[i].id, &un->arr[i].exam_grade, tmp_labs, &un->arr[i].program) != EOF)
- {
- un->arr[i].name = (char*)malloc((strlen(tmp_name) + 1)*sizeof(char));
- if (un->arr[i].name == NULL)
- Error_Msg("Allocation failed");
- strcpy(un->arr[i].name, tmp_name);
- strcpy(un->arr[i].labs, tmp_labs);
- i++;
- un->arr = (Student*)realloc(un->arr, (i+1)*sizeof(Student));
- if (un->arr == NULL)
- Error_Msg("Allocation failed");
- }
- un->amount = i;
- }
- void hw_grades(University* un)
- {
- int i,j;
- for (i = 0; i < un->amount; i++)
- {
- int cnt = 0;
- for (j = 0; j < 5; j++)
- if (un->arr[i].labs[j] == '1')
- cnt++;
- if (cnt>2)
- un->arr[i].hw_grade = '1';
- else
- un->arr[i].hw_grade = '0';
- }
- }
- void output_university(FILE *output, University* un)
- {
- int i;
- for (i = 0; i < un->amount; i++)
- {
- fprintf(output,"Student %d: %s ", i + 1, un->arr[i].name);
- fprintf(output, "%ld %d ", un->arr[i].id, un->arr[i].exam_grade);
- fprintf(output, "%c %c\n", un->arr[i].hw_grade, un->arr[i].program);
- }
- }
- void final_grade(FILE *output, University* un)
- {
- int i;
- fprintf(output, "BEFORE:\n");
- output_university(output, un);
- fprintf(output, "\nATFER:\n");
- for (i = 0; i < un->amount; i++)
- {
- if (un->arr[i].exam_grade>54)
- if (un->arr[i].hw_grade == '1')
- if (un->arr[i].program == 'Y')
- un->arr[i].exam_grade = (float)un->arr[i].exam_grade*0.8 + 20;
- else
- un->arr[i].exam_grade = (float)un->arr[i].exam_grade*0.9 + 10;
- }
- output_university(output, un);
- }
- void uni_stats(FILE *output, University* un)
- {
- float avg=0,sd=0;
- int i, min=100, max=0,old=0,new=0;
- /*Average calculation*/
- for (i = 0; i < un->amount; i++)
- avg += un->arr[i].exam_grade;
- avg /= un->amount;
- /*Standard Deviation, Range, program calculation*/
- for (i = 0; i < un->amount; i++)
- {
- sd += pow(((double)un->arr[i].exam_grade - avg), 2);
- if (un->arr[i].exam_grade>max)
- max = un->arr[i].exam_grade;
- if (un->arr[i].exam_grade < min)
- min = un->arr[i].exam_grade;
- if (un->arr[i].program == 'Y')
- new++;
- else
- old++;
- }
- sd /= un->amount;
- sd = sqrt((double)sd);
- fprintf(output, "Average: %g\n", avg);
- fprintf(output, "Standard Deviation: %g\n", sd);
- fprintf(output, "Student amount: %d\n", un->amount);
- fprintf(output, "Range: %d - %d\n", max,min);
- fprintf(output, "New program: %d\n", new);
- fprintf(output, "Old program: %d", old);
- }
- void above_avg(FILE *output, University* un)
- {
- char prog;
- int i;
- float avg=0;
- /*Average calculation*/
- for (i = 0; i < un->amount; i++)
- avg += un->arr[i].exam_grade;
- avg /= un->amount;
- /*Program Choise*/
- printf("Which program would you like to view? (Y/N): ");
- flushall();
- scanf("%c", &prog);
- /*Input Check*/
- while (prog != 'Y' &&prog != 'N')
- {
- printf("Error, please enter \'Y\' OR \'N\':");
- flushall();
- scanf("%c", &prog);
- }
- for (i = 0; i < un->amount; i++)
- {
- if (un->arr[i].program == prog)
- {
- if (un->arr[i].exam_grade>avg)
- {
- fprintf(output, "Student %d: %s ", i + 1, un->arr[i].name);
- fprintf(output, "%ld %d ", un->arr[i].id, un->arr[i].exam_grade);
- fprintf(output, "%c %c\n", un->arr[i].hw_grade, un->arr[i].program);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement