Advertisement
codegod313

Ksenia_Voronova

Jan 11th, 2021
1,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <Windows.h>
  5.  
  6. using namespace std;
  7.  
  8. struct Student {
  9.     string name{};
  10.     string surname{};
  11.     string patronymic{};
  12.     int number_group{};
  13.     int birthYear{};
  14.     int physics{};
  15.     int math{};
  16.     int informatics{};
  17.     int chemistry{};
  18.     double gap = (physics + math + informatics + chemistry) / 4.;
  19. };
  20.  
  21. void inputInt(int &a)
  22. {
  23.     bool err;
  24.     string ans;
  25.     rewind(stdin);
  26.     do
  27.     {
  28.         err = false;
  29.         char c;
  30.         while ((c = getchar()) != '\n')
  31.         {
  32.             ans += c;
  33.             if (c > '9' || c < '0') err = true;
  34.         }
  35.         if (ans.empty()) err = true;
  36.         if (err)
  37.         {
  38.             ans.clear();
  39.             cout << "Enter a number, please :)" << endl;
  40.         }
  41.     } while (err);
  42.     a = atoi(ans.c_str());
  43. }
  44.  
  45. string normalize(string a)
  46. {
  47.     if (a[0] > 'Z') a[0] -= 32;
  48.     for (int i = 1; i < a.size(); i++)
  49.     {
  50.         if (a[i] < 'a') a[i] += 32;
  51.     }
  52.     return a;
  53. }
  54.  
  55. void inputSTR(string &a)
  56. {
  57.     bool err;
  58.     string ans;
  59.     rewind(stdin);
  60.     do
  61.     {
  62.         err = false;
  63.         char c;
  64.         while ((c = getchar()) != '\n')
  65.         {
  66.             ans += c;
  67.             if (c < 'A' || c > 'z') err = true;
  68.         }
  69.         if (ans.empty()) err = true;
  70.         if (err)
  71.         {
  72.             ans.clear();
  73.             cout << "Enter a word, please :)" << endl;
  74.         }
  75.     } while (err);
  76.     a = normalize(ans);
  77. }
  78.  
  79.  
  80. int strCounter()
  81. {
  82.     ifstream fin("File.txt", ios::in | ios::app);
  83.     char * str = new char[256];
  84.     int ans = 0;
  85.     while (!fin.eof())
  86.     {
  87.         fin.getline(str, 256, '\n');
  88.         ans++;
  89.     }
  90.     fin.close();
  91.     delete[] str;
  92.     return ans-1;
  93. }
  94.  
  95. bool isGood(Student &s)
  96. {
  97.     if (s.informatics > 4 && s.math > 4)
  98.         return true;
  99.     return false;
  100. }
  101.  
  102. void menu();
  103.  
  104. void push_back(Student *&students, Student &s, int &quantity)
  105. {
  106.     quantity++;
  107.     Student *arr = new Student[quantity];
  108.     for (int i = 0; i < quantity-1; i++)
  109.     {
  110.         arr[i] = students[i];
  111.     }
  112.     arr[quantity - 1] = s;
  113.     students = arr;
  114. }
  115.  
  116. void add(Student *&students, int &quantity) {
  117.     ofstream fout("File.txt", ios::app);
  118.     Student student;
  119.     string temp;
  120.     int temp_int = 0;
  121.     int middle = 0;
  122.     cout << "Enter name : ";
  123.     inputSTR(temp);
  124.     student.name = temp;
  125.     fout << temp << " ";
  126.     cout << "Enter surname : ";
  127.     inputSTR(temp);
  128.     student.surname = temp;
  129.     fout << temp << " ";
  130.     cout << "Enter patronymic : ";
  131.     inputSTR(temp);
  132.     student.patronymic = temp;
  133.     fout << temp << " ";
  134.     cout << "Enter number of group : ";
  135.     inputInt(temp_int);
  136.     student.number_group = temp_int;
  137.     fout << temp_int << " ";
  138.     cout << "Enter year of birth : ";
  139.     inputInt(temp_int);
  140.     student.birthYear = temp_int;
  141.     fout << temp_int << " ";
  142.     cout << "Enter mark of physics : ";
  143.     inputInt(temp_int);
  144.     student.physics = temp_int;
  145.     middle += temp_int;
  146.     fout << temp_int << " ";
  147.     cout << "Enter mark of math : ";
  148.     inputInt(temp_int);
  149.     student.math = temp_int;
  150.     middle += temp_int;
  151.     fout << temp_int << " ";
  152.     cout << "Enter mark of informatics : ";
  153.     inputInt(temp_int);
  154.     student.informatics = temp_int;
  155.     middle += temp_int;
  156.     fout << temp_int << " ";
  157.     cout << "Enter mark of chemistry : ";
  158.     inputInt(temp_int);
  159.     student.chemistry = temp_int;
  160.     middle += temp_int;
  161.     fout << temp_int << " ";
  162.     fout << middle / 4.;
  163.     student.gap = temp_int;
  164.     fout << endl;
  165.     fout.close();
  166.     push_back(students, student, quantity);
  167. }
  168.  
  169. void show(const Student *students, int quantity)
  170. {
  171.  
  172.     for (int i = 0; i < quantity; i++)
  173.     {
  174.         cout << "Name : " << students[i].name << endl;
  175.         cout << "Surname : " << students[i].surname << endl;
  176.         cout << "Patronymic : " << students[i].patronymic << endl;
  177.         cout << "Group : " << students[i].number_group << endl;
  178.         cout << "Year of birth : " << students[i].birthYear << endl;
  179.         cout << "Physics : " << students[i].physics << endl;
  180.         cout << "Math : " << students[i].math << endl;
  181.         cout << "Informatics : " << students[i].informatics << endl;
  182.         cout << "Chemistry : " << students[i].chemistry << endl;
  183.         cout << "Middle mark : " << students[i].gap << endl;
  184.     }
  185.  
  186. }
  187. void individual(Student *students, int quantity)
  188. {
  189.     for (int i = 0; i < quantity; i++)
  190.     {
  191.         if (!isGood(students[i])) continue;
  192.         cout << "Name : " << students[i].name << endl;
  193.         cout << "Surname : " << students[i].surname << endl;
  194.         cout << "Patronymic : " << students[i].patronymic << endl;
  195.         cout << "Group : " << students[i].number_group << endl;
  196.         cout << "Year of birth : " << students[i].birthYear << endl;
  197.         cout << "Physics : " << students[i].physics << endl;
  198.         cout << "Math : " << students[i].math << endl;
  199.         cout << "Informatics : " << students[i].informatics << endl;
  200.         cout << "Chemistry : " << students[i].chemistry << endl;
  201.         cout << "Middle mark : " << students[i].gap << endl;
  202.     }
  203. }
  204. void edit(Student *&students, int quantity) {
  205.     system("cls");
  206.     if (quantity == 0) {
  207.         cout << "The list of students is empty" << endl;
  208.         return;
  209.     }
  210.     cout << "Vvedite nomer studenta kotorogo nuzhno izmenit: ";
  211.     int number;
  212.     inputInt(number);
  213.     if (number < 1 || number>quantity)
  214.     {
  215.         cout << "Student doesn't exist" << endl;
  216.         return;
  217.     }
  218.     cout << "Viberite chto vam nyzhno izmenit: " << endl;
  219.     cout << "1 - Name" << endl;
  220.     cout << "2 - Surname" << endl;
  221.     cout << "3 - patronymic" << endl;
  222.     cout << "4 - nember_group" << endl;
  223.     cout << "5 - birthYear" << endl;
  224.     cout << "6 - physics" << endl;
  225.     cout << "7 - math" << endl;
  226.     cout << "8 - informatics" << endl;
  227.     cout << "9 - chemistry" << endl;
  228.     int choice;
  229.     inputInt(choice);
  230.     switch (choice)
  231.     {
  232.     case 1:
  233.     {
  234.         cout << "Vvedite imya: ";
  235.         string nam;
  236.         inputSTR(nam);
  237.         students[number-1].name = nam;
  238.     }
  239.         break;
  240.     case 2:
  241.     {
  242.         cout << "Vvedite famil: ";
  243.         string nam;
  244.         inputSTR(nam);
  245.         students[number-1].surname = nam;
  246.     }
  247.         break;
  248.     case 3:
  249.     {
  250.         cout << "Vvedite otchestv: ";
  251.         string nam;
  252.         inputSTR(nam);
  253.         students[number-1].patronymic = nam;
  254.     }
  255.         break;
  256.     case 4:
  257.     {
  258.         cout << "Vvedite nomer_group: ";
  259.         int nam;
  260.         inputInt(nam);
  261.         students[number-1].number_group = nam;
  262.     }
  263.         break;
  264.     case 5:
  265.     {
  266.         cout << "Vvedite data rozhdeniya: ";
  267.         int nam;
  268.         inputInt(nam);
  269.         students[number-1].birthYear = nam;
  270.     }
  271.         break;
  272.     case 6:
  273.     {
  274.         cout << "Vvedite ocenky po fizike: ";
  275.         int nam;
  276.         inputInt(nam);
  277.         students[number-1].physics = nam;
  278.     }
  279.         break;
  280.     case 7:
  281.     {
  282.         cout << "Vvedite ocenky po mat: ";
  283.         int nam;
  284.         inputInt(nam);
  285.         students[number-1].math = nam;
  286.     }
  287.         break;
  288.     case 8:
  289.     {
  290.         cout << "Vvedite ocenky po informat: ";
  291.         int nam;
  292.         inputInt(nam);
  293.         students[number-1].informatics = nam;
  294.     }
  295.         break;
  296.     case 9:
  297.     {
  298.         cout << "Vvedite ocenky po himii: ";
  299.         int nam;
  300.         inputInt(nam);
  301.         students[number-1].chemistry = nam;
  302.     }
  303.         break;
  304.     default:
  305.         cout << "ERROR" << endl;
  306.         break;
  307.     }
  308.     ofstream fout("File.txt", ios::out);
  309.     for (int i = 0; i < quantity; i++) {
  310.         fout << students[i].name << " ";
  311.         fout << students[i].surname << " ";
  312.         fout << students[i].patronymic << " ";
  313.         fout << students[i].number_group << " ";
  314.         fout << students[i].birthYear << " ";
  315.         fout << students[i].physics << " ";
  316.         fout << students[i].math << " ";
  317.         fout << students[i].informatics << " ";
  318.         fout << students[i].chemistry << " ";
  319.         fout << students[i].gap << endl;
  320.     }
  321.     system("cls");
  322.     cout << "Izmenino!!!" << endl;
  323. }
  324.  
  325. int initiaize(Student *&students)
  326. {
  327.     int n = strCounter();
  328.     students = new Student[n];
  329.     ifstream fin("File.txt");
  330.     string temp;
  331.     int t;
  332.     double tt;
  333.     for (int i = 0; i < n; i++)
  334.     {
  335.         fin >> temp;
  336.         students[i].name = temp;
  337.         fin >> temp;
  338.         students[i].surname = temp;
  339.         fin >> temp;
  340.         students[i].patronymic = temp;
  341.         fin >> t;
  342.         students[i].number_group = t;
  343.         fin >> t;
  344.         students[i].birthYear = t;
  345.         fin >> t;
  346.         students[i].physics = t;
  347.         fin >> t;
  348.         students[i].math = t;
  349.         fin >> t;
  350.         students[i].informatics = t;
  351.         fin >> t;
  352.         students[i].chemistry = t;
  353.         fin >> tt;
  354.         students[i].gap = t;
  355.         temp = "";
  356.         t = 0;
  357.         tt = 0;
  358.     }
  359.     return n;
  360. }
  361.  
  362. int main()
  363. {
  364.     Student *students;
  365.     int quantity = initiaize(students);
  366.     while (true)
  367.     {
  368.         system("cls");
  369.         int choice = 99;
  370.         menu();
  371.         inputInt(choice);
  372.         switch (choice)
  373.         {
  374.         case 1: show(students, quantity); break;
  375.         case 2: edit(students, quantity); break;
  376.         case 3: add(students, quantity); break;
  377.         case 4: individual(students, quantity); break;
  378.         case 0: return 0;
  379.         default:cout << "ERROR!" << endl;
  380.         }
  381.         system("pause");
  382.     }
  383. }
  384.  
  385. void menu()
  386. {
  387.     cout << "Enter , what do you want :" << endl;
  388.     cout << "1 - Show all data " << endl;
  389.     cout << "2 - Edit " << endl;
  390.     cout << "3 - Add " << endl;
  391.     cout << "4 - Individual task " << endl;
  392.     cout << "0 - Exit" << endl;
  393. }
  394.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement