Advertisement
vencinachev

Hw3-Bobi

Feb 5th, 2021
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. // Упражнение 6: Задачa 5
  2. #include <iostream>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. struct Item {
  8.     int fnumber;
  9.     double mark;
  10.     Item *next;
  11. };
  12.  
  13.  
  14. Item* Create(Item* Head){
  15.     Item* Last=NULL, *P;
  16.     char ch;
  17.     cout << " Enter new student (Y/N) ?:";
  18.     cin >> ch;
  19.     while (ch == 'Y' || ch == 'y') {
  20.         P = new Item;
  21.         cout << " Enter faculty number : ";
  22.         cin >> P->fnumber;
  23.         cout << " Enter mark : ";
  24.         cin >> P->mark;
  25.         P->next = NULL;
  26.         if (Head == NULL) Head = P;
  27.         else Last->next = P;
  28.         Last = P;
  29.         cout << " enter new element (Y/N) ?:";
  30.         cin >> ch;
  31.     }
  32.     return Head;
  33. }
  34.  
  35.  
  36. void Traverse(Item* P) {
  37.     while (P != NULL) {
  38.         cout << "FN: " << P->fnumber << " Mark: " << P->mark << endl;
  39.         P = P->next;
  40.     }
  41. }
  42.  
  43. Item* Search(int fnumber, Item* P) {
  44.     while (P != NULL && P->fnumber != fnumber) {
  45.         P = P->next;
  46.     }
  47.     return P;
  48. }
  49.  
  50. double AverageMark(Item* P){
  51.     int cnt = 0;
  52.     double sum = 0;
  53.     while (P != NULL) {
  54.         cnt++;
  55.         sum += P->mark;
  56.         P = P->next;
  57.     }
  58.     return cnt == 0 ? 0 : sum / cnt;
  59. }
  60.  
  61. int main() {
  62.     Item* Students = NULL;
  63.     Students = Create(Students);
  64.     Traverse(Students);
  65.  
  66.     double num;
  67.     cout << "Enter faculty number for searching: ";
  68.     cin >> num;
  69.     Item* El = Search(num, Students);
  70.     if (El != NULL){
  71.         cout << "Found." << endl;
  72.         cout << "FN: " << El->fnumber << " Mark: " << El->mark << endl;
  73.     } else {
  74.         cout << "Not found" << endl;
  75.     }
  76.  
  77.     cout << "Average mark: " << AverageMark(Students) << endl;
  78.  
  79.     return 0;
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement