У меня 2 вопроса:
getWinner() объекта sportsmenArray получить определенные данные одного конкретного спортсмена (сейчас реализовано через костыль).test.txt помимо нужной информации появляются какие-то символы.Собственно сам код:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#define SIZE 10
#define DEBUG 10
using namespace std;
char* ptochar(int num){
char str[8] = "";
itoa(num, str, 10);
return str;
}
template <class typeArray> class tArray{
protected:
typeArray B[SIZE];
int n;
public:
tArray(int l){n=l;}
void input();
void output();
};
template <class typeArray > void tArray < typeArray >::input(){
for (int i=0;i<n;i++){
cin>>B[i];
}
}
template <class typeArray > void tArray < typeArray >::output(){
for (int i=0;i<n;i++){
cout<<B[i];
}
}
class sportsmen{
#ifndef DEBUG
int id;
char surname[10];
int result;
#endif //DEBUG
public:
#ifdef DEBUG
int result;
int id;
char surname[10];
#endif //DEBUG
int operator<(sportsmen ob2);
friend ostream &operator<<(ostream &stream, sportsmen ob);
friend istream &operator>>(istream &stream, sportsmen &ob);
friend ofstream &operator<<(ofstream &stream, sportsmen ob);
friend ifstream &operator>>(ifstream &stream, sportsmen &ob);
void store(fstream &stream);
void retrive(fstream &stream);
};
int sportsmen::operator <(sportsmen ob2){
if(result<ob2.result)
return true;
else
return false;
}
class sportsmenArray: public tArray<sportsmen>{
public:
sportsmenArray(int l):tArray<sportsmen>(l){};
void inputBFile(char* filename);
void outputBFile(char* filename);
void getWinner();
};
void sportsmenArray::outputBFile(char* filename){
fstream out(filename, ios::out | ios::binary);
if (!out){
cout << "Cann't open file " << filename << " for output!" << endl;
exit;
}
for (int i=0;i<n;i++){
B[i].store(out);
}
out.close();
}
void sportsmenArray::inputBFile(char* filename){
fstream in(filename, ios::in | ios::binary);
if (!in){
cout << "Cann't open file " << filename << " for input!" << endl;
exit;
}
for (int i=0;i<n;i++){
B[i].retrive(in);
}
in.close();
}
void sportsmenArray::getWinner(){
int max = B[0].result;
int k=-1;
for(int i=0; i < n; i++){
if(B[i].result>max){
max = B[i].result;
k=i;
}
}
cout <<
"winner is:\nid: " << B[k].id <<
"\tname: " << B[k].surname <<
"\tresult: " << B[k].result << endl;
}
ostream &operator<<(ostream &stream, sportsmen ob){
cout << ob.id << " \t " << ob.surname << " \t " << ob.result << endl;
return stream;
}
istream &operator>>(istream &stream, sportsmen &ob){
cout << endl;
cout << "Input id: "; cin>>ob.id;
cout << "Input Surname: "; cin>>ob.surname;
cout << "Input result: "; cin>>ob.result;
return stream;
}
ofstream &operator << (ofstream &stream, sportsmen ob){
stream << ob.id << " " << ob.surname << " " << ob.result << endl;
return stream;
}
ifstream &operator >> (ifstream &stream, sportsmen &ob){
stream >> ob.id;
stream >> ob.surname;
stream >> ob.result;
return stream;
}
void sportsmen::store(fstream &stream){
stream.write(ptochar(id),sizeof(int));
stream.write(surname,sizeof(surname));
stream.write(ptochar(result),sizeof(int));
}
void sportsmen::retrive(fstream &stream){
stream.read(ptochar(id),sizeof(int));
stream.read(surname,sizeof(surname));
stream.read(ptochar(result),sizeof(int));
}
int main(){
sportsmenArray C(3);
C.input();
C.outputBFile("test.txt");
C.inputBFile("test.txt");
C.output();
C.getWinner();
return 0;
}
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости