Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <map>
- #include <algorithm>
- #include <cmath>
- using namespace std;
- float round_to_tenth(float a){
- if (int(a*100) % 10 > 5) return (float(int(a*10))/10) + 0.1;
- else return float(int(a*10))/10;
- }
- int main()
- {
- map<string, int> resistors;
- vector<string> names;
- float resistance = 0;
- int n, count = 0;
- cin >> n; cin.ignore();
- for (int i = 0; i < n; i++) {
- string name;
- int r;
- cin >> name >> r; cin.ignore();
- names.push_back(name);
- resistors.insert({name, r});
- }
- string circuit;
- getline(cin, circuit);
- count += std::count(circuit.begin(), circuit.end(), '(');
- count += std::count(circuit.begin(), circuit.end(), '[');
- for (int i = 0; i < circuit.size(); i++) {
- for (auto x: names) {
- if (circuit.find(x) == i) {
- string l = to_string(resistors[x]);
- circuit = circuit.substr(0, i) + l + circuit.substr(i + x.length());
- }
- }
- }
- for (int i = 0; count > 0; i++) {
- string substr;
- float a = 0, b = 0, c = 0;
- if (i > circuit.size()) {
- i = -1;
- continue;
- }
- if (circuit[i] == '(') {
- substr = circuit.substr(i + 1);
- int j = substr.find(')');
- if ((substr.find('[') > j || substr.find('[') == string::npos) && (substr.find('(') > j || substr.find('(') == string::npos)) {
- a = 0, b = 0, c = 0;
- substr = circuit.substr(i, j);
- c = stof(substr.substr(substr.find_last_of(' ')));
- substr = substr.substr(0, substr.find_last_of(' '));
- if (substr.find_last_of(' ') != string::npos) {
- b = stof(substr.substr(substr.find_last_of(' ')));
- substr = substr.substr(0, substr.find_last_of(' '));
- }
- if (substr.find_last_of(' ') != string::npos) {
- a = stof(substr.substr(substr.find_last_of(' ')));
- substr = substr.substr(0, substr.find_last_of(' '));
- }
- substr = to_string(a + b + c);
- circuit = circuit.substr(0, i) + substr + circuit.substr(j + i + 2);
- count--;
- i = 0;
- }
- }
- if (circuit [i] == '[') {
- substr = circuit.substr(i + 1);
- int j = substr.find(']');
- if ((substr.find('(') > j || substr.find('(') == string::npos) && (substr.find('[') > j || substr.find('[') == string::npos)) {
- a = 0, b = 0, c = 0;
- substr = circuit.substr(i, j);
- c = stof(substr.substr(substr.find_last_of(' ')));
- substr = substr.substr(0, substr.find_last_of(' '));
- if (substr.find_last_of(' ') != string::npos) {
- b = stof(substr.substr(substr.find_last_of(' ')));
- substr = substr.substr(0, substr.find_last_of(' '));
- }
- if (substr.find_last_of(' ') != string::npos) {
- a = stof(substr.substr(substr.find_last_of(' ')));
- substr = substr.substr(0, substr.find_last_of(' '));
- }
- (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));
- circuit = circuit.substr(0, i) + substr + circuit.substr(j + i + 2);
- count--;
- i = 0;
- }
- }
- }
- resistance = round_to_tenth(stof(circuit));
- (resistance == floor(resistance))? cout << resistance << ".0" << endl: cout << resistance << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement