⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lmatrix.cpp

📁 稀疏矩阵程序源代码
💻 CPP
字号:
#include<iostream.h>
#include<stdlib.h>
#define maxrows 100  //矩阵行数最大值100

struct triplenode
{
	int row,col;   //非零元素的行下标row ,列下标col
	float val;   //元素值
    triplenode *next;
};

struct lmatrix
{	
	int m,n,t;             // 矩阵行数m,列数 n,非零元素个数 t
	triplenode *vector[maxrows+1];  //存储m个行单链表的表头指针,其中第0分量未用
	                                 //第i行单链表的表头指针存于第i分量vector[i]中	                                 //
};


void initmatrix(lmatrix &M)  //矩阵初始化
{
	M.m=0;  M.n=0;  M.t=0;
	for(int i=1;i<=maxrows;i++)
	{
		M.vector[i]=NULL;
	}
}

void inputmatrix(lmatrix &M,int m,int n)
{
	M.m=m;  M.n=n;
	int row,col;
	float val;
	int k=1;

	//triplenode *cp;
    //cp=new triplenode;
	M.vector[1]=new triplenode; 

    cout<<"请输入第"<<k<<"个元素的行号、列号、值:";
	cin>>row>>col>>val;
	while(row!=0)
	{
		k++;
		triplenode *newptr;
		//得到和建立一个新结点
		newptr=new triplenode;
		newptr->row=row;
		newptr->col=col;
		newptr->val=val;
		newptr->next=NULL;
		//把新结点链接到所在行单链表的末尾	
		M.vector[k]->next=newptr->next;
		M.vector[k]=newptr;
		//M.vector[k]->next=newptr;
		//M.vector[k]=newptr;
		//M.vector[k]->next=newptr->next;
		//cout<<cp->row <<"  "<<cp->col<<"  "<<cp->val<<endl; 
		cout<<M.vector[k]->row<<"  "<<M.vector[k]->col<<"  "<<M.vector[k]->val<<endl;    
		cout<<"请输入第"<<k++<<"个元素的行号、列号、值:";
		cin>>row>>col>>val;
	}
	M.t=k;
}

void outputmatrix(lmatrix &M)
{
	//M.vector[1]=M.vector[1]->next;
	for(int i=1;i<M.m;i++)
	{
		for(int j=1;j<M.n;j++)
		{
			if(i==M.vector[i]->row || j==M.vector[i]->col)
			{
				cout<<M.vector[i]->val;
			    M.vector[i]=M.vector[i]->next;
			}
			else{
				cout<<0;
				}
		}
		cout<<endl;
	}
}

lmatrix lmatrixadd(lmatrix &M1,lmatrix &M2)
{
	lmatrix M;
	initmatrix(M);
	if((M1.m!=M2.m)||(M1.n!=M2.n))
	{
		cout<<"你输入的矩阵不能相加!!"<<endl;
	}
	M.m=M1.m;
	M.n=M1.n;
	if((M1.t==0)&&(M2.t==0))
		return M;
	int k=0;
	for(int i=1;i<=M1.m;i++)
	{
		triplenode *p1,*p2,*p3;
		p1=M1.vector[i];
		p2=M2.vector[i];
		p3=M.vector[i];
		while((p1!=NULL)&&(p2!=NULL))
		{
			triplenode *newptr=new triplenode;
			if(p1->col<p2->col)
			{
				*newptr=*p1;
				p1=p1->next;
			}
			else if (p1->col>p2->col)
			{
				*newptr=*p2;
				p2=p2->next;
			}
			     else  if(p1->val+p2->val==0)
				 {
					 p1=p1->next;
					 p2=p2->next;
					 continue;
				 }
				       else
					   {
						   *newptr=*p1;
						   p1=p1->next;
						   p2=p2->next;
					   }
             newptr->next=NULL;
			 if(p3==NULL)
				 M.vector[i]=newptr;
			 else
				 p3->next=newptr;
			 p3=newptr;
			 k++;			 
		} //end while 1

		while(p1!=NULL)
		{
			triplenode *newptr=new triplenode;
			*newptr=*p1;
			newptr->next=NULL;
			if(p3==NULL)
				M.vector[i]=newptr;
			else
				p3->next=newptr;
			p3=newptr;
			p1=p1->next;
			k++;			
		} //end while 2

		while(p2!=NULL)
		{
			triplenode *newptr=new triplenode;
			*newptr=*p2;
			newptr->next=NULL;
			if(p3==NULL)
				M.vector[i]=newptr;
			else
				p3->next=newptr;
			p3=newptr;
			p2=p2->next;
			k++;			
		} //end while 3

	} //end for
	M.t=k;
	return M;
}




void main()
{
	lmatrix M; 
	int m,n;
	initmatrix(M);
	cout<<"请输入矩阵地行数、列数:";
	cin>>m>>n;
	inputmatrix(M,m,n);
	outputmatrix(M);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -