###前提・実現したいこと
C++でプログラムを書いています。
以下のプログラム書き換えたいのです。
boostを使わずのやることはできないでしょうか?
(何度も試したのですがビルドできませんでした...)
やりたいことはinput.txt内にGコードが入っており、XとYの値を別々に配列に格納するためのものです。
(例)
X0.027Y0.008
X0.028Y0.007
X0.028Y0.004
X0.029Y0.003
X0.028
X9.315
X-9.315
X-0.028Y0.002
X-0.029Y0.003
・
・
・
この場合、Yの数値がない配列は1つ前のY配列の数値を代入します。(Xも同様)
ご存知の方がいらっしゃいましたら教えていただきたいです。
よろしくお願いします。
#include <iostream>
#include <iomanip>
#include <string>
#include <stdexcept>
#include <cctype>
#include <vector>
#include <fstream>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
#include <boost/geometry/geometries/adapted/boost_polygon/point.hpp>
using point2i = boost::geometry::model::d2::point_xy<int>;
point2i make_point_from_string(std::string s, const point2i& last_p)
{
bool missing_x = false;
point2i re;
if ('x' != std::tolower(s[0])) {
missing_x = true;
}
else {
s.erase(0, 1);//pop_front
std::size_t y_front_pos = 0;
re.x(std::stoi(s, &y_front_pos));
s.erase(0, y_front_pos);
}
if ('y' != std::tolower(s[0])) {
if (missing_x) throw std::invalid_argument("unexpected input detect.");
re.y(last_p.y());
return re;
}
s.erase(0, 1);//pop_front
re.y(std::stoi(s));
return re;
}
std::vector<point2i> read_points(std::istream& is)
{
std::vector<point2i> re;
for (std::string buf; std::getline(is, buf); ) {
re.push_back(make_point_from_string(std::move(buf), (re.empty()) ? point2i{} : re.back()));
}
return re;
}
void calc_distance_and_output(std::ostream& os, const std::vector<point2i>& points)
{
for (std::size_t i = 0; i < points.size() - 1; ++i) {
os << std::fixed << boost::geometry::distance(points[i], points[i + 1]) << std::endl;
}
}
int main()
{
std::ifstream file("input.txt");
const auto points = read_points(file);
calc_distance_and_output(std::cout, points);
}
###発生している問題・エラーメッセージ
エラーメッセージ
###該当のソースコード
ここにご自身が実行したソースコードを書いてください
###試したこと
課題に対してアプローチしたことを記載してください
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
回答1件
あなたの回答
tips
プレビュー