parva-88
پنج شنبه 12 اسفند 1389, 13: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();
}
}
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();
}
}