اون کد بازم مشکل داره من این کد رو مینویسم میخام این درست شه اگه ممکنه کمک کنید تا این رو درست کنم ممنون میشم!
namespace SparceMatrix
{
publicclasssparcematrix
{
privateint Row;
privateint Col;
privateint Terms;
Term[]Data;
//************************************************** **************************************************
publicvoid ReadSparse()
{
int i = 0;
Console.WriteLine("please enter your information about sparce matrix:");
Console.WriteLine("enter number of rows");
Row=int.Parse(Console.ReadLine());
Console.WriteLine("enter number of columns");
Col=int.Parse(Console.ReadLine());
Console.WriteLine( "$Enter number of values: ");
Terms=int.Parse(Console.ReadLine());
if( Terms > ( Row * Col ) || Terms < 0 )
Console.WriteLine("ERROR!NUMBER oF TERMS ARE ILLIGEAL");
while( i < Terms )
{
Console.WriteLine("\n Enter number of row and column of "+ ( i + 1 ));
Data[i].I = int.Parse(Console.ReadLine());
Data[i].J=int.Parse(Console.ReadLine());
Console.WriteLine("enter the value");
Data[i].Valu=int.Parse(Console.ReadLine());
i++;
}
}
//************************************************** *******
publicvoid WriteSparse()
{
int i;
Console.WriteLine("matrix information is:");
Console.WriteLine("\n\n number of rows are:\n"+Row);
Console.WriteLine("\n\n number of cols are:"+Col);
Console.WriteLine("\n\n number of terms are:\n"+Terms);
Console.WriteLine("\n\nrow,col,value in sparce is:");
for( i = 0; i < Terms; i++ )
Console.WriteLine(Data[i].I+"\t"+Data[i].J+"\t"+Data[i].Valu);
Console.WriteLine("\n");
}
/************************************************** *******/
publicvoid AddSparse( sparcematrix a, sparcematrix b )
{
int i=0;
int j=0;
int k=0;
if( a.Row != b.Row || a.Col != b.Col )
{
Console.WriteLine("\n ERROR !!! CANT ADD THESE 2 MATRIXES ");
return;
}
Row = a.Row;
Col = a.Col;
while( i < a.Terms && j < b.Terms )
{
if( a.Data[i].I < b.Data[j].I || ( a.Data[i].I == b.Data[j].I && a.Data[i].J < b.Data[j].J ) )
{
Data[k].I = a.Data[i].I;
Data[k].J= a.Data[i].J;
Data[k++].Valu = a.Data[i++].Valu;
}
elseif( a.Data[i].I > b.Data[j].I || ( a.Data[i].I == b.Data[j].I && a.Data[i].J > b.Data[j].J ) )
{
Data[k].I = b.Data[j].I;
Data[k].J = b.Data[j].J;
Data[k++].Valu = b.Data[j++].Valu;
}
elseif( a.Data[i].Valu == b.Data[j].Valu )
{
Data[k].I= a.Data[i].I;
Data[k].J = a.Data[i].J;
Data[k++].Valu = a.Data[i++].Valu + b.Data[j++].Valu;
}
else
{
i++;
j++;
}
}
while( i < a.Terms )
{
Data[k].I = a.Data[i].I;
Data[k].J = a.Data[i].J;
Data[k++].Valu = a.Data[i++].Valu;
}
while( j < b.Terms )
{
Data[k].I= b.Data[j].I;
Data[k].J = b.Data[j].J;
Data[k++].Valu = b.Data[j++].Valu;
}
Terms = k;
}
/************************************************** *******/
//void Sparse :: ManfiSparse()
//{
//for( int i = 0; i < Terms; i++ )
//Data[i].value *= -1;
//}
/************************************************** *******/
publicvoid FastTranspose(sparcematrix a)
{
int i, k;
int[] RowSize = newint[Terms];
int[] RowStart = newint[Terms];
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].J - 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].J - 1]++;
Data[k].I = a.Data[i].J;
Data[k].J = a.Data[i].I;
Data[k].Valu = a.Data[i].Valu;
}
}
/************************************************** *******/
publicbool StoreSum( int sum, int LastInResult, int r, int c )
{
if( sum != 0 )
if( LastInResult < Terms-1 )
{
LastInResult++;
Data[LastInResult].I = r;
Data[LastInResult].J = c;
Data[LastInResult].Valu = sum;
returnfalse;
}
else
{
Console.WriteLine( "\n*** ERROR !!! Tedade Anasore Gheyre Sefr Az Fazaye Arayeh Biroon Mizanad ***\n");
returntrue;
}
else
returnfalse;
}
/************************************************** *******/
publicchar compare(int x, int y)
{
if( x < y )
return'<';
elseif( x == y )
return'=';
return'>';
}
/************************************************** *******/
publicvoid MulSparse(sparcematrix a, sparcematrix b)
{
if( a.Col != b.Row )
{
Console.WriteLine( "\nERROR !!! CANT MULTIPLY 2 MATRIXES");
Row = 0;
Col = 0;
Terms = 0;
return;
}
sparcematrix d=newsparcematrix();
d.FastTranspose(b);
int currRowIndex = 0, LastInResult = -1, currRowBegin = 0, currRowA = a.Data[0].I;
a.Data[a.Terms].I = a.Row;
d.Data[b.Terms].I = b.Col;
d.Data[b.Terms].J = -1;
int sum = 0;
while( currRowIndex < a.Terms )
{
int currColB = d.Data[0].I;
int currColIndex = 0;
while( currColIndex <= b.Terms )
{
if( a.Data[currRowIndex].I != currRowA )
{
if( StoreSum( sum, LastInResult, currRowA, currColB ) )
{
Row = 0;
Col = 0;
Terms = 0;
Console.WriteLine("errr");
return;
}//if
else
sum = 0;
currRowIndex = currRowBegin;
while ( d.Data[currColIndex].I == currColB )
currColIndex++;
currColB = d.Data[currColIndex].I;
}//if
elseif( d.Data[currColIndex].I != currColB)
{
if( StoreSum( sum, LastInResult, currRowA, currColB ) )
{
Row = 0;
Col = 0;
Terms = 0;
Console.WriteLine("\n *** ERROR !!! ***");
return;
}//if
else
sum = 0;
currRowIndex = currRowBegin;
currColB = d.Data[currColIndex].I;
}//else if
elseswitch( compare( a.Data[currRowIndex].J, d.Data[currColIndex].J ) )
{
case'<' :
currRowIndex++;
break;
case'=' :
sum += a.Data[currRowIndex].Valu * d.Data[currColIndex].Valu;
currRowIndex++;
currColIndex++;
break;
case'>' :
break;
currColIndex++;
}//else switch
}//while
while( a.Data[currRowIndex].I == currRowA )
currRowIndex++;
currRowBegin = currRowIndex;
currRowA = a.Data[currRowIndex].I;
}//while
Row = a.Row;
Col = b.Col;
Terms = LastInResult + 1;
}//func
}//class
}//namespace
publicclassTerm
{
privateint i;
privateint j;
privateint valu;
publicint I{
get
{
return i;
}
set
{
i = value;
}
}
publicint J {
get
{
return j;
}
set
{
j = value;
}
}
publicint Valu {
get
{
return valu;
}
set
{
valu = value;
}
}