C ++中的多个输入和输出
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C ++中的多个输入和输出相关的知识,希望对你有一定的参考价值。
我有一个用C ++ 17编写的代码,将值x和y作为输入,并提供一些值作为输出。我想更改它以接受尽可能多的输入(x和y值)并给出输出。需要在代码中进行哪些更改
#include <iostream>
#include <vector>
#include <utility>
constexpr double CellWidth = 340.0;
constexpr double CellHeight = 320.0;
constexpr double OffsetLeft = 220.0;
constexpr double OffsetTop = 200.0;
constexpr double SpaceHorizontal = 608.0;
constexpr double SpaceVertical = 500.0;
constexpr unsigned int TileNumberOfRows = 17u;
constexpr unsigned int TileNumberOfColumns = 10u;
constexpr unsigned int NumberOfTilesHorizontal = 2;
constexpr unsigned int NumberOfTilesVertical = 2;
struct Tile {
Tile(unsigned int tn);
std::pair<bool, unsigned int> getCellNumber(const double x, const double y) const;
unsigned int tileNumber{};
double posLeft{};
double posTop{};
double posRight{};
double posBottom{};
};
// tielnumer starting with 0. From left to right, then next row
Tile::Tile(unsigned int tn) : tileNumber(tn)
{
// Calculate the index of the tile
unsigned int tileIndexX = tileNumber % NumberOfTilesHorizontal;
unsigned int tileIndexY = tileNumber / NumberOfTilesVertical;
// Calculate X position of tile
posLeft =
// Left margin to paper
OffsetLeft - (CellWidth / 2.0)
+ tileIndexX * (
// Tile Width
TileNumberOfColumns * CellWidth +
SpaceHorizontal - (CellWidth / 2.0)
);
posTop =
OffsetTop - (CellHeight / 2.0)
+ tileIndexY * (
TileNumberOfRows * CellHeight +
SpaceVertical - (CellHeight / 2.0));
// Right position of tile
posRight = posLeft + TileNumberOfColumns * CellWidth;
// Bottom position of tile
posBottom = posTop + TileNumberOfRows * CellHeight;
}
// Check, if in pos is in tile and at what position
std::pair<bool, unsigned int> Tile::getCellNumber(const double x, const double y) const
{
unsigned int cellNumber{ 0 };
// Is the position in range of this tile?
const bool inTile{ (x >= posLeft) && (x <= posRight) && (y > posTop) && (y < posBottom) };
// If so
if (inTile) {
// Calculate row in local tile
unsigned int col = static_cast<unsigned int>((x - posLeft) / CellWidth +
((tileNumber % NumberOfTilesHorizontal) * TileNumberOfColumns));
// Calculate col in local tile
unsigned int row = static_cast<unsigned int>((y - posTop) / CellHeight +
((tileNumber / NumberOfTilesVertical) * TileNumberOfRows));
// And now calculate the overall cell Number
cellNumber = row * TileNumberOfColumns * NumberOfTilesHorizontal + col;
}
return std::make_pair(inTile, cellNumber);
}
int main(void) {
const std::vector<Tile> tiles{ Tile(0),Tile(1),Tile(2),Tile(3) };
// Test values
const double x{ 3700 }; // want to add multiple entries here
const double y{ 11261 }; // want to add multiple entries here
// Check cell number
for (const Tile& tile : tiles) {
if (const auto [isInTile, cellNumber] = tile.getCellNumber(x, y); isInTile) {
std::cout << "
Cellnumber: " << cellNumber << "
:)
";
}
}
return 0;
}
我已经尝试了许多更改,但是总是以某些错误结尾,而且我对C ++还是陌生的,主要语言是python。
我有一个用C ++ 17编写的代码,将值x和y作为输入,并提供一些值作为输出。我想更改它以接受尽可能多的输入(x和y值)并给出输出。有什么变化...
答案
如果要使用多个输入值,则将x,y放入向量中,就像平铺瓷砖一样:
以上是关于C ++中的多个输入和输出的主要内容,如果未能解决你的问题,请参考以下文章
C语言函数参数描述中的[in][out]是什么意思?(输入型参数和输出型参数)(表示这是传递给函数用于读取数据的,还是用来输出的地址指针地址,多个值可以用结构体指针)