Treceți la conținutul principal

vectori template cpp

//************************************************
//***********CLASA TEMPLATE VECTOR*****************
//*************************************************
//Postoarca Costinela, gr.1051
//#include "stdafx.h"
#include "iostream.h"
#include "conio.h"
#include "fstream.h"
#include "stdlib.h"

template<class T>class vector
{
 T *pv;
 int dim;
 friend int cresc(T a,T b) {return a>b?0:1;}
 T suma2();
 T suma();
 T prscalar(vector<T>);
public:
 vector(int);
 vector(vector<T>&);
 ~vector() {delete[] pv;}
 T&operator[](int i){return pv[i];}
 int operator!();
 vector<T>operator+(vector<T>);
 friend ostream&operator<<(ostream &,vector<T>&);
 friend istream&operator>>(istream &,vector<T>&);
 ofstream&operator<<(ofstream&);
 ifstream&operator>>(ifstream&);
 vector<T>*operator+=(int);
 int operator==(vector<T>&);
 int operator!=(vector<T>&);
 int operator<(vector<T>&);
 int operator>(vector<T>&);
 operator vector<T>() {return *pv;}
 void sort(int(*cresc)(T,T));
 void regr_lin(vector<T>,double&,double&);
};
//constructorul clasei
template<class T> vector<T>::vector(int n)
{
 pv=new T[n];
 dim=n;
 for(int i=0;i<dim;i++)
  pv[i]=0;
}
//constructor de copiere
template<class T> vector<T>::vector(vector<T> &v)
{
 for(int i=0;i<dim;i++)
  v.pv[i]=pv[i];
}
//popularea vectorului(incarcarea operatorului >> pe fisier standard)
template<class T>istream& operator>>(istream &is,vector<T> &v)
{
 for(int i=0;i<v.dim;i++)
 {
  cout<<"\n\t elem_"<<i<<":";
  is>>v.pv[i];
 }
return is;
}
//incarcarea operatorului>>pe disc
template<class T>ifstream&vector<T>::operator>>(ifstream&intr)
{
 for(int i=0;i<dim;i++)
 {
  cout<<"\n\t elem_"<<i<<":";
  intr>>pv[i];
 }
return intr;
}
//afisarea elementelor vectorului(incarcarea operatorului << pe fisier standard)
template<class T>ostream& operator<<(ostream&os,vector<T> &v)
{
 os<<"\n\n";
 for(int i=0;i<v.dim;i++)
  os<<"\t"<<v.pv[i]<<" ";
return os;
}
//incarcarea operatorului<<pe disc
template<class T>ofstream& vector<T>::operator<<(ofstream&ies)
{
 ies<<"\n\n";
 for(int i=0;i<dim;i++)
  ies<<"\t"<<pv[i]<<" ";
return ies;
}
//incarcarea operatorului !
template<class T>int vector<T>::operator !()
{
 int i=0;
 while(i<dim&&pv[i]==0) i++;
 if(i==dim) return 0;
 else return 1;
}
//incarcarea operatorului +
template<class T>vector<T> vector<T>::operator +(vector<T> v1)
{
 vector<T> v(dim+v1.dim);
 int i;
 for(i=0;i<dim;i++)
  v[i]=pv[i];
 for(i=0;i<v1.dim;i++)
  v[i+dim]=v1[i];
return v;
}
//incarcare operator +=(concatenare vector cu un element)
template<class T>vector<T>* vector<T>::operator+=(int n)
{
 pv[dim]=n;
 dim++;
 return this;
}
//incarcarea operatorului ==(verificarea identitatii a doi vectori)
template<class T>int vector<T>::operator ==(vector<T> &v)
{
 int i=0;
 if(dim!=v.dim) return 0;
 else
 {
  while(i<dim&&pv[i]==v[i]) i++;
  if(i==dim) return 1;
  else return 0;
 }
}
//incarcare operator!=
template<class T>int vector<T>::operator!=(vector<T> &v)
{
 int i=0;
 if(dim!=v.dim) return 1;
 while(i<dim&&pv[i]==v[i]) i++;
 if(i==dim) return 0;
 else return 1;
}
//incarcare operator<
template<class T>int vector<T>::operator<(vector<T>&v)
{
 if(dim<v.dim) return 1;
 else return 0;
}
//incarcare operator>
template<class T>int vector<T>::operator>(vector<T>&v)
{
 if(dim>v.dim) return 1;
 else return 0;
}
//functie de sortare vector
template<class T>void vector<T>::sort(int(*cresc)(T,T))
{
 T aux;int i,j;
 for(i=0;i<dim;i++)
  for(j=i;j<dim;j++)
   if(!(*cresc)(pv[i],pv[j]))
    {aux=pv[i];pv[i]=pv[j];pv[j]=aux;}
}
template<class T> T vector<T>::suma2()
{
 T s=0;
 for(int i=0;i<dim;i++)
 s+=(pv[i]*pv[i]);
 return s;
}
template<class T> T vector<T>::suma()
{
 T s=0;
 for(int i=0;i<dim;i++)
 s+=pv[i];
 return s;
}
template<class T>T vector<T>::prscalar(vector<T>v)
{
 T p=0;
 for(int i=0;i<dim;i++)
 p=p+pv[i]*v[i];
 return p;
}

