niedziela, 29 grudnia 2013

C++ overloading operators part 2 /przeładowanie operatorów cześć 2

Table overloading :)
______________________________________
#include <iostream>
using namespace std;
class B
{
public:
    B(int a=1,int b=2){tab[0]=a; tab[1]=b;}
    int& operator[](int ninja );
    void write();
private:
    int tab[2];
};
void B::write()
{
    cout<<"tab1="<<tab[0]<<" tab2="<<tab[1]<<endl;
}
int& B::operator[](int ninja)
{
    return tab[ninja];
}
int main()
{
    cout << "start" << endl;
    B o1,o2(44,55);
    o1.write();
    o2.write();
    o1[0]=12;
    o1.write();
    cout<< "o1[0]="<<o1[0];
    return 0;
}

C++ overloading operators part 1 /przeładowanie operatorów cześć 1

This is simple, when You see it;
________________________________
#include <iostream>
using namespace std;
class A
{
public:
    A(int X=0,int Y=0):x(X),y(Y){}    //construktor conversions
    void write();
    A operator +(const A &o1);
    A operator ++ (int);
    A operator ++ ();
    A operator +=(const A &o1);
    A operator =(const A &o1);
   int operator ==(const A &o1);
private:
    int x,y;

};
void A::write()
{
    cout<<"x="<<x<<" y="<<y<<endl;
}

A A::operator +(const A &o1)    //           1
{
    A result;
    result.x=x+o1.x;
    result.y=y+o1.y;
    return result;
}
A A::operator ++(int)          //          2
{
    x++;
    y++;
    return *this;

}
A A::operator ++()              //          3
{
    ++x;
    ++y;
    return *this;
}
A A::operator +=(const A &o1)  //  4
{
    x+=o1.x;
    y+=o1.y;
     return *this;
}
A A::operator =(const A &o1)   // 5
{
    x=o1.x;
    y=o1.y;
    return *this;
}
int A::operator==(const A &o1)    //6
{
    if(x!=o1.x) return 0;
    else if(y!=o1.y) return 0;
    else return 1;
}
int main()
{
    A o1(1,2),o2(10,20);
    A o3=o1+o2;
    cout << " (1) o3=o2+o1" << endl;
    o3.write();
    cout << " (2) o1++" << endl;
    o1++;
    o1.write();
    cout << " (3) ++o1" << endl;
    ++o1;
    o1.write();
    cout << " (4) o1+=o2" << endl;
    o1+=o2;
    o1.write();
     cout << " (5) o5=o1" << endl;
    A o5=o1;
    o5.write();
     cout << " (6) test ==" << endl;
     if(o5==o1) cout << " SUCCES" << endl;
     if(o5==o2) {}
     else cout << " SUCCES" << endl;
    return 0;
}

C++ class conversion / c++ konwersja

Next step- conversion
________________________________________
#include <iostream>
using namespace std;
class A
{
public:
    A(int X=0,int Y=0):x(X),y(Y){}    //construktor conversions
    void write();
    operator int(){return (x+y)/2;}  //conversion operator
private:
    int x,y;
friend A add(A,A);
};
void A::write()
{
    cout<<"x="<<x<<" y="<<y<<endl;
}
A add(A a1,A a2)
{
    A a3;
    a3.x=a1.x+a2.x;
    a3.y=a1.y+a2.y;
    return a3;
}
int dual(int number)
{
    return 2*number;
}
int main()
{   A o1(1,2),o2(6,4),o3(1);
    //construktor`s test of correctness
    cout << "o1" << endl;
    o1.write();
    cout << "o2" << endl;
    o2.write();
    cout << "o3" << endl;
    o3.write();
    //normal use
    A o4=add(o1,o2);
    cout << "o4=o1+o2" << endl;
    o4.write();
    A o5=add(o1,5);
    //construktor conversions
    cout << "o5=o1+5" << endl;
    o5.write();
    cout<<"dual="<<dual(6)<<endl;
    //conversion operator
    cout<<"dual o2="<<dual(o2)<<endl;
    return 0;
}


sobota, 28 grudnia 2013

C++ class pointers / c++ klasy wskazniki

I started to study for exams, so write and write :)
___________________________________________
#include <iostream>

using namespace std;
class A
{
public:
    A():x(0),y(0){}
    A(int X,int Y);
    void write();
    int A::*wsk1;
    int (A::*w)();
    int go_to_y();
    int(A::*t[1])();
private:
    int x,y;

};
//definitions
A:: A(int X,int Y):x(X),y(Y)
{
    wsk1=&A::x;
    w=&A::go_to_y;
    t[0]=&A::go_to_y;
}
int A::go_to_y()
{
    return y;
}
void A::write()
{
    cout<<"x="<<x<<" y="<<y<<endl;
}
int main()
{
    cout << "Simple class" << endl;
    A o1;
    A o2(1,2);
    cout<<"Write basic"<<endl;
    o1.write();
    cout<<"Write with arguments"<<endl;
    o2.write();
    cout<<"Pointer to x="<<o2.wsk1<<endl;
    cout<<"Pointer to function y="<<o2.w<<endl;
    cout<<"Pointer to function y="<<o2.t[0]<<endl;

    return 0;
}

