Advertisement
xTheEc0

5. Assignment. (Array of classes in class)

Apr 4th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.01 KB | None | 0 0
  1. #include <cstdlib>  // Standard General Utilities Library
  2. #include <cstdio>   // Input/Output operations (C)
  3. #include <iostream> // Input/Output stream objects
  4. #include <iomanip>  // Input/Output manipulators
  5. #include <fstream>  // File input and output
  6. #include <ctime>    // Date and time information
  7. #include <cmath>    // Mathematical operations and transformations
  8.  
  9. using namespace std;
  10.  
  11. #define CMax 100
  12.  
  13. // Classes
  14. class student {
  15. protected:
  16.     int ID;
  17.     double m1, m2, m3;
  18. public:
  19.     void addStudent();
  20.     double average();
  21.     int giveID();
  22.     void printShortInfo();
  23.     void printFullInfo();
  24.     student();
  25.     student(int, double, double, double);
  26. };
  27.  
  28. void student::addStudent() {
  29.     cout << "Iveskite studento ID: "; cin >> ID;
  30.     cout << "Iveskite studento triju egzaminu rezultatus" << endl;
  31.     cin >> m1 >> m2 >> m3;
  32. }
  33.  
  34. double student::average() {
  35.     return (m1 + m2 + m3) / 3;
  36. }
  37.  
  38. int student::giveID() {
  39.     return ID;
  40. }
  41.  
  42. void student::printShortInfo() {
  43.     cout << "ID: " << ID;
  44.     cout << " Vidurkis: " << average() << endl << endl;
  45. }
  46.  
  47. void student::printFullInfo() {
  48.     cout << "Studento ID: " << ID << endl;
  49.     cout << "Egzaminu pazymiai: " << m1 << " " << m2 << " " << m3 << endl;
  50.     cout << "Vidurkis: " << average() << endl << endl;
  51. }
  52.  
  53. //Student constructors
  54. student::student(){
  55.     ID = 0;
  56.     m1 = 0;
  57.     m2 = 0;
  58.     m3 = 0;
  59. }
  60.  
  61. // Nepanaudota. :  (
  62. student::student(int varID, double var1, double var2, double var3) {
  63.     ID = varID;
  64.     m1 = var1;
  65.     m2 = var2;
  66.     m3 = var3;
  67. }
  68.  
  69. class course {
  70. public:
  71.     student students[CMax];
  72.     student tempStudents[1];
  73.     int size;
  74. public:
  75.     void addStudent();
  76.     void deleteStudent(int);
  77.     void courseAverage();
  78.     void print();
  79.     void printByID(int);
  80.     void sortStudents();
  81.  
  82.     course() {size = 0;}
  83. };
  84.  
  85. void course::addStudent() {
  86.     students[size].addStudent();
  87.     size++;
  88. }
  89.  
  90. void course::deleteStudent(int deleteID) {
  91.     int counter = 0;
  92.     for (int i = 0; i < size; i++)
  93.     {
  94.         if (students[i].giveID() == deleteID)
  95.         {
  96.             counter++;
  97.             for (int j = i; j < size; j++)
  98.                 students[j] = students[j+1];
  99.         }
  100.     }
  101.     size -= counter;
  102. }
  103.  
  104. void course::courseAverage() {
  105.     double sum;
  106.     for (int i = 0; i < size; i++)
  107.     {
  108.         sum += students[i].average();
  109.     }
  110.     cout << "Kurso vidurkis yra: " << sum / size << endl;
  111. }
  112.  
  113. void course::print() {
  114.     for (int i = 0; i < size; i++)
  115.         students[i].printShortInfo();
  116. }
  117.  
  118. void course::printByID(int printID) {
  119.     for (int i = 0; i < size; i++)
  120.     {
  121.         if (students[i].giveID() == printID)
  122.             students[i].printFullInfo();
  123.     }
  124. }
  125.  
  126. void course::sortStudents() {
  127.     for (int i = 0; i < size-1; i++)
  128.     {
  129.         for (int j = i; j < size; j++)
  130.         {
  131.             if (students[j].average() > students[i].average())
  132.             {
  133.                 tempStudents[0] = students[i];
  134.                 students[i] = students[j];
  135.                 students[j] = tempStudents[0];
  136.  
  137.             }
  138.         }
  139.     }
  140. }
  141. // Main program
  142. int main(int argc, char *argv[]) {
  143.  
  144.     course S;
  145.     int n = 0;
  146.  
  147.     int userInput;
  148.     do {
  149.         system("CLS");
  150.         cout << "1 - Ivesti nauja studenta." << endl;
  151.         cout << "2 - Atspauzdinti visu kurso studentu duomenis" << endl;
  152.         cout << "3 - Atspausdinti pilna studento informacija pagal ID" << endl;
  153.         cout << "4 - Suskaiciuoti kurso bendra studentu vidurki" << endl;
  154.         cout << "5 - Rikiuoti sutentus pagal vidutini egzaminu ivertinima" << endl;
  155.         cout << "6 - Istrinti studenta pagal ID" << endl;
  156.  
  157.         cout << "\n0 - Baigti darba." << endl;
  158.         cin >> userInput;
  159.  
  160.         if (userInput == 1)
  161.         {
  162.             system("CLS");
  163.             S.addStudent();
  164.             n++;
  165.         }
  166.         if (userInput == 2)
  167.         {
  168.             system("CLS");
  169.             S.print();
  170.             system("pause");
  171.  
  172.         }
  173.         if (userInput == 3)
  174.         {
  175.             int tempID;
  176.             system("CLS");
  177.             cout << "Iveskite studento ID, kurio informacija norite pamatyti: "; cin >> tempID;
  178.             S.printByID(tempID);
  179.             system("pause");
  180.  
  181.         }
  182.         if (userInput == 4)
  183.         {
  184.             system("CLS");
  185.             S.courseAverage();
  186.             system("pause");
  187.         }
  188.         if (userInput == 5)
  189.         {
  190.             system("CLS");
  191.             S.sortStudents();
  192.             cout << "Studentai isrikuoti pagal ju egzaminu vidurki" << endl;
  193.             system("pause");
  194.         }
  195.         if (userInput == 6)
  196.         {
  197.             int tempID;
  198.             system("CLS");
  199.             cout << "Iveskite studento ID, kury norite istrinti: "; cin >> tempID;
  200.             S.deleteStudent(tempID);
  201.             cout << "Studentas istrintas." << endl;
  202.             system("pause");
  203.         }
  204.  
  205.     } while (userInput != 0);
  206.     system("CLS");
  207.     cout << "Programa baige darba." << endl;
  208.  
  209.    return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement