Advertisement
enkov

Задача със структура (без функции)

Oct 31st, 2018
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.26 KB | None | 0 0
  1. /*
  2. В един университет се прави малка програма за работа със студенти.
  3. Всеки студент се описва с име, факултетен номер, курс (число от 1 до 4)
  4. и (средно-аритметичен) успех.
  5. Изведете информация за:
  6. 1. всички студенти;
  7. 2. студенти, които имат успех под 3.50 от 2-ри курс;
  8. 3. студентите с максимален и минимален успех от 3-ти курс;
  9. 4. за всеки курс средните успехи.
  10. */
  11.  
  12. // Вариант без функции
  13.  
  14. #include "stdafx.h"
  15. #include <iostream>
  16. #include <string>
  17. using namespace std;
  18.  
  19. int main()
  20. {
  21.     const int n = 50;
  22.     /* Вариант с 4 отделни масива: - сложно и неудобно
  23.     string Name[n];
  24.     string FacNo[n];
  25.     int Course[n];
  26.     float Uspeh[n];
  27.     */
  28.  
  29.     // Със структура е по-удобно и по-лесно
  30.     struct Student {
  31.         string Name;
  32.         string FacNo;
  33.         int Course;
  34.         float Uspeh;
  35.     };
  36.  
  37.     Student Students[n];
  38.  
  39.     int x = 5; // ще работим с 5, за по-бързо въвеждане
  40.  
  41.     for (int i = 0; i < x; i++)
  42.     {
  43.         cout << "Enter information for student " << i << endl;
  44.         cout << " Name: ";
  45.         cin.get();  // skip the buffer
  46.         getline(cin, Students[i].Name);
  47.         cout << " Fac number: ";
  48.         getline(cin, Students[i].FacNo);
  49.         cout << " Course: ";
  50.         cin >>  Students[i].Course;
  51.         cout << " Uspeh: ";
  52.         cin >> Students[i].Uspeh;
  53.     }
  54.  
  55.     // 1. всички студенти
  56.     cout << "1. Information for all students: " << endl;
  57.     for (int i = 0; i < x; i++)
  58.     {
  59.         cout << "Student " << i << ":"
  60.             << " Name: " << Students[i].Name
  61.             << " Fac number: " << Students[i].FacNo
  62.             << " Course: " << Students[i].Course
  63.             << " Uspeh: " << Students[i].Uspeh << endl;
  64.     }
  65.  
  66.     // 2. студенти, които имат успех под 3.50 от 2 - ри курс;
  67.     cout << "2. Students with grade less than 3.50 from 2-nd course: " << endl;
  68.     for (int i = 0; i < x; i++)
  69.     {
  70.         if (Students[i].Uspeh < 3.50 && Students[i].Course == 2)
  71.         cout << "Student " << i << ":"
  72.             << " Name: " << Students[i].Name
  73.             << " Fac number: " << Students[i].FacNo
  74.             << " Course: " << Students[i].Course
  75.             << " Uspeh: " << Students[i].Uspeh << endl;
  76.     }
  77.  
  78.     // 3. студентите с максимален и минимален успех от 3 - ти курс;
  79.     float minUspeh3, maxUspeh3;
  80.     minUspeh3 = 0; maxUspeh3 = 0;
  81.     for (int i = 0; i < x; i++)
  82.     {
  83.         if (Students[i].Course == 3)
  84.         {
  85.             // ако е 1-вия от 3-ти курс - запомняме
  86.             if (minUspeh3 == 0)
  87.                 minUspeh3 = Students[i].Uspeh;
  88.             if (maxUspeh3 == 0)
  89.                 maxUspeh3 = Students[i].Uspeh;
  90.             // проверяваме
  91.             if (Students[i].Uspeh < minUspeh3)
  92.                 minUspeh3 = Students[i].Uspeh;
  93.             if (Students[i].Uspeh > maxUspeh3)
  94.                 maxUspeh3 = Students[i].Uspeh;
  95.         }
  96.     }
  97.     cout << "3.1. Students with minimal grade from 3-rd course: " << endl;
  98.     for (int i = 0; i < x; i++)
  99.     {
  100.         if (Students[i].Uspeh == minUspeh3 && Students[i].Course == 3)
  101.             cout << "Student " << i << ":"
  102.             << " Name: " << Students[i].Name
  103.             << " Fac number: " << Students[i].FacNo
  104.             << " Course: " << Students[i].Course
  105.             << " Uspeh: " << Students[i].Uspeh << endl;
  106.     }
  107.     cout << "3.2. Students with maximal grade from 3-rd course: " << endl;
  108.     for (int i = 0; i < x; i++)
  109.     {
  110.         if (Students[i].Uspeh == maxUspeh3 && Students[i].Course == 3)
  111.             cout << "Student " << i << ":"
  112.             << " Name: " << Students[i].Name
  113.             << " Fac number: " << Students[i].FacNo
  114.             << " Course: " << Students[i].Course
  115.             << " Uspeh: " << Students[i].Uspeh << endl;
  116.     }
  117.  
  118.     // 4. за всеки курс средните успехи
  119.     float sums[5]; // 1..4
  120.     int count[5];
  121.     for (int i = 1; i <= 4; i++)
  122.     {
  123.         count[i] = 0; sums[i] = 0;
  124.     }
  125.     for (int i = 0; i < x; i++)
  126.     {
  127.         sums[Students[i].Course] += Students[i].Uspeh;
  128.         count[Students[i].Course]++;
  129.     }
  130.     cout << "4. Average grades for course: " << endl;
  131.     for (int i = 1; i <= 4; i++)
  132.     {
  133.         if (count[i] > 0)
  134.             cout << " Course " << i << ": " << sums[i] / count[i] << endl;
  135.         else
  136.             cout << " Course " << i << ": has no students " << endl;
  137.     }
  138.  
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement