Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cstdlib>
- using namespace std;
- struct SPiwo{
- int h;
- float r;
- string nazwa;
- };
- struct SKarton{
- unsigned n;
- SPiwo *piwo;
- };
- bool wczytaj_piwa(istream &file, SKarton &karton){
- bool wczytane;
- SPiwo * piwo = 0;
- if(!file){
- cerr << "Error while reading!" << endl;
- file.clear();
- wczytane = false;
- }
- file >> karton.n;
- if(!file.good()){
- cerr << "Error while reading!" << endl;
- file.clear();
- wczytane = false;
- }
- if(karton.n > 0){
- piwo = new SPiwo[karton.n];
- for(unsigned i = 0; i < karton.n; i++){
- file >> piwo[i].h;
- if(!file.good()){
- cerr << "Error 1" << endl;
- delete[] piwo;
- piwo = 0;
- file.clear();
- wczytane = false;
- }
- file >> piwo[i].r;
- if(!file.good()){
- cerr << "Error 1" << endl;
- delete[] piwo;
- piwo = 0;
- file.clear();
- wczytane = false;
- }
- file >> piwo[i].nazwa; // WHY?a
- }
- karton.piwo = piwo;
- wczytane = true;
- }
- else{
- cerr << "Amount is less or equals 0" << endl;
- wczytane = false;
- }
- return wczytane;
- }
- void wypisz(ostream &out, const SPiwo piwasik){ // Функція, що виписує інфу тільки одного пива. Аргументи
- out << piwasik.h << " " << piwasik.r << " " << piwasik.nazwa << endl;
- }
- void wypisz(ostream &out, const SKarton karton){
- out << "Karton ma w sobie " << karton.n << " piw" << endl;
- for(unsigned i = 0; i < karton.n; i++){
- out << karton.piwo[i].h << " " << karton.piwo[i].r << " ";
- out << karton.piwo[i].nazwa << endl;
- }
- }
- float podaj_wysokosc(const SKarton karton){
- float maxh = karton.piwo[0].h;
- for(unsigned i = 1; i < karton.n; i++){
- if(karton.piwo[i].h > maxh)
- maxh = karton.piwo[i].h;
- }
- return maxh;
- }
- float V(const SPiwo piwo){
- return (3.14 * piwo.r * piwo.r * piwo.h);
- }
- float podaj_objetosc(const SKarton karton){
- float maxV = V(karton.piwo[0]);
- for(unsigned i = 1; i < karton.n; i++){
- if(V(karton.piwo[i]) > maxV)
- maxV = V(karton.piwo[i]);
- }
- return maxV;
- }
- //g++ -Wall -pedantic -g -o zad surname.cpp
- //valgrind --leak-check=full ./zad karton.txt
- int main(int argc, char **argv)
- {
- ifstream fin(argv[1]);
- if(!fin){
- fin.clear();
- fin.close();
- cerr << "Blad otwarcia!" << endl;
- return 0;
- }
- SKarton karton;
- if(wczytaj_piwa(fin, karton)){
- wypisz(cout, karton.piwo[0]);
- wypisz(cout, karton);
- float wysokosc_kartonu = podaj_wysokosc(karton);
- float objetosc_kartonu = podaj_objetosc(karton);
- cout << "Wysokosc kartonu: " << wysokosc_kartonu << endl;
- cout << "Objetosc kartonu: " << objetosc_kartonu << endl;
- fin.clear();
- fin.close();
- delete[] karton.piwo;
- karton.piwo = 0;
- return 0;
- }
- else{
- cout << "Dane nie wczytane!" << endl;
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement