Guest User

Untitled

a guest
Nov 2nd, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <math.h>
  4.  
  5. typedef struct {
  6.     int id;
  7.     char name[100];
  8.     char document[14];
  9.     float testGrade;
  10. } Candidate;
  11.  
  12. typedef struct {
  13.     int id;
  14.     float testGrade;
  15. } Grade;
  16.  
  17. FILE * openFile(char filename[100], char filemode[3]) {
  18.     FILE *p = fopen(filename, filemode);
  19.  
  20.     if (!p) {
  21.         printf("Error to open %s file. \nThe program will be closed.",filename);
  22.         exit(1);
  23.     }
  24. }
  25.  
  26.  
  27. void addCandidate(int newId, int numbersOfNewCandidate, char candidateFilename[100], char gradeFilename[100]) {
  28.     int counter = 0;
  29.     float testGrade = 0;
  30.  
  31.     Candidate*candidate;
  32.     candidate= malloc(sizeof(Candidate) * numbersOfNewCandidate);
  33.  
  34.     Grade *grade;
  35.     grade= malloc(sizeof(Grade) * numbersOfNewCandidate);
  36.  
  37.     for(;counter< numbersOfNewCandidate; counter++) {
  38.         system("@cls||clear");
  39.         printf("Adding  #%d:",newId);
  40.         printf("\n---------------\n");
  41.  
  42.         printf("\nName of candidate: ");
  43.         gets(&candidate[counter].name);
  44.  
  45.         printf("\nDocument: ");
  46.         gets(&candidate[counter].document);
  47.  
  48.         do {
  49.             printf("\nTest grade (between 0 and 10): ");
  50.             scanf("%f",&testGrade);
  51.             printf("\n---------------\n");
  52.  
  53.             if (testGrade < 0 || testGrade > 10) {
  54.                 printf("\nERROR!\nTest grade %.2f invalid, please try again with a grade between 0 and 10.\n",testGrade);
  55.             }
  56.  
  57.         } while(testGrade < 0 || testGrade > 10);
  58.  
  59.         candidate[counter].id = newId;
  60.         grade[counter].id = newId;
  61.         candidate[counter].testGrade = testGrade;
  62.         grade[counter].testGrate = testGrade;
  63.         newId++;
  64.         fflush(stdin);
  65.     }
  66.  
  67.     FILE *candidateFile = openFile(candidateFilename, "a+b");
  68.     fwrite(candidate, sizeof(candidate),numbersOfNewCandidate, candidateFile );
  69.  
  70.     FILE *gradeFile = openFile(gradeFilename, "a+b");
  71.     fwrite(grade, sizeof(grade),numbersOfNewCandidate, gradeFile );
  72.  
  73.     fclose(candidateFile);
  74.     fclose(gradeFile);
  75.  
  76.     free(candidate);
  77.     free(grade);
  78. }
  79.  
  80. void showCandidate(int typeOfSearch, char valueToSearch[100]) {}
  81.  
  82. void listAllCandidates(char candid[100]) {
  83.     FILE *fp = openFile(candidateFilename, "rb");
  84.  
  85.     //fseek(fp,0,SEEK_SET);
  86.  
  87.     Candidate *candidate = NULL;
  88.  
  89.     candidate = malloc(sizeof(Candidate) + 1);
  90.  
  91.     while(fread(&candidate,sizeof(Candidate),1,fp) == 1) {
  92.         printf("\n\nId: %d \nName: %s \nDocument: %s \nGrade: %.2f",candidate->id,candidate->name,candidate->document, candidate->testGrade);
  93.     }
  94.     getche();
  95.     free(candidate);
  96. }
  97.  
  98. void main(){
  99.     int lastId = 0;
  100.     char candidateFilename[100] = "candidates.bin";
  101.     char gradeFilename[100] = "classificationList.bin";
  102.     char option;
  103.  
  104.     do {
  105.         system("@cls||clear");
  106.         printf("Menu: \n");
  107.         printf("1 - Add candidates \n");
  108.         // printf("2 - Search by name \n");
  109.         // printf("3 - Search by document  \n");
  110.         // printf("---------------------------\n");
  111.         // printf("4 - Show Max Grade, Minimum, Avg \n");
  112.         printf("5 - List candidates \n");
  113.         printf("6 - Erase files \n");
  114.         printf("---------------------------\n");
  115.         printf("S - Exit \n");
  116.         printf("\n\n");
  117.  
  118.         option = toupper(getche());
  119.  
  120.         switch(option) {
  121.             case '1':
  122.                 system("@cls||clear");
  123.  
  124.                 int numbersOfNewCandidate = 0;
  125.                 int newId = 0;
  126.  
  127.                 printf("Home > Add candidates\n\n");
  128.                 printf("Please give the number of new candidates: ");
  129.                 scanf("%d",&numbersOfNewCandidate);
  130.                 newId = lastId;
  131.                 lastId += numbersOfNewCandidate;
  132.  
  133.                 fflush(stdin);
  134.                 addCandidate(newId + 1, numbersOfNewCandidate, candidateFilename, gradeFilename);
  135.  
  136.                 printf("\n\nAdding new candidates: Finished \n");
  137.             break;
  138.             // case '2':
  139.             //     printf("\noption %c@\n",option);
  140.             // break;
  141.             // case '3':
  142.             //     printf("\noption %c#\n",option);
  143.             // break;
  144.             // case '4':
  145.             //     printf("\noption %c?\n",option);
  146.             // break;
  147.             case '5':
  148.                 listAllCandidates(candidateFilename);
  149.             break;
  150.             case '6':
  151.                 remove(candidateFilename);
  152.                 remove(gradeFilename);
  153.                 printf("\nRemoved!!\n");
  154.             break;
  155.             case 'S':
  156.                 printf("\noption %c, the program will be ended...\n",option);
  157.             break;
  158.             default:
  159.                 printf("\nWrong option!!\n");
  160.             break;
  161.         }
  162.     } while (option != 'S');
  163. }
Add Comment
Please, Sign In to add comment