نمایش نتایج 1 تا 7 از 7

نام تاپیک: رفع مشکل کد

  1. #1

    رفع مشکل کد

    من برنامه ی ماتریس ها رو نوشتم و مطمنم که کدهاش درسته(کد ترانهاده)ولی نمیدونم چرا موقع اجرا ترانهاده رو غلط چاپ میکنه خاهشا نگا کنید و بگین خیلی ضروریه

    publicclassSparcematrix
    {
    /// ///////////////////////////////////////////////////data member////////////////////////////////////////////////////////////
    publicint Row { get; set; }
    publicint Col { get; set; }
    publicint Size { get; set; }
    publicint Terms { get; set; }
    privateterm[] tarray;
    ///////////////////////////////////////////////////////ctor//////////////////////////////////////////////////////////////////
    public Sparcematrix(int Maxrow, int Maxcol)
    {
    this.Row = Maxrow;
    this.Col = Maxcol;
    }
    public Sparcematrix()
    {
    }
    /// ///////////////////////////////////////////////////Methods/////////////////////////////////////////////////////////////
    publicvoid FastTranspose(Sparcematrix a )
    {
    int i, k;
    int[] RowSize=newint[Col];
    int[] RowStart = newint[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;
    }
    }
    publicvoid 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 =
    newterm[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] =
    newterm(int.Parse(values[0]),
    int.Parse(values[1]),
    int.Parse(values[2]));
    }
    }
    //readsparce
    publicvoid 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");
    }
    }
    privateint GetValue(int row, int col)
    {
    foreach (var item in tarray)
    {
    if (item.I == row)
    {
    if (item.J == col)
    return item.Valu;
    }
    }
    return 0;
    }
    publicclassterm
    {
    public term()
    {
    }
    public term(int i, int j, int valu)
    {
    this.I = i;
    this.J = j;
    this.Valu = valu;
    }
    //////////////////////////////////////////////////////properties////////////////////////////////////////////////////////////
    publicint I { get; set; }
    publicint J { get; set; }
    publicint Valu { get; set; }
    }
    classProgram
    {
    staticvoid Main(string[] args)
    {
    Sparcematrix p = newSparcematrix();
    p.readsparce();
    p.writematrix();
    p.FastTranspose(p);
    Console.ReadKey();
    }

    }

  2. #2

    نقل قول: رفع مشکل کد

            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;
    }

  3. #3

    نقل قول: رفع مشکل کد

    خب اخه بازم مشکل داره و تازه من میخام با fast transpose ترانهاده رو چاپ کنه!!!

  4. #4

    نقل قول: رفع مشکل کد

    نقل قول نوشته شده توسط parva-88 مشاهده تاپیک
    خب اخه بازم مشکل داره و تازه من میخام با fast transpose ترانهاده رو چاپ کنه!!!
    چه مشکلی ؟ منظورتون از fast transpose چیه ؟

  5. #5

    نقل قول: رفع مشکل کد

    بازم ترانهاده اش رو با کد شما غلط چاپ میکنه fast transpose کدیه که من نوشتم تو اولی!

  6. #6

    نقل قول: رفع مشکل کد

    تعداد سطر و ستون رو عوض نکرده بودم من ...
            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;
    }

  7. #7

    نقل قول: رفع مشکل کد

    اون کد بازم مشکل داره من این کد رو مینویسم میخام این درست شه اگه ممکنه کمک کنید تا این رو درست کنم ممنون میشم!

    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
    return
    false;
    }
    /************************************************** *******/
    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;
    }
    }

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •