Structuri de date: matrici rare - aplicatii rezolvate


1) Operatii elementare cu matrici rare.
#include<stdio.h>
#include<conio.h>
#include <iostream>
using namespace std;
 
template <class T>
class element
{
public:
	int lin;
	int col;
	int val;
	element()
	{
		lin=0;
		col=0;
		val=0;
	}
	element(int a, int b, int c)
	{
		lin=a;
		col=b;
		val=c;
	}
 
};
 
//lista in care avem memorata o matrice rara
template <class T>
class matrice
{
	int nr_lin;
	int nr_col;
	int nr_elem;
	element<T> data[10];
public:
	matrice();
	void atribuire(matrice<T> ob);
	matrice<T> creare();                    //creare matrice rara
	void print();                             //afisarea unei matrici rare
	void adaugaElement(element<T> e);    //adauga un element
	void stergere(element<T> e);         //sterge un element
	void modificaElement(element<T> e);  //modifica un element
	void adunare(matrice<T> ob1, matrice<T> ob2);  //adunarea a doua matrici rare
	//void inmultire(matrice<T> ob1, matrice<T> ob2); //inmultirea a doua matrici rare
	//void inmulConstanta(int cst);             //inmultirea unei matrici rare cu o constanta        
	matrice<T> transpusa(matrice<T> a);               //calcul transpusa
};
 
 
template <class T>
matrice<T>::matrice()
{
	nr_col=0;   //nr de linii
	nr_lin=0;   //nr de coloane
	nr_elem=0;  //nr de elemente nule
}
 
template <class T>
void matrice<T>::atribuire(matrice<T> ob)
{
	nr_lin=ob.nr_lin;
	nr_col=ob.nr_col;
	nr_elem=ob.nr_elem;
	for(int i=0; i<nr_elem; i++)
		data[i]=ob.data[i];
}
 
 
template <class T>
matrice<T> matrice<T>::creare()
{
	cout <<"Nr de linii matrice= ";
	cin >>nr_lin;
	cout <<"Nr de coloane matrice= ";
	cin >>nr_col;
	cout <<"Nr de elmente nenule in matrice= ";
	cin >>nr_elem;
	
	cout <<"Introduceti elementele: " <<endl;
	for(int i=1; i<=nr_elem; i++)
	{
		cout <<"elementul " <<i+1 <<endl;
        cout<<"linia= ";
		cin >>data[i].lin;
		cout<<"coloana= ";
		cin >>data[i].col;
		cout <<"valoare= ";
		cin >>data[i].val;
	}
	return *this;
}
 
template <class T>
void matrice<T>::print()
{
	for(int i=1; i<=nr_lin; i++)
	{
		for (int j=1; j<=nr_col; j++)
		{
			int ok=1;
            for(int k=1; k<=nr_elem; k++)
			     if((data[k].lin==i) && (data[k].col==j))
				 {
						cout<<data[k].val <<" ";
						ok=0;
				  }
 
			if(ok==1)
				cout <<"0 ";
		}
		cout <<endl;
	}
}
 
template <class T>
void matrice<T>::adaugaElement(element<T> e)
{
	int ok=1;
    for(int i=0; i<nr_elem; i++)
		if((data[i].lin == e.lin) &&  (data[i].col == e.col))
		{
			data[i].val += e.val;
			ok=0;
		}
	if(ok==1)
	{
            nr_elem++;
			data[nr_elem-1].lin=e.lin;
			data[nr_elem-1].col=e.col;
			data[nr_elem-1].val=e.val;
	}
}
 
template <class T>
void matrice<T>::stergere(element<T> e)
{
   for(int i=1; i<nr_elem; i++)
   {
	   if((data[i].lin == e.lin) && (data[i].col == e.col) &&  (data[i].val == e.val))
	   {
			data[i].val -= e.val;
	   }
   }
}
 
template <class T>
void matrice<T>::modificaElement(element<T> m)
{
   for(int i=0; i<nr_elem; i++)
   {
	   if((data[i].lin == m.lin) && (data[i].col == m.col))
	   {
			data[i].val= m.val;
	   }
   }
 
}
template <class T>
void matrice<T>::adunare(matrice<T> ob1, matrice<T> ob2)
{
	atribuire(ob1);
    for(int i=0; i<ob2.nr_elem; i++)
		adaugaElement(ob2.data[i]);
}
 
//linile devin coloane
template <class T>
matrice<T> matrice<T>::transpusa(matrice<T> a)
{
   nr_lin=a.nr_col;
   nr_col=a.nr_lin;
   nr_elem=a.nr_elem;
   for(int i=0;i<=nr_elem;i++)
     {data[i].val=a.data[i].val;
      data[i].lin=a.data[i].col;
      data[i].col=a.data[i].lin;
     }
   return *this;
}
 
void main()
{
	matrice<int>ob1; matrice<int>ob2; matrice<int>ob3;
	element<int>e; element<int>s; element<int>m;
 
	cout <<"Creare matrice 1 " <<endl;
	ob1.creare();
    cout <<"Afisare matrice 1 " <<endl;
	ob1.print();
 
    cout<<"Matricea transpusa este: "<<endl;
    ob2.transpusa(ob1);
    ob2.print();
 
	cout <<"Creare matrice 2 " <<endl;
	ob2.creare();
    cout <<"Afisare matrice 2 " <<endl;
	ob2.print();
 
	cout <<"ADUNAREA A 2 MATRICI"  <<endl;
	ob3.adunare(ob1, ob2);
	ob3.print();
 
	cout <<"ADAUGARE ELEMENT IN MATRICE" <<endl;
	cout <<"linia= ";
    cin >>e.lin;
	cout <<"coloana= ";
	cin >>e.col;
	cout <<"valoare= ";
	cin >>e.val;
    ob1.adaugaElement(e);
	ob1.print();
 
	cout <<"STERGE UN ELEMENT IN MATRICE" <<endl;
	cout <<"linia= ";
    cin >>s.lin;
	cout <<"coloana= ";
	cin >>s.col;
	cout <<"valoare= ";
	cin >>s.val;
    ob1.stergere(s);
	ob1.print();
 
	cout <<"MODIFICA UN ELEMENT IN MATRICE" <<endl;
	cout <<"linia= ";
    cin >>m.lin;
	cout <<"coloana= ";
	cin >>m.col;
	cout <<"Noua valoare= ";
	cin >>m.val;
    ob1.modificaElement(m);
	ob1.print();

	int g;
	cin >>g;
}
COMENTARII

optional (nu va fi afisat pe website)
max: 255 caractere


Acest site utilizeaza cookie-uri. Navigand in continuare va exprimati acordul asupra folosirii cookie-urilor.