//functie pentru calculul regresiei liniare
template<class T>void vector<T>::regr_lin(vector<T>v,double&a,double&b)
{
 T sa,sb,sa2,sb2,p;
 sa=suma();
 sb=v.suma();
 sa2=suma2();
 p=prscalar(v);
 a=(sa2*sb-sa*p)/(dim*sa2-sa*sa);
 b=(dim*p-sa*sb)/(dim*sa2-sa*sa);
}

void main(void)
{
 int dim1,dim2;
 cout<<"\n\t Dimensiunea 1:";cin>>dim1;
 cout<<"\n\tDimensiunea 2:";cin>>dim2;
 vector<int> v1(dim1),v3(4);
 vector<int> v2(dim2);
 cin>>v1;cin>>v2;
 cout<<"\n\tVector initial:";cout<<v1;
 v1.sort(cresc);
 cout<<"\n\n\tVector sortat:";cout<<v1;
/* ifstream fisin;
 fisin.open("fis.dat",ios::in);
 if(!fisin) {cout<<"\n\tFisier negasit!!";exit(2);}
 v3>>fisin;v3<<cout;
 fisin.close();
 ofstream fisout;
 fisout.open("fis.dat",ios::out);
 if(!fisout){cout<<"\n\tEroare la deschidere fisier!!";exit(1);}
 v1<<fisout;
 fisout.close(); */
 !v1;cout<<"\n";!v2;vector<int> v(dim1+dim2);
 v=(v1+v2);cout<<"\n\tVectorul rezultat este:";cout<<v;
 v1+=10;
 if(v1!=v2) cout<<"\n\tVectorii nu sunt identici!!";
 else cout<<"\n\tVectori identici!!";
getch();
}

Comentarii

Postări populare de pe acest blog

WINDOWS 10 COMPUTER FREEZING PROBLEM SOLVED

good news : a BIOS UPDATE can resolve the problem but just for a Windows 7 on 64 bits o.s. and the system is not stable all the time. even after  bios update the system can freeze.
new info : u can try to low the screen brightness and see if this error appear so often after 
news: last info !!! maybe a virus. scann our system now with an antivirus i generate this error using other device ( a tablet pc) connected in the same network and the laptop i have this problem just freeze  http://thehackernews.com/2013/10/backdoor-found-in-chinese-tenda.html

news : if u use a tenda router this make couse all this problems



what i discover so far :
1.the electric company have many failure and affect the main ISP router/switch for building  also the router/switch installed by the ISP may be affected by overheating and will crash after a long utilisation on heat conditions 2.the router/switch of ISP affect any router of the user between this router and pc/laptop of client 3.the router and any other device of t…

program principal cpp

#include "clasa.h" #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> #define DELAY 9000000 void delay() { for(long i=0;i<DELAY;i++); } //constructor cu initializare de la tastatura BigInt::BigInt() {char x; signed char t[400]; int i; printf("\nNumarul cu semn "); do s=getche(); while((s!='+')&&(s!='-')); n=0; do {x=getche(); t[n]=x-'0'; n++; } while((x>='0')&&(x<='9')); n--; for(i=0;i<n;i++) nr[i]=t[n-i-1]; } //constructor cu initializare prin parametri BigInt::BigInt(char semn,signed char numar[],int dim) {int i; s=semn; n=dim; for(i=0;i<n;i++) nr[i]=numar[n-i-1]; } //transform un int negativ in pozitiv int BigInt::Pozitiv(int x) {int a,vb; a=0; vb=0; while(vb==0) if((x+a)==0) vb=1; else a=a+1; x=a; return x; } //constructor dintr-un nr int obisnuit BigInt::BigInt(int x) {int i; if(x>=0) s='+'…

o aplicatie php localitati romania

//APLICATIA SE REFERA LA BAZA DE DATE SIRUTA

//dragtable.js


/* dragtable v1.0 June 26, 2008 Dan Vanderkam, http://danvk.org/dragtable/ http://code.google.com/p/dragtable/ \Bsortabledraggable\B Instructions: - Download this file - Add <script src="dragtable.js"></script> to your HTML. - Add class="draggable" to any table you might like to reorder. - Drag the headers around to reorder them. This is code was based on: - Stuart Langridge's SortTable (kryogenix.org/code/browser/sorttable) - Mike Hall's draggable class (http://www.brainjar.com/dhtml/drag/) - A discussion of permuting table columns on comp.lang.javascript Licensed under the MIT license. */ // Here's the notice from Mike Hall's draggable script: //***************************************************************************** // Do not remove this notice. // // Copyright 2001 by Mike Hall. // See http…