ماتریس اسپارس

سلام کسی هست این برنامه رو داشته باشه ؟
البته حالا برنامه ای میخوام که کلا درباره ماتریس اسپارس باشه علاوه بر ضرب جمع و... رو هم انجام بده!
 
آخرین ویرایش:

winsent

Banned
من داشتم اما بايد بگردم ببينم هنوز هست يا نه، اگه بود ميذارم
موفق باشيد
 
این برنامه رو از یکی از سایت ها گرفتم که یادم رفت لینکشو بگیرم... شرمنده ... با اجازه ی پرگرمرش میذارم اینجا تا دوستان استفاده کنن و یه فاتحه واسه رفتگان پرگرمر عزیز بخونن...
کد:
#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;
 }
}
 

oranoos_mta

New Member
بابا یکی کمکم کنه خیلی فوریه خواهشن
این برنامه من تو کامپایل ارور نمیده ولی موقع اجرا ارور میده مشکل کارم کجاست؟
سالک جان واقعا کمک میخوام خیلی حیاتیهو خیلی هم فوریه
HTML:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#define max  30
void main()
{
	int a[max][max],b[max][max],c[max][max];
	int i=0;
 cout << "\n\n@ Lotfan Ettelaat Zir Ra Dar Mored Matrise Sparse Vared Konid : \n\n"
	 << "$ enter n for matrix[n][n]  [--> in matrix A , B] \nn : ";
	 int Row,Col,Terms,Row1,Col1,Terms1;
 cin >> Row;
 Col=Row1=Col1=Row;
 do
 {
  cout << "$ Tedade Anasore Gheyre Sefr in matrix A : ";
  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 : ";
  int r,c;
  cin>>r>>c;
  if( r > Row || c > Row || r < 1 || c < 1 )
	cerr << "\nERROR ! ( In Jayegah Dar Moshakhkhasat Matris Nemigonjad )\n";
	else
  {
  cout << "* Lotfan Meghdar Onsor Ra Vared Konid : ";
  cout<<"\nmeghdaresh : ";
  cin>>a[r][c];
  i+=1;
  }
  cout<<"matrix A is :\n\n";
  }
  for(i=1;i<=Row;i++)
  {
	for(int j=1;j<=Col;j++)
	{
	 if(a[i][j]!= 0)
	 cout<<a[i][j]<<"  ";
	 else
	 {
		a[i][j]=0;
		cout<<a[i][j]<<"  ";
	 }
	}
	cout<<"\n\n";
	}
  cout<<"\n\ntranspose matrix A is : \n";
  for(int j=1;j<=Col;j++){
	for(i=1;i<=Row;i++)
	cout<<a[i][j]<<"  ";
	cout<<endl; }
	do
 {
  cout << "\n\n$ Tedade Anasore Gheyre Sefr in matrix B : ";
  cin >> Terms1;
  if( Terms1 > ( Row1 * Col1 ) || Terms1 < 0 )
	cerr << "\nERROR ! ( In Tedad Ghabele Ghabool Nemibashad )\n\n";
 }
 while( Terms1 > ( Row1 * Col1 ) || Terms1 < 0 );
 i=0;
 while( i < Terms1 )
 {
  cout << "\n# Lotfan Shomarehye Satr Va Sotoone Onsore "
	  << ( i + 1 )
	  << " -om Ra Vared Konid : ";
  int r1,c1;
  cin>>r1>>c1;
  if( r1 > Row1 || c1 > Row1 || r1 < 1 || c1 < 1 )
	cerr << "\nERROR ! ( In Jayegah Dar Moshakhkhasat Matris Nemigonjad )\n";
	else
  {
  cout << "* Lotfan Meghdar Onsor Ra Vared Konid : ";
  cout<<"\nmeghdaresh : ";
  cin>>b[r1][c1];
  i+=1;
  }
  cout<<"matrix B is : \n\n";
  }
  for(i=1;i<=Row1;i++)
  {
	for(int j=1;j<=Col1;j++)
	{
	 if(b[i][j]!= 0)
	 cout<<b[i][j]<<"  ";
	 else
	 {
		b[i][j]=0;
		cout<<b[i][j]<<"  ";
	 }
	}
	cout<<"\n\n";
	}
	cout<<"\n\ntranspose matrix B is : \n";
  for( j=1;j<=Col1;j++){
	for(i=1;i<=Row1;i++)
	cout<<b[i][j]<<"  ";
	cout<<endl;
  }
  cout<<"plus matrix A & B is :\n\n";
  for(i=1;i<=Col;i++)
  {
	  for(int j=1;j<=Col;j++)
	  {
	  c[i][j]=a[i][j]+b[i][j];
	  cout<<c[i][j]<<"  ";
	  }
	cout<<endl;
  }
  cout<<"multiplication matrix A & B is : \n\n";
  int sum;
  for(i=1;i<=Row;i++)
	for(int j=1;j<=Row;j++)
	{
		sum=0;
		for(int k=1;k<=Row;k++)
			sum+=(a[i][k]*b[k][j]);
		c[i][j]=sum;
	}
  for(i=1;i<=Col;i++)
  {
	  for(int j=1;j<=Col;j++)
	  {
	  cout<<c[i][j]<<"  ";
	  }
  cout<<endl;
  }
}
 

جدیدترین ارسال ها

بالا