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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
#include"Carte.h"
Carte::Carte()
{
NomCarte[0]=0;
NomSerie="";
/*
OU
NomSerie=new char[1];
NomSerie[0]=0;
*/
Numero=0;
CarSco;
}
Carte::Carte(char * nomc,char * noms,int num,char * NomCaract,int score)
{
strcpy(NomCarte,nomc);
NomSerie=new char[strlen(noms)+1];
strcpy(NomSerie,noms);
Numero=num;
CarSco.setNomCaract(NomCaract);
CarSco.setScore(score);
}
Carte::Carte(char * nomc,char * noms,int num,CaractScore cs)
{
strcpy(NomCarte,nomc);
NomSerie=new char[strlen(noms)+1];
strcpy(NomSerie,noms);
Numero=num;
CarSco=cs;
}
Carte::Carte(char * nomc)
{
strcpy(NomCarte,nomc);
NomSerie=new char[1];
NomSerie[0]=0;
Numero=0;
CarSco;
}
Carte::Carte(const Carte & c)
{
strcpy(NomCarte,c.NomCarte);
NomSerie=new char[strlen(c.NomSerie)+1];
strcpy(NomSerie,c.NomSerie);
Numero=c.Numero;
CarSco=c.CarSco;
}
void Carte::setNomCarte(char * nomc)
{
strcpy(NomCarte,nomc);
}
void Carte::setNomSerie(char * noms)
{
NomSerie=new char[strlen(noms)+1];
strcpy(NomSerie,noms);
}
void Carte::setNumero(int num)
{
Numero=num;
}
void Carte::setCaractScore(char * NomCaract,int score)
{
CarSco.setNomCaract(NomCaract);
CarSco.setScore(score);
}
void Carte::setCaractScore(CaractScore cs)
{
CarSco=cs;
}
char * Carte::getNomCarte()
{
return NomCarte;
}
char * Carte::getNomSerie()
{
return NomSerie;
}
int Carte::getNumero()
{
return Numero;
}
CaractScore Carte::getCaractScore()
{
return CarSco;
}
void Carte::Affiche()
{
cout<<"Nom de la carte: "<<NomCarte<<endl;
cout<<"Nom de la serie: "<<NomSerie<<endl;
cout<<"Numero de la carte: "<<Numero<<endl;
CarSco.Affiche();
}
/*
ostream & operator <<(ostream & s,const Carte &c)
{
s<<"Nom de la carte: "<<c.NomCarte<<endl;
s<<"Nom de la serie: "<<c.NomSerie<<endl;
s<<"Numero de la carte: "<<c.Numero<<endl;
s<<c.CarSco;
return s;
}
*/
ostream & operator <<(ostream & s,const Carte &c)
{
ostream * ptostream=&s;
// attention si on travaille avec des fstream (voir main), ne surtout pas declarer ptofstream comme un pointeur de ofstream
fstream * ptofstream=dynamic_cast <fstream *> (ptostream);
if(ptofstream)
{
(*ptofstream)<<c.NomCarte<<endl;
(*ptofstream)<<c.NomSerie<<endl;
(*ptofstream)<<c.Numero<<endl;
(*ptofstream)<<c.CarSco;
}
else
{
s<<"Nom de la carte: "<<c.NomCarte<<endl;
s<<"Nom de la serie: "<<c.NomSerie<<endl;
s<<"Numero de la carte: "<<c.Numero<<endl;
s<<c.CarSco;
}
return s;
}
/*
istream & operator >>(istream & s, Carte &c)
{
cout<<"Nom de la carte? ";
s>>c.NomCarte;
char temp[20];
cout<<"Nom de la serie? ";
s>>temp;
c.setNomSerie(temp);
cout<<"Numero de la carte? ";
s>>c.Numero;
return s;
}
*/
istream & operator >>(istream & s, Carte &c)
{
istream * ptistream=&s;
// attention si on travaille avec des fstream (voir main), ne surtout pas declarer ptifstream comme un pointeur de ifstream mais comme un pointeur de fstream
fstream * ptifstream=dynamic_cast <fstream *> (ptistream);
if(ptifstream)
{
char temp[30];
char car;
/* REMARQUE IMPORTANTE
//avec l'instruction ci-dessous, le \n reste ds le buffer
(*ptifstream)>>c.NomCarte;
(*ptifstream).get(car);
//cout<<"char="<<car;
*/
(*ptifstream).getline(c.NomCarte,30);
//avec l'instruction ci-dessous, le \n est lu (ne reste pas ds le buffer) et ds temp, il est écrase par \0
(*ptifstream).getline(temp,30);
c.setNomSerie(temp);
(*ptifstream)>>c.Numero;
(*ptifstream).get(car);
(*ptifstream)>>c.CarSco;
}
else
{
char car;
cout<<"Nom de la carte? ";
s.getline(c.NomCarte,30);
char temp[30];
cout<<"Nom de la serie? ";
s.getline(temp,30);
c.setNomSerie(temp);
cout<<"Numero de la carte? ";
s>>c.Numero;
s>>c.CarSco;
//s.get(car) sinon qd je lis 2 cartes à la suite l'une de l'autre, ne me permettait pas d'entrer le nom de la carte lors de la lecture de la deuxième carte
s.get(car);
}
}
/*
// pour eviter que la lecture ne s'arrete au 1er blanc
istream & operator >>(istream & s, Carte &c)
{
char car;
cout<<"Nom de la carte? ";
s.getline(c.NomCarte,30);
char temp[30];
cout<<"Nom de la serie? ";
s.getline(temp,30);
c.setNomSerie(temp);
cout<<"Numero de la carte? ";
s>>c.Numero;
s>>c.CarSco;
//s.get(car) sinon qd je lis 2 cartes à la suite l'une de l'autre, ne me permettait pas d'entrer le nom de la carte lors de la lecture de la deuxième carte
s.get(car);
return s;
}
*/
int Carte::IdentNomCaract(Carte c)
{
if(CarSco.IdentNomCaract(c.CarSco))
return 1;
return 0;
}
Carte & Carte::operator=(const Carte & c)
{
strcpy(NomCarte,c.NomCarte);
NomSerie=new char[strlen(c.NomSerie)+1];
strcpy(NomSerie,c.NomSerie);
Numero=c.Numero;
CarSco=c.CarSco;
return *this;
}
Carte Carte::operator+(int s)
{
Carte c(*this);
c.CarSco=c.CarSco+s;
return c;
}
Carte Carte::operator-(int s)
{
Carte c(*this);
c.CarSco=c.CarSco-s;
return c;
}
Carte Carte::operator+(Carte c)
{
Carte cart(*this);
cart.CarSco=cart.CarSco+c.CarSco;
return cart;
}
int Carte::operator-(Carte c)
{
int s;
s=CarSco-c.CarSco;
return s;
}
int Carte::operator<(Carte &c)
{
if(CarSco<c.CarSco)
return 1;
return 0;
}
int Carte::operator>(Carte &c)
{
if(CarSco>c.CarSco)
return 1;
return 0;
}
Carte Carte::operator++()
{
CarSco++;
return *this;
}
Carte Carte::operator++(int)
{
Carte c(*this);
CarSco++;
return c;
}
int Carte::operator!=(Carte & c)
{
if(strcmp(NomSerie,c.NomSerie)!=0 || Numero!=c.Numero)
return 1;
return 0;
} |
Partager