BeginnerProgrammer
جمعه 05 آذر 1389, 17:24 عصر
سلام، کد زیر برنامه خوندن ماتریس خلوت با استفاده از لیست پیوندی لطفا اگه کسی کدو فهمید توضیح بده
istream& operator>>(istream& is, Matrix& matrix)
{ Triple s; int p; is >> s.row >> s.col >> s.value;
if (s.row > s.col) p = s.row; else p = s.col;
matrix.headnode = new MatrixNode(FALSE, &s);
if (p==0) {matrix.headnode->right = matrix.headnode; return is;}
MatrixNodePtr *head = new MatrixNodePtr[p];
for (int i = 0; i < p; i++) head[i] = new MatrixNode(TRUE, 0);
int CurrentRow = 0; MatrixNode *last = head[0];
for (i = 0; i < s.value; i++)
{ Triple t; is >> t.row >> t.col >> t.value;
if (t.row > CurrentRow) {
last->right = head[CurrentRow];
CurrentRow = t.row;
last = head[CurrentRow]; }
last = last->right = new MatrixNode(FALSE, &t);
head[t.col]->next = head[t.col]->next->down = last;
}
last->right = head[CurrentRow];
for (i = 0; i < s.col; i++) head[i]->next->down = head[i];
for (i = 0; i < p-1 ; i++) head[i]->next = head[i+1];
head[p-1]->next = matrix.headnode;
matrix.headnode->right = head[0];
delete [] head;
return is;
}
اینم کلاسایی که استفاده شده:
struct Triple{int value,row,clo;}
class Matrix;
class MatrixNode{
friend class Matrix;
friend istream& operator>>(istream&,Matrix&);
MatrixNode *down,*right;
bool head; //a head node or not
union{ //anonymous union
MatrixNode *next;
Triple triple;
};
public:
MatrixNode(bool b,Triple *t): head(b){//ctor
if(b){ right=down=next=this;}
else triple =*t;
}
};
typedef MatrixNode *MatrixNodePtr;
class Matrix{
friend istream& operator>>(istream&,Matrix&);
MatrixNode *headnode;
public:
~Marix();//dtor
};
istream& operator>>(istream& is, Matrix& matrix)
{ Triple s; int p; is >> s.row >> s.col >> s.value;
if (s.row > s.col) p = s.row; else p = s.col;
matrix.headnode = new MatrixNode(FALSE, &s);
if (p==0) {matrix.headnode->right = matrix.headnode; return is;}
MatrixNodePtr *head = new MatrixNodePtr[p];
for (int i = 0; i < p; i++) head[i] = new MatrixNode(TRUE, 0);
int CurrentRow = 0; MatrixNode *last = head[0];
for (i = 0; i < s.value; i++)
{ Triple t; is >> t.row >> t.col >> t.value;
if (t.row > CurrentRow) {
last->right = head[CurrentRow];
CurrentRow = t.row;
last = head[CurrentRow]; }
last = last->right = new MatrixNode(FALSE, &t);
head[t.col]->next = head[t.col]->next->down = last;
}
last->right = head[CurrentRow];
for (i = 0; i < s.col; i++) head[i]->next->down = head[i];
for (i = 0; i < p-1 ; i++) head[i]->next = head[i+1];
head[p-1]->next = matrix.headnode;
matrix.headnode->right = head[0];
delete [] head;
return is;
}
اینم کلاسایی که استفاده شده:
struct Triple{int value,row,clo;}
class Matrix;
class MatrixNode{
friend class Matrix;
friend istream& operator>>(istream&,Matrix&);
MatrixNode *down,*right;
bool head; //a head node or not
union{ //anonymous union
MatrixNode *next;
Triple triple;
};
public:
MatrixNode(bool b,Triple *t): head(b){//ctor
if(b){ right=down=next=this;}
else triple =*t;
}
};
typedef MatrixNode *MatrixNodePtr;
class Matrix{
friend istream& operator>>(istream&,Matrix&);
MatrixNode *headnode;
public:
~Marix();//dtor
};