Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////main.cpp
- #include <iostream>
- #include "Laba4.h"
- int main() {
- student* studentsList = getStudentsFromFile();
- std::cout << "Cout of students is: " << studentsCount;
- std::cout << "\n STUDENTS : \n\n";
- printStudentsInformation(studentsList);
- std::cout << "\n Students, which have avg 4 and more : \n\n";
- calculateAndPrintAvgMarkMore4(studentsList);
- std::cout << "\n The best Disziplin is : \n\n";
- calculateAndPrintTheBestDiszipline(studentsList);
- system("pause");
- return 0;
- }
- /////////////////////////////////Laba4.h
- #pragma once
- extern char* fileName;
- extern int studentsCount;
- struct student {
- char Name[12];
- int Group,
- Math,
- Phys,
- Prog;
- };
- student* getStudentsFromFile();
- void printStudentInformation(student Students);
- int countStudentsValue();
- void printStudentsInformation(student* TempStudent);
- void calculateAndPrintAvgMarkMore4(student* Students);
- void calculateAndPrintTheBestDiszipline(student* Students);
- /////////////////////////////////Laba4.cpp
- #include "Laba4.h"
- #include <iostream>
- #include <string.h>
- #include <stdio.h>
- #include <math.h>
- #pragma warning(disable : 4996)
- char* fileName = "Students.txt";
- int studentsCount = countStudentsValue();
- student* getStudentsFromFile() {
- student* studentsList = new student[studentsCount];
- FILE* file = fopen(fileName, "rt");
- if (!file) {
- std::cout << "File not found!\n";
- system("PAUSE");
- exit(0);
- }
- for (int i = 0; i < studentsCount; i++) {
- fscanf(file, "%s%d%d%d%d;",
- studentsList[i].Name,
- &studentsList[i].Group,
- &studentsList[i].Math,
- &studentsList[i].Phys,
- &studentsList[i].Prog);
- }
- fclose(file);
- return studentsList;
- }
- void printStudentsInformation(student* Students) {
- for (int i = 0; i < studentsCount; i++) {
- printStudentInformation(Students[i]);
- }
- }
- void printStudentInformation(student TempStudent) {
- std::cout << TempStudent.Name << " "
- << TempStudent.Group << " "
- << TempStudent.Math << " "
- << TempStudent.Phys << " "
- << TempStudent.Prog << ";\n";
- }
- int countStudentsValue() {
- FILE* file = fopen(fileName, "rt");
- if (!file) {
- std::cout << "File not found!\n";
- system("PAUSE");
- exit(0);
- }
- int studentsCount = 0;
- while (true) {
- char buffer = ' ';
- if (feof(file)) {
- break;
- }
- fread(&buffer, sizeof(char), 1, file);
- if (buffer == ';') studentsCount++;
- }
- fclose(file);
- return studentsCount;
- }
- void calculateAndPrintAvgMarkMore4(student* Students) {
- for (int i = 0; i < studentsCount; i++) {
- double avg = (Students[i].Math + Students[i].Phys + Students[i].Prog) / 3.0;
- avg = round(avg * 10) / 10.;
- if (avg >= 4.0) {
- std::cout << Students[i].Name << " has " << avg << std::endl;
- }
- }
- }
- void calculateAndPrintTheBestDiszipline(student* Students) {
- int Math = 0;
- int Phys = 0;
- int Prog = 0;
- for (int i = 0; i < studentsCount; i++) {
- Math += Students[i].Math;
- Phys += Students[i].Phys;
- Prog += Students[i].Prog;
- }
- if (Math > Phys) {
- if (Prog > Math) {
- std::cout << "Programm\n";
- }
- else {
- std::cout << "Mathematik\n";
- }
- }
- else if (Prog > Phys) {
- std::cout << "Programm\n";
- }
- else {
- std::cout << "Physik\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement