C++语言应用项目(数组)——地形高度勘探(简易版)

目录

一、 项目架构:

二、文件的打开 || 关闭

三、文件的读取  --> 添加防御性措施

四、判断高度 --> 数组与打印

五、全部代码呈现:


一、 项目架构:

1.文件的打开 || 关闭

2.文件的读取  --> 添加防御性措施

3.判断高度 --> 数组与打印

4.实例:

( 保存文本文件至源码目录)

6  7
5039 5127 5238 5259 5248 5310 5299
5150 5392 5410 5401 5320 5820 5321
5290 5560 5490 5421 9999 5831 5210
5110 5429 5430 5411 5459 5630 5319
4920 5129 4921 5821 4722 4921 5129
5023 5129 4822 4872 4794 4862 4245

二、文件的打开 || 关闭

ifstream(文件输入流)

i:input 输入

f:file 文件

stream:流

包含的库文件: #include <fstream>  

文件基本操作:
a. ofstream        //文件写操作
b. ifstream         //文件读操作
c. fstream          //文件读写操作

 本项目中要获取 C 类型的文件,获取存储文件的指针:

file.open( filename.c_str() );

-.c_str():转化为C类型的文件。

file.open():文件的打开

( 注:file ---> 定义:ifstream file :对文件进行读取操作!)

习惯:文件的打开必须先要进行检验

目的:防止后续代码运行崩溃!

file.fail():文件读取失败!

    if (file.fail()) {
        cout << "文件打开失败!" << endl;
        exit(1);    
    }                

( 注: exit(1); ---> 正常的结束:0 || 异常的结束:1) 

代码呈现:

#include<iostream>
#include<fstream> // 文件操作 库文件-----------------
#include<string>
#include<Windows.h>

using namespace std;


int main(){

	string	filename;
	ifstream file;

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

	file.open(filename.c_str()); // 获取 C 类型的文件,获取存储文件的指针
	// 必须检查文件是否打开成功!
	if (file.fail()) {
		cout << "文件打开失败!" << endl;
		exit(1);	// 正常的结束:0 || 异常的结束:1
	}
    
    return 0;
}

三、文件的读取  --> 添加防御性措施


  对比 cin >> ...    方法类似!

  file>>...>>... ;

防御性处理:即检查输入是否合理!

    if (rows > MAX_ROW || cols > MAX_COL) {
        cout << "数据输入错误,过大!" << endl;
        exit(0);
    }

开始读取:从数据文件读取数据 --> 二维数组:for 循环遍历即可

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            file >> map[i][j];
        }
    }

代码呈现:

#define MAX_ROW 16
#define MAX_COL 16

int main(){
    .
    .
    .
    .

	int rows, cols;
	int map[MAX_ROW][MAX_COL] = {0};

	file >> rows >> cols;

	if (rows > MAX_ROW || cols > MAX_COL) {
		cout << "数据输入错误,过大!" << endl;
		exit(0);
	}
 
	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < cols; j++) {
			file >> map[i][j];
		}
	}
    .
    .
    .
    return 0;

}

四、判断高度 --> 数组与打印

封装并调用一个判断函数:

比对上下左右:找出最高的峰值点!

bool IsMax(int arg[16][16],int row,int col) {
    if (   arg[row][col] > arg[row - 1][col]
        && arg[row][col] > arg[row + 1][col]
        && arg[row][col] > arg[row][col - 1]
        && arg[row][col] > arg[row][col + 1]
        ) {
        return true;
    }
    return false;
}
( 注:循环中:rows(cols)-1:去除边界 )

代码呈现:



int main(){
    .
    .
    .
    .

	for (int i = 1; i < rows-1; i++) {

		for (int j = 1; j < cols-1; j++) {
			if (IsMax(map, i, j)) {
				//cout << "峰值点在:("<<i<<","<<j<<") " << map[i][j] << endl;
				printf("峰值点在:(%d,%d)%d\n", i, j, map[i][j]);
			}

		}
	}
	system("pause");

	//	文件关闭!对应文件的打开!!!--> 避免资源的泄露!
	file.close();
    return 0;

}

五、全部代码与实例实现

有问题及时沟通,一起成长!  (^_^)

 6  7
5039 5127 5238 5259 5248 5310 5299
5150 5392 5410 5401 5320 5820 5321
5290 5560 5490 5421 9999 5831 5210
5110 5429 5430 5411 5459 5630 5319
4920 5129 4921 5821 4722 4921 5129
5023 5129 4822 4872 4794 4862 4245

#include<iostream>
#include<fstream> // 文件操作 库文件
#include<string>
#include<Windows.h>


using namespace std;

#define MAX_ROW 16
#define MAX_COL 16

bool IsMax(int arg[16][16],int row,int col) {
	if (   arg[row][col] > arg[row - 1][col]
		&& arg[row][col] > arg[row + 1][col]
		&& arg[row][col] > arg[row][col - 1]
		&& arg[row][col] > arg[row][col + 1]
		) {
		return true;
	}
	return false;
}


int main() {

	int rows, cols;
	int map[MAX_ROW][MAX_COL] = {0};

	string	filename;
	ifstream file;

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

	file.open(filename.c_str()); // 获取 C 类型的文件,获取存储文件的指针
	// 必须检查文件是否打开成功!
	if (file.fail()) {
		cout << "文件打开失败!" << endl;
		exit(1);	// 正常的结束:0 || 异常的结束:1
	}

	// 对比 cin >> ...	方法类似!
	file >> rows >> cols;

	// 防御性处理:检查输入是否合理!
	if (rows > MAX_ROW || cols > MAX_COL) {
		cout << "数据输入错误,过大!" << endl;
		exit(0);
	}

	// 开始读取:从数据文件读取数据 --> 二维数组
	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < cols; j++) {
			file >> map[i][j];
		}
	}

	//	判断,打印峰值 --> 1.判断 、2.打印
	//					bool IsMax();

	for (int i = 1; i < rows-1; i++) {
		//				rows(cols)-1:去除边界
		for (int j = 1; j < cols-1; j++) {
			if (IsMax(map, i, j)) {
				//cout << "峰值点在:("<<i<<","<<j<<") " << map[i][j] << endl;
				printf("峰值点在:(%d,%d)%d\n", i, j, map[i][j]);
			}

		}
	}
	system("pause");

	//	文件关闭!对应文件的打开!!!--> 避免资源的泄露!
	file.close();

	return 0;
}

 ( 越努力,越幸运!)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员-King.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值