Advertisement
mitya_00

Laba4

Nov 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. /////////////////////////////////main.cpp
  2.  
  3. #include <iostream>
  4. #include "Laba4.h"
  5.  
  6. int main() {
  7.    
  8.     student* studentsList = getStudentsFromFile();
  9.     std::cout << "Cout of students is: " << studentsCount;
  10.     std::cout << "\n STUDENTS : \n\n";
  11.     printStudentsInformation(studentsList);
  12.     std::cout << "\n Students, which have avg 4 and more : \n\n";
  13.     calculateAndPrintAvgMarkMore4(studentsList);
  14.     std::cout << "\n The best Disziplin is : \n\n";
  15.     calculateAndPrintTheBestDiszipline(studentsList);
  16.  
  17.     system("pause");
  18.     return 0;
  19. }
  20.  
  21. /////////////////////////////////Laba4.h
  22.  
  23. #pragma once
  24.  
  25. extern char* fileName;
  26. extern int studentsCount;
  27.  
  28. struct student {
  29.     char Name[12];
  30.     int Group,
  31.         Math,
  32.         Phys,
  33.         Prog;
  34. };
  35.  
  36. student* getStudentsFromFile();
  37. void printStudentInformation(student Students);
  38. int countStudentsValue();
  39. void printStudentsInformation(student* TempStudent);
  40. void calculateAndPrintAvgMarkMore4(student* Students);
  41. void calculateAndPrintTheBestDiszipline(student* Students);
  42.  
  43. /////////////////////////////////Laba4.cpp
  44.  
  45. #include "Laba4.h"
  46. #include <iostream>
  47. #include <string.h>
  48. #include <stdio.h>
  49. #include <math.h>
  50.  
  51. #pragma warning(disable : 4996)
  52.  
  53. char* fileName = "Students.txt";
  54. int studentsCount = countStudentsValue();
  55.  
  56. student* getStudentsFromFile() {
  57.     student* studentsList = new student[studentsCount];
  58.     FILE* file = fopen(fileName, "rt");
  59.     if (!file) {
  60.         std::cout << "File not found!\n";
  61.         system("PAUSE");
  62.         exit(0);
  63.     }
  64.     for (int i = 0; i < studentsCount; i++) {
  65.         fscanf(file, "%s%d%d%d%d;",
  66.             studentsList[i].Name,
  67.             &studentsList[i].Group,
  68.             &studentsList[i].Math,
  69.             &studentsList[i].Phys,
  70.             &studentsList[i].Prog);
  71.     }
  72.     fclose(file);
  73.     return studentsList;
  74. }
  75.  
  76. void printStudentsInformation(student* Students) {
  77.     for (int i = 0; i < studentsCount; i++) {
  78.         printStudentInformation(Students[i]);
  79.     }
  80. }
  81.  
  82. void printStudentInformation(student TempStudent) {
  83.     std::cout << TempStudent.Name << " "
  84.         << TempStudent.Group << " "
  85.         << TempStudent.Math << " "
  86.         << TempStudent.Phys << " "
  87.         << TempStudent.Prog << ";\n";
  88. }
  89.  
  90. int countStudentsValue() {
  91.     FILE* file = fopen(fileName, "rt");
  92.     if (!file) {
  93.         std::cout << "File not found!\n";
  94.         system("PAUSE");
  95.         exit(0);
  96.     }
  97.     int studentsCount = 0;
  98.     while (true) {
  99.         char buffer = ' ';
  100.         if (feof(file)) {
  101.             break;
  102.         }
  103.         fread(&buffer, sizeof(char), 1, file);
  104.         if (buffer == ';')  studentsCount++;
  105.     }
  106.     fclose(file);
  107.     return studentsCount;
  108. }
  109.  
  110. void calculateAndPrintAvgMarkMore4(student* Students) {
  111.     for (int i = 0; i < studentsCount; i++) {
  112.         double avg = (Students[i].Math + Students[i].Phys + Students[i].Prog) / 3.0;
  113.         avg = round(avg * 10) / 10.;
  114.         if (avg >= 4.0) {
  115.             std::cout << Students[i].Name << " has " << avg << std::endl;
  116.         }
  117.     }
  118. }
  119.  
  120. void calculateAndPrintTheBestDiszipline(student* Students) {
  121.     int Math = 0;
  122.     int Phys = 0;
  123.     int Prog = 0;
  124.     for (int i = 0; i < studentsCount; i++) {
  125.         Math += Students[i].Math;
  126.         Phys += Students[i].Phys;
  127.         Prog += Students[i].Prog;
  128.     }
  129.     if (Math > Phys) {
  130.         if (Prog > Math) {
  131.             std::cout << "Programm\n";
  132.         }
  133.         else {
  134.             std::cout << "Mathematik\n";
  135.         }
  136.     }
  137.     else if (Prog > Phys) {
  138.         std::cout << "Programm\n";
  139.     }
  140.     else {
  141.         std::cout << "Physik\n";
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement