导航:首页 > 创造发明 > 用c创造矩阵类

用c创造矩阵类

发布时间:2021-10-08 13:40:12

❶ C语言中如何定义矩阵

两种方式可以参考:
1、最简单的就是二维数组,比如存储全是整形的一个m*n的矩阵。然后可以定义int a[m][n]。
输入或者输出可以用两层循环来完成,外层控制行m比如for(i=0;i<m;++i),内层控制列n比如for(j=0;j<n;++j);
2、第二种方式就是压缩矩阵进行存储,如果学了数据结构应该比较好理解。
结构体进行封装,比如:
第一步:先定义一个有效数据的位置
typedef struct node
{
int hang;int lie;int data;//用来存储一个有效数据位的行列和值
}node;
typedef struct matrix
{
node *m;//一个数组,用来存储所有的node数据
int sum;//记录一共有多少个有效数据位
}matrix;

❷ 怎样用C语言写矩阵

用二维数组,如下:
#include <stdio.h>
main()
{
int i, j, a[4][4];

for ( i = 1; i < 4; i++ )
{
for ( j = 1; j < 4; j++ )
{
scanf ("%d", &a[i][j]);
}
}
//这样就可以将一个3*3 的矩阵存在2维数组中了

for ( i = 1; i < 4; i++ )
{
for ( j = 1; j < 4; j++ )
{
printf (" %-4d ", a[i][j]);
}
printf ("\n");
}
//这样就可以显示矩阵
return 0;
}

❸ 如何用C语言定义矩阵

矩阵其实就是二维数组,在进行编码的时候,矩阵就会被定义成为二维数组

❹ 用C语言输出矩阵

//螺旋赋值数组并输出的问题
#include<stdio.h>
#defineM100
#defineN100
intmain()
{
inta[M][N];
intm=3,n=4;//这里设置数组的维度
inti,j;
intt;
intup=0,down=m-1;
intleft=0,right=n-1;
i=up,j=left;
t=1;
while(1)
{
while(j!=right+1)
{
a[i][j]=t;
t++;
j++;
}
j--;
if((t-1)==(m*n))
{
break;
}
up++;
i=up;
while(i!=down+1)
{
a[i][j]=t;
t++;
i++;
}
if((t-1)==(m*n))
{
break;
}
i--;
right--;
j=right;
while(j!=left-1)
{
a[i][j]=t;
t++;
j--;
}
if((t-1)==(m*n))
{
break;
}
j++;
down--;
i=down;
while(i!=up-1)
{
a[i][j]=t;
t++;
i--;
}
if((t-1)==(m*n))
{
break;
}
i++;
left++;
j=left;
}
printf("---------螺旋形输出-------- ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",a[i][j]);
}
printf(" ");
}
printf("--------------------------- ");
return0;
}

哥们你很抠门儿呀,才5分!!

❺ 怎样用C语言定义一个矩阵

用二维数组

❻ 矩阵类的设计,用C++,要求:定义矩阵类,包含行、列和矩阵数据元素;

我没有重载函数,只重载了运算符,你看看行不行。

这是头文件:

#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
using std::cout;
using std::endl;

class Matrix{
public:
Matrix():row(0),column(0),m(0){}
Matrix(const Matrix& ma);
~Matrix();
void setMatrix(int r,int c,double **ma);
int getRow(){return row;}
int getColumn(){return column;}
void display();
Matrix& operator=(const Matrix& ma);
Matrix operator+(const Matrix& ma)const;
Matrix operator-(const Matrix& ma)const;
Matrix operator*(const Matrix& ma)const;
private:
int row;
int column;
double** m;
};

Matrix::Matrix(const Matrix &ma){
this->row=ma.row;
this->column=ma.column;
m=new double *[row];
for(int i=0;i<row;i++)
m[i]=new double[column];
for(int i=0;i<row;i++)
for(int j=0;j<column;j++)
m[i][j]=ma.m[i][j];
}

Matrix::~Matrix(){
for(int i=0;i<this->row;i++)
delete[] m[i];
delete[] m;
}

void Matrix::setMatrix(int r,int c,double** ma){
for(int i=0;i<this->row;i++)
delete[] m[i];
delete[] m;
this->row=r;
this->column=c;
m=new double *[row];
for(int i=0;i<row;i++)
m[i]=new double[column];
for(int i=0;i<row;i++)
for(int j=0;j<column;j++)
m[i][j]=ma[i][j];
}

void Matrix::display(){
if(m==0) cout<<"Matrix has not been initialised.\n";
else{
for(int i=0;i<row;i++){
for(int j=0;j<column;j++)
cout<<m[i][j]<<" ";
cout<<endl;
}
}
}

Matrix& Matrix::operator =(const Matrix &ma){
if(m!=0){
for(int i=0;i<this->row;i++)
delete[] m[i];
delete[] m;
}
this->row=ma.row;
this->column=ma.column;
m=new double *[row];
for(int i=0;i<row;i++)
m[i]=new double[column];
for(int i=0;i<row;i++)
for(int j=0;j<column;j++)
m[i][j]=ma.m[i][j];
return *this;
}

Matrix Matrix::operator +(const Matrix &ma) const{
double** result=new double *[this->row];
for(int i=0;i<this->row;i++)
result[i]=new double[this->column];
for(int i=0;i<this->row;i++)
for(int j=0;j<this->column;j++)
result[i][j]=this->m[i][j]+ma.m[i][j];
Matrix temp;
temp.setMatrix(this->row,this->column,result);
for(int i=0;i<this->row;i++)
delete[] result[i];
delete[] result;
return temp;
}

Matrix Matrix::operator -(const Matrix &ma) const{
double** result=new double *[this->row];
for(int i=0;i<this->row;i++)
result[i]=new double[this->column];
for(int i=0;i<this->row;i++)
for(int j=0;j<this->column;j++)
result[i][j]=this->m[i][j]-ma.m[i][j];
Matrix temp;
temp.setMatrix(this->row,this->column,result);
for(int i=0;i<this->row;i++)
delete[] result[i];
delete[] result;
return temp;
}

Matrix Matrix::operator *(const Matrix &ma) const{
double** result=new double *[this->row];
for(int i=0;i<this->row;i++)
result[i]=new double[this->column];
double entry;
for(int i=0;i<this->row;i++)
for(int j=0;j<ma.column;j++){
entry=0;
for(int k=0;k<this->column;k++)
entry=entry+(this->m[i][k])*(ma.m[k][j]);
result[i][j]=entry;
}
Matrix temp;
temp.setMatrix(this->row,ma.column,result);
for(int i=0;i<this->row;i++)
delete[] result[i];
delete[] result;
return temp;
}

#endif

这是主程序:

#include <iostream>
#include <cstring>
using std::cout;
using std::cin;
using std::endl;
#include "Matrix.h"

int main(){
int i;
Matrix op1;
Matrix op2;
Matrix result;
do{
cout<<"1.Enter operand 1 2.Enter operand 2 3.Add 4.Subtract 5.Multiply 6.Display 7.Quit"<<endl;
cout<<"Enter your choice: ";
cin>>i;
if(i==1){
int r,c;
cout<<"Enter the number of rows for operand 1: ";
cin>>r;
cout<<"Enter the number of columns for operand 1: ";
cin>>c;
double** m=new double *[r];
for(int j=0;j<r;j++)
m[j]=new double[c];
cout<<"Enter the elements of operand 1, from left to right and from top to bottom:"<<endl;
for(int j=0;j<r;j++)
for(int k=0;k<c;k++)
cin>>m[j][k];
op1.setMatrix(r,c,m);
for(int j=0;j<r;j++)
delete[] m[j];
delete m;
}
else if(i==2){
int r,c;
cout<<"Enter the number of rows for operand 2: ";
cin>>r;
cout<<"Enter the number of columns for operand 2: ";
cin>>c;
double** m=new double *[r];
for(int j=0;j<r;j++)
m[j]=new double[c];
cout<<"Enter the elements of operand 2, from left to right and from top to bottom:"<<endl;
for(int j=0;j<r;j++)
for(int k=0;k<c;k++)
cin>>m[j][k];
op2.setMatrix(r,c,m);
for(int j=0;j<r;j++)
delete[] m[j];
delete m;
}
else if(i==3){
if(op1.getRow()>0&&op1.getColumn()>0&&op2.getRow()>0&&op2.getColumn()>0&&op1.getRow()==op2.getRow()&&op1.getColumn()==op2.getColumn()){
result=op1+op2;
cout<<endl<<"The result of op1 plus op2 is:"<<endl;
result.display();
}
else cout<<"The dimensions of the two operands do not match. Unable to do the addition."<<endl;
}
else if(i==4){
if(op1.getRow()>0&&op1.getColumn()>0&&op2.getRow()>0&&op2.getColumn()>0&&op1.getRow()==op2.getRow()&&op1.getColumn()==op2.getColumn()){
result=op1-op2;
cout<<endl<<"The result of op1 minus op2 is:"<<endl;
result.display();
}
else cout<<"The dimensions of the two operands do not match. Unable to do the subtraction."<<endl;
}
else if(i==5){
if(op1.getRow()>0&&op1.getColumn()>0&&op2.getRow()>0&&op2.getColumn()>0&&op1.getColumn()==op2.getRow()){
result=op1*op2;
cout<<endl<<"The result of op1 times op2 is:"<<endl;
result.display();
}
else cout<<"The dimensions of the two operands do not match. Unable to do the multiplication."<<endl;
}
else if(i==6){
int j;
cout<<"Which matrices do you want to display?"<<endl;
cout<<"1.Operand 1 2.Operand 2 3.Result"<<endl;
cin>>j;
if(j==1) op1.display();
if(j==2) op2.display();
if(j==3) result.display();
}
else if(i==7) break;
}while(1);
return 0;
}

❼ 用c 写矩阵

写的不是很好。。时间不多,你自己可以改一下有结果在后面.

#include<stdio.h>

#defineN30

#defineM30

voidmain()

{inta[N][M];

inti,j,n,m,sum[6]={0,0,0,0,0,0};

printf(" 输入行数n:");

scanf("%d",&n);

printf(" 输入列数m:");

scanf("%d",&m);

for(i=0;i<n;i++)

{

printf(" 第%d行 ",i+1);

for(j=0;j<m;j++)

scanf("%d",&a[i][j]);

printf(" ");

}

for(i=0;i<6;i++)

{

for(j=0;j<6;j++)

{printf("%3d",a[i][j]);

sum[i]+=a[i][j];

}

printf(" ");

}

for(i=0;i<6;i++)

printf("第%d行数只和=%4d ",i,sum[i]);

}

❽ C语言如何生成一个随机矩阵

#include <stdio.h>                      
#include <stdlib.h>

#define M 10
#define N 10

int main(void)
{
int i = 0, j = 0;
int Arr[M][N] = {{0}};

srand(time(NULL));

for (i = 0; i < M; ++i)
{
for (j = 0; j < N; ++j)
{
Arr[i][j] = rand() % 1000;
}
}

printf("Array[%d][%d] is: ", M, N);
for (i = 0; i < M; ++i)
{
for (j = 0; j < N; ++j)
{
printf("%d ", Arr[i][j]);
}
printf(" ");
}
return 0;
}
Array[10][10] is:
265 610 286 703 424 389 529 686 495 6
497 111 347 957 680 980 471 596 736 728
250 457 661 348 776 795 619 701 39 149
79 304 759 366 359 184 755 240 222 602
247 719 714 594 28 746 926 499 343 14
580 593 472 241 293 600 36 913 654 428
414 733 732 174 451 444 710 558 684 932
161 931 651 227 877 680 973 804 531 316
818 463 262 642 705 907 595 93 172 249
873 587 334 958 761 786 402 471 696 86

❾ C语言 简单的矩阵生成

i=i+1;//这里你i加了1造成下面的for循环不成立
a[i][j]=count;
count++;
for (;i==0;)上面你给i加了1,使得i不等于0,for语句循环条件也就不成立。所以不进循环。
下面另一个循环不进也是相同原因。

❿ 用C语言编写如下矩阵

我改了可以运行了
加了两个变量 s,c来控制两个数组下标
那么for语句只需要输出的次数就好了

你的 主要是for语句没起做用
{for(n=1;n<=5;n++)
printf("%5d",a[i]);
就会输出5次相同a[i])

#include<stdio.h>
void main()
{int a[15],b[10];
int l,n,i,j,k;
for(i=0;i<=14;i++)
a[i]=41+i;
for(j=0;j<=9;j++)
b[j]=35-j;
int s=0,c=0;
for(l=1;l<=5;l++)
{
for(n=0;n<l;n++)
printf("%5d ",a[s++]);
for(int x=0; x<5-l;x++)
printf("%5d ",b[c++]);

printf("\n");

}
}

阅读全文

与用c创造矩阵类相关的资料

热点内容
农业信用卡积分有效期 浏览:172
马鞍山上门服务 浏览:889
校本研修成果摘抄 浏览:332
谁发明了明天 浏览:864
购买版权开发票一般开票内容写什么 浏览:817
九台工商局电话是多少 浏览:429
网培研修成果 浏览:127
股东认缴出资额期限 浏览:236
土地使用权转让协议书范本 浏览:877
银川工商局上班时间 浏览:666
西瓜谁发明的 浏览:108
莆田市工商局企业查询 浏览:490
职工安全生产保证书 浏览:951
顾亮马鞍山 浏览:961
工商局胡小勇 浏览:996
专业技术人员知识产权著作权 浏览:829
马鞍山李群 浏览:440
创造101之无敌导师 浏览:170
关于公司知识产权的内控管理制度 浏览:72
矛盾纠纷专项排查工作方案 浏览:103