Advertisement
Firebolt_Samil

Equivalent resistance

Aug 26th, 2022 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <map>
  5. #include <algorithm>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. float round_to_tenth(float a){
  11.     if (int(a*100) % 10 > 5) return (float(int(a*10))/10) + 0.1;
  12.     else return float(int(a*10))/10;
  13. }
  14.  
  15. int main()
  16. {
  17.     map<string, int> resistors;
  18.     vector<string> names;
  19.     float resistance = 0;
  20.     int n, count = 0;
  21.     cin >> n; cin.ignore();
  22.     for (int i = 0; i < n; i++) {
  23.         string name;
  24.         int r;
  25.         cin >> name >> r; cin.ignore();
  26.         names.push_back(name);
  27.         resistors.insert({name, r});
  28.     }
  29.     string circuit;
  30.     getline(cin, circuit);
  31.     count += std::count(circuit.begin(), circuit.end(), '(');
  32.     count += std::count(circuit.begin(), circuit.end(), '[');
  33.     for (int i = 0; i < circuit.size(); i++) {
  34.         for (auto x: names) {
  35.             if (circuit.find(x) == i) {
  36.                 string l = to_string(resistors[x]);
  37.                 circuit = circuit.substr(0, i) + l + circuit.substr(i + x.length());
  38.             }
  39.         }
  40.     }
  41.     for (int i = 0; count > 0; i++) {
  42.         string substr;
  43.         float a = 0, b = 0, c = 0;
  44.         if (i > circuit.size()) {
  45.             i = -1;
  46.             continue;
  47.         }
  48.         if (circuit[i] == '(') {
  49.             substr = circuit.substr(i + 1);
  50.             int j = substr.find(')');
  51.             if ((substr.find('[') > j || substr.find('[') == string::npos) && (substr.find('(') > j || substr.find('(') == string::npos)) {
  52.                 a = 0, b = 0, c = 0;
  53.                 substr = circuit.substr(i, j);
  54.                 c = stof(substr.substr(substr.find_last_of(' ')));
  55.                 substr = substr.substr(0, substr.find_last_of(' '));
  56.                 if (substr.find_last_of(' ') != string::npos) {
  57.                     b = stof(substr.substr(substr.find_last_of(' ')));
  58.                     substr = substr.substr(0, substr.find_last_of(' '));
  59.                 }
  60.                 if (substr.find_last_of(' ') != string::npos) {
  61.                     a = stof(substr.substr(substr.find_last_of(' ')));
  62.                     substr = substr.substr(0, substr.find_last_of(' '));
  63.                 }
  64.                 substr = to_string(a + b + c);
  65.                 circuit = circuit.substr(0, i) + substr + circuit.substr(j + i + 2);
  66.                 count--;
  67.                 i = 0;
  68.             }
  69.         }
  70.         if (circuit [i] == '[') {
  71.             substr = circuit.substr(i + 1);
  72.             int j = substr.find(']');
  73.             if ((substr.find('(') > j || substr.find('(') == string::npos) && (substr.find('[') > j || substr.find('[') == string::npos)) {
  74.                 a = 0, b = 0, c = 0;
  75.                 substr = circuit.substr(i, j);
  76.                 c = stof(substr.substr(substr.find_last_of(' ')));
  77.                 substr = substr.substr(0, substr.find_last_of(' '));
  78.                 if (substr.find_last_of(' ') != string::npos) {
  79.                     b = stof(substr.substr(substr.find_last_of(' ')));
  80.                     substr = substr.substr(0, substr.find_last_of(' '));
  81.                 }
  82.                 if (substr.find_last_of(' ') != string::npos) {
  83.                     a = stof(substr.substr(substr.find_last_of(' ')));
  84.                     substr = substr.substr(0, substr.find_last_of(' '));
  85.                 }
  86.                 (b == 0)? substr = to_string(c): (a == 0)? substr = to_string(b * c/ (b + c)) : substr = to_string(a*b*c/(b*c + a*b + a*c));
  87.                 circuit = circuit.substr(0, i) + substr + circuit.substr(j + i + 2);
  88.                 count--;
  89.                 i = 0;
  90.             }
  91.         }
  92.     }
  93.     resistance = round_to_tenth(stof(circuit));
  94.     (resistance == floor(resistance))? cout << resistance << ".0" << endl: cout << resistance << endl;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement