Finding equivalent resistance

image/svg+xml SVG Picture created as project.sch.svg date 2017/08/11 23:04:07 Picture generated by Eeschema-SVG #include <iostream> #include <vector> using namespace std; class Resistor { private: double resistance; public: Resistor() {resistance = 0.0;} Resistor(double res) { resistance = res; } double get_resistance() { return resistance; } void set_resistance(double res) { resistance = res; } }; Resistor connect_in_series(const vector<Resistor>& v) { Resistor r; double total = 0.0; for(auto ri : v) { total += ri.get_resistance(); } r.set_resistance(total); return r; }; Resistor connect_in_parallel(const vector<Resistor>& v) { Resistor r; double total = 0.0; for(auto ri : v) { total += 1 / ri.get_resistance(); } r.set_resistance(1 / total); return r; }; int main() { Resistor R1(300), R2(1.8), R3(120), R4(240), R5(470), R6(270), R7(220), R8(160), R9(180); cout << connect_in_series({R1, R2, R3, connect_in_parallel({R4, R5, R6, connect_in_series({R7, connect_in_parallel({R8, R9})})})}).get_resistance() << " ohms" << endl; // 497.102 ohms return 0; }