Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.cpp
- #include <iostream>
- #include <cstdlib>
- #include "Planet.hpp"
- using namespace std;
- int main(int argc, char **argv) {
- Planet p;
- string fajlnev;
- do {
- cout << "Add meg a fajl nevet! "; cin >> fajlnev;
- if (p.Bolygo(fajlnev)) {
- break;
- } else {
- cout << "A fajlt nem lehet betolteni! ";
- }
- } while (true);
- p.szimulacio();
- p.elok();
- return 0;
- }
- //Deltafa.hpp
- #pragma once
- #include "Plant.hpp"
- #include "Radiation.hpp"
- class Deltafa : public Plant {
- public:
- Deltafa(std::string nev, int tapanyag):Plant(nev,tapanyag) {}
- void futtatas(Radiation& sugarzas) {
- if (!el) {
- return;
- }
- switch(sugarzas.getallapot()) {
- case Radiation::alfa:
- usetapanyag(3);
- break;
- case Radiation::delta:
- addtapanyag(4);
- break;
- case Radiation::nincs:
- usetapanyag(1);
- break;
- }
- if (tapanyag < 1) {
- el = false;
- } else if (tapanyag < 5) {
- sugarzas.kerdelta(4);
- } else if (tapanyag <= 10) {
- sugarzas.kerdelta(1);
- }
- }
- };
- //Parabokor.hpp
- #pragma once
- #include "string"
- #include "Plant.hpp"
- #include "Radiation.hpp"
- class Parabokor : public Plant {
- public:
- Parabokor(std::string nev, int tapanyag):Plant(nev,tapanyag) {}
- void futtatas(Radiation& sugarzas) {
- if (!el) {
- return;
- }
- switch(sugarzas.getallapot()) {
- case Radiation::alfa:
- addtapanyag(1);
- break;
- case Radiation::delta:
- addtapanyag(1);
- break;
- case Radiation::nincs:
- usetapanyag(1);
- break;
- }
- if (tapanyag < 1) {
- el = false;
- }
- }
- };
- //Puffancs.hpp
- #pragma once
- #include "Plant.hpp"
- #include "Radiation.hpp"
- class Puffancs : public Plant {
- public:
- Puffancs(std::string nev, int tapanyag):Plant(nev,tapanyag) {}
- void futtatas(Radiation& sugarzas) {
- if (!el) {
- return;
- }
- switch(sugarzas.getallapot()) {
- case Radiation::alfa:
- addtapanyag(2);
- break;
- case Radiation::delta:
- usetapanyag(2);
- break;
- case Radiation::nincs:
- addtapanyag(1);
- break;
- }
- if (tapanyag > 10 || tapanyag < 1) {
- el = false;
- } else {
- sugarzas.keralfa(10*tapanyag);
- }
- }
- };
- //Planet.hpp
- #pragma once
- #include "fstream"
- #include "Radiation.hpp"
- #include "Plant.hpp"
- #include "Deltafa.hpp"
- #include "Parabokor.hpp"
- #include "Puffancs.hpp"
- class Planet {
- private:
- int napok;
- Radiation sugarzas;
- int dbnoveny;
- Plant** novenyek;
- public:
- Planet() {}
- ~Planet() {
- delete[] novenyek;
- }
- void szimulacio() {
- for (int i = 0; i < napok; i++) {
- for (int i = 0; i < dbnoveny; i++) {
- novenyek[i]->futtatas(sugarzas);
- }
- sugarzas.futtatas();
- }
- }
- bool Bolygo(std::string fnev) {
- std::ifstream f;
- f.open(fnev.c_str());
- if (f.fail()) {
- return false;
- }
- f >> dbnoveny;
- novenyek = new Plant*[dbnoveny];
- std::string nev;
- char tipus;
- int tapanyag;
- for (int i = 0; i < dbnoveny; i++) {
- f >> nev;
- f >> tipus;
- f >> tapanyag;
- switch (tipus) {
- case 'a':
- novenyek[i] = new Puffancs(nev,tipus);
- break;
- case 'd':
- novenyek[i] = new Deltafa(nev,tipus);
- break;
- case 'p':
- novenyek[i] = new Parabokor(nev,tipus);
- break;
- }
- std::cout << "Uj noveny jelent meg a bolygon: " << nev << std::endl;
- }
- std::cout << "A bolygon " << dbnoveny << " noveny el a szimulacio elejen!" << std::endl;
- f >> napok;
- std::cout << napok << " napot szimulalunk" << std::endl;
- f.close();
- return true;
- }
- void elok() {
- bool elso = true;
- for (int i = 0; i < dbnoveny; i++) {
- if (novenyek[i]->isel()) {
- if (elso) {
- std::cout << "Eletben maradt novenyek: " << novenyek[i]->getnev();
- elso = false;
- } else {
- std::cout << ", " << novenyek[i]->getnev();
- }
- }
- }
- if (elso) {
- std::cout << "Nem maradt eletben egy noveny sem!";
- }
- std::cout << std::endl;
- }
- };
- //Plant.hpp
- #pragma once
- #include <string>
- #include "Radiation.hpp"
- class Plant {
- protected:
- std::string nev;
- int tapanyag;
- bool el;
- int gettapanyag() { return tapanyag; }
- void addtapanyag(int tapanyag) { this->tapanyag = this->tapanyag + tapanyag; };
- void usetapanyag(int tapanyag) { this->tapanyag = this->tapanyag - tapanyag; };
- public:
- Plant(std::string nev, int tapanyag):nev(nev),tapanyag(tapanyag),el(true) {}
- std::string getnev() { return nev; }
- bool isel() { return el; }
- virtual void futtatas(Radiation& sugarzas) = 0;
- };
- //Radiation.hpp
- #pragma once
- class Radiation {
- public:
- enum State { alfa, delta, nincs };
- Radiation():allapot(nincs),ker(0) {}
- State getallapot() { return allapot; }
- void keralfa(const int& r) { ker -= r; }
- void kerdelta(const int& r) { ker += r; }
- void futtatas() {
- if (ker < -2) {
- allapot = alfa;
- } else if (ker > 2) {
- allapot = delta;
- } else {
- allapot = nincs;
- }
- ker = 0;
- }
- private:
- State allapot;
- int ker;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement