C++ Bc. 19 cpp

Z GeoWikiCZ
Přejít na: navigace, hledání
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iomanip>

struct Bod
{
  int cislo;
  int kod;
  double x, y, z;
};

void cti_xy(std::istream&, std::vector<Bod>& z);

int main()
{
  using namespace std;
  istringstream 
    data("# vstupni soubor obsahuje seznam souradnic           \n"
         "# * komentare zacinaji znakem '#'                    \n"
         "# * na jednom radku muze byt cislo bodu a souradnice \n"
         "#    * cb x y z                                      \n"
         "#    * cb x y                                        \n"
         "#    * cb     z                                      \n"
         "   101   234.32  664.22  333.32  # xyz               \n"
         "   257   743.23  734.83          # xy                \n"
         "   142                   456.26  # z                 \n"
         "   232   643.53  324.63  263.73                      \n"
         "   183   232.43  392,12                              \n");


  vector<Bod>  seznam;
  cti_xy(data, seznam);

  int    ix = 0;
  int    iz = 0;
  double sx = 0;
  double sy = 0;
  double sz = 0;
  cout.precision(2);
  cout.setf(ios_base::fixed, ios_base::floatfield);
  for (vector<Bod>::iterator i=seznam.begin(), e=seznam.end(); i!=e; ++i)
    {
      Bod b = *i;
      cout << setw(6) << b.cislo;
      if (b.kod == 2 || b.kod == 3)
        {
          cout << " " << setw(8) << b.x << " " << setw(8) << b.y; 
          sx += b.x;
          sy += b.y;
          ix++;
        }
      if (b.kod == 1)
        cout << setw(18) << " ";
      if (b.kod == 3 || b.kod == 1)
        {
          cout << " " << setw(8) << b.z;
          sz += b.z;
          iz++;
        }
      cout << endl;
    }

  if (ix) { sx /= ix; sy /= ix; }
  if (iz) { sz /= iz; }

  cout << "\nprumer:" 
       << setw(8) << sx << " " << setw(8) << sy << " " 
       << setw(8) << sz << "\n";
}


void cti_xy(std::istream& inp, std::vector<Bod>& seznam)
{
  using namespace std;

  int       cb;
  string radek;
  while(getline(inp, radek))
    {
      for (int i=0; i<radek.size(); i++)
        if (radek[i] == '#')
          {
            // pokud existuje, vymazu komentar
            radek.erase(i, radek.size());
            break;
          }

      // vytvorim vstupni pametovy proud pro dany radek
      istringstream irad(radek);

      int    cb;
      double a, b, c;
      int    pocet = 0;
      irad >> cb;
      if (irad >> a) pocet++;
      if (irad >> b) pocet++;
      if (irad >> c) pocet++;

      if (pocet)
        {
          Bod tmp;
          tmp.cislo = cb;
          tmp.kod   = pocet;
          if (pocet == 1) 
            {
              tmp.z = a;
            }
          else if (pocet == 2)
            {
              tmp.x = a; tmp.y = b;
            }
          else if (pocet == 3)
            {
              tmp.x = a; tmp.y = b; tmp.z = c;
            }

          seznam.push_back(tmp);
        }
    }
}

[ Zpět ]