64,281
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <iomanip>
#include <memory>
#include <cstring>
#define MAX_ROW 50
#define MAX_COLUMN 50
using namespace std;
typedef struct
{
float data[MAX_ROW][MAX_COLUMN];
int rows, columns;
} M, *pM;
void inputMatrix(M *m)
{
int i, j;
//printf("请输入矩阵的行数和列数:\n");
cout << "请输入矩阵的行数和列数:\n";
//scanf("%d", &m->rows);
cin >> m->rows;
//scanf("%d", &m->columns);
cin >> m->columns;
//printf("请按行输入矩阵的数据:\n");
cout << "请按行输入矩阵的数据:\n";
for (i = 0; i < m->rows; i++) {
for (j = 0; j < m->columns; j++) {
//scanf("%f", &m->data[i][j]);
cin >> m->data[i][j];
}
}
}
void displayMatrix(M *m)
{
int i, j;
for (i = 0; i < m->rows; i++) {
for (j = 0; j < m->columns; j++) {
//printf("%10.3f ", m->data[i][j]);
cout.setf(ios::fixed);
cout.precision(3);
cout << setw(10) << m->data[i][j];
}
//printf("\n");
cout << endl;
}
//printf("\n");
cout << endl;
}
void transposeMatrix(M *m, M *res)
{
int i, j;
for (i = 0; i < m->rows; i++) {
for (j = 0; j < m->columns; j++) {
res->data[j][i] = m->data[i][j];
}
}
}
bool addMatrix(M *a, M *b, M *res)
{
int i, j;
if (a->rows == b->rows && a->columns == b->columns) {
for (i = 0; i < a->rows; i++) {
for (j = 0; j < a->columns; j++) {
res->data[i][j] = a->data[i][j] + b->data[i][j];
}
}
}
else {
//printf("矩阵不能相加\n");
cout << "矩阵不能相加\n";
return false;
}
res->rows = a->rows;
res->columns = a->columns;
return true;
}
bool multiplyMatrix(M *a, M *b, M *res)
{
int i, j, k;
if (a->columns == b->rows) {
for (i = 0; i < a->rows; i++) {
for (j = 0; j < b->columns; j++) {
for (k = 0; k < a->columns; k++) {
res->data[i][j] += a->data[i][k] * b->data[k][j];
}
}
}
}
else {
//printf("矩阵不能相乘\n");
cout << "矩阵不能相乘\n";
return false;
}
res->rows = a->rows;
res->columns = b->columns;
return true;
}
bool scalarMultiplyMatrix(M *m, float n, M *res)
{
int i, j;
for (i = 0; i < m->rows; i++) {
for (j = 0; j < m->columns; j++) {
res->data[i][j] = m->data[i][j] * n;
}
}
res->rows = m->rows;
res->columns = m->columns;
return true;
}
bool subtractMatrix(M *a, M *b, M *res)
{
int i, j;
if (a->rows == b->rows && a->columns == b->columns) {
for (i = 0; i < a->rows; i++) {
for (j = 0; j < a->columns; j++) {
res->data[i][j] = a->data[i][j] - b->data[i][j];
}
}
}
else {
//printf("矩阵不能相减\n");
cout << "矩阵不能相减\n";
return false;
}
res->rows = a->rows;
res->columns = a->columns;
return true;
}
int main()
{
M A, B, res, finalRes, *tmpArg1, *tmpArg2;
bool bErr;
int sel;
char op[32], arg1[32], arg2[32];
//初始化
LABEL:
memset(&A, 0, sizeof(M));
memset(&B, 0, sizeof(M));
memset(&res, 0, sizeof(M));
memset(&finalRes, 0, sizeof(M));
//printf("按提示输入矩阵A:\n");
cout << "按提示输入矩阵A:\n";
inputMatrix(&A);
//printf("按提示输入矩阵B:\n");
cout << "按提示输入矩阵A:\n";
inputMatrix(&B);
LABEL1:
//printf("选择要进行的操作:\n");
cout << "选择要进行的操作:\n";
//printf("1.输入算式\n");
cout << "1.输入算式\n";
//printf("2.矩阵转置\n");
cout << "2.矩阵转置\n";
//printf("3.打印矩阵\n");
cout << "3.打印矩阵\n";
//printf("4.重新输入\n");
cout << "4.重新输入\n";
//printf("0.退出\n");
cout << "0.退出\n";
//scanf("%d", &sel);
cin >> sel;
memset(op, 0, 32);
memset(arg1, 0, 32);
memset(arg2, 0, 32);
switch (sel) {
case 1:
memset(&finalRes, 0, sizeof(M));
//scanf("%s", op);
cin >> op;
//scanf("%s", arg1);
cin >> arg1;
//scanf("%s", arg2);
cin >> arg2;
if (!strcmp(arg1, "A")) {
tmpArg1 = &A;
}
else if (!strcmp(arg1, "B")) {
tmpArg1 = &B;
}
else if (!strcmp(arg1, "res")) {
tmpArg1 = &res;
}
if (!strcmp(arg2, "B")) {
tmpArg2 = &B;
}
else if (!strcmp(arg2, "A")) {
tmpArg2 = &A;
}
else {
tmpArg2 = &res;
}
if (!strcmp(op, "add")) {
bErr = addMatrix(tmpArg1, tmpArg2, &finalRes);
}
else if (!strcmp(op, "sub")) {
bErr = subtractMatrix(tmpArg1, tmpArg2, &finalRes);
}
else if (!strcmp(op, "mul")) {
bErr = multiplyMatrix(tmpArg1, tmpArg2, &finalRes);
}
else if (!strcmp(op, "smul")) {
bErr = scalarMultiplyMatrix(tmpArg2, atof(arg1), &finalRes);
}
res = finalRes;
if (bErr)displayMatrix(&finalRes);
goto LABEL1;
case 2:
//scanf("%s", arg1);
cin >> arg1;
if (!strcmp(arg1, "A")) {
transposeMatrix(&A, &finalRes);
}
else if (!strcmp(arg1, "B")) {
transposeMatrix(&B, &res);
}
else if (!strcmp(arg1, "res")) {
transposeMatrix(&res, &finalRes);
}
res = finalRes;
displayMatrix(&finalRes);
goto LABEL1;
case 3:
//scanf("%s", arg1);
cin >> arg1;
if (!strcmp(arg1, "A")) {
displayMatrix(&A);
}
else if (!strcmp(arg1, "B")) {
displayMatrix(&B);
}
else if (!strcmp(arg1, "res")) {
displayMatrix(&res);
}
goto LABEL1;
case 4:
goto LABEL;
case 0:
return 0;
}
return 0;
}