Treceți la conținutul principal

matrice rara tempate cpp

// matriceraratempate.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include<stdio.h>
#include<iostream.h>
#include<malloc.h>
#include<iomanip.h>
#include <conio.h>
#include<stdlib.h>
#include<math.h>

template <class T>
class MatriceRara
{ 
  T l[10];// l reprezinta vectorul liniilor elementelor nenule
  T c[10];//c  reprezinta vectorul coloanelor elementelor nenule.
  T val[10]; //val reprezinta vectorul valorilor
  int dim;  //dim serveste pentru memorarea numarului de elemente nenule,  
  int m;  //cate linii are matricea
  int n; //cate coloane are matricea
public:MatriceRara();
       MatriceRara(int,int,int); //constructor
       MatriceRara( MatriceRara &); //constructor de copiere
       ~MatriceRara();   //destructor
    MatriceRara  operator+=(MatriceRara & ); //   operator pentru adaugarea unui triplet
    MatriceRara operator -=(MatriceRara & ); //operatorul pentru eliminarea unui operator
    MatriceRara  operator+(MatriceRara & );  //  operatorul care implementeaza operatia de adunare
    MatriceRara  operator-(MatriceRara & ); //operatorul pentru implementarea operatiei de sacdere
    MatriceRara operator *(MatriceRara &x); //operatorul pentru implementarea operatei de inmultire
    MatriceRara operator*(float *v);    //operatorul pentru implementarea operatiei de imnultire cu un vector.
    MatriceRara &operator =( MatriceRara &);  //operatorul de atribuire
    T operator[](int i) {return val[i];} //Operatorul [] pentru adresarea unui element 
  friend ostream & operator<<(ostream &, MatriceRara&); //operatorul pentru intrari standard de date
       friend istream & operator>>(istream &, MatriceRara&); //operatorul pentru iesiri standard de date
  int Nr_linii();  //determina numarul liniilor matricei
  int Nr_coloane(); //determina numarul coloanelor matricei
  float Grad();  //determina gradul de umplere
};

template <class T>
MatriceRara <T>::MatriceRara()
  { 
   

}
template <class T> 
MatriceRara <T>::MatriceRara(int ii,int jj,int iii)
  { 
   m=ii;
   n=jj;
   dim=iii;

 }
 
template <class T>
MatriceRara <T> MatriceRara <T>::operator +=(MatriceRara <T> & x) 
 { MatriceRara <T> nou(m,n,dim+1);


int lin,col; float va;

     for(int i=0;i<x.dim;i++)
  {  
   nou.l[i]=x.l[i];
   nou.c[i]=x.c[i];
   nou.val[i]=x.val[i];
  }
  cout<<"noua lin "<<endl;

           int vb=1;
                do
    {
                 cin>>lin;
     if (lin<x.m)
     {vb=0;
      cout<<"Gresit!Mai introduceti o data!\n";
     } 
     else {nou.l[dim]=lin;
           vb=1; 
      }
    }
    while(!vb);  

 cout<<"noua col "<<endl;
            vb=1;
                do
    {
                 cin>>col;
     if (col<x.n)
     {vb=0;
      cout<<"Gresit!Mai introduceti o data!\n";
     } 
     else {nou.c[dim]=col;
           vb=1; 
      }
    }
    while(!vb);  
 cout<<"noua val:  "<<endl; 
 do
    {
                 cin>>va;
     if (va==0)
     {vb=0;
      cout<<"Gresit!Valoare nenula\n";
     } 
     else {x.val[i]=va;
           vb=1; 
      }
    }
    while(!vb); 
// cin>>va;
nou.val[dim]=va;
*this=nou;

return *this;

 }

template <class T>
MatriceRara <T> MatriceRara <T>::operator -=(MatriceRara <T> & x) 
 { MatriceRara <T>  nou(m,n,dim-1);



     for(int i=0;i<x.dim-1;i++)
  {  
   nou.l[i]=x.l[i];
   nou.c[i]=x.c[i];
   nou.val[i]=x.val[i];
  }
  
*this=nou;

return *this;

 }
template <class T>
MatriceRara <T>::MatriceRara <T> ( MatriceRara <T> &x)
{
   m=x.m;
   n=x.n;
   dim=x.dim;
   for(int i=0;i<dim;i++)
   {
   l[i]=x.l[i];
   c[i]=x.c[i];
   val[i]=x.val[i];
   }
}

template <class T>
 MatriceRara <T>  MatriceRara <T>::operator +(MatriceRara <T> & x)

{ MatriceRara <T> p;
  int i,j,k;
 i=j=k=0;
 while((i<dim)&&(j<x.dim))
      { if(l[i]<x.l[j]) i++;
          else if(l[i]>x.l[j]) j++;
             else if (c[i]<x.c[j]) i++;
                  else if (c[i]>x.c[j]) j++;
                        else { k++;
                               if(!(val[i]+x.val[j])) k++;
                               i++;j++;
                              }
       }
 p.dim=dim+x.dim-k;

 for(i=0,j=0,k=0;k<p.dim;)
 { if (l[i]<x.l[j]) { p.l[k]=l[i];
                      p.c[k]=c[i];
                      p.val[k]=val[i];
                      i++;k++;
                     }
      else if (l[i]>x.l[j])  { p.l[k]=x.l[j];
                          p.c[k]=x.c[j];
                          p.val[k]=x.val[j];
                          j++;k++;
                        }
            else if (c[i]<x.c[j]) { p.l[k]=l[i];
                           p.c[k]=c[i];
                           p.val[k]=val[i];
                           i++;k++;
                          }

                  else if(c[i]>x.c[j])  { p.l[k]=x.l[j];
                                  p.c[k]=x.c[j];
                              p.val[k]=x.val[j];
                                          j++;k++;
                                         }
                        else if (!(val[i]+x.val[j])) { i++; j++;}
      else  {       p.l[k]=l[i];
                                p.c[k]=c[i];
                                p.val[k]=val[i]+x.val[j];
                                j++;i++;k++;
                                    }
 }
 
 
 return p;
 }

template <class T>
MatriceRara <T>  MatriceRara <T> ::operator -(MatriceRara <T>  & x)

{ MatriceRara <T> p;
  int i,j,k;
 i=j=k=0;
 while((i<dim)&&(j<x.dim))
      { if(l[i]<x.l[j]) i++;
          else if(l[i]>x.l[j]) j++;
             else if (c[i]<x.c[j]) i++;
                  else if (c[i]>x.c[j]) j++;
                        else { k++;
                               if(!(val[i]-x.val[j])) k++;
                               i++;j++;
                              }
       }
 p.dim=dim+x.dim-k;

 for(i=0,j=0,k=0;k<p.dim;)
 { if (l[i]<x.l[j]) { p.l[k]=l[i];
                      p.c[k]=c[i];
                      p.val[k]=val[i];
                      i++;k++;
                     }
      else if (l[i]>x.l[j])  { p.l[k]=x.l[j];
                          p.c[k]=x.c[j];
                          p.val[k]=-x.val[j];
                          j++;k++;
                        }
            else if (c[i]<x.c[j]) { p.l[k]=l[i];
                           p.c[k]=c[i];
                           p.val[k]=val[i];
                           i++;k++;
                          }

                  else if(c[i]>x.c[j])  { p.l[k]=x.l[j];
                                  p.c[k]=x.c[j];
                              p.val[k]=-x.val[j];
                                          j++;k++;
                                         }
                        else if (!(val[i]-x.val[j])) { i++; j++;}
      else  {       p.l[k]=l[i];
                                p.c[k]=c[i];
                                p.val[k]=val[i]-x.val[j];
                                j++;i++;k++;
                                    }
 }
 
 
 return p;
 }

template <class T>
MatriceRara <T> &MatriceRara <T>::operator =(MatriceRara <T> &x)
{

      m=x.m;
   n=x.n;
   dim=x.dim;
   for(int i=0;i<dim;i++)
   {
   l[i]=x.l[i];
   c[i]=x.c[i];
   val[i]=x.val[i];
   }
 return *this;
}

template <class T>
  MatriceRara <T> MatriceRara <T>::operator*(float *v)
{
  MatriceRara <T> r;
  int k=0;
int j=0;
 for(int i=0;i<m;i++)
  {if(l[k]==i) r.val[j]=0;
    while(l[k]==i) { r.l[j]=l[i];
                  r.c[j]=0;  
                r.val[j]+=val[k]*v[c[k]];
                      k++;j++;
                     }
  }
return r;
}


