Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <map>
- #include <set>
- #include <vector>
- #include <string>
- #include <stdio.h>
- using namespace std;
- class Electrical_facility //электрическая установка
- {
- protected:
- string type;
- double r;
- public:
- Electrical_facility(string newtype, double newr)
- {
- type = newtype;
- r = newr;
- }
- string get_type()
- {
- return type;
- }
- //void set_type(string newtype)
- //{
- // this->type = newtype;
- //}
- double get_r()
- {
- return r;
- }
- void set_r(double newr)
- {
- this->r = newr;
- }
- virtual void print()
- {
- cout << this->get_type() << ": " << this->get_r() << " Om" << endl;
- }
- };
- class Lamp : public Electrical_facility
- {
- protected:
- bool light;
- public:
- Lamp(double newr) : Electrical_facility("LMP", newr)
- {
- light = false;
- }
- bool get_light()
- {
- return light;
- }
- void set_light(bool newlight)
- {
- this->light = newlight;
- }
- void print()
- {
- cout << this->get_type() << ": " << this->get_r() << " Om ";
- if(this->get_light())
- cout << "light: on\n";
- else
- cout << "light: off\n";
- }
- };
- class Resistor : public Electrical_facility
- {
- public:
- Resistor(double newr) : Electrical_facility("RES", newr) {}
- void print()
- {
- cout << this->get_type() << ": " << this->get_r() << " Om" << endl;
- }
- };
- class Appliance : public Electrical_facility
- {
- protected:
- double req_u, req_i, du, di;
- bool is_working;
- public:
- Appliance(double newr, double newrequ, double newreqi,
- double newdu, double newdi) : Electrical_facility("APP", newr)
- {
- req_u = newrequ;
- du = newdu;
- req_i = newreqi;
- di = newdi;
- is_working = false;
- }
- double get_req_u()
- {
- return req_u;
- }
- void set_req_u(double newreq_u)
- {
- this->req_u = newreq_u;
- }
- double get_req_i()
- {
- return req_i;
- }
- void set_req_i(double newreq_i)
- {
- this->req_i = newreq_i;
- }
- double get_du()
- {
- return du;
- }
- void set_du(double newdu)
- {
- this->du = newdu;
- }
- double get_di()
- {
- return di;
- }
- void set_di(double newdi)
- {
- this->di = newdi;
- }
- void set_is_working(bool state)
- {
- this->is_working = state;
- }
- bool get_is_working()
- {
- return is_working;
- }
- void print()
- {
- cout << this->get_type() << ": " << this->get_r() << " Om ";
- if(this->get_is_working())
- cout << "work: on\n";
- else
- cout << "work: off\n";
- cout << " required: " << this->get_req_u() << "V " << this->get_req_i() << "A \n";
- cout << " variation: " << this->get_du() << "V " << this->get_di() << "A \n";
- }
- };
- class CCS : public Electrical_facility
- {
- protected:
- double e;
- public:
- CCS(double newr, double newe) : Electrical_facility("CCS", newr)
- {
- e = newe;
- }
- double get_e()
- {
- return e;
- }
- void set_e(double newe)
- {
- this->e = newe;
- }
- void print()
- {
- cout << this->get_type() << ": " << this->get_r() << " Om " << this->get_e() << " V" << endl;
- }
- };
- class Key : public Electrical_facility
- {
- protected:
- bool state;
- public:
- Key(bool newstate) : Electrical_facility("KEY", 0)
- {
- state = newstate;
- }
- bool get_state()
- {
- return state;
- }
- void set_state(bool newstate)
- {
- this->state = newstate;
- }
- void print()
- {
- cout << this->get_type() << ": ";
- if(this->get_state())
- cout << "locked \n";
- else
- cout << "unlocked \n";
- }
- };
- class Branch
- {
- protected:
- vector<Electrical_facility*> branch;
- public:
- Branch() {}
- void set_el_f(int chain_number)
- {
- cout << "If you want to set a new lamp, please type 1" << endl;
- cout << "If you want to set a new resistor, please type 2" << endl;
- cout << "If you want to set a new appliance, please type 3" << endl;
- cout << "If you want to set a new CCS, please type 4" << endl;
- cout << "If you want to set a new key, please type 5" << endl;
- cout << "Be careful: you can set a CCS only in main (first) branch" << endl;
- int type, place; cin >> type;
- cout << "Now please type the number of position, where you want to put it: ";
- cin >> place;
- Electrical_facility* new_obj = new Electrical_facility("error", -100500);
- double r, ru, ri, du, di, e;
- bool f = true;
- switch (type)
- {
- case 1:
- cout << "Please enter the lamp resistance: ";
- cin >> r;
- new_obj = new Lamp(r);
- break;
- case 2:
- cout << "Please enter the resistor resistance: ";
- cin >> r;
- new_obj = new Resistor(r);
- break;
- case 3:
- cout << "Please enter the appliance resistance, then required voltage and amperage, ";
- cout << "then variation of voltage and amperage: ";
- cin >> r >> ru >> ri >> du >> di;
- new_obj = new Appliance(r, ru, ri, du, di);
- break;
- case 4:
- cout << "Please enter the CCS resistance and electromotive force: ";
- cin >> r >> e;
- if(chain_number != 0)
- {
- cout << "You can't place CCS here\n";
- f = false;
- }
- else
- new_obj = new CCS(r, e);
- break;
- case 5:
- cout << "Please enter the key state: ";
- bool st;
- cin >> st;
- new_obj = new Key(st);
- break;
- default:
- f = false;
- cout << "Something went wrong, please try again\n";
- break;
- }
- place--;
- if(f)
- if(place < 0 || place > branch.size())
- cout << "Something went wrong, please try again\n";
- else
- {
- branch.insert(branch.begin() + place, new_obj);
- }
- }
- //bool is_empty(){
- // return branch.empty();
- //}
- void remove_el_f()
- {
- cout << "Please enter the number of the facility you want to remove: ";
- int n; cin >> n; n--;
- if(n >= branch.size() || n < 0)
- cout << "Something went wrong, please try again\n";
- else
- branch.erase(branch.begin() + n);
- }
- bool is_locked()
- {
- bool res = true;
- for(int i = 0; i < branch.size(); i++)
- {
- if(branch[i]->get_type() == "KEY")
- {
- Key key = *(dynamic_cast<Key*>(branch[i]));
- if(!key.get_state())
- {
- res = false;
- break;
- }
- }
- }
- return res;
- }
- double get_r_from_to(int from, int to)
- {
- double res = 0;
- from--, to--;
- if(to >= 0 && to < branch.size() && from >=0 && from < branch.size()
- && to >= from && this->is_locked())
- for(int i = from; i <= to; i++)
- res += branch[i]->get_r();
- return res;
- }
- void change_parameters()
- {
- cout << "There are " << branch.size() << " elements in this branch\n";
- cout << "Enter the number of element which parameters you want to change: ";
- int i; cin >> i; i--;
- if(i >= branch.size() || i < 0)
- cout << "Incorrect number of element, please try again\n";
- else
- {
- string type = branch[i]->get_type();
- cout << "Availiable changes: \n";
- if(type == "LMP")
- {
- Lamp* lmp = (dynamic_cast<Lamp*>(branch[i]));
- cout << "Internal resistance: now" << lmp->get_r() << " Om\n";
- cout << "Type 1 for changing the internal resistance, \n";
- cout << " 0 for exit: ";
- int answer; cin >> answer;
- switch (answer)
- {
- case 1:
- cout << "Enter new value of internal resistance: ";
- double r; cin >> r;
- lmp->set_r(r);
- break;
- case 0:
- break;
- default:
- cout << "Incorrect command\n";
- break;
- }
- }
- else if(type == "RES")
- {
- Resistor* res = (dynamic_cast<Resistor*>(branch[i]));
- cout << "Resistance: now" << res->get_r() << " Om\n";
- cout << "Type 1 for changing the resistance, \n";
- cout << " 0 for exit: ";
- int answer; cin >> answer;
- switch (answer)
- {
- case 1:
- cout << "Enter new value of resistance: ";
- double r; cin >> r;
- res->set_r(r);
- break;
- case 0:
- break;
- default:
- cout << "Incorrect command\n";
- break;
- }
- }
- else if(type == "APP")
- {
- Appliance* app = (dynamic_cast<Appliance*>(branch[i]));
- cout << "Internal resistance: now" << app->get_r() << " Om\n";
- cout << "Required voltage: now " << app->get_req_u() << " V\n";
- cout << "Required amperage: now " << app->get_req_i() << " A\n";
- cout << "Variation of voltage: now " << app->get_du() << " V\n";
- cout << "Variation of amperage: now " << app->get_di() << " A\n";
- cout << "Type 1 for changing the internal resistance, \n";
- cout << " 2 for changing the required voltage, \n";
- cout << " 3 for changing the required amperage, \n";
- cout << " 4 for changing the variation of voltage, \n";
- cout << " 5 for changing the variation of amperage, \n";
- cout << " 0 for exit: ";
- int answer; cin >> answer;
- double u, i, r;
- switch (answer)
- {
- case 1:
- cout << "Enter new value of internal resistance: ";
- cin >> r;
- app->set_r(r);
- break;
- case 2:
- cout << "Enter new value of required voltage: ";
- cin >> u;
- app->set_req_u(u);
- break;
- case 3:
- cout << "Enter new value of required amperage: ";
- cin >> i;
- app->set_req_i(i);
- break;
- case 4:
- cout << "Enter new value of variation of voltage: ";
- cin >> u;
- app->set_du(u);
- break;
- case 5:
- cout << "Enter new value of variation of amperage: ";
- cin >> i;
- app->set_di(i);
- break;
- case 0:
- break;
- default:
- cout << "Incorrect command\n";
- break;
- }
- }
- else if(type == "CCS")
- {
- CCS* ccs = (dynamic_cast<CCS*>(branch[i]));
- cout << "Internal resistance: now" << ccs->get_r() << " Om\n";
- cout << "Electromotive force of the CCS: now " << ccs->get_e() << " V\n";
- cout << "Type 1 for changing the internal resistance, \n";
- cout << " 2 for changing the electromotive force, \n";
- cout << " 0 for exit: ";
- int answer; cin >> answer;
- switch (answer)
- {
- case 1:
- cout << "Enter new value of internal resistance: ";
- double r; cin >> r;
- ccs->set_r(r);
- break;
- case 2:
- cout << "Enter new value of electromotive force: ";
- double e; cin >> e;
- ccs->set_e(e);
- break;
- case 0:
- break;
- default:
- cout << "Incorrect command\n";
- break;
- }
- }
- else if(type == "KEY")
- {
- Key* key = (dynamic_cast<Key*>(branch[i]));
- cout << "State of the key: now ";
- if(key->get_state())
- cout << "locked\n";
- else cout << "unlocked\n";
- cout << "Type 1 for changing the key state, 0 for exit: ";
- int answer; cin >> answer;
- switch (answer)
- {
- case 1:
- cout << "Type 1 (or 0) for locking (or unlocking) the key: ";
- bool newstate; cin >> newstate;
- key->set_state(newstate);
- case 0:
- break;
- default:
- cout << "Incorrect command\n";
- break;
- }
- }
- else
- {
- cout << "Coder, you have a great error! Fix it immideately!" << endl;
- }
- }
- }
- int size()
- {
- return branch.size();
- }
- bool is_shorted_path()
- {
- if(branch.size() == 0)
- return true;
- if(!this->is_locked())
- return false;
- for(int i = 0; i < branch.size(); i++)
- {
- if(branch[i]->get_type() != "KEY")
- return false;
- }
- return true;
- }
- void print(){
- for(int i = 0; i < branch.size(); i++)
- branch[i]->print();
- }
- };
- class Subchain //please test me, i am not tested
- {
- protected:
- vector<Branch*> subchain;
- public:
- void set_branch()
- {
- subchain.push_back(new Branch());
- }
- void set_el(int chain_number)
- {
- cout << "There are " << subchain.size() << " branches in subchain\n";
- cout << "Please type the number of branch where you want to set the element: ";
- int j; cin >> j; j--;
- if(j >= 0 && j < subchain.size())
- subchain[j]->set_el_f(chain_number);
- else
- cout << "Incorrect number of branch\n";
- }
- void change_parameters()
- {
- cout << "There are " << subchain.size() << " branches in subchain\n";
- cout << "Please type the number of branch where you want to\n";
- cout << "change parameters of the element: ";
- int j; cin >> j; j--;
- if(j >= 0 && j < subchain.size())
- subchain[j]->change_parameters();
- else
- cout << "Incorrect number of branch\n";
- }
- void remove_el()
- {
- cout << "There are " << subchain.size() << " branches in subchain\n";
- cout << "Please type the number of branch where you want to delete the element: ";
- int j; cin >> j; j--;
- if(j >= 0 && j < subchain.size())
- subchain[j]->remove_el_f();
- else
- cout << "Incorrect number of branch\n";
- }
- bool has_shorted_paths()
- {
- for(int i = 0; i < subchain.size(); i++)
- {
- if(subchain[i]->is_shorted_path())
- return true;
- }
- return false;
- }
- double get_r_from_to()
- {
- cout << "Please enter the number of branch where needed elements are: ";
- int j; cin >> j; j--;
- if(j >= 0 && j < subchain.size())
- {
- cout << "Please enter the numbers of first and last elements for calculating\n"
- int from, to; cin >> from >> to;
- subchain[j]->get_r_from_to(from, to);
- }
- else
- cout << "Incorrect number of branch\n";
- }
- double get_r_branch()
- {
- cout << "Please enter the number of branch where you want to find resistance value: ";
- int j; cin >> j; j--;
- if(j >= 0 && j < subchain.size())
- subchain[j]->get_r_from_to(1, subchain[j]->size());
- else
- cout << "Incorrect number of branch\n";
- }
- double get_r_subchain()
- {
- double mult = 1.0; //multiplication
- double sum = 0.0;
- int cnt = 0;
- if(this->has_shorted_paths())
- return 0;
- for(int i = 0; i < subchain.size(); i++)
- {
- if(subchain[i]->is_locked())
- {
- double r = subchain[i]->get_r_from_to(1, subchain[i]->size());
- mult *= r;
- sum += r;
- cnt++;
- }
- }
- if(cnt == 0)
- return 0;
- else if(cnt == 1)
- return sum;
- else
- return mult / sum;
- }
- bool is_locked()
- {
- bool res = true;
- if(!subchain.empty())
- {
- res = subchain[0]->is_locked();
- for(int i = 1; i < subchain.size(); i++)
- {
- res = res || subchain[i]->is_locked();
- }
- }
- return res;
- }
- void remove_branch()
- {
- cout << "There are " << subchain.size() << " branches in subchain\n";
- cout << "Please enter the number of branch you want to remove: ";
- int j; cin >> j; j--;
- if(j >= 0 && j < subchain.size())
- subchain.erase(subchain.begin() + j);
- else
- cout << "Incorrect number of branch\n";
- }
- void print()
- {
- for(int i = 0; i < subchain.size(); i++)
- {
- cout << "Branch " << i + 1 << endl;
- subchain[i]->print();
- cout << endl;
- }
- }
- };
- class Chain
- {
- protected:
- vector<Subchain*> chain;
- public:
- void set()
- {
- cout << "Type 0 to exit\n";
- cout << "Type 1 to set a new subchain\n";
- cout << "Type 2 to set a new branch\n";
- cout << "Type 3 to set a new element\n";
- int n; cin >> n;
- if(n == 1) chain.push_back(new Subchain());
- else
- if(n == 2 || n == 3)
- {
- cout << "Please enter the number of subchain where you want to set new ";
- if(n == 2) cout << "branch: ";
- if(n == 3) cout << "element: ";
- int i; cin >> i; i--;
- if(i >= 0 && i < chain.size())
- {
- if(n == 2) chain[i]->set_branch();
- if(n == 3) chain[i]->set_el(i);
- }
- else
- cout << "Incorrect number of subchain\n";
- }
- }
- /*void set_subchain()
- {
- chain.push_back(new Subchain());
- }
- void set_branch()
- {
- cout << "Please enter the number of subchain where you want to set new branch: ";
- int i; cin >> i; i--;
- if(i >= 0 && i < chain.size())
- chain[i]->set_branch();
- else
- cout << "Incorrect number of subchain\n";
- }
- void set_el()
- {
- cout << "Please enter the number of subchain where you want to set new element: ";
- int i; cin >> i; i--;
- if(i >= 0 && i < chain.size())
- chain[i]->set_el(i);
- else
- cout << "Incorrect number of subchain\n";
- }*/
- //void remove_el()
- //{
- // cout << "Please enter the number of chain where you want to remove the element: ";
- // int i; cin >> i; i--;
- // if(i >= 0 && i < chain.size())
- // chain[i]->remove_el();
- // else
- // cout << "Incorrect number of chain\n";
- //}
- //void remove_branch()
- //{
- // cout << "Please enter the number of chain where you want to remove the branch: ";
- // int i; cin >> i; i--;
- // if(i >= 0 && i < chain.size())
- // chain[i]->remove_branch();
- // else
- // cout << "Incorrect number of chain\n";
- //}
- //void remove_chain()
- //{
- // cout << "Please enter the number of chain you want to remove: ";
- // int i; cin >> i; i--;
- // if(i >= 0 && i < chain.size())
- // chain.erase(chain.begin() + i);
- // else
- // cout << "Incorrect number of chain\n";
- //}
- void remove()
- {
- map<int, pair<string, string>> m;
- m[0] = make_pair("exit", "error");
- m[1] = make_pair("element",
- "Please enter the number of subchain where you want to remove the element: ");
- m[2] = make_pair("branch",
- "Please enter the number of subchain where you want to remove the branch: ");
- m[3] = make_pair("subchain",
- "Please enter the number of subchain you want to remove: ");
- for(int i = 0; i < 4; i++)
- cout << "Type " << i << " to remove the " << m[i].first << endl;
- int n; cin >> n;
- if(n > 0 && n < 4)
- {
- cout << m[n].second;
- int i; cin >> i; i--;
- if(i >= 0 && i < chain.size())
- {
- if(n == 1) chain[i]->remove_el();
- else if(n == 2) chain[i]->remove_branch();
- else if(n == 3) chain.erase(chain.begin() + i);
- }
- else
- cout << "Incorrect number of subchain\n";
- }
- }
- void change_parameters()
- {
- cout << "Please enter the number of subchain where you want to\n";
- cout << "change paramaters of the element: ";
- int i; cin >> i; i--;
- if(i >= 0 && i < chain.size())
- chain[i]->change_parameters();
- else
- cout << "Incorrect number of subchain\n";
- }
- bool is_locked()
- {
- bool res = true;
- for(int i = 0; i < chain.size(); i++)
- {
- res = res && chain[i]->is_locked();
- }
- return res;
- }
- double get_r_chain()
- {
- double r = 0.0;
- if(this->is_locked())
- {
- for(int i = 0; i < chain.size(); i++)
- r += chain[i]->get_r_subchain();
- }
- else
- cout << "Chain is unlocked\n";
- return r;
- }
- void get_r_subchain()
- {
- cout << "Please enter the number of subchain which resistance you need: ";
- int i; cin >> i; i--;
- if(i >= 0 && i < chain.size())
- chain[i]->get_r_subchain();
- else
- cout << "Incorrect number of subchain\n";
- }
- void get_r_branch()
- {
- cout << "Please enter the number of subchain where needed branch is: ";
- int i; cin >> i; i--;
- if(i >= 0 && i < chain.size())
- chain[i]->get_r_branch();
- else
- cout << "Incorrect number of subchain\n";
- }
- void get_r_from_to()
- {
- cout << "Please enter the number of subchain where needed elements are: ";
- int i; cin >> i; i--;
- if(i >= 0 && i < chain.size())
- chain[i]->get_r_from_to();
- else
- cout << "Incorrect number of subchain\n";
- }
- void print()
- {
- for(int i = 0; i < chain.size(); i++)
- {
- cout << "Subchain " << i + 1 << endl;
- chain[i]->print();
- }
- }
- };
- int main()
- {
- //#ifdef _DEBUG
- // freopen("in.txt", "r", stdin);
- // freopen("out.txt", "w", stdout);
- //#endif
- Subchain* my_subchain = new Subchain();
- int n = 0;
- while(n != 26)
- {
- cout << "1 - set new branch\n";
- cout << "2 - set new element\n";
- cout << "3 - change parameters\n";
- cout << "4 - remove the element\n";
- cout << "5 - get r of subchain\n";
- cout << "6 - check if locked\n";
- cout << "7 - print the subchain\n";
- cout << "8 - check for shrted paths\n";
- cout << "9 - remove the branch\n";
- cin >> n;
- switch (n)
- {
- case 1:
- my_subchain->set_new_branch();
- break;
- case 2:
- my_subchain->set_el();
- break;
- case 3:
- my_subchain->change_parameters();
- break;
- case 4:
- my_subchain->remove_el();
- break;
- case 5:
- cout << my_subchain->get_r_subchain() << endl;
- break;
- case 6:
- cout << my_subchain->is_locked() << endl;
- break;
- case 7:
- my_subchain->print();
- break;
- case 8:
- cout << my_subchain->has_shorted_paths() << endl;
- break;
- case 9:
- my_subchain->remove_branch();
- break;
- default:
- break;
- }
- }
- cout << "compilation completed\n";
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement