Catalog
  1. 1. 问题描述
    1. 1.1. 对一个对称矩阵进行压缩及解压缩
    2. 1.2.
    3. 1.3. 运行界面
数据结构实验三

问题描述

对一个对称矩阵进行压缩及解压缩

注意

压缩时:

第一步:完整地显示矩阵

img

第二步: 完整地显示一维数组

img

———————————————————————————————————————

解压缩时:

第一步:完整地显示一维数组

img

第二步:完整地显示矩阵

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include<stdio.h>
#include<math.h>

#define Matrix_Num 5

void input_Matrix(int *Matrix)
{
printf("输入方阵:\n");
for (int i = 0; i < Matrix_Num; i++) {
for (int j = 0; j < Matrix_Num; j++) {
scanf("%d", &Matrix[Matrix_Num * i + j]);
}
}
printf("\n");
return;
}
void show1D(int Compressed[])
{
for (int i = 0; i < Matrix_Num*(Matrix_Num + 1) / 2; i++)
printf("%d ", Compressed[i]);
printf("\n");
return;
}
void show2D(int Matrix[])
{
for (int i = 0; i < Matrix_Num; i++) {
for (int j = 0; j < Matrix_Num; j++) {
printf("%d ", Matrix[Matrix_Num*i + j]);
}
printf("\n");
}
return;
}
void compress(int Matrix[],int *Compressed)
{
int n = 0;
printf("您输入的方阵为:\n");
show2D(Matrix);
printf("压缩后\n");
for (int i = 0; i < Matrix_Num; i++)
for (int j = 0; j <= i; j++)
Compressed[n++] = Matrix[Matrix_Num*i + j];
show1D(Compressed);
printf("\n");
return;
}
void uncompress_O1(int Compressed[],int *Uncompressed)
{
printf("压缩方阵为:\n");
show1D(Compressed);
printf("解压缩后\n");
for (int k = 0; k < Matrix_Num * (Matrix_Num + 1) / 2; k++) {
int i, j;
i = ceil(sqrt(2.25 + 2 * k) - 0.5);
j = k - i * (i - 1) / 2;
i--;
Uncompressed[Matrix_Num*i + j] = Compressed[k];
Uncompressed[Matrix_Num*j + i] = Compressed[k];
}
show2D(Uncompressed);
printf("\n");
return;
}
void uncompress_On(int Compressed[], int *Uncompressed)
{
printf("压缩方阵为:\n");
show1D(Compressed);
printf("解压缩后\n");
for (int k = 0; k < Matrix_Num * (Matrix_Num + 1) / 2; k++) {
for (int i = 1; i <= Matrix_Num; i++) {
int j = k - i * (i - 1) / 2 + 1;
if (i >= j && j >= 1) {
Uncompressed[Matrix_Num*(i - 1) + (j - 1)] = Compressed[k];
Uncompressed[Matrix_Num*(j - 1) + (i - 1)] = Compressed[k];
break;
}
}
}
show2D(Uncompressed);
printf("\n");
return;
}
void uncompress_On2(int Compressed[], int *Uncompressed)
{
printf("压缩方阵为:\n");
show1D(Compressed);
printf("解压缩后\n");
for (int k = 0; k < Matrix_Num * (Matrix_Num + 1) / 2; k++) {
for (int i = 1; i <= Matrix_Num; i++) {
for (int j = 1; j <= i; j++) {
if (k == i * (i - 1) / 2 + j - 1) {
Uncompressed[Matrix_Num*(i - 1) + (j - 1)] = Compressed[k];
Uncompressed[Matrix_Num*(j - 1) + (i - 1)] = Compressed[k];
}
}
}
}
show2D(Uncompressed);
printf("\n");
return;
}
int main()
{
int M[Matrix_Num * Matrix_Num];
int C[Matrix_Num * (Matrix_Num + 1) / 2];
int U[Matrix_Num * Matrix_Num];
input_Matrix(M);
compress(M, C);
uncompress_O1(C, U);
//uncompress_On(C, U);
//uncompress_On2(C, U);
return 0;
}

运行界面

img

Author: Christopher Shen
Link: https://www.pasxsenger.com/2019/10/21/数据结构实验三/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
  • 支付寶