Advertisement
Guest User

Untitled

a guest
Oct 16th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include "json2/json2.hpp"
  2.  
  3. #include <vector>
  4. #include <string>
  5. #include <fstream>
  6. #include <iostream>
  7.  
  8. struct coordinates
  9. {
  10.     double x;
  11.     double y;
  12.     double z;
  13. };
  14.  
  15. struct target
  16. {
  17.     std::vector<coordinates> coord;
  18. };
  19.  
  20. int main()
  21. {
  22.     try
  23.     {
  24.         const auto coord_parser = json::make_object<coordinates>(
  25.             json::pair("x", &coordinates::x),
  26.             json::pair("y", &coordinates::y),
  27.             json::pair("z", &coordinates::z)
  28.         );
  29.  
  30.         auto parser = json::make_object<target>(
  31.             json::pair("coordinates", json::array(&target::coord, coord_parser))
  32.         );
  33.  
  34.         target result;
  35.         std::ifstream file("./1.json");
  36.         parser.read(file, result);
  37.  
  38.         double x = 0.0;
  39.         double y = 0.0;
  40.         double z = 0.0;
  41.         std::size_t size = result.coord.size();
  42.  
  43.         for (std::size_t i = 0; i < size; ++i)
  44.         {
  45.             const auto& coord = result.coord[i];
  46.             x += coord.x;
  47.             y += coord.y;
  48.             z += coord.z;
  49.         }
  50.  
  51.         std::cout << x / static_cast<double>(size) << std::endl
  52.             << y / static_cast<double>(size) << std::endl
  53.             << z / static_cast<double>(size) << std::endl;
  54.     }
  55.     catch (const std::exception& e)
  56.     {
  57.         std::cerr << e.what() << std::endl;
  58.     }
  59.  
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement