PDA

View Full Version : سوال: پیاده سازی لیست پیوندی



SEZAR_200002000
سه شنبه 27 دی 1390, 15:45 عصر
80988سلام.من الگوریتم زیر رو با توجه به چیزایی که سایت یاد گرفتم نوشتم ولی بازم ایراد داره.لطفا ایراد رو بهم بگید و در صورت امکان اصلاحش هم بکنید.متشکر و منتظرم

در ضمن سوال اینه:


یک لیست پیوندی کامل را به گونه ای پیاده سازی کنیم که اعمال ریاضی را با استفاده از کلاس های تعریف شده در برنامه بر روی ماتریس ها انجام دهد.


//using namespace std;
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

int menu();
void readmatrix();
void entesab();
void addition(); //addition 2 matrices
void Subtract();
void multi(); //mutiply 2 matrices
void display( ); // display matrix to screen
void transpose ( ) ;
//*************************************************//
enum boolean {false,true};
struct Triple{int value,row,col;};
class sparsematrix;
class matrixNode{
friend class sparsematrix;
public:
matrixNode *down,*right;
boolean head;
union {
matrixNode *next;
Triple triple;
};
matrixNode(boolean,Triple*);//constructor
};//end of class matrixNode
matrixNode::matrixNode(boolean b,Triple *t)
{//constructor
head=b;
if (b){right=next=this;} //row/column head node
else triple=*t;//head node for list of headnodes OR element node
}
typedef MatrixNode *MatrixNodePtr; //to allow subsequent creation of array pointers //????
//************************************************** **********//
class sparsematrix{
public:
~sparsematrix();//destructor
void readmatrix();
void entesab();
void addition(sparsematrix ); //addition 2 matrices
void Subtract();
void multi(); //mutiply 2 matrices
void display( ); // display matrix to screen
void transpose ( ) ;
private:
matrixNode *headnode;
};
//************************** main() **********************************//
int main(){
clrscr();
sparsematrix a;
sparsematrix b;
sparsematrix c;
int p;
for(;;){
p=menu();
switch(p){
case 1:readmatrix(); break;
case 2:display(); break;
case 3:~sparsematrix(); break;
case 4:entesab(); break;
case 5:addition(); break;
case 6:Subtract(); break;
case 7:multi(); break;
case 8:transpose(); break;
case 9:exit(0); break;
}//end of switch
}//end of for
}//end of main
//*************************** menu() **************************//
int menu(){
int p;
cout<<" ------- MENU ------- \n" ;
cout<<"1.Read matrix"<<endl;
cout<<"2.display matrix to screen"<<endl;
// cout<<"3.~sparsematrix"<<endl;
cout<<"4.entesab"<<endl;
cout<<"5.addition 2 matrices"<<endl;
cout<<"6.subtract 2 matrices"<<endl;
cout<<"7.multiply 2 matrices"<<endl;
cout<<"8.transpose of sparse matrix"<<endl;
cout<<"9.exit"<<endl;
cout<<"enter your select(1-9)"<<endl;
cin>>p;
// cin.get();
return p;
}
//********************* readmatrix() *************************//
istream &operator>>(istream& is,sparsematrix& sparsematrix)
{
Triple s;
int p;
int i;
is>>s.row>>s.col>>s.value;// abaade matrix
if(s.row>s.col) p=s.row;
else p=s.col;
sparsematrix.headnode=new matrixNode(false,&s);
if(p==0){sparsematrix.headnode->right=sparsematrix.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=head[currentRow];
currentRow=t.row;
last=head[currentRow];
}//end of if
last=last->right=new matrixNode(false,&t); //link new node into row list
head[t.col]->next=head[t.col]->next->down=last; //link into column list
}//end of for
last->right=head[currentRow]; //close last row
for(i=0;i<s.col;i++)
head[i]->next->down=head[i]; //close all column list
// link the head nodes together
for(i=0;i<p-1;i++) head[i]->next=head[i+1];
head[p-1]->next=sparsematrix.headnode;
sparsematrix.headnode->right=head[0];
delete []head;
return is;
}
//****************************** ~sparsematrix() ***********************************//
sparsematrix::~sparsematrix()
{
matrixNode *av=new matrixNode;
if(!headnode) return; // no node to dispose
matrixNode *x=headnode->right *y;
headnode->right=av; av=headnode;//return headnode
while(x!=headnode){ //eraser by rows
y=x->right;
x->right=av;
av=y;
x=x->next;//next row
}
headnode=0;
}
//**************************** addition() **************************************//
void sparsematrix::addition(sparsematrix b) {
node *lead1, *lead2;
int m,n,m1,n1,i,j,kq;
Triple s;
//rows1 and cols1 are stored size of matrix 1 when call init_matrix1
m=s.row;
n=s.col;
m1=b.row;
n1=b.col;
if ((m!=m1) || (n!=n1))
{
cout<<"Size of matrices is not the same !!";
return;
}
sparsematrix c(s.row,s.col);
for (i=0;i<= ;i++) // ??
for (j=0;j<=;j++ )
{
kq=0;
if (headnode[i]->right!=NULL)
{
lead1=headnode[i]->right;
while (lead1!=NULL)
{
if ((lead1->s.row==(i+1)) && (lead1->s.col==(j+1)))
kq=kq+lead1->value;
lead1=lead1->right;
} }
if (b.headnode[i]->right!=NULL)
{
lead2=b.headnode[i]->right;
while (lead2!=NULL)
{
if ((lead2->s.row==(i+1)) && (lead2->s.col==(j+1)))
kq=kq+lead2->s.value;
lead2=lead2->right;
}
}
if (kq!=0)

/// c.insert_remove(i+1,j+1,kq); } // ????
char fn[LGMAX+1]; //?????
cout<<"Write to file:";
gotoxy(7,25);
cin>>fn;
c.write_file(fn);

cout<<"FIRST MATRIX";
display();
getch();
cout<<"SECOND MATRIX";
b.display();
getch();
cout<<"THE RESULT OF ADDITION MATRICE";
c.display();
}
//********************************** Operator-=( ) ***********************//
void sparsematrix::subtract(sparsematrix b)


//******************************** sazandeye copy ****************************************//


//******************************** operator*= () ****************************************//
void sparsematrix:: multi(sparsematrix b)
{
node *lead1, *lead2;
int m,n,i,j,kq,k,m1,n1;
Triple p;
//rows1 and cols1 are stored size of matrix 1 when call init_matrix1
m=p.row;
n=b.col; // ??
if (p.col!=b.row)
{
cout<<"Sizes of matrices are not allow for multiplication...";
return;
}
matrix c(p.row,b.col);
for (i=0;i ) // ??
{
if (headnode[i]->right!=NULL) //
for(k=0;k) //??
{
kq=0;
lead1=headnode[i]->right;
while (lead1!=NULL)
{
lead2=b.headnode[k]->down; //***farghe headnode ba colhead ?????
while (lead2!=NULL)
{
if (lead1->p.col==lead2->row)
kq=kq+lead1->value*lead2->value;
lead2=lead2->down;
}
lead1=lead1->right; }
if (kq!=0)
// c.insert_remove(i+1,k+1,kq); // ?????
}
}
char fn[LGMAX+1]; //????
gotoxy(5,24);cout<<"Write to file:";gotoxy(7,25);cin>>fn;
c.write_file(fn);

gotoxy(22,2);
cout<<"FIRST MATRIX";
display();
getch();
gotoxy(22,2);
cout<<"SECOND MATRIX";
b.display();
getch();
gotoxy(22,2);
cout<<"THE RESULT OF MULTIPLY MATRICE";
c.display();
}

//****************************** transpose() *******************************************//
void sparsematrix:: transpose(sparsematrix b){

}
//******************************** display() ****************************************//
void SparseMatrix::display()
{
Node* Temp;
for (int i = 0; i < this->m; i ++)
{
for (int j = 0; j < this->n; j ++)
{
Temp = GetNode(i, j, this->headnode); //????
if (Temp == NULL)
cout << "\t-";
else
cout << "\t" << Temp->data;
}//end of for2
cout << endl;
}//end of for1

}
//*************************** operator=() *******************************//
void SparseMatrix::entesab(SparseMatrix a)
{
for (int i = 0; i < this->m; i ++)
this->DeleteRow(i); //????
this->m = a.m;
this->n = a.n;
this->headnode = NULL;
for (i = 0; i < a.m; i ++)
{
for (int j = 0; j < a.n; j ++)
{
Node* Temp = GetNode(i, j, a.headnode); //???
if (Temp != NULL)
this->headnode = InsertNode(i,j,Temp->data,this->headnode); //????
}
}
}
//************************************************** *********//



خود برنامه رو هم در فایل ضمیمه گذاشتم در صورت تمایل یه نگاهی بهش بندازید.ممنون



بی صبرانه منتظر کمک شما مهندسین عزیز هستم.