PDA

View Full Version : سوال: رفع مشکل کد



parva-88
پنج شنبه 12 اسفند 1389, 12:01 عصر
من برنامه ی ماتریس ها رو نوشتم و مطمنم که کدهاش درسته(کد ترانهاده)ولی نمیدونم چرا موقع اجرا ترانهاده رو غلط چاپ میکنه خاهشا نگا کنید و بگین خیلی ضروریه

public class Sparcematrix
{
/// ///////////////////////////////////////////////////data member////////////////////////////////////////////////////////////
public int Row { get; set; }
public int Col { get; set; }
public int Size { get; set; }
public int Terms { get; set; }
private term[] tarray;
///////////////////////////////////////////////////////ctor//////////////////////////////////////////////////////////////////
public Sparcematrix(int Maxrow, int Maxcol)
{
this.Row = Maxrow;
this.Col = Maxcol;
}
public Sparcematrix()
{
}
/// ///////////////////////////////////////////////////Methods/////////////////////////////////////////////////////////////
public void FastTranspose(Sparcematrix a )
{
int i, k;
int[] RowSize=new int[Col];
int[] RowStart = new int[Col];
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.tarray[i].J]++;
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.tarray[i].J-1]++;
tarray[k].I = a.tarray[i].J;
tarray[k].J = a.tarray[i].I;
tarray[k].Valu = a.tarray[i].Valu;
}
}
public void readsparce()
{
Console.WriteLine("Tedad Sotun : ");
this.Col = int.Parse(Console.ReadLine());
Console.WriteLine("Tedad Satr : ");
this.Row = int.Parse(Console.ReadLine());
Console.WriteLine("enter term number(elemnts oppsite 0)");
Terms = int.Parse(Console.ReadLine());
tarray = new term[Terms];
Console.WriteLine("Megdar Dehi matrix : dade hara be surate zir vared konid :");
Console.WriteLine("Row Column Value");
for (int i = 0; i < Terms;i++ )
{
string strVal = Console.ReadLine();
string [] values = strVal.Split(' ');
tarray[i] = new term(int.Parse(values[0]),
int.Parse(values[1]),
int.Parse(values[2]));
}
}//readsparce
public void writematrix()
{
Console.WriteLine("matrix is:");
for (int i = 0; i < Terms; i++)
{
Console.WriteLine(tarray[i].I + "\t" + tarray[i].J + "\t" + tarray[i].Valu);
}
Console.WriteLine();

for (int i = 1; i <= Row; i++)
{
for (int j = 1; j <= Col; j++)
{
Console.Write(this.GetValue(i, j));
Console.Write(" ");
}
Console.Write("\n");
}
}
private int GetValue(int row, int col)
{
foreach (var item in tarray)
{
if (item.I == row)
{
if (item.J == col)
return item.Valu;
}
}
return 0;
}
public class term
{
public term()
{
}
public term(int i, int j, int valu)
{
this.I = i;
this.J = j;
this.Valu = valu;
}
//////////////////////////////////////////////////////properties////////////////////////////////////////////////////////////
public int I { get; set; }
public int J { get; set; }
public int Valu { get; set; }
}
class Program
{
static void Main(string[] args)
{
Sparcematrix p = new Sparcematrix();
p.readsparce();
p.writematrix();
p.FastTranspose(p);
Console.ReadKey();
}

}

exlord
پنج شنبه 12 اسفند 1389, 12:17 عصر
public Sparcematrix transpose(Sparcematrix b)
{
Sparcematrix s = new Sparcematrix();
s.tarray = new term[b.tarray.Length];
for (int i = 0; i < b.tarray.Length;i++ )
{
s.tarray[i] = new term(b.tarray[i].J, b.tarray[i].I, b.tarray[i].Value);
}
return s;
}

parva-88
پنج شنبه 12 اسفند 1389, 14:20 عصر
خب اخه بازم مشکل داره و تازه من میخام با fast transpose ترانهاده رو چاپ کنه!!!

exlord
پنج شنبه 12 اسفند 1389, 15:36 عصر
خب اخه بازم مشکل داره و تازه من میخام با fast transpose ترانهاده رو چاپ کنه!!!

چه مشکلی ؟ منظورتون از fast transpose چیه ؟

parva-88
پنج شنبه 12 اسفند 1389, 19:41 عصر
بازم ترانهاده اش رو با کد شما غلط چاپ میکنه fast transpose کدیه که من نوشتم تو اولی!

exlord
پنج شنبه 12 اسفند 1389, 21:48 عصر
تعداد سطر و ستون رو عوض نکرده بودم من ...
public Sparcematrix transpose(Sparcematrix b)
{
Sparcematrix s = new Sparcematrix();
s.Row = b.Col;
s.Col = b.Row;
s.tarray = new term[b.tarray.Length];
for (int i = 0; i < b.tarray.Length;i++ )
{
s.tarray[i] = new term(b.tarray[i].J, b.tarray[i].I, b.tarray[i].Value);
}
return s;
}

parva-88
جمعه 13 اسفند 1389, 12:54 عصر
اون کد بازم مشکل داره من این کد رو مینویسم میخام این درست شه اگه ممکنه کمک کنید تا این رو درست کنم ممنون میشم!

namespace SparceMatrix
{
public class sparcematrix
{
private int Row;
private int Col;
private int Terms;
Term[]Data;
//************************************************** **************************************************
public void 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++;
}
}

//************************************************** *******
public void 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");
}
/************************************************** *******/
public void 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;
}
else 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 = b.Data[j].I;
Data[k].J = b.Data[j].J;
Data[k++].Valu = b.Data[j++].Valu;
}
else if( 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;
//}
/************************************************** *******/
public void FastTranspose(sparcematrix a)
{
int i, k;
int[] RowSize = new int[Terms];
int[] RowStart = new int[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;
}
}
/************************************************** *******/
public bool 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;
return false;
}
else
{
Console.WriteLine( "\n*** ERROR !!! Tedade Anasore Gheyre Sefr Az Fazaye Arayeh Biroon Mizanad ***\n");
return true;
}
else
return false;
}
/************************************************** *******/
public char compare(int x, int y)
{
if( x < y )
return '<';
else if( x == y )
return '=';
return '>';
}
/************************************************** *******/
public void 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=new sparcematrix();
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
else if( 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
else switch( 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
public class Term
{
private int i;
private int j;
private int valu;
public int I{
get
{
return i;
}
set
{
i = value;
}
}
public int J {
get
{
return j;
}
set
{
j = value;
}
}

public int Valu {
get
{
return valu;
}
set
{
valu = value;
}
}