1.题目要求
2.代码实现
#include <stdio.h>
#define MAX_TERM 80
// 定义稀疏矩阵结构体
typedef struct juzhen {
int row;
int col;
int value;
} Juzhen;
// 显示稀疏矩阵
void show(Juzhen a[], int count_a) {
printf(" i row col val\n");
for (int i = 1; i <= count_a; i++) {
printf(" %d| %d %d %d\n", i, a[i].row, a[i].col, a[i].value);
}
}
// 转置稀疏矩阵
void zhuanzhi(Juzhen a[], Juzhen b[]) {
int i, j, k;
b[0].row = a[0].col;
b[0].col = a[0].row;
b[0].value = a[0].value;
if (a[0].value > 0) {
k = 1;
for (i = 0; i < a[0].col; i++) {
for (j = 1; j <= a[0].value; j++) {
if (a[j].col == i) {
b[k].row = a[j].col;
b[k].col = a[j].row;
b[k].value = a[j].value;
k++;
}
}
}
}
}
// 初始化稀疏矩阵
void init(Juzhen a[], int *rows, int *cols, int *count) {
printf("please input the number of rows,cols and values\n");
scanf("%d,%d,%d", rows, cols, count);
a[0].row = *rows;
a[0].col = *cols;
a[0].value = *count;
for (int i = 1; i <= *count; i++) {
printf("please input the row,col and value\n");
scanf("%d,%d,%d", &a[i].row, &a[i].col, &a[i].value);
}
}
int main() {
Juzhen a[MAX_TERM];
Juzhen b[MAX_TERM];
int rows, cols, count;
init(a, &rows, &cols, &count);
show(a, count);
printf("\n");
zhuanzhi(a, b);
show(b, count);
return 0;
}