Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <optional>
- #include <cstdint>
- #include <vector>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <cassert>
- #if __has_include(<format>)
- # include <format>
- using std::format;
- #elif __has_include(<fmt/core.h>)
- # include <fmt/core.h>
- using fmt::format;
- #else
- # error "not format function found."
- #endif
- struct data_t {
- uint64_t individual_number;
- std::string name;
- double height;
- double weight;
- std::optional<double> bmi;
- void calc_bmi();
- void debug_print() const;
- void print(std::ostream& os) const;
- };
- constexpr std::size_t init_line_buf_length = 512;
- constexpr std::size_t init_vec_length = 256;
- std::vector<data_t> data_read_from_file(const char* file_name)
- {
- #ifdef EXEC_ON_GODBOLT
- std::istream& in = std::cin;
- #else
- std::ifstream in(file_name);
- if (!in) {
- std::cerr << "In function data_read_from_file:\nFail to open " << file_name << std::endl;
- return {};
- }
- #endif
- std::vector<data_t> result_v;
- result_v.reserve(init_vec_length);
- // 動的メモリ確保は遅いのでループの外で行う
- std::string buf;
- buf.reserve(init_line_buf_length);
- while (std::getline(in, buf)) {
- data_t re = {};
- std::istringstream ss(buf);
- ss >> re.individual_number >> re.name >> re.height >> re.weight;
- result_v.emplace_back(std::move(re));
- }
- return result_v;
- }
- void data_t::calc_bmi()
- {
- this->bmi = this->weight * 10000.0 / (this->height * this->height);
- }
- void data_t::debug_print() const
- {
- std::cout << format("{0} {1:8} {2:.1f} {3:.1f}", this->individual_number, this->name, this->height, this->weight) << std::endl;
- //printf("%" PRIu64 " %-8s %.1lf %.1lf\n", this->individual_number, this->name, this->height, this->weight);
- }
- void data_t::print(std::ostream& os) const
- {
- os << format("{0} {1:8} {2:.1f} {3:.1f}", this->individual_number, this->name, this->height, this->weight);
- //fprintf(stream, "%" PRIu64 " %-8s %.1lf %.1lf", d->individual_number, d->name, d->height, d->weight);
- if (this->bmi) {
- os << format(" {0:.1f}", *this->bmi) << std::endl;
- //fprintf(stream, " %.1lf\n", d->bmi.d);
- }
- else {
- os << " none" << std::endl;
- //fputs(" none\n", stream);
- }
- }
- int main(void)
- {
- auto data = data_read_from_file("R7_in.txt");
- if (data.empty()) {
- return 2;
- }
- std::cout << "ID NAME HIGHT[cm] WEIGHT[kg]" << std::endl;
- for (const auto& d : data) {
- d.debug_print();
- assert(!d.bmi.has_value());
- }
- for (auto&& d : data) {
- d.calc_bmi();
- assert(d.bmi.has_value());
- }
- std::sort(data.begin(), data.end(), [](const data_t& l, const data_t& r){ return l.bmi < r.bmi; });
- #ifdef EXEC_ON_GODBOLT
- std::cout << "---" << std::endl;
- #else
- std::ofstream out("R7_out.txt");
- if (out) {
- std::cerr << "In function main:\nFail to open R7_out.txt" << std::endl;
- return 2;
- }
- #endif
- for (const auto& d : data) {
- #ifdef EXEC_ON_GODBOLT
- d.print(std::cout);
- #else
- d.print(out);
- #endif
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement