1)
Program wprowadzający. Użycie strumieni cin, cout.
#include
<iostream>
Instrukcja using namespace.
stdout /
cout
stdin /
cin
stderr /
cerr
#include <iostream>
using namespace std;
int x=7;
double y=2.3;
char buf[256]=”Ala ma kota”;
std::cout
<< x;
std::cout
<< std::endl;
cout << y;
cout << endl;
cout << buf;
cout << “x
wynosi:” ;
cout << x << “ “
<< y << ” “ << buf
<< endl;
(((((cout << x) << “ “) << y) << ” “) << buf) << endl;
cout << x ® cout
cout <<
“ “
(((cout << x) << “ “) << y) <<
“ “<<buf<<endl;
Strumień
cin
cin
>> x
cin
>> y;
cin
>> buf;
cin
>> x >> y >> buf;
2.
istream
>>
ifstream
cin
istrstrem
ostream
<<
ofstream
cout
ostrsteram
Zapisz do pliku imie, nazwisko , nr indeksu.
////////////////////////////////////////////////////
#include <iostream>
#include
<fstream>
using namespace
std;
void main(){
char
imie[20]=”Jan”;
ofstream
of(”out.txt”);
if(!of){cerr<<”Bład”<<endl;return;}
of
<< imie << “ “<<nazwisko <<” “
<<nr_albumu<<endl;
// cout <<
imie << “ “<<nazwisko <<” “ <<nr_albumu<<endl;
// of.close();
}
ifstream ifs(„out.txt”);
if(!ifs){
cerr<<”nie
moge otworzyc pliku”<<endl;
exit(0);
}
ifs>>imie>>nazwisko>>nr_albumu;
cout << imie << “ “<<nazwisko <<” “
<<nr_albumu<<endl;
// wypisac dane
3 Zadeklaruj klasę student, wypełnij pola
danymi “Jan”, “Kowalski” “23/345” i zapisz zawartość do pliku
class
Student
{
public:
char imie[64];
char nazwisko[64];
char nr_albumu[16];
};
void main(){
Student s;
strcpy(s.imie,”Jan”);
strcpy(s.nazwisko,”Kowalski”);
}
3.a
Metoda w klasie Student
class
Student
{
public:
char
imie[64];
char
nazwisko[64];
char nr_albumu[16];
void set(const char*im,const char*naz, const
char*nr);
};
void Student::set(const char*im,const char*naz, const char*nr)
{
strcpy(this->imie,im);
strcpy(nazwisko,naz);
strcpy(nr_albumu,nr);
}
void main(){
Student s;
s.set(”Jan”,”Kowalski”,”232/12”);
}
3b Zapis, odczyt
class
Student
{
public:
char
imie[64];
char
nazwisko[64];
char
nr_albumu[16];
void
set(const char*im,const char*naz, const char*nr);
void write(ostream& os);
void read(istream& is);
};
void Student::write(ostream& os)
{
os<<imie<<” “<<
}
void Student::read(istream&
os){}
void main(){
Student s;
//s.set(”Jan”,”Kowalski”,”232/12”);
ofstream of(„out.txt”);
s.write(of);
of.close();
s.write(cout);
Student s2;
// przecztać z pliku
s2.write(cout);
}
3c Konstruktor
class
Student
{
public:
Student();
Student(const
char*im,const char*naz, const char*nr);
char imie[64];
char
nazwisko[64];
char nr_albumu[16];
void set(const char*im,const char*naz, const char*nr);
void write(ostream& os);
void read(istream& os);
};
Student::Student()
{
imie[0]=0;// …
}
void main(){
//Student s;
//s.set(”Jan”,”Kowalski”,”232/12”);
Student
s(”Jan”,”Kowalski”,”232/12”);
ofstream of(„out.txt”);
s.write(of);
s.write(cout);
Student s2; // przecztać
z pliku
}
4. Napisz klasę grupa (zbiór studentów). Agregacja!
class Grupa
{
public:
#define SIZE 100
Student tab[SIZE];
int
liczba;
Grupa();
};
Grupa::Grupa(){??????}
4.a Dodać dwie funkcje do klasy Grupa
bool add(Student&s) //
tablica[i]=s;
bool add(const char*imie,
const char*nazw, const char*nr)
bool Grupa::add(Student&s)
{
if(liczba>=SIZE)return false;
tablica[liczba]=s;
liczba++;
return true;
}
bool add(const char*imie, const char*nazw, const char*nr)
{
//tablica[liczba].set(…);
//tablica[liczba]=Student(imie,nazw,nr);
Student s(imie,nazw,nr);
return add(s);
}
4.b Dodać dwie funkcje, które czytają i zapisują zawartość grupy
void read(istream&is);
void
write(ostream&os);
void
main()
{
Grupa
g;
g.add(“Jan”,”Kowalski”,”123”);
g.add(“Anna”,”Kowalska”,”124”);
g.write(cout);
}
pszwed.ia.agh.edu.pl
-> WSTIE -> lista.txt
void Grupa::read(istream&is)
{
for(;liczba
< ??;){
if(!is)break;
tablica[liczba].read(is);
liczba++;
}
}
void Grupa::read(istream&is)
{
for(;;){
if(!is)break;
Student s;
s.read(is);
if(!add(s))break;
}
}
void Grupa::write(ostream&os)const
{
for()
tablica[i].write(os);
}
5 Przeorganizować sposób składowania danych w klasie Grupa
int*tab=new int[100]
for(int i=0;i<100;i++)tab[i]=2*i+1;
delete []tab;
Student*tab=new Student[100];
for(int
i=0;i<100;i++)tab[i].write(os);
delete []tab;
class
Grupa
{
public:
Student*tablica;
int liczba;
// liczba studentów
int size; // rozmiar tablicy
Grupa();
};
Grupa::Grupa(){
size=100;
tablica =
new Student[size];
liczba =
0;
}
Usuń stałą SIZE i sprawdź wszystkie funkcje!
5.a Destruktor
Group::~Group(){
if(tablica)delete []tablica;
}
5.c. Dodaj funkcję w klasie Student, która zwróci true, jeśli jeden z atrybutów zawiera tekst key.
bool match(const
char*key)const;
strstr
strnicmp
5.d Dodaj funkcję w klasie Grupa
bool
select(Grupa&wynik, const char*key)
5.e Zintegruj program.
1) odczytaj grupę z pliku
2) wprowadź szukany tekst
3) wypisz grupę powstałą w wyniku operacji select na ekranie oraz do pliku