项目:地形导航系统

需求

地形的高度已经转化成数字保存在TXT文件中,要求从文件中读取数据并统计峰点和波点的个数及位置.
峰点:上下左右及斜侧八个数据大小均小于该数据
谷点:上下左右及斜侧八个数据大小均大于该数据
例:

2 2 3 4
3 6 1 3
4 4 2 5

中的6为峰点,1为谷点.

总代码:

#include <iostream>
#include <Windows.h>
#include <fstream>
#include <string>
#define MAXSIZE 64

using namespace std;

bool isPeak(int map[][MAXSIZE],int row,int col);
bool isValley(int map[][MAXSIZE],int row,int col);

int main(void){
	int nrows,ncols;
	int map[MAXSIZE][MAXSIZE];
	int peakcount=0,valleycount=0;
	string filename;
	ifstream file;

	cout << "请输入文件名:";
	cin >> filename;

	file.open(filename.c_str());
	if(file.fail()){
		cout << "读取文件失败!!!" <<endl;
		exit(1);
	}
	
	file>>nrows>>ncols;
	if(nrows>MAXSIZE||ncols>MAXSIZE){
		cout << "数据过多!!!" <<endl;
		exit(1);
	}

	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(isPeak(map,i,j)){
				cout << "峰点:第" << i << "行第" << j << "列" <<endl;
				peakcount++;
			}
	for(int i=1;i<nrows-1;i++)
		for(int j=1;j<ncols-1;j++)
			if(isValley(map,i,j)){
				cout << "谷点:第" << i << "行第" << j << "列" <<endl;
				vallycount++;
			}

	cout << "峰点有" << peakcount << "个" <<endl;
	cout << "谷点有" << vallycount << "个" <<endl;

	system("pause");
	file.close();
	return 0;
}

bool isPeak(int map[][MAXSIZE],int row,int col){
	if((map[row][col]>map[row][col-1])&&(map[row][col]>map[row][col+1])&&
		(map[row][col]>map[row-1][col])&&(map[row][col]>map[row+1][col])){
		if((map[row][col]>map[row-1][col-1])&&(map[row][col]>map[row+1][col-1])&&
		(map[row][col]>map[row-1][col+1])&&(map[row][col]>map[row+1][col+1]))return true;
	}
		
	return false;
}

bool isValley(int map[][MAXSIZE],int row,int col){
	if((map[row][col]<map[row][col-1])&&(map[row][col]<map[row][col+1])&&
		(map[row][col]<map[row-1][col])&&(map[row][col]<map[row+1][col])){
		if((map[row][col]<map[row-1][col-1])&&(map[row][col]<map[row+1][col-1])&&
		(map[row][col]<map[row-1][col+1])&&(map[row][col]<map[row+1][col+1]))return true;
	}
		
	return false;
}

用到了两个函数,isPeak()和isValley()分别判断该点是否是峰点或谷点,如果是,返回true,否则返回false.
峰点:在周围一圈八个点中数据最大的点,谷点:周围一圈八个点中数据最小的点
使用了txt文件记录地形的高低以及地形数据的行数和列数.将该文件放入项目所在的文件夹.
在这里插入图片描述
要读取文件我们必须包含一个头文件<fstream>

	int nrows,ncols;
	int map[MAXSIZE][MAXSIZE];
	int peakcount=0,valleycount=0;
	string filename;
	ifstream file;

这里定义了行数,列数,用于存储从文件读取数据的二维数组,峰点和谷点的计数变量,存文件名的变量filename以及头文件fstream里定义的ifstream类型变量file.

cout << "请输入文件名:";
	cin >> filename;

	file.open(filename.c_str());
	if(file.fail()){
		cout << "读取文件失败!!!" <<endl;
		exit(1);
	}
	
	file>>nrows>>ncols;
	if(nrows>MAXSIZE||ncols>MAXSIZE){
		cout << "数据过多!!!" <<endl;
		exit(1);
	}

输入要分析的文件,file.open打开文件,这里filename使用了一个.c_str()方法,然后检测文件是否打开成功以及文件的数据是否超出最大范围

c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同,这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string对象转换成c中的字符串样式。

exit()的用法:

exit()通常是用在子程序中用来终结程序用的,使用后程序自动结束,跳回操作系统。
exit(0) 表示程序正常退出,exit(1)/exit(-1)表示程序异常退出。

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(isPeak(map,i,j)){
				cout << "峰点:第" << i << "行第" << j << "列" <<endl;
				peakcount++;
			}
	for(int i=1;i<nrows-1;i++)
		for(int j=1;j<ncols-1;j++)
			if(isValley(map,i,j)){
				cout << "谷点:第" << i << "行第" << j << "列" <<endl;
				vallycount++;
			}

这里存储数据到二维数组里,然后在for循环里用判断峰点和谷点的函数判断并输出.
结果:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值