Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Упражнение 6: Задачa 5
- #include <iostream>
- #include <cmath>
- using namespace std;
- struct Item {
- int fnumber;
- double mark;
- Item *next;
- };
- Item* Create(Item* Head){
- Item* Last=NULL, *P;
- char ch;
- cout << " Enter new student (Y/N) ?:";
- cin >> ch;
- while (ch == 'Y' || ch == 'y') {
- P = new Item;
- cout << " Enter faculty number : ";
- cin >> P->fnumber;
- cout << " Enter mark : ";
- cin >> P->mark;
- P->next = NULL;
- if (Head == NULL) Head = P;
- else Last->next = P;
- Last = P;
- cout << " enter new element (Y/N) ?:";
- cin >> ch;
- }
- return Head;
- }
- void Traverse(Item* P) {
- while (P != NULL) {
- cout << "FN: " << P->fnumber << " Mark: " << P->mark << endl;
- P = P->next;
- }
- }
- Item* Search(int fnumber, Item* P) {
- while (P != NULL && P->fnumber != fnumber) {
- P = P->next;
- }
- return P;
- }
- double AverageMark(Item* P){
- int cnt = 0;
- double sum = 0;
- while (P != NULL) {
- cnt++;
- sum += P->mark;
- P = P->next;
- }
- return cnt == 0 ? 0 : sum / cnt;
- }
- int main() {
- Item* Students = NULL;
- Students = Create(Students);
- Traverse(Students);
- double num;
- cout << "Enter faculty number for searching: ";
- cin >> num;
- Item* El = Search(num, Students);
- if (El != NULL){
- cout << "Found." << endl;
- cout << "FN: " << El->fnumber << " Mark: " << El->mark << endl;
- } else {
- cout << "Not found" << endl;
- }
- cout << "Average mark: " << AverageMark(Students) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement