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

📄 matrix.cpp

📁 这个一个有关矩阵运算的程序
💻 CPP
字号:
#include "Matrix.h"
#include<fstream.h>
#include<stdlib.h>
#include<math.h>
#include"stdio.h"
#define max 50
MatrixNode::MatrixNode(Triple  t){
	triple.col =t.col ;
	triple.row =t.row ;
	triple.value =t.value ;
	link=NULL;
}
Matrix::Matrix()
{
	headnode=NULL;
	Current=NULL;
	rows=0;
	column=0;
}
void Matrix::Add (Triple triple)
{
	MatrixNode* node=new MatrixNode(triple);//构造新的节点
	if(headnode==NULL){//如果矩阵为空
		headnode =node;
		Current =node;
		node->link =NULL;
	}
	else//如果矩阵中有元素,则元素添加到现有元素之后
	{
	       Current->link  =node;
	       Current=node;
	       
	}      
}
void Matrix::Transpose ()
{
	MatrixNode* temp;
	temp=headnode;
	while(temp !=NULL)//交换元素的行列值
	{
		int t=0;
		t=temp->triple.col  ;
		temp->triple.col =temp->triple.row ;
		temp->triple.row =t;
		temp=temp->link ;
	}
	int x;
	x=rows;
	rows=column;
	column=x;

}
void Matrix::Print ()//打印作的不是很好,其中用到了一个二维数组,需要进一步改善
{
	if(headnode==NULL)
	{
		cout<<"还没有读取矩阵"<<endl;
		return;
	}
	MatrixNode* temp=NULL;
	float Pt[max][max];
	for(int i =0 ; i< max;i++){
		for(int j=0;j<max;j++){
			Pt[i][j]=0;
		}
	}
	temp =headnode ;
	while(temp !=NULL)
	{
		Pt[temp->triple.row][temp->triple.col]=temp->triple.value ;
		temp =temp->link ;
	}
	for(int m =0 ;m < rows; m++){
		for(int n=0;n<column;n++){
			cout<<Pt[m][n]<<"      ";
		}
		cout<<endl;
	}
}
void main()
{
	Matrix matrix;
	Triple triple;
	FILE *filein;
	char oneline[255];
	int num, x ,y;
	float z;
	filein = fopen("dat.txt", "rt");			// 载入数据
        readstr(filein,oneline);
	sscanf(oneline, "%i %i %i ", &triple.col , &triple.row,&num  );
	matrix.column =triple.col;
	matrix.rows =triple.row ;
	for (int loop = 0; loop < num; loop++)
	{
		readstr(filein,oneline);
		sscanf(oneline, "%i %i %f ", &x , &y , &z );
		triple.row =x;
		triple.col =y;
		triple.value =z;
		matrix.Add(triple);
	}
	fclose(filein);
	cout<<"学号:200528622108006"<<endl;
	cout<<"姓名:柳彤  "<<endl; 

	cout<<"原矩阵:"<<endl;
	matrix.Print ();
	matrix.Transpose ();
	cout<<"转置后矩阵:"<<endl;
	matrix.Print ();

}
		
	



	
	

⌨️ 快捷键说明

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