Advertisement
yumetodo

teratail_425361_answer_cpp_version_code

Nov 4th, 2020
2,605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <optional>
  3. #include <cstdint>
  4. #include <vector>
  5. #include <string>
  6. #include <fstream>
  7. #include <sstream>
  8. #include <cassert>
  9. #if __has_include(<format>)
  10. #   include <format>
  11. using std::format;
  12. #elif __has_include(<fmt/core.h>)
  13. #   include <fmt/core.h>
  14. using fmt::format;
  15. #else
  16. #   error "not format function found."
  17. #endif
  18.  
  19. struct data_t {
  20.     uint64_t individual_number;
  21.     std::string name;
  22.     double height;
  23.     double weight;
  24.     std::optional<double> bmi;
  25.     void calc_bmi();
  26.     void debug_print() const;
  27.     void print(std::ostream& os) const;
  28. };
  29. constexpr std::size_t init_line_buf_length = 512;
  30. constexpr std::size_t init_vec_length = 256;
  31. std::vector<data_t> data_read_from_file(const char* file_name)
  32. {
  33. #ifdef EXEC_ON_GODBOLT
  34.     std::istream& in = std::cin;
  35. #else
  36.     std::ifstream in(file_name);
  37.     if (!in) {
  38.         std::cerr << "In function data_read_from_file:\nFail to open " << file_name << std::endl;
  39.         return {};
  40.     }
  41. #endif
  42.     std::vector<data_t> result_v;
  43.     result_v.reserve(init_vec_length);
  44.     // 動的メモリ確保は遅いのでループの外で行う
  45.     std::string buf;
  46.     buf.reserve(init_line_buf_length);
  47.     while (std::getline(in, buf)) {
  48.         data_t re = {};
  49.         std::istringstream ss(buf);
  50.         ss >> re.individual_number >> re.name >> re.height >> re.weight;
  51.         result_v.emplace_back(std::move(re));
  52.     }
  53.     return result_v;
  54. }
  55. void data_t::calc_bmi()
  56. {
  57.     this->bmi = this->weight * 10000.0 / (this->height * this->height);
  58. }
  59. void data_t::debug_print() const
  60. {
  61.     std::cout << format("{0}     {1:8}     {2:.1f}    {3:.1f}", this->individual_number, this->name, this->height, this->weight) << std::endl;
  62.     //printf("%" PRIu64 "     %-8s     %.1lf    %.1lf\n", this->individual_number, this->name, this->height, this->weight);
  63. }
  64. void data_t::print(std::ostream& os) const
  65. {
  66.     os << format("{0} {1:8} {2:.1f} {3:.1f}", this->individual_number, this->name, this->height, this->weight);
  67.     //fprintf(stream, "%" PRIu64 " %-8s %.1lf %.1lf", d->individual_number, d->name, d->height, d->weight);
  68.     if (this->bmi) {
  69.         os << format(" {0:.1f}", *this->bmi) << std::endl;
  70.         //fprintf(stream, " %.1lf\n", d->bmi.d);
  71.     }
  72.     else {
  73.         os << " none" << std::endl;
  74.         //fputs(" none\n", stream);
  75.     }
  76. }
  77. int main(void)
  78. {
  79.     auto data = data_read_from_file("R7_in.txt");
  80.     if (data.empty()) {
  81.         return 2;
  82.     }
  83.     std::cout << "ID    NAME     HIGHT[cm]     WEIGHT[kg]" << std::endl;
  84.     for (const auto& d : data) {
  85.         d.debug_print();
  86.         assert(!d.bmi.has_value());
  87.     }
  88.     for (auto&& d : data) {
  89.         d.calc_bmi();
  90.         assert(d.bmi.has_value());
  91.     }
  92.     std::sort(data.begin(), data.end(), [](const data_t& l, const data_t& r){ return l.bmi < r.bmi; });
  93.  
  94. #ifdef EXEC_ON_GODBOLT
  95.     std::cout << "---" << std::endl;
  96. #else
  97.     std::ofstream out("R7_out.txt");
  98.     if (out) {
  99.         std::cerr << "In function main:\nFail to open R7_out.txt" << std::endl;
  100.         return 2;
  101.     }
  102. #endif
  103.     for (const auto& d : data) {
  104. #ifdef EXEC_ON_GODBOLT
  105.         d.print(std::cout);
  106. #else
  107.         d.print(out);
  108. #endif
  109.     }
  110.     return 0;
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement