Media voti in C


SUBMITTED BY: Guest

DATE: May 5, 2013, 4:45 p.m.

FORMAT: C++

SIZE: 3.3 kB

HITS: 1032

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct
  5. {
  6. char* name;
  7. int vote;
  8. } SUBJECT;
  9. SUBJECT* subjects;
  10. int subjectsn;
  11. char* getFilename(void)
  12. {
  13. static char* r;
  14. int n = 0;
  15. char fn[80];
  16. while (1)
  17. {
  18. sprintf(fn, "report%i.rpt", n);
  19. FILE* f = fopen(fn, "r");
  20. if (!f)
  21. {
  22. sprintf(fn, "report%i", n);
  23. r = (char*) malloc(strlen((char*) &fn));
  24. strcpy(r, (char*) &fn);
  25. return r;
  26. }
  27. n++;
  28. }
  29. }
  30. int main(void)
  31. {
  32. subjectsn = 0;
  33. printf("Calcolatore media pagella\n");
  34. printf("\nInserisci ogni materia e ogni voto, se hai finito scrivi \"ok\" nel nome materia.\n");
  35. while (1)
  36. {
  37. char name[80];
  38. int vote;
  39. printf("\n_____________________________\n\n");
  40. printf("Nome materia: ");
  41. scanf("%s", (char*) &name);
  42. if (strcmp(name, "ok") == 0) break;
  43. printf("Voto: ");
  44. scanf("%i", &vote);
  45. printf("\nAggiungo... ");
  46. subjects = (SUBJECT*) realloc(subjects, (subjectsn + 1) * sizeof(SUBJECT));
  47. subjects[subjectsn].name = (char*) malloc(strlen((char*) &name));
  48. strcpy(subjects[subjectsn].name, (char*) &name);
  49. subjects[subjectsn].vote = vote;
  50. subjectsn++;
  51. printf("OK\n");
  52. }
  53. printf("_____________________________\n");
  54. printf("\nTerminata aggiunta, creo il report...\n");
  55. int sum = 0;
  56. int i; for (i = 0; i < subjectsn; i++)
  57. sum += subjects[i].vote;
  58. float aver;
  59. aver = (float)((float)sum / (float)subjectsn);
  60. printf("\nReport:");
  61. printf("\n_____________________________\n");
  62. for (i = 0; i < subjectsn; i++)
  63. printf("\n%s\n\t%i\n", subjects[i].name, subjects[i].vote);
  64. printf("_____________________________\n");
  65. printf("\nMedia:\n\t%f\n", aver);
  66. printf("_____________________________\n\n");
  67. printf("Salvataggio report... ");
  68. char* of = getFilename();
  69. char* filename = (char*) malloc(strlen(of) + strlen(".rpt"));
  70. strcpy(filename, of);
  71. strcat(filename, ".rpt");
  72. FILE* f = fopen(filename, "w+");
  73. fprintf(f, "Report voti materie e media aritmetica :: Calcolatore media pagella\n\n");
  74. fprintf(f, "\nReport:");
  75. fprintf(f, "\n_____________________________\n\n");
  76. for (i = 0; i < subjectsn; i++)
  77. fprintf(f, "%s\n\t%i\n\n", subjects[i].name, subjects[i].vote);
  78. fprintf(f, "_____________________________\n");
  79. fprintf(f, "\nMedia:\n\t%f\n", aver);
  80. fprintf(f, "_____________________________\n\n");
  81. fclose(f);
  82. printf("OK\n");
  83. printf("Salvataggio file dati... ");
  84. filename = (char*) malloc(strlen(of) + strlen(".dat"));
  85. strcpy(filename, of);
  86. strcat(filename, ".dat");
  87. f = fopen(filename, "wb");
  88. fputc(0xAF, f);
  89. for (i = 0; i < subjectsn; i++)
  90. {
  91. fputc(0xBC, f);
  92. fprintf(f, "%s", subjects[i].name);
  93. fputc(0xCD, f);
  94. fputc(subjects[i].vote, f);
  95. fputc(0xDE, f);
  96. }
  97. fputc(0xEF, f);
  98. printf("OK\n");
  99. printf("_____________________________\n\n");
  100. printf("Grazie per l'utilizzo! by Gi@cky98\n");
  101. printf("_____________________________\n");
  102. }

comments powered by Disqus