65,211
社区成员
发帖
与我相关
我的任务
分享2.1 5.4 0.0 7.9 6.4
2.1 5.4 0.0 7.9 6.4
2.1 5.4 0.0 7.9 6.4
2.1 5.4 0.0 7.9 6.4
2.1 5.4 0.0 7.9 6.4
Press any key to continue#include "stdafx.h"
#include <iostream>
using namespace std;
#define N 5
typedef struct node{
unsigned int row;
unsigned int column;
double data;
struct node *next;
}node,*pnode;
void mattolin(pnode &l,const double (*m)[N]) /*matric to linked list*/
{
if(l != NULL)
{
cout<<"Linked list is not NULL"<<endl;
exit(0);
}
pnode p;
int i,j;
l = new node;
l->next=NULL;
p=l;
for( i= 0;i < N;i++)
{
for(j = 0;j < N;j++)
{
if((*m)[i*N+j] != 0.0)
{
p->next = new node;
p->next->row = i;
p->next->column = j;
p->next->data = (*m)[i*N+j];
p->next->next = NULL;
p=p->next;
}
}
}
}
void Add2mat(const pnode &l1,const pnode &l2,pnode &l3) /*add the two matrices to linked list 3*/
{
if(l1 == NULL || l2 == NULL || l3 !=NULL)
{
exit(0);
}
int i,j;
pnode p,q,t;
l3 = new node;
l3->next=NULL;
t = l3;
p = l1->next;
q = l2->next;
for(i = 0;i < N;i++)
{
for(j = 0;j < N;j++)
{
if(p != NULL && q !=NULL)
{
if(p && q && p->row == i && p->column == j && q->row == i && q->column == j)
{
t ->next= new node;
t->next->next = NULL;
t->next->row = i;
t->next->column = j;
t->next->data = p->data + q->data;
t = t->next;
p = p->next;
q = q->next;
}
if(p && q && (p->row != i || p->column != j) && (q->row == i && q->column == j))
{
t->next = new node;
t->next->next = NULL;
t->next->row = i;
t->next->column = j;
t->next->data = q->data;
t = t->next;
q = q->next;
}
if(p && q && (p->row == i && p->column == j) && (q->row != i || q->column != j))
{
t->next = new node;
t->next->next = NULL;
t->next->row = i;
t->next->column = j;
t->next->data = p->data;
t = t->next;
p = p->next;
}
}
else if(p != NULL)
{
if(p->row == i && p->column == j)
{
t->next = new node;
t->next->next = NULL;
t->next->row = i;
t->next->column = j;
t->next->data = p->data;
t = t->next;
p = p->next;
}
}
else if(q != NULL)
{
if( q->row == i && q->column == j)
{
t->next = new node;
t->next->next = NULL;
t->next->row = i;
t->next->column = j;
t->next->data = q->data;
t = t->next;
q = q->next;
}
}
}
}
}
void printmatric(const pnode &l) /*print the result matric*/
{
if( l == NULL)
{
cout<<"Linked list is NULL"<<endl;
exit(0);
}
int i,j;
pnode p;
p = l->next;
for(i = 0;i < N;i++)
{
for(j = 0;j < N;j++)
{
if(p != NULL)
{
if(p->row == i && p->column == j)
{
cout << p->data<< ' ';
p = p->next;
continue;
}
}
cout<<"0.0"<<' ';
}
cout<<endl;
}
}
void destroyList(pnode &l)
{
if( l == NULL)
return;
pnode p = l->next;
while(p)
{
l->next = p->next;
delete p;
p = l->next;
}
delete l;
}
void main()
{
double matric1[N][N] = {{2.1,0.0,0.0,6.3,3.5},
{2.1,0.0,0.0,6.3,3.5},
{2.1,0.0,0.0,6.3,3.5},
{2.1,0.0,0.0,6.3,3.5},
{2.1,0.0,0.0,6.3,3.5}};
double matric2[N][N] = {{0.0,5.4,0.0,1.6,2.9},
{0.0,5.4,0.0,1.6,2.9},
{0.0,5.4,0.0,1.6,2.9},
{0.0,5.4,0.0,1.6,2.9},
{0.0,5.4,0.0,1.6,2.9}};
pnode l1 = NULL,l2 = NULL,l3 = NULL;
mattolin(l1,matric1);
mattolin(l2,matric2);
Add2mat(l1,l2,l3);
printmatric(l3);
destroyList(l1);
destroyList(l2);
destroyList(l3);
}#include<iostream>
using namespace std;
template<typename T>
class Node
{
public:
T data;
Node* next;
};
template<typename T, int LEN>
class Matrix
{
private:
Node<T>* head[LEN];
public:
Matrix()
{
for (int i = 0; i < LEN; i++)
{
head[i] = NULL;
}
}
Matrix(const Matrix<T,LEN>& rhs)
{
for (int i = 0; i < LEN; i++)
{
if ( rhs.head[i] == NULL )
return;
}
for (int j = 0; j < LEN; j++)
{
Node<T>* s;
s = rhs.head[j];
Node<T>* current = new Node<T>;
current -> data = s-> data;
s = s -> next;
head[j] = current;
while ( s != NULL )
{
Node<T>* nex = new Node<T>;
current -> next = nex;
nex -> data = s->data;
current = nex;
s = s -> next;
}
current -> next = NULL;
}
}
void operator = (const Matrix<T,LEN>& rhs)
{
for (int i = 0; i < LEN; i++)
{
if ( rhs.head[i] == NULL )
return;
}
for (int j = 0; j < LEN; j++)
{
Node<T>* s;
s = rhs.head[j];
Node<T>* current = new Node<T>;
current -> data = s-> data;
s = s -> next;
head[j] = current;
while ( s != NULL )
{
Node<T>* nex = new Node<T>;
current -> next = nex;
nex -> data = s->data;
current = nex;
s = s -> next;
}
current -> next = NULL;
}
}
Matrix operator+(const Matrix& rhs)
{
Matrix* result = new Matrix();
for (int i = 0; i < LEN; i++)
{
Node<T>* right = rhs.head[i];
Node<T>* left = this->head[i];
for (int j = 0; j < LEN; j++)
{
Node<T>* newp = new Node<T>;
newp -> next = NULL;
newp->data = right->data + left->data;
right = right->next;
left = left->next;
if ( result->head[i] == NULL )
{
newp -> next = result->head[i];
result->head[i] = newp;
}
else
{
Node<T>* q = result->head[i];
while ( q -> next != NULL )
q = q -> next;
q -> next = newp;
}
}
}
return *result;
}
friend istream& operator>> (istream& ins, Matrix<T,LEN>& obj)
{
for (int i = 0; i < LEN; i++)
{
for (int j = 0; j < LEN; j++)
{
Node<T>* newp = new Node<T>;
ins>>newp->data;
newp -> next = NULL;
if ( obj.head[i] == NULL )
{
newp -> next = obj.head[i];
obj.head[i] = newp;
}
else
{
Node<T>* q = obj.head[i];
while ( q -> next != NULL )
q = q -> next;
q -> next = newp;
}
}
}
return ins;
}
friend ostream& operator << (ostream& out, Matrix<T, LEN>& obj)
{
for (int i = 0; i < LEN; i++)
{
Node<T>* temp = obj.head[i];
for (int j = 0; j < LEN; j++)
{
out<<temp->data<<" ";
temp = temp->next;
}
out<<endl;
}
return out;
}
~Matrix()
{
for (int i = 0; i < LEN; i++)
{
Node<T> *p = head[i], *q;
while ( p != NULL )
{
q = p -> next;
delete p;
p = q;
}
head[i] = NULL;
}
}
};
int main()
{
Matrix<int,5> a;
Matrix<int,5> b;
Matrix<int,5> c; //输入一个五行五列的矩阵
cout<<"请输入第一个矩阵"<<endl;
cin>>a;
cout<<"第一个矩阵为:"<<endl<<a;
cout<<"请输入第二个矩阵"<<endl;
cin>>b;
cout<<"第二个矩阵为:"<<endl<<b;
c = a + b;
cout<<"两矩阵相加的结果为:"<<endl<<c;
return 0;
}#include <iostream>
using namespace std;
#define N 5
typedef struct node{
unsigned int row;
unsigned int column;
double data;
struct node *next;
}node,*pnode;
void mattolin(pnode &l,const double (*m)[N]) /*matric to linked list*/
{
if(l != NULL)
{
cout<<"Linked list is not NULL"<<endl;
exit(0);
}
pnode p;
int i,j;
l = new node;
p=l;
for( i= 0;i < N;i++)
{
for(j = 0;j < N;j++)
{
if((*m)[i*N+j] != 0.0)
{
p->next = new node;
p->next->row = i;
p->next->column = j;
p->next->data = (*m)[i*N+j];
p->next->next = NULL;
p=p->next;
}
}
}
}
void Add2mat(const pnode &l1,const pnode &l2,pnode &l3) /*add the two matrices to linked list 3*/
{
if(l1 == NULL || l2 == NULL || l3 !=NULL)
{
cout<<"Linked list is not NULL"<<endl;
exit(0);
}
int i,j;
pnode p,q,t;
l3 = new node;
t = l3;
p = l1->next;
q = l2->next;
for(i = 0;i < N;i++)
{
for(j = 0;j < N;j++)
{
if(p->row == i && p->column == j && q->row == i && q->column == j)
{
t ->next= new node;
t->next->next = NULL;
t->next->row = i;
t->next->column = j;
t->next->data = p->data + q->data;
p = p->next;
q = q->next;
}
if(p->row != i && p->column != j && q->row == i && q->column == j)
{
t->next = new node;
t->next->next = NULL;
t->next->row = i;
t->next->column = j;
t->next->data = q->data;
t=t->next;
q = q->next;
}
if(p->row == i && p->column == j && q->row != i && q->column != j)
{
t->next = new node;
t->next->next = NULL;
t->next->row = i;
t->next->column = j;
t->next->data = p->data;
t = t->next;
p = p->next;
}
}
}
}
void printmatric(const pnode &l) /*print the result matric*/
{
if( l == NULL)
{
cout<<"Linked list is NULL"<<endl;
exit(0);
}
int i,j;
pnode p;
p = l->next;
for(i = 0;i < N;i++)
{
for(j = 0;j < N;j++)
{
if(p != NULL)
{
if(p->row == i && p->column == j)
{
cout << p->data<< ' ';
p = p->next;
continue;
}
}
cout<<"0.0"<<' ';
}
cout<<endl;
}
}
void destroyList(pnode l)
{
if( l == NULL)
return;
pnode p = l->next;
while(p)
{
l->next = p->next;
delete p;
}
delete l;
}
void main()
{
double matric1[N][N] = {{2.1,0.0,0.0,6.3,3.5},
{2.1,0.0,0.0,6.3,3.5},
{2.1,0.0,0.0,6.3,3.5},
{2.1,0.0,0.0,6.3,3.5},
{2.1,0.0,0.0,6.3,3.5}};
double matric2[N][N] = {{0.0,5.4,0.0,1.6,2.9},
{0.0,5.4,0.0,1.6,2.9},
{0.0,5.4,0.0,1.6,2.9},
{0.0,5.4,0.0,1.6,2.9},
{0.0,5.4,0.0,1.6,2.9}};
pnode l1 = NULL,l2 = NULL,l3 = NULL;
mattolin(l1,matric1);
mattolin(l2,matric2);
printmatric(l1);
Add2mat(l1,l2,l3);
printmatric(l3);
destroyList(l1);
destroyList(l2);
destroyList(l3);
}