w5.pdf

(187 KB) Pobierz
#include <iostream>
using namespace std;
int main()
{
cout<< "Podaj a" <<endl;
double a;
cin>>a;
cout<< "Podaj b" <<endl;
double b;
cin>>b;
cout<< "Podaj c" <<endl;
double c;
cin>>c;
...
Wykad 5
1
Funkcje (metody)
2
3
abc.cc
#include <iostream>
using namespace std;
int main()
{
double a=czytaj( "Podaj a" );
cout<< "Podaj b" <<endl;
double b;
cin>>b;
cout<< "Podaj c" <<endl;
double c;
cin>>c;
...
#include <iostream>
using namespace std;
int main()
{
double a=czytaj( "Podaj a" );
double b=czytaj( "Podaj b" );
cout<< "Podaj c" <<endl;
double c;
cin>>c;
...
1
1
2
2
3
3
abc.cc
abc.cc
#include <iostream>
using namespace std;
double czytaj( const char * polecenie)
{
cout<<polecenie<<endl;
double d;
cin>>d;
return d;
}
int main()
{
double a=czytaj( "Podaj a" ),
b=czytaj( "Podaj b" ),
c=czytaj( "Podaj c" );
#include <iostream>
using namespace std;
int main()
{
double a=czytaj( "Podaj a" );
double b=czytaj( "Podaj b" );
double c=czytaj( "Podaj c" );
...
1
nazwa
2
argumenty
rezultat
3
abc.cc
abc.cc
757827204.045.png 757827204.046.png 757827204.047.png 757827204.048.png 757827204.001.png 757827204.002.png 757827204.003.png 757827204.004.png 757827204.005.png
 
double czytaj( const char * polecenie)
{
double d;
while ( true )
{
cout<<polecenie<<endl;
cin>>d;
if (!cin)
{
if (cin.eof())
{
cout<< "Koniec danych?! No to cze!" <<endl;
exit( 0 );
}
cout<< "To nie bya warto double!" <<endl;
cin.clear();
string s;
cin>>s;
continue ;
}
if (d< 0 )
{
cout<< "Ujemna warto!" <<endl;
continue ;
}
break ;
}
return d;
}
double czytaj( const char * polecenie)
{
double d;
while ( true )
{
cout<<polecenie<<endl;
cin>>d;
if (!cin)
{
cout<< "To nie bya warto double!" <<endl;
cin.clear();
string s;
cin>>s;
continue ;
}
break ;
}
return d;
}
if (!cin)
{
if (cin.eof())
{
cout<< "Koniec danych?! No to cze!" <<endl;
exit( 0 );
}
cout<< "To nie bya warto double!" <<endl;
cin.clear();
string s;
cin>>s;
continue ;
}
if (d< 0 )
{
cout<< "Ujemna warto!" <<endl;
continue ;
}
% ./abc
Podaj a
sto
To nie bya warto double!
Podaj a
100
Podaj b
-20
Ujemna warto!
Podaj b
20
Podaj c
^D
Koniec danych?! No to cze!
%
Jzyki C/C++ w rodowisku systemu UNIX
g++ -o hello hello1.o hello2.o
hello1.o
abc.o
hello1.cc
Konsolidacja
abc.cc
Konsolidacja
g++ -c -o hello1.o hello1.cc
gcc -c -o hello2.o hello2.c
hello2.o
Kompilacja
czytaj.o
Kompilacja
hello2.c
czytaj.cc
hello
abc
pom1.o
Biblioteki
standardowe
pom2.o
757827204.006.png 757827204.007.png 757827204.008.png 757827204.009.png 757827204.010.png 757827204.011.png 757827204.012.png 757827204.013.png 757827204.014.png 757827204.015.png 757827204.016.png 757827204.017.png 757827204.018.png 757827204.019.png 757827204.020.png 757827204.021.png 757827204.022.png
 
#include <iostream>
using namespace std;
Kompilacja
abc.o
abc.cc
double czytaj( const char * polecenie);
int main()
{
double a=czytaj( "Podaj a" ),
b=czytaj( "Podaj b" ),
c=czytaj( "Podaj c" );
...
Plik
nagówkowy
czytaj.h
deklaracja
funkcji
czytaj.cc
czytaj.o
abc.cc
double czytaj( const char * polecenie);
czytaj.h
#include <iostream>
#include "czytaj.h"
using namespace std;
Argumenty
domylne
int main()
{
double a=czytaj( "Podaj a" ),
b=czytaj( "Podaj b" ),
c=czytaj( "Podaj c" );
...
abc.cc
double czytaj( const char * polecenie=
"Podaj warto" );
czytaj.h
#include <iostream>
#include "czytaj.h"
using namespace std;
Atrybut
static
int main()
{
double a=czytaj( "Podaj a" ),
b=czytaj(),
c=czytaj();
...
Domylne wartoci
argumentu polecenie.
abc.cc
757827204.023.png 757827204.024.png 757827204.025.png 757827204.026.png 757827204.027.png 757827204.028.png 757827204.029.png 757827204.030.png
#include <iostream>
using namespace std;
static
double czytaj( const
char * polecenie)
{
cout<<polecenie
<<endl;
double d;
cin>>d;
return d;
}
double czytaj( const
char * polecenie=
"Podaj warto" ); czytaj.h
Atrybut static gwarantuje
wyczno w korzystaniu
z funkcji w pliku, w którym
zostaa zdefiniowana.
#include <iostream>
#include "czytaj.h"
using namespace std;
Nazw funkcji moe by take
symbol operatora
int main()
{
double a=czytaj(),
b=czytaj(),
c=czytaj();
...
czytaj.cc
abc.cc
#include <iostream>
using namespace std;
void operator +(istream&, double d)
{
cout<< "Halo, tu plus! Ja nie sumuj tylko"
<<endl
<< "wypisuj warto prawego argumentu = "
<<d
<<endl;
}
int main()
{
double a= 2005 ;
cin+a;
return 0 ;
}
Wywoanie funkcji
przez nazw czytaj("Podaj a");
sqrt(M_PI);
na rzecz obiektu imie.size();
przez operator cin+a; cout<<
"Hello world!"
przy pomocy wskanika pf();
(&imie)->size();
(*p).f();
definiujc
funkcj
moemy nie
korzysta
z wybranych
argumentów
p->f();
stos
#include <iostream>
#include <limits.h>
using namespace std;
#include <iostream>
#include <limits.h>
using namespace std;
void minmax( int mn, int mx, int i)
{
if (i<mn) mn=i;
if (i>mx) mx=i;
}
int main()
{
int mn=INT_MAX, mx=INT_MIN, i;
while (cin>>i) minmax(mn,mx,i);
cout<< "min=" <<mn<< ", max=" <<mx<<endl;
return 0 ;
}
mn
mx
i
int main()
{
int mn=INT_MAX, mx=INT_MIN;
int i;
while (cin>>i)
{
if (i<mn) mn=i;
if (i>mx) mx=i;
}
cout<< "min=" <<mn<< ", max=" <<mx<<endl;
return 0 ;
}
% ./minmax
1
2
3
^D
min=2147483647, max=-2147483648
%
minmax.cc
minmax.cc
757827204.031.png 757827204.032.png 757827204.033.png 757827204.034.png
 
stos
stos
#include <iostream>
#include <limits.h>
using namespace std;
void minmax( int mn, int mx, int i)
{
if (i<mn) mn=i;
if (i>mx) mx=i;
}
int main()
{
int mn=INT_MAX, mx=INT_MIN, i;
while (cin>>i) minmax(mn,mx,i);
cout<< "min=" <<mn<< ", max=" <<mx<<endl;
return 0 ;
}
#include <iostream>
#include <limits.h>
using namespace std;
void minmax( int mn, int mx, int i)
{
if (i<mn) mn=i;
if (i>mx) mx=i;
}
int main()
{
int mn=INT_MAX, mx=INT_MIN, i;
while (cin>>i) minmax(mn,mx,i);
cout<< "min=" <<mn<< ", max=" <<mx<<endl;
return 0 ;
}
mn
mx
i
mn
mx
i
mn
mx
i
minmax.cc
minmax.cc
stos
#include <iostream>
#include <limits.h>
using namespace std;
void minmax( int mn, int mx, int i)
{
if (i<mn) mn=i;
if (i>mx) mx=i;
}
int main()
{
int mn=INT_MAX, mx=INT_MIN, i;
while (cin>>i) minmax(mn,mx,i);
cout<< "min=" <<mn<< ", max=" <<mx<<endl;
return 0 ;
}
mn
mx
i
Wskaniki - moliwo
przekazania wielu wartoci
minmax.cc
stos
stos
#include <iostream>
#include <limits.h>
using namespace std;
void minmax( int* pmn, int* pmx, int i)
{
if (i<*pmn) *pmn=i;
if (i>*pmx) *pmx=i;
}
int main()
{
int mn=INT_MAX, mx=INT_MIN, i;
while (cin>>i) minmax(&mn,&mx,i);
cout<< "min=" <<mn<< ", max=" <<mx<<endl;
return 0 ;
}
#include <iostream>
#include <limits.h>
using namespace std;
void minmax( int* pmn, int* pmx, int i)
{
if (i<*pmn) *pmn=i;
if (i>*pmx) *pmx=i;
}
int main()
{
int mn=INT_MAX, mx=INT_MIN, i;
while (cin>>i) minmax(&mn,&mx,i);
cout<< "min=" <<mn<< ", max=" <<mx<<endl;
return 0 ;
}
mn
mx
i
mn
mx
i
pmn
pmx
i
minmax.cc
minmax.cc
757827204.035.png 757827204.036.png 757827204.037.png 757827204.038.png 757827204.039.png 757827204.040.png 757827204.041.png 757827204.042.png 757827204.043.png 757827204.044.png
 
Zgłoś jeśli naruszono regulamin