Treceți la conținutul principal

clasa template vector cpp

#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>

#define TRUE 1
#define FALSE 0



template<class T>class vector
{   
 unsigned int nr;
 friend ostream &operator<<(ostream &,vector &);
 friend ofstream &operator<<(ofstream &,vector &);
    friend istream &operator>>(istream &,vector &);
    friend ifstream &operator>>(ifstream &,vector &);
 

  public:
  T *pv;
  vector(unsigned int );
  vector(vector &);
  ~vector()
  {
        delete[nr] pv;
  }
     void sort(bool(*)(vector<T> &,int ,int )); 
  
};
 


template<class T> vector<T>::vector(unsigned int n=1)
{         unsigned int i;
    nr=n;
    pv=new T[nr];
    cout<<"\n Introduceti elementele vectorului: ";
          cout<<"\n";
    for(i=0;i<nr;i++)
    {
     cout<<"v["<<i+1<<"]:";
     cin>>pv[i];
     cout<<"\n";
    }
}


template<class T>vector<T>::vector(vector &v)
  {
      unsigned int i;
   nr=v.nr;
   pv=new T[v.nr];
         for(i=0;i<v.nr;i++)
       pv[i]=v.pv[i];   
  }


template<class T> ostream &operator<<(ostream &k,vector<T> &v)
 {
  unsigned int i;
   for(i=0;i<v.nr;i++)
   {
    k<<"\n";
    cout<<"Elementul"<<" "<<i+1<<":";
         
            k<<v.pv[i];
   }
 return k;
  }


template<class T> istream &operator>>(istream &k,vector<T> &v)
   {
   unsigned int i;
       for(i=0;i<v.nr;i++)
           k>>v.pv[i];
    return k;
   }


template<class T> ifstream &operator>>(ifstream &k,vector<T> &v)
   {
   unsigned int i;
       for(i=0;i<v.nr;i++)
           k>>v.pv[i];
    return k;
   }


template<class T> ofstream &operator<<(ofstream &k,vector<T> &v)
 {
  unsigned int i;
   for(i=0;i<v.nr;i++)
   {
        k<<"\n";
            k<<v.pv[i]; 
   }
 return k;
  }

  
template<class T>bool crescator(vector<T> &g,int i,int j)
  {
      return g.pv[i]<g.pv[j] ? TRUE:FALSE;     
      
  }


template<class T>bool descrescator(vector<T> &g,int i,int j)
  {
      return g.pv[i]>g.pv[j] ? TRUE:FALSE;     
      
  }


template<class T>void comuta(vector<T> &v,int i,int j)
{
 T aux;

   aux=v.pv[i];
      v.pv[i]=v.pv[j];
   v.pv[j]=aux;
}    


template<class T>void vector<T>::sort(bool(*sens)(vector<T> &g,int i,int j))
   {
 unsigned int k,l;
    for(k=0;k<nr-1;k++)
    for(l=k+1;l<nr;l++)
     if(!(*sens)(*this,k,l))
      comuta(*this,k,l);
   }



class persoana
   {
    char nume[20];
    friend class vector;
    friend ostream &operator<<(ostream &,persoana &);
    friend istream &operator>>(istream &,persoana &); 
 friend int operator<(persoana &,persoana &);
    friend int operator>(persoana &,persoana &);

   public:
    
     
    persoana(char n[20]="Anonim")
    {  
     strcpy(nume,n);
               
    }
};


  ostream &operator<<(ostream &k,persoana &p)
   {   cout<<"\n";
    k<<p.nume;
    return k;
  }


  istream &operator>>(istream &k,persoana &p)
  {
    k>>p.nume;
    cout<<"\n";
    return k;
   }

  
  int operator<(persoana &p1,persoana &p2)
  {  
   if((strcmp(p1.nume,p2.nume)<=0))
    return 1;
        else
      return 0;
  }

  
  int operator>(persoana &p1,persoana &p2)
  {  
   if((strcmp(p1.nume,p2.nume)>=0))
    return 1;
        else
      return 0;
  }





 
 void main(void)
  { 
 
   int k;
   printf("k=");
   scanf("%d",&k);
   vector<persoana> v1(k);
   vector<persoana> v2(v1);
   cout<<v1<<"\n"<<"SORTARE CRESCATOARE";
   v2.sort(crescator);
   cout<<v2<<"\n"<<"SORTARE DESCRESCATORE";
   v2.sort(descrescator);
   cout<<v2<<"\n";

  }
  
 

Comentarii

Postări populare de pe acest blog

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='+'…

NUMERE PRIME ALGORITM C++

// NUMERE PRIME ALGORITM C++//  reediting from scratch //on this page is just the study for a next algoritm for generating the parime nr series like Fibonnaci or ....if possibile

74111121313417374414124343447 if u know the red part you can generate the orange part
1 0 1 111112222 1 1 23

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…