Задан массив структур "Спортсмен" (Фамилия спортсмена; порядковый номер; результаты трех попыток). Вывести на экран список спортсменов с указанием суммарного результата.
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
| #include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <iomanip>
using namespace std;
struct sportsmen
{
char NameLast[20];
int attempt1;
int attempt2;
int attempt3;
double sum;
sportsmen* next;
};
class List
{
sportsmen* head;
public:
List() : head(NULL) {};
~List();
void ReadFromFile();
void Add();
void Show();
void LoadToFile();
};
List::~List()
{
while (head != NULL)
{
sportsmen* temp = head->next;
delete head;
head = temp;
}
}
void List::Add()
{
sportsmen sp;
sportsmen* temp = new sportsmen;
temp->next = head;
system("cls");
cin.ignore();
cout << "NameLast: "; cin.getline(sp.NameLast, 20);
cout << "attempt1; "; cin >> sp.attempt1;
cout << "attempt2; "; cin >> sp.attempt2;
cout << "attempt3; "; cin >> sp.attempt3;
strcpy_s(temp->NameLast, sp.NameLast);
temp->attempt1 = sp.attempt1;
temp->attempt2 = sp.attempt2;
temp->attempt3 = sp.attempt3;
head = temp;
}
void List::ReadFromFile()
{
ifstream fin("1.txt");
sportsmen* temp;
if (fin.is_open())
{
cout << "File is open" << endl;
while (!fin.eof())
{
temp = new sportsmen;
fin >> temp->NameLast >> temp->attempt1>> temp->attempt2 >> temp->attempt3;
head = temp;
}
cout << "Data is received" << endl;
fin.close();
}
else cout << "File is not found";
}
void List::LoadToFile()
{
char file_name[30];
system("cls");
cout << "Input file name: ";
cin >> file_name;
ofstream fout(file_name);
if (fout)
{
sportsmen* temp = head;
while (temp != NULL)
{
fout << setw(20) << temp->NameLast << setw(3) << temp->attempt1 << setw(3) << temp->attempt2 << setw(3) << temp->attempt3 << endl;
temp = temp->next;
}
cout << "File is completed" << endl;
system("pause");
fout.close();
}
else cout << "File is not create";
}
void List::Show()
{
sportsmen* temp = head;
system("cls");
cout << setw(20) << "NameLast" << setw(3) << "sum" << endl;
cout << "" << endl << endl;
while (temp != NULL)
{
cout << setw(20) << temp->NameLast;
cout << setw(3) << temp->sum;
temp = temp->next;
}
cout << ""
<< endl << endl;
}
int main()
{
cout.setf(ios::left);
bool flag = true;
int choice;
sportsmen sportsmen;
List spisok;
while (flag)
{
system("cls");
cout << " MENU" << endl;
cout << "1: Read from the file" << endl;
cout << "2: Show List" << endl;
cout << "3: Add sportsmen" << endl;
cout << "4: Save to the file" << endl;
cout << "5: Task: Zadan massiv struktur 'Sportsmen' (familiya sportsmena; poryadkoviy nomer; rezultaty treh popytok). Vivesty na ekran spisok sportsmenov v poryadke ubivaniya summarnogo resultata" << endl;
cout << "6: Exit" << endl;
cout << "7: " << endl << endl;
cout << "Make your choice (1-8): ";
cin >> choice;
switch (choice)
{
case 1: spisok.ReadFromFile(); system("PAUSE"); break;
case 2: spisok.Show(); system("PAUSE"); break;
case 3: spisok.Add(); break;
case 4: spisok.LoadToFile(); break;
// case 5: spisok.Tasf (); break;
case 6: flag = false; break;
default: cout << "you are wrong. ";
}
}
system("PAUSE");
return 0;
} |
|