Hyluss icon

Przeciazanie wektor

Hyluss | PRO | 07/02/16 06:51:50 PM UTC | 0 ⭐ | 413 👁️ | Never ⏰ | []
C |

1.82 KB

|

None

|

0 👍

/

0 👎

// przeciazaneioperatorow.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include <conio.h>
#include <malloc.h>
 
 
class Wektor
{
    int N;
    float *tab;
 
    public:
    
    Wektor::Wektor ()
    {
        tab = NULL;
        N = 0;
    }
 
    Wektor::Wektor (const int n, const float M)
    {
        N = n;
        tab = new float [N];
        if (tab !=NULL)
        {
            for (int i=0;i<N;i++)
            {
                tab[i] = M;
            }       
        }
    }
 
 
    Wektor::Wektor (const Wektor &wek)
    {
        N = 0;
        tab = (float*) malloc (wek.N*sizeof (float));
        if (tab !=NULL)
        {
            N = wek.N;
            for (int i=0;i<N;i++)
            {
                tab[i] = wek.tab[i];
            }
        }   
    }
 
    const int Rozmiar () const
    {
        return N;
    }
    
    const void Wyswietl() const
    {
        printf ("[");
 
        for (int i=0; i<N; i++)
        {
            if (i<N-1)
            {
                printf ("%5.2f, ", tab[i]);
            }
            
            else
            {
                printf ("%5.2f", tab[i]);
            }
 
        }       
        printf ("]");
    }
 
 
    Wektor operator += (const float a)
    {
        tab = (float*)realloc (tab, (N+1)*sizeof(float));   
        if (tab!=NULL)
        {
            tab[N] = a;
        }
        N++;
        return *this;
    }
 
    Wektor operator ~ ()
    {
        for (int i=0, j=N-1; i<=j;i++, j--)
        {
            float x;
            x = tab[i];
            tab[i] = tab[j];
            tab[j] = x;
    //  printf ("tab[%d] - %2.2f, tab[%d] - %2.2f\n",i,tab[i],j,tab[j]);
        }   
        return *this;
    }
 
};
 
int _tmain(int argc, _TCHAR* argv[])
{
    Wektor w1;
    Wektor w2 (3, 10.0);
    printf ("w1 {%2d} = ", w1.Rozmiar() );
    w1.Wyswietl();
    printf ("\nw2 {%2d} = ", w2.Rozmiar() );
    w2.Wyswietl();
    Wektor w3 = w2;
    
    w3 += 1.11;
    w3 += 2.22;
    w3 += 3.33;
    w3 += 4.44;
    w3 += 5.55;
 
    printf ("\nw3 {%2d} = ", w3.Rozmiar() );
    w3.Wyswietl();
    printf ("\n~w3 = ");
 
    (~w3).Wyswietl();
    printf ("\nw3 {%2d} = ", w3.Rozmiar() );
    w3.Wyswietl();
    printf ("\nKoniec");
 
    _getch();
    return 0;
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