سلام دوست عزیز
ببین این به دردت می خوره
کارش اینه:
۱ : خواندن ماتریس اسپارس و چاپ آن
۲ : چاپ ماتریس واقعی
۳ : جمع و تفریق و ضرب با ماتریسی دیگر
کد:
#include<conio.h>
#include<iostream.h>
#define max 30
/*********************************************************/
class string;
class sparse;
/*********************************************************/
class sparsenode
{
int row, col;
float value;
friend class sparse;
};
/*********************************************************/
class sparse
{
int row, col, terms;
sparsenode data[max];
public:
Void readsparse( void );
void writesparse( void );
void writematrix( void );
void addsparse( sparse a, sparse b );
void manfisparse();
void fasttranspose( sparse b );
int storesum( int sum, int&lastinresult, int r, int c );
void sparse :: Mulsparse( sparse a, sparse b );
};
/*********************************************************/
void sparse :: Readsparse( void )
{
clrscr();
int i = 0;
cout << "\n\n@ lotfan ettelaat zir ra dar mored matrise sparse vared konid : \n\n"
<< "$ tadade satrha : ";
cin >> row;
cout << "$ tedade sotoonha : ";
cin >> col;
do
{
cout << "$ tedade anasore gheyre sefr : ";
cin >> terms;
if( terms > ( row * col ) || terms < 0 )
cerr << "\nerror ! ( in tedad ghabele ghabool nemibashad )\n\n";
}
while( terms > ( row * col ) || terms < 0 );
while( i < terms )
{
cout << "\n# lotfan shomarehye satr va sotoone onsore "
<< ( i + 1 )
<< " -om ra vared konid : ";
cin >> data[i].row
>> data[i].col;
if( data[i].row > row || data[i].col > col || data[i].row < 1 || data[i].col < 1 )
cerr << "\nerror ! ( in jayegah dar moshakhkhasat matris nemigonjad )\n";
else
{
cout << "* lotfan meghdar onsor ra vared konid : ";
cin >> data[i].value;
i++;
}
}
clrscr();
}
/*********************************************************/
void sparse :: Writesparse( void )
{
int i;
cout << "\n\n# moshakhkhasat matris be soorate zir ast :"
<< "\n\n~ tedade satrha = "
<< row
<< "\n~ tedade sotoonha = "
<< col
<< "\n~ tedade anasore gheyre sefr = "
<< terms
<< "\n\n~ liste anasor gheyre sefre matris be sharhe zir ast :\n\n"
<< "satr\tsotoon\tmeghdar\n";
for( i = 0; i < terms; i++ )
cout << data[i].row
<< "\t"
<< data[i].col
<< "\t"
<< data[i].value
<< endl;
getch();
}
/*********************************************************/
void sparse :: Writematrix( void )
{
clrscr();
int i, j;
float matrix[max][max] = {0};
cout << "\n\nmatrix vaghei :\n\n";
for( i = 0; i < terms; i++ )
matrix[data[i].row-1][data[i].col-1] = data[i].value;
for( i = 0; i <= col; i++ )
cout << "[" << ( i ) << "]\t";
for( i = 0; i < row; i++ )
{
cout << "\n\n"
<< "[" << ( i+1 ) << "]";
for( j = 0; j < col; j++ )
{
cout << "\t"
<< matrix[i][j];
}
}
getch();
clrscr();
}
/*********************************************************/
void sparse :: Addsparse( sparse a, sparse b )
{
int i, j, k;
i = j = k = 0;
if( a.row != b.row || a.col != b.col )
{
cout << "\n*** error !!! Nemitavan in 2 matris ra ba ham jame kard ***";
return;
}
row = a.row;
col = a.col;
while( i < a.terms && j < b.terms )
{
if( a.data[i].row < b.data[j].row || ( a.data[i].row == b.data[j].row && a.data[i].col < b.data[j].col ) )
{
data[k].row = a.data[i].row;
data[k].col = a.data[i].col;
data[k++].value = a.data[i++].value;
}
else if( a.data[i].row > b.data[j].row || ( a.data[i].row == b.data[j].row && a.data[i].col > b.data[j].col ) )
{
data[k].row = b.data[j].row;
data[k].col = b.data[j].col;
data[k++].value = b.data[j++].value;
}
else if( a.data[i].value + b.data[j].value )
{
data[k].row = a.data[i].row;
data[k].col = a.data[i].col;
data[k++].value = a.data[i++].value + b.data[j++].value;
}
else
{
i++;
j++;
}
}
while( i < a.terms )
{
data[k].row = a.data[i].row;
data[k].col = a.data[i].col;
data[k++].value = a.data[i++].value;
}
while( j < b.terms )
{
data[k].row = b.data[j].row;
data[k].col = b.data[j].col;
data[k++].value = b.data[j++].value;
}
terms = k;
}
/*********************************************************/
void sparse :: Manfisparse()
{
for( int i = 0; i < terms; i++ )
data[i].value *= -1;
}
/*********************************************************/
void sparse :: Fasttranspose( sparse a )
{
int i, k;
int rowsize[max], rowstart[max];
row = a.col;
col = a.row;
terms = a.terms;
for( i = 0; i < a.col; i++ )
rowsize[i] = 0;
for( i = 0; i < a.terms; i++ )
rowsize[a.data[i].col-1]++;
rowstart[0] = 0;
for( i = 1; i < a.col; i++ )
rowstart[i] = rowstart[i-1] + rowsize[i-1];
for( i = 0; i < a.terms; i++ )
{
k = rowstart[a.data[i].col-1]++;
data[k].row = a.data[i].col;
data[k].col = a.data[i].row;
data[k].value = a.data[i].value;
}
}
/*********************************************************/
int sparse :: Storesum( int sum, int&lastinresult, int r, int c )
{
if( sum != 0 )
if( lastinresult < max-1 )
{
lastinresult++;
data[lastinresult].row = r;
data[lastinresult].col = c;
data[lastinresult].value = sum;
return 0;
}
else
{
cerr << "\n*** error !!! Tedade anasore gheyre sefr az fazaye arayeh biroon mizanad ***\n";
return 1;
}
else
return 0;
}
/*********************************************************/
char compare( int x, int y )
{
if( x < y )
return '<';
else if( x == y )
return '=';
return '>';
}
/*********************************************************/
void sparse :: Mulsparse( sparse a, sparse b )
{
if( a.col != b.row )
{
cout << "\n*** error !!! Zarbe 2 matris emkan pazir nist ***";
row = 0;
col = 0;
terms = 0;
return;
}
if( ( a.terms == max ) || ( b.terms == max ) )
{
cout << "*** error !!! Yek fazaye khali dar matris 'a' ya 'b' lazem ast ***";
row = 0;
col = 0;
terms = 0;
return;
}
sparse d;
d.fasttranspose( b );
int currrowindex = 0, lastinresult = -1, currrowbegin = 0, currrowa = a.data[0].row;
a.data[a.terms].row = a.row;
d.data[b.terms].row = b.col;
d.data[b.terms].col = -1;
int sum = 0;
while( currrowindex < a.terms )
{
int currcolb = d.data[0].row;
int currcolindex = 0;
while( currcolindex <= b.terms )
{
if( a.data[currrowindex].row != currrowa )
{
if( storesum( sum, lastinresult, currrowa, currcolb ) )
{
row = 0;
col = 0;
terms = 0;
cout << "\n *** error !!! ***";
return;
}
else
sum = 0;
currrowindex = currrowbegin;
while ( d.data[currcolindex].row == currcolb )
currcolindex++;
currcolb = d.data[currcolindex].row;
}
else if( d.data[currcolindex].row != currcolb)
{
if( storesum( sum, lastinresult, currrowa, currcolb ) )
{
row = 0;
col = 0;
terms = 0;
cout << "\n *** error !!! ***";
return;
}
else
sum = 0;
currrowindex = currrowbegin;
currcolb = d.data[currcolindex].row;
}
else switch( compare( a.data[currrowindex].col, d.data[currcolindex].col ) )
{
case '<' :
Currrowindex++;
break;
case '=' :
Sum += a.data[currrowindex].value * d.data[currcolindex].value;
currrowindex++;
currcolindex++;
break;
case '>' :
Currcolindex++;
}
}
while( a.data[currrowindex].row == currrowa )
currrowindex++;
currrowbegin = currrowindex;
currrowa = a.data[currrowindex].row;
}
row = a.row;
col = b.col;
terms = lastinresult + 1;
}
/*********************************************************/
void moarrefi( void )
{
clrscr();
cout << "\t\t\t\tbe name khoda"
<< "\n\n\nbarname nevis : Mohammad hasani eghtedar"
<< "\n\nshomare daneshjooti : 83525013"
<< "\n\nreshteye tahsili : Olume computer"
<< "\n\nmaghtae tahsili : Karshenasi"
<< "\n\nsale tahsili : 1384 - 1385 nimehye avval"
<< "\n\nmahale tahsil : Daneshgahe dolatiye qom"
<< "\n\nostad : Aghaye sayyed esmaeili"
<< "\n\n\n\n\t\t\t\tkelidi ra befesharid";
getch();
}
/*********************************************************/
void meno( void )
{
clrscr();
cout << "\n# lotfan alamat morede nazar ra vared konid :\n"
<< "\n+ : Jam ba matrisi digar"
<< "\n- : Tafrigh az matrisi digar"
<< "\n* : Zarb dar matrisi digar"
<< "\np : Chape matrise vagheyi"
<< "\nt : Tarane hadeye matris";
}
/*********************************************************/
void main( void )
{
moarrefi();
sparse a, b, c;
a.readsparse();
a.writesparse();
meno();
switch( getch() )
{
case '+' :
B.readsparse();
b.writesparse();
c.addsparse(a,b);
cout << "\n\nanswer is : ";
c.writesparse();
break;
case '-' :
B.readsparse();
b.writesparse();
b.manfisparse();
c.addsparse(a,b);
cout << "\n\nanswer is : ";
c.writesparse();
break;
case '*' :
B.readsparse();
b.writesparse();
c.mulsparse(a,b);
clrscr();
cout << "\n\nanswer is : ";
c.writesparse();
break;
case 'p' :
A.writematrix();
break;
case 't' :
C.fasttranspose( a );
clrscr();
cout << "\n\nanswer is : ";
c.writesparse();
break;
}
}