Hyluss

Przeciazanie wektor

Jul 2nd, 2016
448
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.82 KB | None | 0 0
  1. // przeciazaneioperatorow.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <conio.h>
  6. #include <malloc.h>
  7.  
  8.  
  9. class Wektor
  10. {
  11.     int N;
  12.     float *tab;
  13.  
  14.     public:
  15.    
  16.     Wektor::Wektor ()
  17.     {
  18.         tab = NULL;
  19.         N = 0;
  20.     }
  21.  
  22.     Wektor::Wektor (const int n, const float M)
  23.     {
  24.         N = n;
  25.         tab = new float [N];
  26.         if (tab !=NULL)
  27.         {
  28.             for (int i=0;i<N;i++)
  29.             {
  30.                 tab[i] = M;
  31.             }      
  32.         }
  33.     }
  34.  
  35.  
  36.     Wektor::Wektor (const Wektor &wek)
  37.     {
  38.         N = 0;
  39.         tab = (float*) malloc (wek.N*sizeof (float));
  40.         if (tab !=NULL)
  41.         {
  42.             N = wek.N;
  43.             for (int i=0;i<N;i++)
  44.             {
  45.                 tab[i] = wek.tab[i];
  46.             }
  47.         }  
  48.     }
  49.  
  50.     const int Rozmiar () const
  51.     {
  52.         return N;
  53.     }
  54.    
  55.     const void Wyswietl() const
  56.     {
  57.         printf ("[");
  58.  
  59.         for (int i=0; i<N; i++)
  60.         {
  61.             if (i<N-1)
  62.             {
  63.                 printf ("%5.2f, ", tab[i]);
  64.             }
  65.            
  66.             else
  67.             {
  68.                 printf ("%5.2f", tab[i]);
  69.             }
  70.  
  71.         }      
  72.         printf ("]");
  73.     }
  74.  
  75.  
  76.     Wektor operator += (const float a)
  77.     {
  78.         tab = (float*)realloc (tab, (N+1)*sizeof(float));  
  79.         if (tab!=NULL)
  80.         {
  81.             tab[N] = a;
  82.         }
  83.         N++;
  84.         return *this;
  85.     }
  86.  
  87.     Wektor operator ~ ()
  88.     {
  89.         for (int i=0, j=N-1; i<=j;i++, j--)
  90.         {
  91.             float x;
  92.             x = tab[i];
  93.             tab[i] = tab[j];
  94.             tab[j] = x;
  95.     //  printf ("tab[%d] - %2.2f, tab[%d] - %2.2f\n",i,tab[i],j,tab[j]);
  96.         }  
  97.         return *this;
  98.     }
  99.  
  100. };
  101.  
  102. int _tmain(int argc, _TCHAR* argv[])
  103. {
  104.     Wektor w1;
  105.     Wektor w2 (3, 10.0);
  106.     printf ("w1 {%2d} = ", w1.Rozmiar() );
  107.     w1.Wyswietl();
  108.     printf ("\nw2 {%2d} = ", w2.Rozmiar() );
  109.     w2.Wyswietl();
  110.     Wektor w3 = w2;
  111.    
  112.     w3 += 1.11;
  113.     w3 += 2.22;
  114.     w3 += 3.33;
  115.     w3 += 4.44;
  116.     w3 += 5.55;
  117.  
  118.     printf ("\nw3 {%2d} = ", w3.Rozmiar() );
  119.     w3.Wyswietl();
  120.     printf ("\n~w3 = ");
  121.  
  122.     (~w3).Wyswietl();
  123.     printf ("\nw3 {%2d} = ", w3.Rozmiar() );
  124.     w3.Wyswietl();
  125.     printf ("\nKoniec");
  126.  
  127.     _getch();
  128.     return 0;
  129. }
Comments
  • User was banned
Add Comment
Please, Sign In to add comment