现在的位置: 首页 > 综合 > 正文

2005年武大遥感院考研复试机试题c++实现

2013年09月12日 ⁄ 综合 ⁄ 共 6715字 ⁄ 字号 评论关闭

题目:

 

实现:

#include<vector>
#include<fstream>
#include<iostream>
#include<iterator>
using namespace std;

typedef vector<int> VecInt;
VecInt ConvoKernel1(const VecInt& Matrix);
VecInt ConvoKernel2(const VecInt& Matrix);
int main()
{
	VecInt data(10000);

	ifstream infile("SourceData.txt");
	ofstream outfile("result.txt");

	if(!infile)
	{
		cout<<"SourceData.txt open failed"<<endl;
	}
	if(!outfile)
	{
		cout<<"result.txt open failed"<<endl;
	}

	for(int i=0;i<10000;i++)
	{
		infile>>data[i];
	}
	VecInt Result = ConvoKernel1(data);

	outfile<<"after the first convolution the data are"<<endl;
	for(int m=0 ; m<80 ; m++)
	{
		copy(&Result[m],&Result[m+125],ostream_iterator<int>(outfile," "));
		outfile<<endl;
	}

	Result = ConvoKernel2(data);
	outfile<<"after the second convolution the data are"<<endl;
	for(int n=0 ; n<100 ; n++)
	{
		copy(&Result[n],&Result[n+100],ostream_iterator<int>(outfile," "));
		outfile<<endl;
	}

	outfile.close();
	infile.close();

	return 0;
}

//卷积函数1
VecInt ConvoKernel1(const VecInt& Matrix)
{
	VecInt Result(Matrix);
	int a[] = {0,-1,0,-1,5,-1,0,-1,0};
	for(int i=1 ; i<124 ; i++)
	{
		for(int j=1;j<79;j++)
		{
			Result[i*j]
				= Matrix[(i-1)*(j-1)]*a[0] + Matrix[(i-1)*(j-1)]*a[1] + Matrix[(i+1)*(j-1)]*a[2]
				+ Matrix[i*(j-1)]*a[3]     + Matrix[i*j]*a[4]     + Matrix[i*(j+1)]*a[5]
				+ Matrix[(i+1)*(j-1)]*a[6] + Matrix[(i+1)*j]*a[7] + Matrix[(i+1)*(j+1)]*a[8] ;
		}
	}
	return Result;
}
//卷积函数2
VecInt ConvoKernel2(const VecInt& Matrix)
{
	VecInt Result(Matrix);
	int b[] = {0,0,-1,0,0,0,-1,-2,-1,0,-1,-2,16,-2,-1,0,-1,-2,-1,0,0,0,-1,0,0};
	for(int i=2 ; i<98 ; i++)
	{
		for(int j=2;j<98;j++)
		{
			Result[i*j]
				= Matrix[(i-2)*(j-2)]*b[0] + Matrix[(i-2)*(j-1)]*b[1] + Matrix[(i-2)*j]*b[2] + Matrix[(i-2)*(j+1)]*b[3] + Matrix[(i-2)*(j+2)]*b[4]
				+ Matrix[(i-1)*(j-2)]*b[5] + Matrix[(i-1)*(j-1)]*b[6] + Matrix[(i-1)*j]*b[7] + Matrix[(i-1)*(j+1)]*b[8] + Matrix[(i-1)*(j+2)]*b[9]
				+ Matrix[(i)*(j-2)]*b[10] + Matrix[i*(j-1)]*b[11]  + Matrix[i*j]*b[12]+ Matrix[i*(j+1)]*b[13] + Matrix[i*(j+2)]*b[14] 
				+ Matrix[(i+1)*(j-2)]*b[15] + Matrix[(i+1)*(j-1)]*b[16] + Matrix[(i+1)*j]*b[17] + Matrix[(i+1)*(j+1)]*b[18] + Matrix[(i+2)*(j+2)]*b[19]
				+ Matrix[(i+2)*(j-2)]*b[20] + Matrix[(i+2)*(j-1)]*b[21] + Matrix[(i+2)*j]*b[22] + Matrix[(i+2)*(j+1)]*b[23] + Matrix[(i+2)*(j+2)]*b[24] ;
		}
	}
	return Result;
}

 

 

这个程序,源码来自于网上,经我自己修改,如果有人能看到,可以验证和讨论一下这个程序的正确性, 总感这个程序有问题,自己还一时说不出问题在那里,特别是在卷积函数的实现上,请大家多多帮忙指教。

 

下面是自己编的一个版本:

#include<iostream>
#include<fstream>
#include<vector>
using namespace std;

typedef vector<vector<int> >VecInt;
VecInt ConvolutionFun1(const vector<int>&vec, const int & m, const int& n);
VecInt ConvolutionFun2(const vector<int>&vec, const int&r, const int&s);
int main()
{
	const int M =125;
	const int N =80;
	const int R =100;
	const int S =100;
	VecInt vecInt(M, vector<int>(N)); 
	VecInt vecInt2(R, vector<int>(S)); 
	int i,j;
	ifstream infile("SourceData.txt");
	if(!infile)
	{
		cout<<"SourceData.txt open failed.\n";
		return 1;
	}
	vector<int>vec_int;
	int numeric(0);
	for(i=0;i<10000;i++)
	{
		infile>>numeric;
		vec_int.push_back(numeric);
	}
	vecInt = ConvolutionFun1(vec_int, M, N);
	vecInt2 = ConvolutionFun2(vec_int, R, S);
	ofstream outfile("result.txt");
	if(!outfile)
	{
		cout<<"result.txt open failed.\n";
		return 1;
	}
	outfile<<"卷积一次后的矩阵:\n";
	for(i=0; i<M; i++)
	{
		for(j=0; j<N; j++)
		{
			outfile<<vecInt[i][j]<<" ";
		}
		outfile<<'\n';
	}
	outfile<<'\n';
	outfile<<"第二次卷积后的矩阵:\n";
	for(i=0; i<R; i++)
	{
		for(j=0; j<S; j++)
		{
			outfile<<vecInt2[i][j]<<" ";
		}
		outfile<<'\n';
	}
	outfile<<'\n';
	outfile.close();
	infile.close();
	return 0;
}

VecInt ConvolutionFun1(const vector<int>&vec, const int&m, const int&n)
{
	VecInt vecInt(m, vector<int>(n)); 
	int num =0;
	for(int i=0; i<m; i++)
	{
		for(int j=0; j<n; j++)
		{
			vecInt[i][j] = vec[num];
			num++;
		}
	}

	int a[]={0,-1,0,-1,5,-1,0,-1,0};
	for(int i=1;i<m-1;i++)
	{
		for(int j=1;j<n-1;j++)
		{
			vecInt[i][j] = vecInt[i-1][j-1] * a[0] + vecInt[i-1][j] * a[1] + vecInt[i-1][j+1]* a[2]
						 + vecInt[i][j-1] * a[3] + vecInt[i][j] * a[4] + vecInt[i][j+1] * a[5]
						 + vecInt[i+1][j-1] * a[6] + vecInt[i+1][j] * a[7] + vecInt[i+1][j+1] * a[8];			
		}
	}
	return vecInt;
}
VecInt ConvolutionFun2(const vector<int>&vec, const int&r, const int&s)
{
	VecInt vecInt(r, vector<int>(s)); 
	int num =0;
	for(int i=0; i<r; i++)
	{
		for(int j=0; j<s; j++)
		{
			vecInt[i][j] = vec[num];
			num++;
		}
	}

	int a[]={0,0,-1,0,0,0,-1,-2,-1,0,-1,-2,16,-2,-1,0,-1,-2,-1,0,0,0,-1,0,0};
	for(int i=2;i<r-2;i++)
	{
		for(int j=2;j<s-2;j++)
		{
			vecInt[i][j] = vecInt[i-2][j-2] * a[0] + vecInt[i-2][j-1] * a[1] + vecInt[i-2][j] * a[2] + vecInt[i-2][j+1]* a[3]+ vecInt[i-2][j+2] * a[4] 
						 + vecInt[i-1][j-2] * a[5] + vecInt[i-1][j-1] * a[6] + vecInt[i-1][j] * a[7] + vecInt[i-1][j+1]* a[8]+ vecInt[i-1][j+2] * a[9] 
						 + vecInt[i][j-2] * a[10] + vecInt[i][j-1] * a[11] + vecInt[i][j] * a[12] + vecInt[i][j+1]* a[13]+ vecInt[i][j+2] * a[14] 
						 + vecInt[i+1][j-2] * a[15] + vecInt[i+1][j-1] * a[16] + vecInt[i+1][j] * a[17] + vecInt[i+1][j+1]* a[18]+ vecInt[i+1][j+2] * a[19] 
						 + vecInt[i+2][j-2] * a[20] + vecInt[i+2][j-1] * a[21] + vecInt[i+2][j] * a[22] + vecInt[i+2][j+1]* a[23]+ vecInt[i+2][j+2] * a[24] ;
		   
		}
	}
	return vecInt;
}


再次修改之后:

#include<iostream>
#include<fstream>
#include<vector>
using namespace std;

typedef vector<vector<int> >VecInt;
VecInt ConvolutionFun1(const vector<int>&vec, const int & m, const int& n);
VecInt ConvolutionFun2(const vector<int>&vec, const int&r, const int&s);
int main()
{
	const int M =125;
	const int N =80;
	const int R =100;
	const int S =100;
	VecInt vecInt; 
	int i,j;
	ifstream infile("SourceData.txt");
	if(!infile)
	{
		cout<<"SourceData.txt open failed.\n";
		return 1;
	}
	vector<int>vec_int;
	int numeric(0);
	for(i=0;i<10000;i++)
	{
		infile>>numeric;
		vec_int.push_back(numeric);
	}
	
	ofstream outfile("result.txt");
	if(!outfile)
	{
		cout<<"result.txt open failed.\n";
		return 1;
	}
	outfile<<"卷积一次后的矩阵:\n";

	vecInt = ConvolutionFun1(vec_int, M, N);
	for(i=0; i<M; i++)
	{
		for(j=0; j<N; j++)
		{
			outfile<<vecInt[i][j]<<" ";
		}
		outfile<<'\n';
	}
	outfile<<'\n';

	vecInt = ConvolutionFun2(vec_int, R, S);
	outfile<<"第二次卷积后的矩阵:\n";
	for(i=0; i<R; i++)
	{
		for(j=0; j<S; j++)
		{
			outfile<<vecInt[i][j]<<" ";
		}
		outfile<<'\n';
	}
	outfile<<'\n';
	outfile.close();
	infile.close();
	return 0;
}

VecInt ConvolutionFun1(const vector<int>&vec, const int&m, const int&n)
{
	VecInt vecInt(m, vector<int>(n)); 
	int num =0;
	for(int i=0; i<m; i++)
	{
		for(int j=0; j<n; j++)
		{
			vecInt[i][j] = vec[num];
			num++;
		}
	}

	int a[]={0,-1,0,-1,5,-1,0,-1,0};
	for(int i=1;i<m-1;i++)
	{
		for(int j=1;j<n-1;j++)
		{
			vecInt[i][j] = vecInt[i-1][j-1] * a[0] + vecInt[i-1][j] * a[1] + vecInt[i-1][j+1]* a[2]
						 + vecInt[i][j-1] * a[3] + vecInt[i][j] * a[4] + vecInt[i][j+1] * a[5]
						 + vecInt[i+1][j-1] * a[6] + vecInt[i+1][j] * a[7] + vecInt[i+1][j+1] * a[8];			
		}
	}
	return vecInt;
}
VecInt ConvolutionFun2(const vector<int>&vec, const int&r, const int&s)
{
	VecInt vecInt(r, vector<int>(s)); 
	int num =0;
	for(int i=0; i<r; i++)
	{
		for(int j=0; j<s; j++)
		{
			vecInt[i][j] = vec[num];
			num++;
		}
	}

	int b[]={0,0,-1,0,0,0,-1,-2,-1,0,-1,-2,16,-2,-1,0,-1,-2,-1,0,0,0,-1,0,0};
	for(int i=2;i<r-2;i++)
	{
		for(int j=2;j<s-2;j++)
		{
			vecInt[i][j] = vecInt[i-2][j-2] * b[0] + vecInt[i-2][j-1] * b[1] + vecInt[i-2][j] * b[2] + vecInt[i-2][j+1]* b[3]+ vecInt[i-2][j+2] * b[4] 
						 + vecInt[i-1][j-2] * b[5] + vecInt[i-1][j-1] * b[6] + vecInt[i-1][j] * b[7] + vecInt[i-1][j+1]* b[8]+ vecInt[i-1][j+2] * b[9] 
						 + vecInt[i][j-2] * b[10] + vecInt[i][j-1] * b[11] + vecInt[i][j] * b[12] + vecInt[i][j+1]* b[13]+ vecInt[i][j+2] * b[14] 
						 + vecInt[i+1][j-2] * b[15] + vecInt[i+1][j-1] * b[16] + vecInt[i+1][j] * b[17] + vecInt[i+1][j+1]* b[18]+ vecInt[i+1][j+2] * b[19] 
						 + vecInt[i+2][j-2] * b[20] + vecInt[i+2][j-1] * b[21] + vecInt[i+2][j] * b[22] + vecInt[i+2][j+1]* b[23]+ vecInt[i+2][j+2] * b[24] ;
		   
		}
	}
	return vecInt;
}


 

抱歉!评论已关闭.