piątek, 27 grudnia 2013

c++ podstawowa klasa

Sesja tuz tuz wiec trzeba trochę poćwiczyć obiektowe programowanie. Na początek napisałem coś prostego:
-konstruktory: domyślny, normalny i kopiujący
-destruktor
-static zmienna
-funkcja zaprzyjazniona
_________
#include <iostream>
using namespace std;
class moja
{
public:
    moja();
    moja(double,double);
    moja(moja &wzor);
    ~moja();
    void wypisz();
private:
    double a,b;
    static double s;
friend double dodanie(moja &ta);
};
double moja::s=5;  //statyczny
//konstruktory i destruktory
moja::moja():a(0.0),b(0.0){}
moja::moja(double A,double B):a(A),b(B){}
moja::moja(moja &wzor)
{
a=wzor.a;
b=wzor.b;
}
moja::~moja()
{
    cout<<"Destrukcja"<<endl;
}
//funkcje
void moja::wypisz()
{
cout<< "a="<<a<<" b="<<b<<" s="<<s<<endl;
}

double dodanie(moja &ta)
{
return ta.a+ta.b;
}
int main()
{

    moja o1;
    o1.wypisz();
    moja o2(1.2,3);
    o2.wypisz();
    moja *wsk;
    wsk=&o2;
    wsk->wypisz();
    delete wsk;
    cout<<"Dodanie="<<dodanie(o2)<<endl;
    moja o3(o2);
    o3.wypisz();
    return 0;
}
________________________

C - matrix (macierz)

Tworzenie macierzy n x n i wypełnianie ja pseudolosowymi wartosciami:
___________________________________________________________
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*
               PSEUDO DRAW
               WRITE BY MIKOXP

*/
int n=15;  // range of size n ==>(1,25)  display size of the screen
int **tab=NULL;    //pointer array
int i,j;           //auxiliary
void draw(int **tab,int n)
{
    int i,j;
    srand( time( NULL ) );
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            tab[i][j]=rand()%100;

}
void allocation(int n)
{
    int i;
    tab=(int**) malloc(n * sizeof(int*));
    for(i=0;i<n;i++)
        tab[i] = (int*) malloc(sizeof(int) * n);
}
void vision(int n,int **tab)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
            printf("%d ",tab[i][j]);
        printf("\n");
    }
}
int main()
{
//allocation memory
allocation(n);
//draw n number from 0 to 99
draw(tab,n);
//verification
vision(n,tab);
system("pause");
    return 0;
}

czwartek, 26 grudnia 2013

Python Euklides

Kolejne kody z Pythona:

Klasyka euklides czyli Najmniejszy wspólny dzielnik:
_________________________________
a=input('Podaj a: ')
b=input('Podaj b: ')

while int(a)!=int(b):
    if int(a)>int(b):
        a=int(a)-int(b)
    else:
        b=int(b)-int(a)  
print (a)
________________________

Python-program

Sprawdza szybkość wyjatków
_____________________________________________
import time
def wyjatki(liczba_iteracji):
   
   start = time.time()
   slownik = {'KLUCZ': 123}
   for i in range(0, liczba_iteracji):
       
           if slownik:
               pass
       
   end = time.time()
   return end-start

czas=wyjatki(1000000)
czas=czas*1000
print ("czas= "+str(czas)+" ms")
___________________________________________

Python- prosty, troche wkurzajacy ale warto sprobowac

Nie lubie go ale dobry bo prosty, opisik skopiowany:


Python jest interpretowanym językiem programowania tzw. bardzo wysokiego poziomu, który pozwala na błyskawiczne tworzenie nawet najbardziej skomplikowanych systemów informatycznych i aplikacji. Jego wielkimi zaletami są oszczędność składni - kod źródłowy napisany w Pythonie jest o wiele krótszy niż w językach pokroju C, C++, C#, Java, a także bogata biblioteka standardowa.

Interpreter języka działa na wszystkich bardziej znanych systemach operacyjnych. Powstały również wersje, które umożliwiają uruchamianie programów pythonowych na wirtualnej maszynie Javy (Jython) jak i w środowisku Microsoft .NET Framework (IronPython).


Do pobrania 3.33 : 

                                                                             http://www.programosy.pl/program,python.html
Bez instalacji wersja 2.7 z trochę inna składnią:
                                                                              http://www.codeskulptor.org/

Hello czyli cześć

Założyłem tego bloga bo mi się nudziło ok. Jestem studentem informatyki więc coś tam umiem są lepsi i z takimi chce nawiązać kontakt. Pisze w c ( ostatnio nawet z ++) w Pythonie oraz kiedyś w Pascalu tym dinozaurze. Nie znam ani Javy ani Html-a ani PHP ani  się nauczyć ich mi nie po drodze wiec wybaczcie. Bede wrzucał prace projekty i takie duperele możne komuś pomogę.