1、编写程序:使用行程编码方法对DEM数据进行编码,并将编码后的结果进行输出。提交c++源程序和运行结果截图。
要求:
(1) 使用C++语言编写。自定义dem网格数的宽度Width和高度Height,并根据网格数大小,动态申请内存demBuf。
(2) 使用随机函数,动态将demBuf里的高程点赋值。
(3) 编写二维行程编码函数RunLength2D(sc,sr,v,n)。sc表示起始点的行号,sr表示起始点的列号,v表示当前数据块内元素的高程值,n表示数据块的长度。
(4) 比较编码前后dem数据块占用内存的大小,计算压缩比Ratio,并输出Ratio和编码前后的dem数据。
别问,问就是南望山男子职业学院未来村分校。。。。。
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
//创建动态二维数组
int weight = 20, height = 20, radius=1, now;//初始DEM行列高度,压缩半径,当前网格值
int** dembuf = new int* [height];//创建初始二维数组
int** end_array = new int* [weight * height];//创建结果二维数组
int x = 0;//压缩块儿的个数
//创建结果二维数组
void Creat_Endarray(int** array, int row, int col)
{
for (int i = 0; i < row; ++i) {
array[i] = new int[col];
}
//赋值
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
array[i][j] = 0;
}
}
}
//打印结果二维数组
void Print_Endarray(int** array, int row, int col)
{
for (int i = 0; i < row; i++)
{
while (i != 0 && array[i][3] == 0)
{
return;
}
cout << "(";
for (int j = 0; j < col; j++)
{
if (j == 3)
{
cout << array[i][j];
}
else
{
cout << array[i][j] << ",";
}
}
cout << ")" << endl;
}
}
//创建初始DEM矩阵
void Creat_array(int** array, int row, int col)
{
for (int i = 0; i < row; ++i) {
array[i] = new int[col];
}
//赋值
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
array[i][j] = rand() % 3 + 1;
}
}
}
//遍历半径内网格值,判断是否与当前网格值相同
int Examin(int t,int indexR, int indexL, int Radius)
{
for (int i = indexR; i < indexR + Radius; i++) //遍历当前半径内的所有网格
{
for (int j = indexL; j < indexL + Radius; j++)
{
if (dembuf[i][j] != t)
{
return 0;
}
}
}
return 1;
}
//二维行程压缩(块状编码)
//将行、列、当前值、半径压入二维数组
void compress(int sr,int sc,int v,int n)
{
end_array[x][0] = sr;
end_array[x][1] = sc;
end_array[x][2] = v;
end_array[x][3] = n;
}
//块状编码
void compress_1(int** array, int row, int col)
{
for (int i = 0; i < 20; i++)//遍历每一个网格
{
for (int j = 0; j < 20; j++)
{
while (dembuf[i][j] == 0) //若标记值为1,说明该网格已经压缩,跳到下一个网格
{
j++;
}
now = dembuf[i][j];//获取当前网格值
if ((j + radius + 1) < 20 && (i + radius + 1) < 20)
{
while ((Examin(now,i, j, radius + 1)))
{
radius++;
}
}
cout << " ";
for (int m = i; m < i + radius; m++)
{
for (int n = j; n < j + radius; n++)
{
dembuf[m][n] = 0;
}
}
x++;
compress(i, j, now, radius);
radius = 1;
}
}
}
//打印初始DEM网格
void printarray(int** array, int row, int col)
{
cout << "生成的矩阵为:" << endl;
for (int j = 0; j < col; j++)
{
if (j == 0)
{
cout << "┌" << "─" << "─" << "─";
}
else if (j == col - 1)
{
cout << "┬" << "─" << "─" << "─" << "┐" << endl;
}
else
{
cout << "┬" << "─" << "─" << "─";
}
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (j == 0)
{
cout << "│" << " " << dembuf[i][j] << "│";
}
else if (j != col - 1)
{
cout << " " << dembuf[i][j] << "│";
}
else
{
cout << " " << dembuf[i][j] << "│" << endl;
}
}
if (i != row - 1)
{
for (int x = 0; x < col; x++)
{
if (x == 0)
{
cout << "├" << "─" << "─" << "─";
}
else if (x == col - 1)
{
cout << "┼" << "─" << "─" << "─" << "┤" << endl;
}
else
{
cout << "┼" << "─" << "─" << "─";
}
}
}
}
for (int t = 0; t < col; t++)
{
if (t == 0)
{
cout << "└" << "─" << "─" << "─";
}
else if (t == col - 1)
{
cout << "┴" << "─" << "─" << "─" << "┘" << endl;
}
else
{
cout << "┴" << "─" << "─" << "─";
}
}
}
//主函数
void main()
{
Creat_array(dembuf, weight, height);
printarray(dembuf, weight, height);
Creat_Endarray(end_array, 400, 4);
compress_1(dembuf, 20, 20);
cout << endl;
cout << "块状编码后结果为:" << endl;
Print_Endarray(end_array, 400, 4);
double y = (static_cast<double>(400)-x) / 400;
cout << "压缩率为:" << y << endl;
}