Advertisement
kokokozhina

Untitled

Dec 10th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <map>
  4. #include <set>
  5. #include <vector>
  6. #include <string>
  7. #include <stdio.h>
  8.  
  9. using namespace std;
  10.  
  11. class Electrical_facility //электрическая установка
  12. {
  13. protected:
  14.     string type;
  15.     double r;
  16. public:
  17.     Electrical_facility(string newtype, double newr)
  18.     {
  19.         type = newtype;
  20.         r = newr;
  21.     }
  22.  
  23.     string get_type()
  24.     {
  25.         return type;
  26.     }
  27.  
  28.     //void set_type(string newtype)
  29.     //{
  30.     //  this->type = newtype;
  31.     //}
  32.  
  33.     double get_r()
  34.     {
  35.         return r;
  36.     }
  37.  
  38.     void set_r(double newr)
  39.     {
  40.         this->r = newr;
  41.     }
  42.  
  43.     virtual void print()
  44.     {
  45.         cout << this->get_type() << ": " << this->get_r() << " Om" << endl;
  46.     }
  47. };
  48.  
  49. class Lamp : public Electrical_facility
  50. {
  51. protected:
  52.     bool light;
  53. public:
  54.     Lamp(double newr) : Electrical_facility("LMP", newr)
  55.     {
  56.         light = false;
  57.     }
  58.  
  59.     bool get_light()
  60.     {
  61.         return light;
  62.     }
  63.  
  64.     void set_light(bool newlight)
  65.     {
  66.         this->light = newlight;
  67.     }
  68.  
  69.     void print()
  70.     {
  71.         cout << this->get_type() << ": " << this->get_r() << " Om ";
  72.         if(this->get_light())
  73.             cout << "light: on\n";
  74.         else
  75.             cout << "light: off\n";
  76.     }
  77.  
  78. };
  79.  
  80. class Resistor : public Electrical_facility
  81. {
  82. public:
  83.     Resistor(double newr) : Electrical_facility("RES", newr) {}
  84.  
  85.     void print()
  86.     {
  87.         cout << this->get_type() << ": " << this->get_r() << " Om" << endl;
  88.     }
  89. };
  90.  
  91. class Appliance : public Electrical_facility
  92. {
  93. protected:
  94.     double req_u, req_i, du, di;
  95.     bool is_working;
  96.  
  97. public:
  98.     Appliance(double newr, double newrequ, double newreqi,
  99.          double newdu, double newdi) : Electrical_facility("APP", newr)
  100.     {
  101.         req_u = newrequ;
  102.         du = newdu;
  103.         req_i = newreqi;
  104.         di = newdi;
  105.         is_working = false;
  106.     }
  107.  
  108.     double get_req_u()
  109.     {
  110.         return req_u;
  111.     }
  112.  
  113.     void set_req_u(double newreq_u)
  114.     {
  115.         this->req_u = newreq_u;
  116.     }
  117.  
  118.     double get_req_i()
  119.     {
  120.         return req_i;
  121.     }
  122.  
  123.     void set_req_i(double newreq_i)
  124.     {
  125.         this->req_i = newreq_i;
  126.     }
  127.  
  128.     double get_du()
  129.     {
  130.         return du;
  131.     }
  132.  
  133.     void set_du(double newdu)
  134.     {
  135.         this->du = newdu;
  136.     }
  137.  
  138.     double get_di()
  139.     {
  140.         return di;
  141.     }
  142.  
  143.     void set_di(double newdi)
  144.     {
  145.         this->di = newdi;
  146.     }
  147.  
  148.     void set_is_working(bool state)
  149.     {
  150.         this->is_working = state;
  151.     }
  152.  
  153.     bool get_is_working()
  154.     {
  155.         return is_working;
  156.     }
  157.  
  158.     void print()
  159.     {
  160.         cout << this->get_type() << ": " << this->get_r() << " Om ";
  161.         if(this->get_is_working())
  162.             cout << "work: on\n";
  163.         else
  164.             cout << "work: off\n";
  165.         cout << "     required:  " << this->get_req_u() << "V " << this->get_req_i() << "A \n";
  166.         cout << "     variation: " << this->get_du() << "V " << this->get_di() << "A \n";
  167.     }
  168. };
  169.  
  170. class CCS : public Electrical_facility
  171. {
  172. protected:
  173.     double e;
  174.  
  175. public:
  176.     CCS(double newr, double newe) : Electrical_facility("CCS", newr)
  177.     {
  178.         e = newe;
  179.     }
  180.  
  181.     double get_e()
  182.     {
  183.         return e;
  184.     }
  185.  
  186.     void set_e(double newe)
  187.     {
  188.         this->e = newe;
  189.     }
  190.  
  191.     void print()
  192.     {
  193.         cout << this->get_type() << ": " << this->get_r() << " Om " << this->get_e() << " V" << endl;
  194.     }
  195. };
  196.  
  197. class Key : public Electrical_facility
  198. {
  199. protected:
  200.     bool state;
  201.  
  202. public:
  203.     Key(bool newstate) : Electrical_facility("KEY", 0)
  204.     {
  205.         state = newstate;
  206.     }
  207.  
  208.     bool get_state()
  209.     {
  210.         return state;
  211.     }
  212.  
  213.     void set_state(bool newstate)
  214.     {
  215.         this->state = newstate;
  216.     }
  217.  
  218.     void print()
  219.     {
  220.         cout << this->get_type() << ": ";
  221.         if(this->get_state())
  222.             cout << "locked \n";
  223.         else
  224.             cout << "unlocked \n";
  225.     }
  226. };
  227.  
  228. class Branch
  229. {
  230. protected:
  231.     vector<Electrical_facility*> branch;
  232. public:
  233.     Branch() {}
  234.     void set_el_f(int chain_number)
  235.     {
  236.         cout << "If you want to set a new lamp,      please type 1" << endl;
  237.         cout << "If you want to set a new resistor,  please type 2" << endl;
  238.         cout << "If you want to set a new appliance, please type 3" << endl;
  239.         cout << "If you want to set a new CCS,       please type 4" << endl;
  240.         cout << "If you want to set a new key,       please type 5" << endl;
  241.         cout << "Be careful: you can set a CCS only in main (first) branch" << endl;
  242.         int type, place; cin >> type;
  243.         cout << "Now please type the number of position, where you want to put it: ";
  244.         cin >> place;
  245.  
  246.         Electrical_facility* new_obj = new Electrical_facility("error", -100500);
  247.         double r, ru, ri, du, di, e;
  248.         bool f = true;
  249.         switch (type)
  250.         {
  251.         case 1:
  252.             cout << "Please enter the lamp resistance: ";
  253.             cin >> r;
  254.             new_obj = new Lamp(r);
  255.             break;
  256.         case 2:
  257.             cout << "Please enter the resistor resistance: ";
  258.             cin >> r;
  259.             new_obj = new Resistor(r);
  260.             break;
  261.         case 3:
  262.             cout << "Please enter the appliance resistance, then required voltage and amperage, ";
  263.             cout << "then variation of voltage and amperage: ";
  264.             cin >> r >> ru >> ri >> du >> di;
  265.             new_obj = new Appliance(r, ru, ri, du, di);
  266.             break;
  267.         case 4:
  268.             cout << "Please enter the CCS resistance and electromotive force: ";
  269.             cin >> r >> e;
  270.             if(chain_number != 0)
  271.             {
  272.                 cout << "You can't place CCS here\n";
  273.                 f = false;
  274.             }
  275.             else
  276.                 new_obj = new CCS(r, e);
  277.             break;
  278.         case 5:
  279.             cout << "Please enter the key state: ";
  280.             bool st;
  281.             cin >> st;
  282.             new_obj = new Key(st);
  283.             break;
  284.         default:
  285.             f = false;
  286.             cout << "Something went wrong, please try again\n";
  287.             break;
  288.         }
  289.  
  290.         place--;
  291.         if(f)
  292.             if(place < 0 || place > branch.size())
  293.                 cout << "Something went wrong, please try again\n";
  294.             else
  295.             {
  296.                 branch.insert(branch.begin() + place, new_obj);
  297.             }
  298.     }
  299.  
  300.     //bool is_empty(){
  301.     //  return branch.empty();
  302.     //}
  303.  
  304.     void remove_el_f()
  305.     {
  306.         cout << "Please enter the number of the facility you want to remove: ";
  307.         int n; cin >> n; n--;
  308.         if(n >= branch.size() || n < 0)
  309.             cout << "Something went wrong, please try again\n";
  310.         else
  311.             branch.erase(branch.begin() + n);
  312.     }
  313.  
  314.     bool is_locked()
  315.     {
  316.         bool res = true;
  317.         for(int i = 0; i < branch.size(); i++)
  318.         {
  319.             if(branch[i]->get_type() == "KEY")
  320.             {
  321.                 Key key = *(dynamic_cast<Key*>(branch[i]));
  322.                 if(!key.get_state())
  323.                 {
  324.                     res = false;
  325.                     break;
  326.                 }
  327.             }
  328.         }
  329.         return res;
  330.     }
  331.  
  332.     double get_r_from_to(int from, int to)
  333.     {
  334.         double res = 0;
  335.         from--, to--;
  336.         if(to >= 0 && to < branch.size() && from >=0 && from < branch.size()
  337.             && to >= from && this->is_locked())
  338.             for(int i = from; i <= to; i++)
  339.                 res += branch[i]->get_r();
  340.         return res;
  341.     }
  342.  
  343.     void change_parameters()
  344.     {
  345.         cout << "There are " << branch.size() << " elements in this branch\n";
  346.         cout << "Enter the number of element which parameters you want to change: ";
  347.         int i; cin >> i; i--;
  348.         if(i >= branch.size() || i < 0)
  349.             cout << "Incorrect number of element, please try again\n";
  350.         else
  351.         {
  352.             string type = branch[i]->get_type();
  353.             cout << "Availiable changes: \n";
  354.             if(type == "LMP")
  355.             {
  356.                 Lamp* lmp = (dynamic_cast<Lamp*>(branch[i]));
  357.                 cout << "Internal resistance: now" << lmp->get_r() << " Om\n";
  358.                 cout << "Type 1 for changing the internal resistance, \n";
  359.                 cout << "     0 for exit: ";
  360.                 int answer; cin >> answer;
  361.                 switch (answer)
  362.                 {
  363.                 case 1:
  364.                     cout << "Enter new value of internal resistance: ";
  365.                     double r; cin >> r;
  366.                     lmp->set_r(r);
  367.                     break;
  368.                 case 0:
  369.                     break;
  370.                 default:
  371.                     cout << "Incorrect command\n";
  372.                     break;
  373.                 }
  374.             }
  375.             else if(type == "RES")
  376.             {
  377.                 Resistor* res = (dynamic_cast<Resistor*>(branch[i]));
  378.                 cout << "Resistance: now" << res->get_r() << " Om\n";
  379.                 cout << "Type 1 for changing the resistance, \n";
  380.                 cout << "     0 for exit: ";
  381.                 int answer; cin >> answer;
  382.                 switch (answer)
  383.                 {
  384.                 case 1:
  385.                     cout << "Enter new value of resistance: ";
  386.                     double r; cin >> r;
  387.                     res->set_r(r);
  388.                     break;
  389.                 case 0:
  390.                     break;
  391.                 default:
  392.                     cout << "Incorrect command\n";
  393.                     break;
  394.                 }
  395.             }
  396.             else if(type == "APP")
  397.             {
  398.                 Appliance* app = (dynamic_cast<Appliance*>(branch[i]));
  399.                 cout << "Internal resistance: now" << app->get_r() << " Om\n";
  400.                 cout << "Required voltage: now " << app->get_req_u() << " V\n";
  401.                 cout << "Required amperage: now " << app->get_req_i() << " A\n";
  402.                 cout << "Variation of voltage: now " << app->get_du() << " V\n";
  403.                 cout << "Variation of amperage: now " << app->get_di() << " A\n";
  404.                 cout << "Type 1 for changing the internal resistance, \n";
  405.                 cout << "     2 for changing the required voltage, \n";
  406.                 cout << "     3 for changing the required amperage, \n";
  407.                 cout << "     4 for changing the variation of voltage, \n";
  408.                 cout << "     5 for changing the variation of amperage, \n";
  409.                 cout << "     0 for exit: ";
  410.                 int answer; cin >> answer;
  411.                 double u, i, r;
  412.                 switch (answer)
  413.                 {
  414.                 case 1:
  415.                     cout << "Enter new value of internal resistance: ";
  416.                     cin >> r;
  417.                     app->set_r(r);
  418.                     break;
  419.                 case 2:
  420.                     cout << "Enter new value of required voltage: ";
  421.                     cin >> u;
  422.                     app->set_req_u(u);
  423.                     break;
  424.                 case 3:
  425.                     cout << "Enter new value of required amperage: ";
  426.                     cin >> i;
  427.                     app->set_req_i(i);
  428.                     break;
  429.                 case 4:
  430.                     cout << "Enter new value of variation of voltage: ";
  431.                     cin >> u;
  432.                     app->set_du(u);
  433.                     break;
  434.                 case 5:
  435.                     cout << "Enter new value of variation of amperage: ";
  436.                     cin >> i;
  437.                     app->set_di(i);
  438.                     break;
  439.                 case 0:
  440.                     break;
  441.                 default:
  442.                     cout << "Incorrect command\n";
  443.                     break;
  444.                 }
  445.  
  446.             }
  447.             else if(type == "CCS")
  448.             {
  449.                 CCS* ccs = (dynamic_cast<CCS*>(branch[i]));
  450.                 cout << "Internal resistance: now" << ccs->get_r() << " Om\n";
  451.                 cout << "Electromotive force of the CCS: now " << ccs->get_e() << " V\n";
  452.                 cout << "Type 1 for changing the internal resistance, \n";
  453.                 cout << "     2 for changing the electromotive force, \n";
  454.                 cout << "     0 for exit: ";
  455.                 int answer; cin >> answer;
  456.                 switch (answer)
  457.                 {
  458.                 case 1:
  459.                     cout << "Enter new value of internal resistance: ";
  460.                     double r; cin >> r;
  461.                     ccs->set_r(r);
  462.                     break;
  463.                 case 2:
  464.                     cout << "Enter new value of electromotive force: ";
  465.                     double e; cin >> e;
  466.                     ccs->set_e(e);
  467.                     break;
  468.                 case 0:
  469.                     break;
  470.                 default:
  471.                     cout << "Incorrect command\n";
  472.                     break;
  473.                 }
  474.             }
  475.             else if(type == "KEY")
  476.             {
  477.                 Key* key = (dynamic_cast<Key*>(branch[i]));
  478.                 cout << "State of the key: now ";
  479.                 if(key->get_state())
  480.                     cout << "locked\n";
  481.                 else cout << "unlocked\n";
  482.                 cout << "Type 1 for changing the key state, 0 for exit: ";
  483.                 int answer; cin >> answer;
  484.  
  485.                 switch (answer)
  486.                 {
  487.                 case 1:
  488.                     cout << "Type 1 (or 0) for locking (or unlocking) the key: ";
  489.                     bool newstate; cin >> newstate;
  490.                     key->set_state(newstate);
  491.                 case 0:
  492.                     break;
  493.                 default:
  494.                     cout << "Incorrect command\n";
  495.                     break;
  496.                 }
  497.             }
  498.             else
  499.             {
  500.                 cout << "Coder, you have a great error! Fix it immideately!" << endl;
  501.             }
  502.         }
  503.     }
  504.  
  505.     int size()
  506.     {
  507.         return branch.size();
  508.     }
  509.  
  510.     bool is_shorted_path()
  511.     {
  512.         if(branch.size() == 0)
  513.             return true;
  514.         if(!this->is_locked())
  515.             return false;
  516.         for(int i = 0; i < branch.size(); i++)
  517.         {
  518.             if(branch[i]->get_type() != "KEY")
  519.                 return false;
  520.  
  521.         }
  522.         return true;
  523.     }
  524.  
  525.     void print(){
  526.         for(int i = 0; i < branch.size(); i++)
  527.             branch[i]->print();
  528.     }
  529. };
  530.  
  531. class Subchain //please test me, i am not tested
  532. {
  533. protected:
  534.     vector<Branch*> subchain;
  535. public:
  536.  
  537.     void set_branch()
  538.     {
  539.         subchain.push_back(new Branch());
  540.     }
  541.  
  542.     void set_el(int chain_number)
  543.     {
  544.         cout << "There are " << subchain.size() << " branches in subchain\n";
  545.         cout << "Please type the number of branch where you want to set the element: ";
  546.         int j; cin >> j; j--;
  547.         if(j >= 0 && j < subchain.size())
  548.             subchain[j]->set_el_f(chain_number);
  549.         else
  550.             cout << "Incorrect number of branch\n";
  551.     }
  552.  
  553.     void change_parameters()
  554.     {
  555.         cout << "There are " << subchain.size() << " branches in subchain\n";
  556.         cout << "Please type the number of branch where you want to\n";
  557.         cout << "change parameters of the element: ";
  558.         int j; cin >> j; j--;
  559.         if(j >= 0 && j < subchain.size())
  560.             subchain[j]->change_parameters();
  561.         else
  562.             cout << "Incorrect number of branch\n";
  563.     }
  564.  
  565.     void remove_el()
  566.     {
  567.         cout << "There are " << subchain.size() << " branches in subchain\n";
  568.         cout << "Please type the number of branch where you want to delete the element: ";
  569.         int j; cin >> j; j--;
  570.         if(j >= 0 && j < subchain.size())
  571.             subchain[j]->remove_el_f();
  572.         else
  573.             cout << "Incorrect number of branch\n";
  574.     }
  575.  
  576.     bool has_shorted_paths()
  577.     {
  578.         for(int i = 0; i < subchain.size(); i++)
  579.         {
  580.             if(subchain[i]->is_shorted_path())
  581.                 return true;
  582.         }
  583.         return false;
  584.     }
  585.  
  586.     double get_r_from_to()
  587.     {
  588.         cout << "Please enter the number of branch where needed elements are: ";
  589.         int j; cin >> j; j--;
  590.         if(j >= 0 && j < subchain.size())
  591.         {
  592.             cout << "Please enter the numbers of first and last elements for calculating\n"
  593.             int from, to; cin >> from >> to;
  594.             subchain[j]->get_r_from_to(from, to);
  595.         }
  596.         else
  597.             cout << "Incorrect number of branch\n";
  598.     }
  599.  
  600.     double get_r_branch()
  601.     {
  602.         cout << "Please enter the number of branch where you want to find resistance value: ";
  603.         int j; cin >> j; j--;
  604.         if(j >= 0 && j < subchain.size())
  605.             subchain[j]->get_r_from_to(1, subchain[j]->size());
  606.         else
  607.             cout << "Incorrect number of branch\n";
  608.     }
  609.  
  610.     double get_r_subchain()
  611.     {
  612.         double mult = 1.0; //multiplication
  613.         double sum = 0.0;
  614.         int cnt = 0;
  615.         if(this->has_shorted_paths())
  616.             return 0;
  617.         for(int i = 0; i < subchain.size(); i++)
  618.         {
  619.             if(subchain[i]->is_locked())
  620.             {
  621.                 double r = subchain[i]->get_r_from_to(1, subchain[i]->size());
  622.                 mult *= r;
  623.                 sum += r;
  624.                 cnt++;
  625.             }
  626.         }
  627.         if(cnt == 0)
  628.             return 0;
  629.         else if(cnt == 1)
  630.             return sum;
  631.         else
  632.             return mult / sum;
  633.     }
  634.  
  635.     bool is_locked()
  636.     {
  637.         bool res = true;
  638.         if(!subchain.empty())
  639.         {
  640.             res = subchain[0]->is_locked();
  641.             for(int i = 1; i < subchain.size(); i++)
  642.             {
  643.                 res = res || subchain[i]->is_locked();
  644.             }
  645.         }
  646.         return res;
  647.     }
  648.  
  649.     void remove_branch()
  650.     {
  651.         cout << "There are " << subchain.size() << " branches in subchain\n";
  652.         cout << "Please enter the number of branch you want to remove: ";
  653.         int j; cin >> j; j--;
  654.         if(j >= 0 && j < subchain.size())
  655.             subchain.erase(subchain.begin() + j);
  656.         else
  657.             cout << "Incorrect number of branch\n";
  658.     }
  659.  
  660.     void print()
  661.     {
  662.         for(int i = 0; i < subchain.size(); i++)
  663.         {
  664.             cout << "Branch " << i + 1 << endl;
  665.             subchain[i]->print();
  666.             cout << endl;
  667.         }
  668.     }
  669. };
  670.  
  671. class Chain
  672. {
  673. protected:
  674.     vector<Subchain*> chain;
  675. public:
  676.  
  677.     void set()
  678.     {
  679.         cout << "Type 0 to exit\n";
  680.         cout << "Type 1 to set a new subchain\n";
  681.         cout << "Type 2 to set a new branch\n";
  682.         cout << "Type 3 to set a new element\n";
  683.         int n; cin >> n;
  684.         if(n == 1) chain.push_back(new Subchain());
  685.         else
  686.             if(n == 2 || n == 3)
  687.             {
  688.                 cout << "Please enter the number of subchain where you want to set new ";
  689.                 if(n == 2) cout << "branch: ";
  690.                 if(n == 3) cout << "element: ";
  691.                 int i; cin >> i; i--;
  692.                 if(i >= 0 && i < chain.size())
  693.                 {
  694.                     if(n == 2) chain[i]->set_branch();
  695.                     if(n == 3) chain[i]->set_el(i);
  696.                 }
  697.                 else
  698.                     cout << "Incorrect number of subchain\n";
  699.             }
  700.     }
  701.  
  702.     /*void set_subchain()
  703.     {
  704.         chain.push_back(new Subchain());
  705.     }
  706.  
  707.     void set_branch()
  708.     {
  709.         cout << "Please enter the number of subchain where you want to set new branch: ";
  710.         int i; cin >> i; i--;
  711.         if(i >= 0 && i < chain.size())
  712.             chain[i]->set_branch();
  713.         else
  714.             cout << "Incorrect number of subchain\n";
  715.     }
  716.  
  717.     void set_el()
  718.     {
  719.         cout << "Please enter the number of subchain where you want to set new element: ";
  720.         int i; cin >> i; i--;
  721.         if(i >= 0 && i < chain.size())
  722.             chain[i]->set_el(i);
  723.         else
  724.             cout << "Incorrect number of subchain\n";
  725.     }*/
  726.  
  727.     //void remove_el()
  728.     //{
  729.     //  cout << "Please enter the number of chain where you want to remove the element: ";
  730.     //  int i; cin >> i; i--;
  731.     //  if(i >= 0 && i < chain.size())
  732.     //      chain[i]->remove_el();
  733.     //  else
  734.     //      cout << "Incorrect number of chain\n";
  735.     //}
  736.  
  737.     //void remove_branch()
  738.     //{
  739.     //  cout << "Please enter the number of chain where you want to remove the branch: ";
  740.     //  int i; cin >> i; i--;
  741.     //  if(i >= 0 && i < chain.size())
  742.     //      chain[i]->remove_branch();
  743.     //  else
  744.     //      cout << "Incorrect number of chain\n";
  745.     //}
  746.  
  747.     //void remove_chain()
  748.     //{
  749.     //  cout << "Please enter the number of chain you want to remove: ";
  750.     //  int i; cin >> i; i--;
  751.     //  if(i >= 0 && i < chain.size())
  752.     //      chain.erase(chain.begin() + i);
  753.     //  else
  754.     //      cout << "Incorrect number of chain\n";
  755.     //}
  756.  
  757.     void remove()
  758.     {
  759.         map<int, pair<string, string>> m;
  760.         m[0] = make_pair("exit", "error");
  761.         m[1] = make_pair("element",
  762.             "Please enter the number of subchain where you want to remove the element: ");
  763.         m[2] = make_pair("branch",
  764.             "Please enter the number of subchain where you want to remove the branch: ");
  765.         m[3] = make_pair("subchain",
  766.             "Please enter the number of subchain you want to remove: ");
  767.         for(int i = 0; i < 4; i++)
  768.             cout << "Type " << i << " to remove the " << m[i].first << endl;
  769.         int n; cin >> n;
  770.         if(n > 0 && n < 4)
  771.         {
  772.             cout << m[n].second;
  773.             int i; cin >> i; i--;
  774.             if(i >= 0 && i < chain.size())
  775.             {
  776.                 if(n == 1) chain[i]->remove_el();
  777.                 else if(n == 2) chain[i]->remove_branch();
  778.                 else if(n == 3) chain.erase(chain.begin() + i);
  779.             }
  780.             else
  781.                 cout << "Incorrect number of subchain\n";
  782.         }
  783.     }
  784.  
  785.     void change_parameters()
  786.     {
  787.         cout << "Please enter the number of subchain where you want to\n";
  788.         cout << "change paramaters of the element: ";
  789.         int i; cin >> i; i--;
  790.         if(i >= 0 && i < chain.size())
  791.             chain[i]->change_parameters();
  792.         else
  793.             cout << "Incorrect number of subchain\n";
  794.     }
  795.  
  796.     bool is_locked()
  797.     {
  798.         bool res = true;
  799.         for(int i = 0; i < chain.size(); i++)
  800.         {
  801.             res = res && chain[i]->is_locked();
  802.         }
  803.         return res;
  804.     }
  805.  
  806.     double get_r_chain()
  807.     {
  808.         double r = 0.0;
  809.         if(this->is_locked())
  810.         {
  811.             for(int i = 0; i < chain.size(); i++)
  812.                 r += chain[i]->get_r_subchain();
  813.         }
  814.         else
  815.             cout << "Chain is unlocked\n";
  816.         return r;
  817.     }
  818.  
  819.     void get_r_subchain()
  820.     {
  821.         cout << "Please enter the number of subchain which resistance you need: ";
  822.         int i; cin >> i; i--;
  823.         if(i >= 0 && i < chain.size())
  824.             chain[i]->get_r_subchain();
  825.         else
  826.             cout << "Incorrect number of subchain\n";
  827.     }
  828.  
  829.     void get_r_branch()
  830.     {
  831.         cout << "Please enter the number of subchain where needed branch is: ";
  832.         int i; cin >> i; i--;
  833.         if(i >= 0 && i < chain.size())
  834.             chain[i]->get_r_branch();
  835.         else
  836.             cout << "Incorrect number of subchain\n";
  837.     }
  838.  
  839.     void get_r_from_to()
  840.     {
  841.         cout << "Please enter the number of subchain where needed elements are: ";
  842.         int i; cin >> i; i--;
  843.         if(i >= 0 && i < chain.size())
  844.             chain[i]->get_r_from_to();
  845.         else
  846.             cout << "Incorrect number of subchain\n";
  847.     }
  848.  
  849.     void print()
  850.     {
  851.         for(int i = 0; i < chain.size(); i++)
  852.         {
  853.             cout << "Subchain " << i + 1 << endl;
  854.             chain[i]->print();
  855.         }
  856.     }
  857. };
  858.  
  859. int main()
  860. {
  861. //#ifdef _DEBUG
  862. //  freopen("in.txt", "r", stdin);
  863. //  freopen("out.txt", "w", stdout);
  864. //#endif
  865.    
  866.     Subchain* my_subchain = new Subchain();
  867.     int n = 0;
  868.     while(n != 26)
  869.     {
  870.         cout << "1 - set new branch\n";
  871.         cout << "2 - set new element\n";
  872.         cout << "3 - change parameters\n";
  873.         cout << "4 - remove the element\n";
  874.         cout << "5 - get r of subchain\n";
  875.         cout << "6 - check if locked\n";
  876.         cout << "7 - print the subchain\n";
  877.         cout << "8 - check for shrted paths\n";
  878.         cout << "9 - remove the branch\n";
  879.         cin >> n;
  880.         switch (n)
  881.         {
  882.         case 1:
  883.             my_subchain->set_new_branch();
  884.             break;
  885.         case 2:
  886.             my_subchain->set_el();
  887.             break;
  888.         case 3:
  889.             my_subchain->change_parameters();
  890.             break;
  891.         case 4:
  892.             my_subchain->remove_el();
  893.             break;
  894.         case 5:
  895.             cout << my_subchain->get_r_subchain() << endl;
  896.             break;
  897.         case 6:
  898.             cout << my_subchain->is_locked() << endl;
  899.             break;
  900.         case 7:
  901.             my_subchain->print();
  902.             break;
  903.         case 8:
  904.             cout << my_subchain->has_shorted_paths() << endl;
  905.             break;
  906.         case 9:
  907.             my_subchain->remove_branch();
  908.             break;
  909.         default:
  910.             break;
  911.         }
  912.     }
  913.  
  914.     cout << "compilation completed\n";
  915.     system("pause");
  916.     return 0;
  917. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement