Advertisement
kokokozhina

Untitled

Dec 11th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 26.00 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, bool flag)
  235.     {
  236.         if(flag)
  237.         {
  238.             cout << "If you want to set a new lamp,      please type 1" << endl;
  239.             cout << "If you want to set a new resistor,  please type 2" << endl;
  240.             cout << "If you want to set a new appliance, please type 3" << endl;
  241.             cout << "If you want to set a new CCS,       please type 4" << endl;
  242.             cout << "If you want to set a new key,       please type 5" << endl;
  243.             cout << "Be careful: you can set only CCS in main subchain" << endl;
  244.         }
  245.         int type, place; cin >> type;
  246.         if(flag)
  247.             cout << "Now please type the number of position, where you want to put it: ";
  248.         cin >> place;
  249.  
  250.         Electrical_facility* new_obj = new Electrical_facility("error", -100500);
  251.         double r, ru, ri, du, di, e;
  252.         bool f = true;
  253.         switch (type)
  254.         {
  255.         case 1:
  256.             if(flag)
  257.                 cout << "Please enter the lamp resistance: ";
  258.             cin >> r;
  259.             if(chain_number)
  260.                 new_obj = new Lamp(r);
  261.             else
  262.                 f = false;
  263.             break;
  264.         case 2:
  265.             if(flag)
  266.                 cout << "Please enter the resistor resistance: ";
  267.             cin >> r;
  268.             if(chain_number)
  269.                 new_obj = new Resistor(r);
  270.             else
  271.                 f = false;
  272.             break;
  273.         case 3:
  274.             if(flag)
  275.             {
  276.                 cout << "Please enter the appliance resistance, then required voltage and amperage, ";
  277.                 cout << "then variation of voltage and amperage: ";
  278.             }
  279.             cin >> r >> ru >> ri >> du >> di;
  280.             if(chain_number)
  281.                 new_obj = new Appliance(r, ru, ri, du, di);
  282.             else
  283.                 f = false;
  284.             break;
  285.         case 4:
  286.             if(flag)
  287.                 cout << "Please enter the CCS resistance and electromotive force: ";
  288.             cin >> r >> e;
  289.             if(chain_number != 0)
  290.             {
  291.                 if(flag)
  292.                     cout << "You can't place CCS here\n";
  293.                 f = false;
  294.             }
  295.             else
  296.                 new_obj = new CCS(r, e);
  297.             break;
  298.         case 5:
  299.             if(flag)
  300.                 cout << "Please enter the key state: ";
  301.             bool st;
  302.             cin >> st;
  303.             if(chain_number)
  304.                 new_obj = new Key(st);
  305.             else
  306.                 f = false;
  307.             break;
  308.         default:
  309.             f = false;
  310.             if(flag)
  311.                 cout << "Something went wrong, please try again\n";
  312.             break;
  313.         }
  314.  
  315.         place--;
  316.         if(f)
  317.             if(place < 0 || place > branch.size())
  318.             {
  319.                 if(flag)
  320.                     cout << "Something went wrong, please try again\n";
  321.             }
  322.             else
  323.             {
  324.                 branch.insert(branch.begin() + place, new_obj);
  325.             }
  326.     }
  327.  
  328.     //bool is_empty(){
  329.     //  return branch.empty();
  330.     //}
  331.  
  332.     void remove_el_f()
  333.     {
  334.         cout << "Please enter the number of the facility you want to remove: ";
  335.         int n; cin >> n; n--;
  336.         if(n >= branch.size() || n < 0)
  337.             cout << "Something went wrong, please try again\n";
  338.         else
  339.             branch.erase(branch.begin() + n);
  340.     }
  341.  
  342.     bool is_locked()
  343.     {
  344.         bool res = true;
  345.         for(int i = 0; i < branch.size(); i++)
  346.         {
  347.             if(branch[i]->get_type() == "KEY")
  348.             {
  349.                 Key key = *(dynamic_cast<Key*>(branch[i]));
  350.                 if(!key.get_state())
  351.                 {
  352.                     res = false;
  353.                     break;
  354.                 }
  355.             }
  356.         }
  357.         return res;
  358.     }
  359.  
  360.     double get_r_from_to(int from, int to)
  361.     {
  362.         double res = 0;
  363.         from--, to--;
  364.         if(to >= 0 && to < branch.size() && from >=0 && from < branch.size()
  365.             && to >= from && this->is_locked())
  366.             for(int i = from; i <= to; i++)
  367.                 res += branch[i]->get_r();
  368.         return res;
  369.     }
  370.  
  371.     void change_parameters(bool flag)
  372.     {
  373.         if(flag)
  374.         {
  375.             cout << "There are " << branch.size() << " elements in this branch\n";
  376.             cout << "Enter the number of element which parameters you want to change: ";
  377.         }
  378.         int i; cin >> i; i--;
  379.         if(i >= branch.size() || i < 0)
  380.         {
  381.             if(flag)
  382.                 cout << "Incorrect number of element, please try again\n";
  383.         }
  384.         else
  385.         {
  386.             string type = branch[i]->get_type();
  387.             if(flag)
  388.                 cout << "Availiable changes: \n";
  389.             if(type == "LMP")
  390.             {
  391.                 Lamp* lmp = (dynamic_cast<Lamp*>(branch[i]));
  392.                 if(flag)
  393.                 {
  394.                     cout << "Internal resistance: now" << lmp->get_r() << " Om\n";
  395.                     cout << "Type 1 for changing the internal resistance, \n";
  396.                     cout << "     0 for exit: ";
  397.                 }
  398.                 int answer; cin >> answer;
  399.                 switch (answer)
  400.                 {
  401.                 case 1:
  402.                     if(flag)
  403.                         cout << "Enter new value of internal resistance: ";
  404.                     double r; cin >> r;
  405.                     lmp->set_r(r);
  406.                     break;
  407.                 case 0:
  408.                     break;
  409.                 default:
  410.                     if(flag)
  411.                         cout << "Incorrect command\n";
  412.                     break;
  413.                 }
  414.             }
  415.             else if(type == "RES")
  416.             {
  417.                 Resistor* res = (dynamic_cast<Resistor*>(branch[i]));
  418.                 if(flag)
  419.                 {
  420.                     cout << "Resistance: now" << res->get_r() << " Om\n";
  421.                     cout << "Type 1 for changing the resistance, \n";
  422.                     cout << "     0 for exit: ";
  423.                 }
  424.                 int answer; cin >> answer;
  425.                 switch (answer)
  426.                 {
  427.                 case 1:
  428.                     if(flag)
  429.                         cout << "Enter new value of resistance: ";
  430.                     double r; cin >> r;
  431.                     res->set_r(r);
  432.                     break;
  433.                 case 0:
  434.                     break;
  435.                 default:
  436.                     if(flag)
  437.                         cout << "Incorrect command\n";
  438.                     break;
  439.                 }
  440.             }
  441.             else if(type == "APP")
  442.             {
  443.                 Appliance* app = (dynamic_cast<Appliance*>(branch[i]));
  444.                 if(flag)
  445.                 {
  446.                     cout << "Internal resistance: now" << app->get_r() << " Om\n";
  447.                     cout << "Required voltage: now " << app->get_req_u() << " V\n";
  448.                     cout << "Required amperage: now " << app->get_req_i() << " A\n";
  449.                     cout << "Variation of voltage: now " << app->get_du() << " V\n";
  450.                     cout << "Variation of amperage: now " << app->get_di() << " A\n";
  451.                     cout << "Type 1 for changing the internal resistance, \n";
  452.                     cout << "     2 for changing the required voltage, \n";
  453.                     cout << "     3 for changing the required amperage, \n";
  454.                     cout << "     4 for changing the variation of voltage, \n";
  455.                     cout << "     5 for changing the variation of amperage, \n";
  456.                     cout << "     0 for exit: ";
  457.                 }
  458.                 int answer; cin >> answer;
  459.                 double u, i, r;
  460.                 switch (answer)
  461.                 {
  462.                 case 1:
  463.                     if(flag)
  464.                         cout << "Enter new value of internal resistance: ";
  465.                     cin >> r;
  466.                     app->set_r(r);
  467.                     break;
  468.                 case 2:
  469.                     if(flag)
  470.                         cout << "Enter new value of required voltage: ";
  471.                     cin >> u;
  472.                     app->set_req_u(u);
  473.                     break;
  474.                 case 3:
  475.                     if(flag)
  476.                         cout << "Enter new value of required amperage: ";
  477.                     cin >> i;
  478.                     app->set_req_i(i);
  479.                     break;
  480.                 case 4:
  481.                     if(flag)
  482.                         cout << "Enter new value of variation of voltage: ";
  483.                     cin >> u;
  484.                     app->set_du(u);
  485.                     break;
  486.                 case 5:
  487.                     if(flag)
  488.                         cout << "Enter new value of variation of amperage: ";
  489.                     cin >> i;
  490.                     app->set_di(i);
  491.                     break;
  492.                 case 0:
  493.                     break;
  494.                 default:
  495.                     if(flag)
  496.                         cout << "Incorrect command\n";
  497.                     break;
  498.                 }
  499.  
  500.             }
  501.             else if(type == "CCS")
  502.             {
  503.                 CCS* ccs = (dynamic_cast<CCS*>(branch[i]));
  504.                 if(flag)
  505.                 {
  506.                     cout << "Internal resistance: now" << ccs->get_r() << " Om\n";
  507.                     cout << "Electromotive force of the CCS: now " << ccs->get_e() << " V\n";
  508.                     cout << "Type 1 for changing the internal resistance, \n";
  509.                     cout << "     2 for changing the electromotive force, \n";
  510.                     cout << "     0 for exit: ";
  511.                 }
  512.                 int answer; cin >> answer;
  513.                 switch (answer)
  514.                 {
  515.                 case 1:
  516.                     if(flag)
  517.                         cout << "Enter new value of internal resistance: ";
  518.                     double r; cin >> r;
  519.                     ccs->set_r(r);
  520.                     break;
  521.                 case 2:
  522.                     if(flag)
  523.                         cout << "Enter new value of electromotive force: ";
  524.                     double e; cin >> e;
  525.                     ccs->set_e(e);
  526.                     break;
  527.                 case 0:
  528.                     break;
  529.                 default:
  530.                     if(flag)
  531.                         cout << "Incorrect command\n";
  532.                     break;
  533.                 }
  534.             }
  535.             else if(type == "KEY")
  536.             {
  537.                 Key* key = (dynamic_cast<Key*>(branch[i]));
  538.                 if(flag)
  539.                     cout << "State of the key: now ";
  540.                 if(key->get_state())
  541.                 {
  542.                     if(flag)
  543.                         cout << "locked\n";
  544.                 }
  545.                 else
  546.                 {
  547.                     if(flag)
  548.                         cout << "unlocked\n";
  549.                 }
  550.                 if(flag)
  551.                     cout << "Type 1 for changing the key state, 0 for exit: ";
  552.                 int answer; cin >> answer;
  553.  
  554.                 switch (answer)
  555.                 {
  556.                 case 1:
  557.                     if(flag)
  558.                         cout << "Type 1 (or 0) for locking (or unlocking) the key: ";
  559.                     bool newstate; cin >> newstate;
  560.                     key->set_state(newstate);
  561.                 case 0:
  562.                     break;
  563.                 default:
  564.                     if(flag)
  565.                         cout << "Incorrect command\n";
  566.                     break;
  567.                 }
  568.             }
  569.             else
  570.             {
  571.                 if(flag)
  572.                     cout << "Coder, you have a great error! Fix it immideately!" << endl;
  573.             }
  574.         }
  575.     }
  576.  
  577.     int size()
  578.     {
  579.         return branch.size();
  580.     }
  581.  
  582.     bool is_shorted_path()
  583.     {
  584.         if(branch.size() == 0)
  585.             return true;
  586.         if(!this->is_locked())
  587.             return false;
  588.         for(int i = 0; i < branch.size(); i++)
  589.         {
  590.             if(branch[i]->get_type() != "KEY")
  591.                 return false;
  592.  
  593.         }
  594.         return true;
  595.     }
  596.  
  597.     void print(){
  598.         for(int i = 0; i < branch.size(); i++)
  599.             branch[i]->print();
  600.     }
  601.  
  602.     double get_emf()
  603.     {
  604.         double res = 0;
  605.         for(int i = 0; i < branch.size(); i++)
  606.         {
  607.             if(branch[i]->get_type() == "CCS")
  608.             {
  609.                 CCS* ccs = (dynamic_cast<CCS*>(branch[i]));
  610.                 res += ccs->get_e();
  611.             }
  612.         }
  613.         return res;
  614.     }
  615. };
  616.  
  617. class Subchain //please test me, i am not tested
  618. {
  619. protected:
  620.     vector<Branch*> subchain;
  621. public:
  622.  
  623.     void set_branch(int subchain_number, bool flag)
  624.     {
  625.         if(subchain_number == 0 && subchain.size() > 0)
  626.         {
  627.             if(flag)
  628.                 cout << "Sorry, you can't have more than 1 branch in main subchain\n";
  629.         }
  630.         else
  631.             subchain.push_back(new Branch());
  632.     }
  633.  
  634.     void set_el(int subchain_number, bool flag)
  635.     {
  636.         if(flag)
  637.         {
  638.             cout << "There are " << subchain.size() << " branches in subchain\n";
  639.             cout << "Please type the number of branch where you want to set the element: ";
  640.         }
  641.         int j; cin >> j; j--;
  642.         if(j >= 0 && j < subchain.size())
  643.             subchain[j]->set_el_f(subchain_number, flag);
  644.         else
  645.         {
  646.             if(flag)
  647.                 cout << "Incorrect number of branch\n";
  648.         }
  649.     }
  650.  
  651.     void change_parameters(bool flag)
  652.     {
  653.         if(flag)
  654.         {
  655.             cout << "There are " << subchain.size() << " branches in subchain\n";
  656.             cout << "Please type the number of branch where you want to\n";
  657.             cout << "change parameters of the element: ";
  658.         }
  659.         int j; cin >> j; j--;
  660.         if(j >= 0 && j < subchain.size())
  661.             subchain[j]->change_parameters(flag);
  662.         else
  663.             if(flag)
  664.                 cout << "Incorrect number of branch\n";
  665.     }
  666.  
  667.     void remove_el()
  668.     {
  669.         cout << "There are " << subchain.size() << " branches in subchain\n";
  670.         cout << "Please type the number of branch where you want to delete the element: ";
  671.         int j; cin >> j; j--;
  672.         if(j >= 0 && j < subchain.size())
  673.             subchain[j]->remove_el_f();
  674.         else
  675.             cout << "Incorrect number of branch\n";
  676.     }
  677.  
  678.     bool has_shorted_paths()
  679.     {
  680.         for(int i = 0; i < subchain.size(); i++)
  681.         {
  682.             if(subchain[i]->is_shorted_path())
  683.                 return true;
  684.         }
  685.         return false;
  686.     }
  687.  
  688.     double get_r_from_to(int j, int from, int to)
  689.     {
  690.         if(j == -1)
  691.         {
  692.             cout << "Please enter the number of branch where needed elements are: ";
  693.             cin >> j; j--;
  694.         }
  695.         double res = 0;
  696.         if(j >= 0 && j < subchain.size())
  697.         {
  698.             if(from == -1 || to == -1)
  699.             {
  700.                 cout << "Please enter the numbers of first and last elements for calculating\n";
  701.                 cin >> from >> to;
  702.             }
  703.             res = subchain[j]->get_r_from_to(from, to);
  704.         }
  705.         else
  706.             cout << "Incorrect number of branch\n";
  707.         return res;
  708.     }
  709.  
  710.     double get_r_branch(int j)
  711.     {
  712.         if(j == -1)
  713.         {
  714.             cout << "Please enter the number of branch where you want to find resistance value: ";
  715.             cin >> j; j--;
  716.         }
  717.         double res = 0;
  718.         if(j >= 0 && j < subchain.size())
  719.             res = subchain[j]->get_r_from_to(1, subchain[j]->size());
  720.         else
  721.             cout << "Incorrect number of branch\n";
  722.         return res;
  723.     }
  724.  
  725.     double get_r_subchain()
  726.     {
  727.         double sum = 0.0;
  728.         double sum_of_branch = 0.0;
  729.         int cnt = 0;
  730.         if(this->has_shorted_paths())
  731.             return 0;
  732.         for(int i = 0; i < subchain.size(); i++)
  733.         {
  734.             if(subchain[i]->is_locked())
  735.             {
  736.                 double r = subchain[i]->get_r_from_to(1, subchain[i]->size());
  737.                 sum_of_branch = r;
  738.                 sum += 1 / r;
  739.                 cnt++;
  740.             }
  741.         }
  742.         if(cnt == 0)
  743.             return 0;
  744.         else if(cnt == 1)
  745.             return sum_of_branch;
  746.         else
  747.             return 1 / sum;
  748.     }
  749.  
  750.     bool is_locked()
  751.     {
  752.         bool res = true;
  753.         if(!subchain.empty())
  754.         {
  755.             res = subchain[0]->is_locked();
  756.             for(int i = 1; i < subchain.size(); i++)
  757.             {
  758.                 res = res || subchain[i]->is_locked();
  759.             }
  760.         }
  761.         return res;
  762.     }
  763.  
  764.     void remove_branch()
  765.     {
  766.         cout << "There are " << subchain.size() << " branches in subchain\n";
  767.         cout << "Please enter the number of branch you want to remove: ";
  768.         int j; cin >> j; j--;
  769.         if(j >= 0 && j < subchain.size())
  770.             subchain.erase(subchain.begin() + j);
  771.         else
  772.             cout << "Incorrect number of branch\n";
  773.     }
  774.  
  775.     int size()
  776.     {
  777.         return subchain.size();
  778.     }
  779.  
  780.     void print()
  781.     {
  782.         for(int i = 0; i < subchain.size(); i++)
  783.         {
  784.             cout << "Branch " << i + 1 << endl;
  785.             subchain[i]->print();
  786.             cout << endl;
  787.         }
  788.     }
  789.  
  790.     double get_emf()
  791.     {
  792.         return subchain[0]->get_emf();
  793.     }
  794. };
  795.  
  796. class Chain
  797. {
  798. protected:
  799.     vector<Subchain*> chain;
  800.  
  801. public:
  802.  
  803.     void set(bool flag)
  804.     {
  805.         if(flag)
  806.         {
  807.             cout << "Type 0 to exit\n";
  808.             cout << "Type 1 to set a new subchain\n";
  809.             cout << "Type 2 to set a new branch\n";
  810.             cout << "Type 3 to set a new element\n";
  811.         }
  812.         int n; cin >> n;
  813.         if(n == 1) chain.push_back(new Subchain());
  814.         else
  815.             if(n == 2 || n == 3)
  816.             {
  817.                 if(flag)
  818.                 {
  819.                     cout << "Please enter the number of subchain where you want to set new ";
  820.                     if(n == 2) cout << "branch: ";
  821.                     if(n == 3) cout << "element: ";
  822.                 }
  823.                 int i; cin >> i; i--;
  824.                 if(i >= 0 && i < chain.size())
  825.                 {
  826.                     if(n == 2) chain[i]->set_branch(i, flag); //
  827.                     if(n == 3) chain[i]->set_el(i, flag);
  828.                 }
  829.                 else
  830.                 {
  831.                     if(flag)
  832.                         cout << "Incorrect number of subchain\n";
  833.                 }
  834.             }
  835.     }
  836.  
  837.     /*void set_subchain()
  838.     {
  839.         chain.push_back(new Subchain());
  840.     }
  841.  
  842.     void set_branch()
  843.     {
  844.         cout << "Please enter the number of subchain where you want to set new branch: ";
  845.         int i; cin >> i; i--;
  846.         if(i >= 0 && i < chain.size())
  847.             chain[i]->set_branch();
  848.         else
  849.             cout << "Incorrect number of subchain\n";
  850.     }
  851.  
  852.     void set_el()
  853.     {
  854.         cout << "Please enter the number of subchain where you want to set new element: ";
  855.         int i; cin >> i; i--;
  856.         if(i >= 0 && i < chain.size())
  857.             chain[i]->set_el(i);
  858.         else
  859.             cout << "Incorrect number of subchain\n";
  860.     }*/
  861.  
  862.     //void remove_el()
  863.     //{
  864.     //  cout << "Please enter the number of chain where you want to remove the element: ";
  865.     //  int i; cin >> i; i--;
  866.     //  if(i >= 0 && i < chain.size())
  867.     //      chain[i]->remove_el();
  868.     //  else
  869.     //      cout << "Incorrect number of chain\n";
  870.     //}
  871.  
  872.     //void remove_branch()
  873.     //{
  874.     //  cout << "Please enter the number of chain where you want to remove the branch: ";
  875.     //  int i; cin >> i; i--;
  876.     //  if(i >= 0 && i < chain.size())
  877.     //      chain[i]->remove_branch();
  878.     //  else
  879.     //      cout << "Incorrect number of chain\n";
  880.     //}
  881.  
  882.     //void remove_chain()
  883.     //{
  884.     //  cout << "Please enter the number of chain you want to remove: ";
  885.     //  int i; cin >> i; i--;
  886.     //  if(i >= 0 && i < chain.size())
  887.     //      chain.erase(chain.begin() + i);
  888.     //  else
  889.     //      cout << "Incorrect number of chain\n";
  890.     //}
  891.  
  892.     void remove()
  893.     {
  894.         map<int, pair<string, string>> m;
  895.         m[0] = make_pair("exit", "error");
  896.         m[1] = make_pair("element",
  897.             "Please enter the number of subchain where you want to remove the element: ");
  898.         m[2] = make_pair("branch",
  899.             "Please enter the number of subchain where you want to remove the branch: ");
  900.         m[3] = make_pair("subchain",
  901.             "Please enter the number of subchain you want to remove: ");
  902.         for(int i = 0; i < 4; i++)
  903.             cout << "Type " << i << " to remove the " << m[i].first << endl;
  904.         int n; cin >> n;
  905.         if(n > 0 && n < 4)
  906.         {
  907.             cout << m[n].second;
  908.             int i; cin >> i; i--;
  909.             if(i >= 0 && i < chain.size())
  910.             {
  911.                 if(n == 1) chain[i]->remove_el();
  912.                 else if(n == 2) chain[i]->remove_branch();
  913.                 else if(n == 3) chain.erase(chain.begin() + i);
  914.             }
  915.             else
  916.                 cout << "Incorrect number of subchain\n";
  917.         }
  918.     }
  919.  
  920.     void change_parameters(bool flag)
  921.     {
  922.         if(flag)
  923.         {
  924.             cout << "Please enter the number of subchain where you want to\n";
  925.             cout << "change paramaters of the element: ";
  926.         }
  927.         int i; cin >> i; i--;
  928.         if(i >= 0 && i < chain.size())
  929.             chain[i]->change_parameters(flag);
  930.         else
  931.         {
  932.             if(flag)
  933.                 cout << "Incorrect number of subchain\n";
  934.         }
  935.     }
  936.  
  937.     bool is_locked()
  938.     {
  939.         bool res = true;
  940.         for(int i = 0; i < chain.size(); i++)
  941.         {
  942.             res = res && chain[i]->is_locked();
  943.         }
  944.         return res;
  945.     }
  946.  
  947.     double get_r_chain()
  948.     {
  949.         double r = 0.0;
  950.         for(int i = 0; i < chain.size(); i++)
  951.             r += chain[i]->get_r_subchain();
  952.  
  953.         return r;
  954.     }
  955.  
  956.     double get_r_subchain(int i)
  957.     {
  958.         if(i == -1)
  959.         {
  960.             cout << "Please enter the number of subchain which resistance you need: ";
  961.             cin >> i; i--;
  962.         }
  963.         double r = 0;
  964.         if(i >= 0 && i < chain.size())
  965.             r = chain[i]->get_r_subchain();
  966.         else
  967.             cout << "Incorrect number of subchain\n";
  968.         return r;
  969.     }
  970.  
  971.     double get_r_branch(int i, int j)
  972.     {
  973.         if(i == -1)
  974.         {
  975.             cout << "Please enter the number of subchain where needed branch is: ";
  976.             cin >> i; i--;
  977.         }
  978.         double r = 0;
  979.         if(i >= 0 && i < chain.size())
  980.             r = chain[i]->get_r_branch(j);
  981.         else
  982.             cout << "Incorrect number of subchain\n";
  983.         return r;
  984.     }
  985.  
  986.     double get_r_from_to(int i, int j, int from, int to)
  987.     {
  988.         if(i == -1)
  989.         {
  990.             cout << "Please enter the number of subchain where needed elements are: ";
  991.             cin >> i; i--;
  992.         }
  993.         double r = 0;
  994.         if(i >= 0 && i < chain.size())
  995.             r = chain[i]->get_r_from_to(j, from, to);
  996.         else
  997.             cout << "Incorrect number of subchain\n";
  998.         return r;
  999.     }
  1000.  
  1001.     void print()
  1002.     {
  1003.         for(int i = 0; i < chain.size(); i++)
  1004.         {
  1005.             cout << "Subchain " << i + 1 << endl;
  1006.             chain[i]->print();
  1007.         }
  1008.     }
  1009.  
  1010.     double get_i(int subch, int br, bool flag)
  1011.     {
  1012.         if(subch == -1 && br == -1)
  1013.         {
  1014.             cout << "Type the coordinates: number of subchain and number of branch: ";
  1015.             cin >> subch >> br;
  1016.             subch--; br--;
  1017.         }
  1018.         double res = 0;
  1019.         string s = "";
  1020.         if(subch >= 0 && subch < chain.size())
  1021.         {
  1022.             if(br >= 0 && br < chain[subch]->size())
  1023.             {
  1024.                 if(this->is_locked())
  1025.                 {
  1026.                     //double r_all = this->get_r_chain();
  1027.                     //double u_all = chain[0]->get_emf();
  1028.                     double i_all = this->get_i_all(flag);
  1029.                     //if(r_all == 0)
  1030.                     //{
  1031.                     //  s = "Chain is shorted\n";
  1032.                     //}
  1033.                     //else
  1034.                     //{
  1035.                         //i_all = u_all / r_all;
  1036.                     double r_subch = this->get_r_subchain(subch);
  1037.                     double r_branch = this->get_r_branch(subch, br);
  1038.                     //cout << u_all << " " << i_all << " " << r_all << endl;
  1039.                     //cout << r_subch << " " << r_branch << endl;
  1040.                     if(r_subch == 0)
  1041.                         s = "Subchain is shorted\n";
  1042.                     else if(r_branch == 0)
  1043.                         s = "Branch is shorted\n";
  1044.                     else
  1045.                         res = i_all * r_subch / r_branch;
  1046.                     //}
  1047.                 }
  1048.                 else
  1049.                     s = "Chain is unlocked\n";
  1050.             }
  1051.             else
  1052.                 s = "Incorrect number of branch\n";
  1053.         }
  1054.         else
  1055.             s = "Incorrect number of subchain\n";
  1056.         if(flag)
  1057.             cout << s;
  1058.  
  1059.         return res;
  1060.     }
  1061.  
  1062.     double get_i_all(bool flag)
  1063.     {
  1064.         double i_all = 0;
  1065.         string s;
  1066.         if(this->is_locked())
  1067.         {
  1068.             if(chain.size())
  1069.             {
  1070.                 double r_all = this->get_r_chain();
  1071.                 double u_all = chain[0]->get_emf();
  1072.  
  1073.                 if(r_all == 0)
  1074.                 {
  1075.                     s = "Chain is shorted\n";
  1076.                 }
  1077.                 else
  1078.                 {
  1079.                     i_all = u_all / r_all;
  1080.                 }
  1081.             }
  1082.             else
  1083.                 s = "Chain has no CCS in it\n";
  1084.         }
  1085.         else
  1086.             s = "Chain is unlocked\n";
  1087.         if(flag)
  1088.             cout << s;
  1089.         return i_all;
  1090.     }
  1091.  
  1092.     void check_state()
  1093.     {
  1094.         for(int i = 0; i < chain.size(); i++)
  1095.         {
  1096.  
  1097.             chain[i]->check_state();
  1098.         }
  1099.     }
  1100.  
  1101.     double get_u_chain(bool flag)
  1102.     {
  1103.         double res = 0;
  1104.         if(this->is_locked())
  1105.         {
  1106.             if(chain.size())
  1107.                 res = chain[0]->get_emf();
  1108.             else
  1109.             {
  1110.                 if(flag)
  1111.                     cout << "Chain has no CCS in it\n";
  1112.             }
  1113.         }
  1114.         else
  1115.         {
  1116.             if(flag)
  1117.                 cout << "Chain is unlocked\n";
  1118.         }
  1119.         return res;
  1120.     }
  1121.  
  1122.     double get_u_subchain(bool flag)
  1123.     {
  1124.         double res = 0;
  1125.         if(flag)
  1126.             cout << "Enter the number of subchain which voltage you want to calculate: ";
  1127.         int subch; cin >> subch; subch--;
  1128.         if(subch >=0 && subch < chain.size())
  1129.         {
  1130.             if(this->is_locked())
  1131.             {
  1132.                 double r = this->get_r_subchain(subch);
  1133.                 if(r == 0)
  1134.                 {
  1135.                     if(flag)
  1136.                         cout << "Subchain is shorted\n";
  1137.                 }
  1138.                 else
  1139.                 {
  1140.                     double i_all = this->get_i_all(flag);
  1141.                     res = r * i_all;
  1142.                 }
  1143.             }
  1144.             else
  1145.             {
  1146.                 if(flag)
  1147.                     cout << "Chain is unlocked\n";
  1148.             }
  1149.         }
  1150.         else
  1151.         {
  1152.             if(flag)
  1153.                 cout << "Incorrect subchain number\n";
  1154.         }
  1155.         return res;
  1156.     }
  1157.  
  1158.     double get_u_branch(bool flag)
  1159.     {
  1160.         if(flag)
  1161.             cout << "Type the coordinates: number of subchain and number of branch: ";
  1162.         int subch, br;
  1163.         cin >> subch >> br;
  1164.         subch--; br--;
  1165.         double i = this->get_i(subch, br, flag);
  1166.         double r = this->get_r_branch(subch, br);
  1167.         return i * r;
  1168.     }
  1169.  
  1170.     double get_u_from_to(bool flag)
  1171.     {
  1172.         if(flag)
  1173.         {
  1174.             cout << "Type the coordinates: number of subchain and number of branch, ";
  1175.             cout << "number of start and finish objects for calculating: ";
  1176.         }
  1177.         int subch, br, from, to;
  1178.         cin >> subch >> br >> from >> to;
  1179.         subch--; br--;
  1180.         double i = this->get_i(subch, br, flag);
  1181.         double r = this->get_r_from_to(subch, br, from, to);
  1182.         return i * r;
  1183.     }
  1184. };
  1185.  
  1186. int main()
  1187. {
  1188. //#ifdef _DEBUG
  1189. //  freopen("in.txt", "r", stdin);
  1190. //  freopen("out.txt", "w", stdout);
  1191. //#endif
  1192.    
  1193.     Chain* chain = new Chain();
  1194.  
  1195.     int n = 1;
  1196.     while(n != 26)
  1197.     {
  1198.         cout << "1 - set\n";
  1199.         cout << "2 - is_locked\n";
  1200.         cout << "3 - remove\n";
  1201.         cout << "4 - change_parameters\n";
  1202.         cout << "5 - print\n";
  1203.         cout << "6 - get_r_chain\n";
  1204.         cout << "7 - get_r_subchain\n";
  1205.         cout << "8 - get_r_branch\n";
  1206.         cout << "9 - get_r_from_to1\n";
  1207.         cout << "10 - get_i_all\n";
  1208.         cout << "11 - get_u_chain\n";
  1209.         cout << "12 - get_u_subchain\n";
  1210.         cout << "13 - get_u_branch\n";
  1211.         cout << "14 - get_u_from_to1\n";
  1212.         cout << "15 - get_i\n";
  1213.         cin >> n;
  1214.  
  1215.         switch (n)
  1216.         {
  1217.  
  1218.         case 1:
  1219.             chain->set(true);
  1220.             break;
  1221.         case 2:
  1222.             cout << chain->is_locked() << endl;
  1223.             break;
  1224.         case 3:
  1225.             chain->remove();
  1226.             break;
  1227.         case 4:
  1228.             chain->change_parameters(true);
  1229.             break;
  1230.         case 5:
  1231.             chain->print();
  1232.             break;
  1233.         case 6:
  1234.             cout << chain->get_r_chain() << endl;
  1235.             break;
  1236.         case 7:
  1237.             cout << chain->get_r_subchain(-1) << endl;
  1238.             break;
  1239.         case 8:
  1240.             cout << chain->get_r_branch(-1, -1) << endl;
  1241.             break;
  1242.         case 9:
  1243.             cout << chain->get_r_from_to(-1, -1, -1, -1) << endl;
  1244.             break;
  1245.         case 10:
  1246.             cout << chain->get_i_all(true) << endl;
  1247.             break;
  1248.         case 11:
  1249.             cout << chain->get_u_chain(true) << endl;
  1250.             break;
  1251.         case 12:
  1252.             cout << chain->get_u_subchain(true) << endl;
  1253.             break;
  1254.         case 13:
  1255.             cout << chain->get_u_branch(true) << endl;
  1256.             break;
  1257.         case 14:
  1258.             cout << chain->get_u_from_to(true) << endl;
  1259.             break;
  1260.         case 15:
  1261.             cout << chain->get_i(-1, -1, true) << endl;
  1262.             break;
  1263.         default:
  1264.             break;
  1265.         }
  1266.     }
  1267.     cout << "compilation completed\n";
  1268.     system("pause");
  1269.     return 0;
  1270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement