注意!!!只是添加生成矩阵A和B,显示矩阵,矩阵相加这几个函数的代码和有?的地方,不能改变其他内容!
图片中有要求!!!

将代码添加完整,使输出如图所示
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- fx_dev 2016-12-03 11:04关注
完整的示例程序如下:
#includetypedef struct
{
int pMat;/保存矩阵数据*/
int row;
int col;
}MAT,*pMAT;
/*字符串类型*/
typedef char * string;
/*生成矩阵A*/
void CreateMatA(pMAT a);
/*生成矩阵B*/
void CreateMatB(pMAT a);
/*显示矩阵*/
void ShowMat(string str,pMAT a);
/*矩阵相加*/
void MatAdd(pMAT a,pMAT b,pMAT c);void main()
{
int m = 3;
int n = 4;
MAT A={0,m,n},B={0,m,n};
pMAT C = new MAT;CreateMatA(&A); CreateMatB(&B); MatAdd(&A,&B,C); ShowMat("A:",&A); ShowMat("B:",&B); ShowMat("C=A+B:",C); delete A.pMat;/*释放内存*/ delete B.pMat; delete C->pMat; delete C; printf("按任意键退出"); getch();/*按任意键退出*/
}
void CreateMatA(pMAT a)
{
int row,col;
int offset = 0;
int nV1,nV2;
a->pMat = new int[a->row * a->col];
for(row = 0;row < a->row;row++)
{
nV1 = (row + 1) * 10;
for (col = 0;col < a->col;col++)
{
nV2 = col * 2;
offset = row * a->col + col;
a->pMat[offset] = nV1 + nV2;
}
}
}
void CreateMatB(pMAT a)
{
int row,col;
int offset = 0;
int nV1,nV2;
a->pMat = new int[a->row * a->col];
for(row = 0;row < a->row;row++)
{
nV1 = (row + 1) * 10;
for (col = 0;col < a->col;col++)
{
nV2 = col * 1;
offset = row * a->col + col;
a->pMat[offset] = nV1 + nV2;
}
}
}
void ShowMat(string str,pMAT a)
{
int row,col;
int offset = 0;
printf("%s\n",str);
for(row = 0;row < a->row;row++)
{
for (col = 0;col < a->col;col++)
{
offset = row * a->col + col;
printf("%2d ",a->pMat[offset]);
}
printf("\n");
}
printf("\n");
}
void MatAdd(pMAT a,pMAT b,pMAT c)
{
int row,col;
int offset = 0;
c->row = a->row;
c->col = a->col;
c->pMat = new int[a->row * a->col];
for(row = 0;row < a->row;row++)
{
for (col = 0;col < a->col;col++)
{
offset = row * a->col + col;
c->pMat[offset] = a->pMat[offset] + b->pMat[offset];
}
}
}
运行结果如下:本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报