Advertisement
bazmikel

Untitled

May 21st, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7. struct SPiwo{
  8.     int h;
  9.     float r;
  10.     string nazwa;
  11. };
  12.  
  13.  
  14. struct SKarton{
  15.     unsigned n;
  16.     SPiwo *piwo;
  17. };
  18.  
  19. bool wczytaj_piwa(istream &file, SKarton &karton){
  20.     bool wczytane;
  21.     SPiwo * piwo = 0;
  22.  
  23.     if(!file){
  24.         cerr << "Error while reading!" << endl;
  25.         file.clear();
  26.         wczytane = false;
  27.     }
  28.  
  29.     file >> karton.n;
  30.     if(!file.good()){
  31.         cerr << "Error while reading!" << endl;
  32.         file.clear();
  33.         wczytane = false;
  34.     }
  35.  
  36.     if(karton.n > 0){
  37.         piwo = new SPiwo[karton.n];
  38.         for(unsigned i = 0; i < karton.n; i++){
  39.             file >> piwo[i].h;
  40.             if(!file.good()){
  41.             cerr << "Error 1" << endl;
  42.             delete[] piwo;
  43.             piwo = 0;
  44.             file.clear();
  45.             wczytane = false;
  46.             }
  47.             file >> piwo[i].r;
  48.             if(!file.good()){
  49.             cerr << "Error 1" << endl;
  50.             delete[] piwo;
  51.             piwo = 0;
  52.             file.clear();
  53.             wczytane = false;
  54.             }
  55.             file >> piwo[i].nazwa; // WHY?a
  56.     }
  57.     karton.piwo = piwo;
  58.     wczytane = true;
  59. }
  60.     else{
  61.         cerr << "Amount is less or equals 0" << endl;
  62.         wczytane = false;
  63.     }
  64.     return wczytane;
  65. }
  66.  
  67. void wypisz(ostream &out, const SPiwo piwasik){ // Функція, що виписує інфу тільки одного пива. Аргументи
  68.     out << piwasik.h << " " << piwasik.r << " " << piwasik.nazwa << endl;
  69. }
  70.  
  71. void wypisz(ostream &out, const SKarton karton){
  72.     out << "Karton ma w sobie " << karton.n << " piw" << endl;
  73.     for(unsigned i = 0; i < karton.n; i++){
  74.         out << karton.piwo[i].h << " " << karton.piwo[i].r << " ";
  75.         out << karton.piwo[i].nazwa << endl;
  76.     }
  77. }
  78.  
  79. float podaj_wysokosc(const SKarton karton){
  80.     float maxh = karton.piwo[0].h;
  81.     for(unsigned i = 1; i < karton.n; i++){
  82.         if(karton.piwo[i].h > maxh)
  83.             maxh = karton.piwo[i].h;
  84.     }
  85.     return maxh;
  86. }
  87.  
  88. float V(const SPiwo piwo){
  89.     return (3.14 * piwo.r * piwo.r * piwo.h);
  90. }
  91.  
  92. float podaj_objetosc(const SKarton karton){
  93.     float maxV = V(karton.piwo[0]);
  94.     for(unsigned i = 1; i < karton.n; i++){
  95.         if(V(karton.piwo[i]) > maxV)
  96.             maxV = V(karton.piwo[i]);
  97.     }
  98.     return maxV;
  99. }
  100. //g++ -Wall -pedantic -g -o zad surname.cpp
  101. //valgrind --leak-check=full ./zad karton.txt
  102.  
  103. int main(int argc, char **argv)
  104. {
  105.     ifstream fin(argv[1]);
  106.     if(!fin){
  107.         fin.clear();
  108.         fin.close();
  109.         cerr << "Blad otwarcia!" << endl;
  110.         return 0;
  111.     }
  112.     SKarton karton;
  113.     if(wczytaj_piwa(fin, karton)){
  114.     wypisz(cout, karton.piwo[0]);
  115.     wypisz(cout, karton);
  116.  
  117.  
  118.     float wysokosc_kartonu = podaj_wysokosc(karton);
  119.     float objetosc_kartonu = podaj_objetosc(karton);
  120.     cout << "Wysokosc kartonu: " << wysokosc_kartonu << endl;
  121.     cout << "Objetosc kartonu: " << objetosc_kartonu << endl;
  122.     fin.clear();
  123.     fin.close();
  124.     delete[] karton.piwo;
  125.     karton.piwo = 0;
  126.     return 0;
  127.     }
  128.     else{
  129.         cout << "Dane nie wczytane!" << endl;
  130.         return 0;
  131.     }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement