Y@SiN
02-26-2010, 10:35 PM
برنامه ضمیمه یک برنامه نمونه برای محاسبه ترانهاده مجموع دو ماتریس اسپارس است
کد:
# include <iostream.h>
# include <conio.h>
int sp1[5][3],sp2[5][3],sps[10][3],tar[10][3];
int n1,n2,k,i,j,a,b,max;
void ch(int);
bool x=false;
void main()
{
//*****************************************input sp1
cout <<"**************************please enter spars 1 <max 4>************************** ";
for (i=0;i<3;i++)
{
cout << "\nplease enter row 0 , column " <<i<<": ";
cin >> sp1[0][i];
}
n1 = sp1[0][2]+1;
for (i=1;i<n1;i++)
{
for (j=0;j<3;j++)
{
cout << "\nplease enter row "<<i<<" ,column " <<j<<" : ";
cin >> sp1[i][j];
}
}
//***************************************input sp2
q:
cout <<"**************************please enter spars 2 <max 4>************************** ";
for (i=0;i<3;i++)
{
cout << "\nplease enter row 0 , column " <<i<<": ";
cin >> sp2[0][i];
}
if (sp1[0][0]!=sp2[0][0] || sp1[0][1]!=sp2[0][1]) goto q ;
n2 = sp2[0][2]+1;
for (i=1;i<n2;i++)
{
for (j=0;j<3;j++)
{
cout << "\nplease enter row "<<i<<" ,column " <<j<<" : ";
cin >> sp2[i][j];
}
}
//****************************************barrasi
a=1;k=1;
if (n1>n2)
max=n1;
else
max=n2;
for (a=1;a<max;a++)
{
if (sp1[a][0]==sp2[a][0])
{
if ( sp1[a][1]==sp2[a][1] && ((sp1[a][2]+sp2[a][2])!=0) )
{
sps[k][0]=sp1[a][0];
sps[k][1]=sp1[a][1];
sps[k][2]=sp1[a][2]+sp2[a][2];
k++;
continue;
}
else if ((sp1[a][1]<sp2[a][1])&& (sp1[a][2]!=0))
{
sps[k][0]=sp1[a][0];
sps[k][1]=sp1[a][1];
sps[k][2]=sp1[a][2];
k++;
sps[k][0]=sp2[a][0];
sps[k][1]=sp2[a][1];
sps[k][2]=sp2[a][2];
k++;
continue;
}
else if (sp1[a][1]>sp2[a][1])
{
sps[k][0]=sp2[a][0];
sps[k][1]=sp2[a][1];
sps[k][2]=sp2[a][2];
k++;a++;
sps[k][0]=sp1[a][0];
sps[k][1]=sp1[a][1];
sps[k][2]=sp1[a][2];
k++;
continue;
}
}
else if ((sp1[a][0]<sp2[a][0])&& (sp1[a][2]!=0))
{
sps[k][0]=sp1[a][0];
sps[k][1]=sp1[a][1];
sps[k][2]=sp1[a][2];
k++;
sps[k][0]=sp2[a][0];
sps[k][1]=sp2[a][1];
sps[k][2]=sp2[a][2];
k++;
continue;
}
else if (sp1[a][0]>sp2[a][0])
{
sps[k][0]=sp2[a][0];
sps[k][1]=sp2[a][1];
sps[k][2]=sp2[a][2];
k++;
sps[k][0]=sp1[a][0];
sps[k][1]=sp1[a][1];
sps[k][2]=sp1[a][2];
k++;
continue;
}
else if ((sp1[a][0]==0) && (sp2[a][2]!=0))
{
sps[k][0]=sp2[a][0];
sps[k][1]=sp2[a][1];
sps[k][2]=sp2[a][2];
k++;
}
}
sps[0][0]=sp1[0][0];
sps[0][1]=sp1[0][1];
sps[0][2]=(k-1);
//tabdil be tara_nahadeh
for (i=1;i<=k;i++)
{
tar[i][0]=sps[i][1];
tar[i][1]=sps[i][0];
tar[i][2]=sps[i][2];
}
tar[0][0]=sps[0][0];
tar[0][1]=sps[0][1];
tar[0][2]=sps[0][2];
//sort
for (i=k-1;i>0;i--)
{
for (j=1;j<i;j++)
{
if (tar[j][0]>tar[(j+1)][0])
{
ch(i);
}
}
}
for (i=k-1;i>0;i--)
{
for (j=1;j<i;j++)
{
if ((tar[j][0]==tar[(j+1)][0]) && (tar[j][1]>tar[(j+1)][1]))
{
ch(i);
}
}
}
//print sp1
cout<<"***************************** sp1 ***********************************\n";
for (i=0;i<n1;i++)
{
for (j=0;j<3;j++)
{
cout << sp1[i][j]<< "\t";
}
cout <<"\n";
}
//print sp2
cout<<"****************************** sp2 **********************************\n";
for (i=0;i<n2;i++)
{
for (j=0;j<3;j++)
{
cout << sp2[i][j]<< "\t";
}
cout <<"\n";
}
//print sps
cout<<"****************************** sps **********************************\n";
for (i=0;i<k;i++)
{
for (j=0;j<3;j++)
{
cout << sps[i][j]<< "\t";
}
cout <<"\n";
}
//print sps
cout<<"****************************** tara **********************************\n";
for (i=0;i<k;i++)
{
for (j=0;j<3;j++)
{
cout << tar[i][j]<< "\t";
}
cout <<"\n";
}
getch();
}
void ch(int ii)
{
int temp[3];
temp[0]=tar[ii][0];
temp[1]=tar[ii][1];
temp[2]=tar[ii][2];
tar[ii][0]=tar[i+1][0];
tar[ii][1]=tar[i+1][1];
tar[ii][2]=tar[i+1][2];
tar[ii+1][0]=temp[0];
tar[ii+1][1]=temp[1];
tar[ii+1][2]=temp[2];
}
کد:
# include <iostream.h>
# include <conio.h>
int sp1[5][3],sp2[5][3],sps[10][3],tar[10][3];
int n1,n2,k,i,j,a,b,max;
void ch(int);
bool x=false;
void main()
{
//*****************************************input sp1
cout <<"**************************please enter spars 1 <max 4>************************** ";
for (i=0;i<3;i++)
{
cout << "\nplease enter row 0 , column " <<i<<": ";
cin >> sp1[0][i];
}
n1 = sp1[0][2]+1;
for (i=1;i<n1;i++)
{
for (j=0;j<3;j++)
{
cout << "\nplease enter row "<<i<<" ,column " <<j<<" : ";
cin >> sp1[i][j];
}
}
//***************************************input sp2
q:
cout <<"**************************please enter spars 2 <max 4>************************** ";
for (i=0;i<3;i++)
{
cout << "\nplease enter row 0 , column " <<i<<": ";
cin >> sp2[0][i];
}
if (sp1[0][0]!=sp2[0][0] || sp1[0][1]!=sp2[0][1]) goto q ;
n2 = sp2[0][2]+1;
for (i=1;i<n2;i++)
{
for (j=0;j<3;j++)
{
cout << "\nplease enter row "<<i<<" ,column " <<j<<" : ";
cin >> sp2[i][j];
}
}
//****************************************barrasi
a=1;k=1;
if (n1>n2)
max=n1;
else
max=n2;
for (a=1;a<max;a++)
{
if (sp1[a][0]==sp2[a][0])
{
if ( sp1[a][1]==sp2[a][1] && ((sp1[a][2]+sp2[a][2])!=0) )
{
sps[k][0]=sp1[a][0];
sps[k][1]=sp1[a][1];
sps[k][2]=sp1[a][2]+sp2[a][2];
k++;
continue;
}
else if ((sp1[a][1]<sp2[a][1])&& (sp1[a][2]!=0))
{
sps[k][0]=sp1[a][0];
sps[k][1]=sp1[a][1];
sps[k][2]=sp1[a][2];
k++;
sps[k][0]=sp2[a][0];
sps[k][1]=sp2[a][1];
sps[k][2]=sp2[a][2];
k++;
continue;
}
else if (sp1[a][1]>sp2[a][1])
{
sps[k][0]=sp2[a][0];
sps[k][1]=sp2[a][1];
sps[k][2]=sp2[a][2];
k++;a++;
sps[k][0]=sp1[a][0];
sps[k][1]=sp1[a][1];
sps[k][2]=sp1[a][2];
k++;
continue;
}
}
else if ((sp1[a][0]<sp2[a][0])&& (sp1[a][2]!=0))
{
sps[k][0]=sp1[a][0];
sps[k][1]=sp1[a][1];
sps[k][2]=sp1[a][2];
k++;
sps[k][0]=sp2[a][0];
sps[k][1]=sp2[a][1];
sps[k][2]=sp2[a][2];
k++;
continue;
}
else if (sp1[a][0]>sp2[a][0])
{
sps[k][0]=sp2[a][0];
sps[k][1]=sp2[a][1];
sps[k][2]=sp2[a][2];
k++;
sps[k][0]=sp1[a][0];
sps[k][1]=sp1[a][1];
sps[k][2]=sp1[a][2];
k++;
continue;
}
else if ((sp1[a][0]==0) && (sp2[a][2]!=0))
{
sps[k][0]=sp2[a][0];
sps[k][1]=sp2[a][1];
sps[k][2]=sp2[a][2];
k++;
}
}
sps[0][0]=sp1[0][0];
sps[0][1]=sp1[0][1];
sps[0][2]=(k-1);
//tabdil be tara_nahadeh
for (i=1;i<=k;i++)
{
tar[i][0]=sps[i][1];
tar[i][1]=sps[i][0];
tar[i][2]=sps[i][2];
}
tar[0][0]=sps[0][0];
tar[0][1]=sps[0][1];
tar[0][2]=sps[0][2];
//sort
for (i=k-1;i>0;i--)
{
for (j=1;j<i;j++)
{
if (tar[j][0]>tar[(j+1)][0])
{
ch(i);
}
}
}
for (i=k-1;i>0;i--)
{
for (j=1;j<i;j++)
{
if ((tar[j][0]==tar[(j+1)][0]) && (tar[j][1]>tar[(j+1)][1]))
{
ch(i);
}
}
}
//print sp1
cout<<"***************************** sp1 ***********************************\n";
for (i=0;i<n1;i++)
{
for (j=0;j<3;j++)
{
cout << sp1[i][j]<< "\t";
}
cout <<"\n";
}
//print sp2
cout<<"****************************** sp2 **********************************\n";
for (i=0;i<n2;i++)
{
for (j=0;j<3;j++)
{
cout << sp2[i][j]<< "\t";
}
cout <<"\n";
}
//print sps
cout<<"****************************** sps **********************************\n";
for (i=0;i<k;i++)
{
for (j=0;j<3;j++)
{
cout << sps[i][j]<< "\t";
}
cout <<"\n";
}
//print sps
cout<<"****************************** tara **********************************\n";
for (i=0;i<k;i++)
{
for (j=0;j<3;j++)
{
cout << tar[i][j]<< "\t";
}
cout <<"\n";
}
getch();
}
void ch(int ii)
{
int temp[3];
temp[0]=tar[ii][0];
temp[1]=tar[ii][1];
temp[2]=tar[ii][2];
tar[ii][0]=tar[i+1][0];
tar[ii][1]=tar[i+1][1];
tar[ii][2]=tar[i+1][2];
tar[ii+1][0]=temp[0];
tar[ii+1][1]=temp[1];
tar[ii+1][2]=temp[2];
}