#include<iostream>
#include<Windows.h>
#include<string>
#include<fstream>
using namespace std;
#define N 128
//判断峰值函数
bool isPark(int grid[N][N], int r, int c)
{
if ((grid[r][c]>grid[r-1][c])&& (grid[r][c] > grid[r+1][c])&&
(grid[r][c] > grid[r][c-1])&& (grid[r][c] > grid[r][c+1]))
{
return true;
}
else
{
return false;
}
}
int main()
{
int nrows, ncols;
int map[N][N];
string filename;
ifstream file; //对文件进行操作
cout << "请输入要打开的文件名" << endl;
cin >> filename;
file.open(filename.c_str()); //打开文件
if (file.fail())
{
cout << "打开错误,请进行调试" << endl;
exit(1);
}
//将文件中的数据输入到二维数组中
file >> nrows >> ncols;//从文件中输入行数和列数
if (nrows>N || ncols>N)
{
cout << "网格太大,请调试程序" << endl;
}
for (int i = 0; i < nrows; i++)
{
for (int j = 0; j < ncols; j++)
{
file >> map[i][j];
}
}
//判断并打印峰值的位置
for (int i = 1; i < nrows-1; i++)
{
for (int j = 1; j < ncols-1; j++)
{
if (isPark(map,i ,j))
{
cout << "行" << i << "列" << j << endl;
}
}
}
system("pause");
return 0;
}
注解:要将map.txt文件放置在与main.cpp相同的文件夹下面。