background image

Podstawowy program: 
#include <iostream> 
using namespace std; 
int main() 

    return 0; 

For 
for(int i=0; i<Max; ++i) 

nie ma ; bo pominie i wykona tylko funkcje w {}, ale wartość i = Max

 

    { 
        cout<<tab[i]<<endl; 
    } 
while 

while (x < 5) 

jak nie ma ; to pętla sie nieskończy

 

    cout<<x<<endl; 

    x++; 

do 
do 
{cout<<i<<endl; 
i++;} while(i<x); 
Pobieranie danych z pliku: 
#include <fstream> 
    string tab[Max]; 
    ifstream plik; 
        plik.open("zespolona.cpp"); 
    for(int i = 0; i < Max; ++i) 
    { 
        getline(plik, tab[i]); 
        cout<<tab[i]<<endl; 
    } 
        plik.close(); 
   return 0; 
zapisywanie do pliku 
#include <fstream> 
     ofstream plik; 
        plik.open("plik.txt"); 
    for(int i = 0; i < Max; ++i) 
    { 
        plik<<i<<"lol"; 
    } 
        plik.close(); 
return 0; 
Implementacja funkcji: 
w funkcje.h 
extern void zamien (float&, float&); 
w funkcje.cpp 
void zamien (float &x, float &y) 
w main lub innych plikach implementacyjnych 
zamien(x,y); 

background image

Consty 
#include "funkcje.h" 
..... 
using namespace std; 
const int Max=15;  

stała liczba

 

int f(float x, float y) //od najmniejszego do nawiekszego  

stała funkcja obowiązująca cały prog

 

{    return x<y ? 0 : 1;} 
int main()..... 
Tablice statyczne 
float * tab;//deklaracja tablicy - wlasciwie wskaznika, zeby z wskaznika zrobic tablice trzeba 
tab=new float[Max];//deklaracja tablicy o rozmiarze Max i polach rzeczywistych 
    init(tab,Max);//nazwa tablicy, liczba elementow tablicy 
    print(tab,Max); 
  float tablica[10];  

działa tak jak to na poczatku 

    return 0; 

tablica indexowana jest od 0! 

cout<<tab[1]; 

wypisuje 2 el tablicy 

cout<<*tab;

 wypisuje 1 el tablicy

   

sortowanie 
void sortowanie(int  tab, int Max, int (*r)(T,T)) 

    for(int i=0; i<Max; i++) 
    { 
        for(int j=0;j<Max; j++) 
            if(r(tab[j],tab[j+1])) 
            zamien(tab[j],tab[j+1]); 
    } 

sortuje tablicę bąbelkowe 

struktura 
zespolone.h 
#ifndef SRTUKTURY_H_INCLUDED 
#define SRTUKTURY_H_INCLUDED 
struct zespolona 

 float re; 
 float im; 
 zespolona(); //konstruktor 
zespolona& operator=(const zespolona&); 
 ~zespolona(); //destructor 
}; 
#endif // SRTUKTURY_H_INCLUDED 
w zespolone.cpp: 
zespolona:: zespolona() 
{ re=0; 
 im=0;}.... 
zespolona& zespolona::operator=(const zespolona& z) 
{ this->re=z.re; 
 this->im=z.im; 
 return *this;} 
 
 

background image

klasa 
zespolone.h 
#ifndef ZESPOLONA_H 
#define ZESPOLONA_H 
class Zespolona 

    public: 
        Zespolona(); 
        Zespolona(float,float); 
        float Re() const ; 
        float Im() const; 
        void print() const; 
        ~Zespolona(); 
    protected: 
    private: 
        float x; 
        float y; 
}; 
#endif // ZESPOLONA_H 
w zespolone.cpp 
Zespolona::Zespolona() 
{ this->x=0; 
 this->y=0;} 
Zespolona::~Zespolona() 
{ x=0; y=0;} 
void Zespolona::print() const 
{cout<<x<<"+i"<<y<<endl;} 
float Zespolona::Re() const 
{ return x;} 
float Zespolona::Im() const 
{ return y;} 
w main Zespolona w(2,2); 
template 
template <class T, class Q> 
void zamien (T& x, Q& y) 
{T temp; 
temp=x; 
x=y; 
y=temp;} 
... template <class T> extern 
void init (T*,int);... 
template <class T> 
void init(T* tab, int Max).{}... 
operator tenarny 
(x<y) ? 1 : 0; 
warunek ? 1spełniony : 0niespełniony; 
This 
zespolona& zespolona::operator=(const zespolona& z) 
{ this->re=z.re;   

this reprezentuje z z nagłówka funkcji. this wskazanie na re=z.re 

 this->im=z.im; 
 return *this;} 

background image

wariadyczne 
funkcje.cpp 
#include <stdarg.h> 
int suma_(int first, ...) //funkcja variadyczna 
{va_list list; //lista zapamietujaca argumenty naszej funkcji 
va_start(list, first);//kopjuje arg naszej funkji od pierwszego 
int t1,t2; 
t1=first; //pierwszy arg który potem sie przesuwa 
t2=va_arg(list,int);//kolejny arg 
if(t2==-1) 
{    return t1;} 
t2=t2+t1; 
while (true) 
{    t1=va_arg(list, int); 
if(t1==-1)  {return t2;} 
    t2=t2+t1;}} 
funkcje.h extern int suma_(int, ...); 
main  cout << suma_(2,2,3,-1) << endl; 
Operatory 
const Zespolona operator+(const Zespolona& z,const Zespolona& w) 
{ return Zespolona(z.Re()+w.Re(),z.Im()+w.Im());} 
ostream& operator<<(ostream& wy,const Zespolona& z) 
{ wy<<z.Re()<<"+i"<<z.Im(); 
 return wy;} 
Zaprzyjaźnianie funkcji 
class zespolona 
{    friend istream& operator>>(istream& , zespolona&); 
public:..... 
w operatory.cpp  
istream& operator>>(istream& we, zespolona& z) 
{    we>>z.x; 
    we>>z.y; 
    return we;} 
Cacht Try Throw 
void f (string* tab, int Max) 
{    ifstream plik; 
    try{    plik.open("funkcje.h");} 
    catch(...) 
    {        cout<<"blad"; 
    throw;} 
    for(int i=0; i<Max;++i) 
    {        getline(plik,tab[i]);} 
     try{    plik.close();} 
    catch(...) 
    {        cout<<"blad"; 
  throw; }} 
długość ciągu znaków 
for(int k=1;k<Max;k++) 
{    q=q+tab[k].length();} 
gdzie  tab to tablica z ciągami znaków 
 

background image

złożoność czasowa 
#include<time.h>//do mierzenia czasu algorytmu 
using namespace std; 
const int Max=15; 
int main() 
{    float tab[Max]; 
   time_t x,y; //mierzymy czas wykonania algorytmu przed posortowaniem 
    init(tab,Max);//nazwa tablicy, liczba elementow tablicy 
print(tab,Max); 
   x=time(0);//mierzymy czas wykonania algorytmu przed posortowaniem 
    sortowanie(tab,Max,&h); 
    y=time(0); // a tutaj czas wykonania algorytmu po posortowaniu 
    cout<<y-x<<endl; 
    return 0;} 
Switch 
float potega (float a, int p) 
{  switch(p) 
  { case 0: 
    return 1; 
    case 1: 
    return p; 
    default: 
        float temp=1; 
        for(int i=1; i<=p; i++) 
        { temp=temp*a;} 
  return temp;  } 
zmienna losowa 
#include <cstdlib> 
#include <time.h> 
template <class T> 
void init(T* tab, int Max) 
{srand(time(0)); 
for(int i=0; i<Max; ++i) 
{    tab[i]=(T)(rand() % 100);}} 
FUNKTORY 
class Funktor               // Deklaruje obiekt definiujacy typ sortowania. 
{    public: 
        Funktor() {} 
        virtual ~Funktor() {} 
        Funktor(const Funktor& other) {} 
        Funktor& operator=(const Funktor& other) { return *this; } 
        template<class T, class Q> 
          static int f(const T&x, const Q& y) 
            { return  x<y ? 1 : 0; } 
    protected: 
    private:  };