template <class T>
 MatriceRara <T> MatriceRara <T>:: operator *(MatriceRara <T> &x)
{ int i,k,j;
  
MatriceRara <T> d,p;
  for(k=0,i=0;i<dim;i++)
   for(j=0;j<x.dim;j++)
     if(l[i]==x.c[j]) k++;

d.dim=k;
  for(k=0,i=0;i<dim;i++)
   for(j=0;j<x.dim;j++)
    if (l[i]==x.c[j]) { d.l[k]=x.l[j];
         d.c[k]=c[i];
                       d.val[k]=val[i]*x.val[j];
                       k++;
                      }
    for(i=0;i<k-1;i++)
    for(j=i+1;j<k;j++)
       if((d.l[i]==d.l[j])&&(d.c[i]==d.c[j]))
           { d.val[i]+=d.val[j];
                    d.val[j]=0;
                   }


    p.dim=k;
    for(i=0;i<k;i++)
     if (!(d.val[i])) p.dim--;

      for(j=0,i=0;i<k;i++)
           if(d.val[i]) {p.l[j]=d.l[i];
                      p.c[j]=d.c[i];
                      p.val[j]=d.val[i];
                      j++;
                     }
  return p;
}

template <class T>
istream &operator>>(istream &intrare, MatriceRara <T> &x)
  {
         int i,t;
   float f;
             cout<<"Liniile matricei unde sunt elemente nenule: \n";
            for(i=0;i<x.dim;i++)
                {
                 int vb=1;
                do
    {
                 intrare>>t;
     if (t>=x.m)
     {vb=0;
      cout<<"Gresit!Mai introduceti o data!\n";
     } 
     else {x.l[i]=t;
           vb=1; 
      }
    }
    while(!vb);  
   }
         cout<<"Coloanele matricei unde sunt elemente nenule: \n";
            for(i=0;i<x.dim;i++)
   {
   int vb=1;
                do
    {
                 intrare>>t;
     if (t>=x.n)
     {vb=0;
      cout<<"Gresit!Mai introduceti o data!\n";
     } 
     else {x.c[i]=t;
           vb=1; 
      }
    }
    while(!vb);  
   }
   cout<<"Valorile elementelor nenule sunt: \n";
            for(i=0;i<x.dim;i++)
            {int vb=1;
                do
    {
                 intrare>>f;
     if (f==0)
     {vb=0;
      cout<<"Gresit!Valoare nenula\n";
     } 
     else {x.val[i]=f;
           vb=1; 
      }
    }
    while(!vb);  
    }
            return intrare;
  

  }

template <class T>
ostream &operator<<(ostream &iesire, MatriceRara <T> &x)
  {
         int i;
cout<<"\nRezultatul este: "<<endl;
            for(i=0;i<x.dim;i++)
   {
                iesire<<x.l[i]<<"  "<<x.c[i]<<"  "<<x.val[i]<<endl;
   }
         return iesire;
  }

template <class T>
int MatriceRara <T>:: Nr_linii()
{ 
return m;
} 
template <class T> 
int MatriceRara <T>:: Nr_coloane()
{
return n;
}
template <class T>
float MatriceRara <T>:: Grad()
{float gr;
gr= float((dim*100)/(m*n));
return gr;
}

template <class T>
MatriceRara <T>::~MatriceRara()
 {
}
 

  
int main(int argc, char* argv[])
{
MatriceRara <double> x(4,4,3),c=x;
MatriceRara <double> b(4,4,3);    
cin>>x;
 cout<<x;
cout<<"\n Adaugarea unui nou triplet:"<<endl;
 x+=x;
 cout<<" "<<x<<endl;
 cout<<"\n Eliminarea ultimului triplet:"<<endl;
 x-=x;
 cout<<" "<<x<<endl;
   cin>>b;
 cout<<b;
// cout<<"\n Adunarea celor doua matrice:"<<endl;
    cout<<" Suma este "<<x+b<<endl;
// cout<<"\n Scaderea celor doua matrice:"<<endl;
 cout<<" Diferenta este  "<<x-b<<endl;
// cout<<"\n Inmultirea celor doua matrice:"<<endl;
 cout<<"Inmultirea lor este "<<b*x<<endl;
 cout<<"Numarul de linii al mat: "<<x.Nr_linii()<<endl;
 cout<<"Numarul de coloane al mat: "<<x.Nr_coloane()<<endl;
 cout<<"Gradul de umplere al matricei este: "<<x.Grad()<<"%"<<endl;

 
 int i=1;
 cout<<" elementul "<<i <<" al vectorului valoare este: "<<x[i-1]<<endl;
 
 c=x;
  cout<<"\n";
 cout<<"Matricea initiala este: ";
cout<<" "<<c<<endl;

/*
 float *v;
 v=new float(3);

for( i=0;i<3;i++)
{cout<<"v[ "<<i<<" ]= ";
 cin>>v[i];
 }
cout<<" "<<x*v<<endl;*/
 return 0;
}

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…