-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconductance.cpp
140 lines (130 loc) · 3.53 KB
/
conductance.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
This is the code written to programme the computation of conductance of a set of vacuum pipes. Once an input of the proper format is given,
the total consuctance is outputted considering the design and individual conductances.
*/
#include <bits/stdc++.h>
using namespace std;
#define forl(i, a, n) for (int i = a; i < n; i++)
#define fore(i, a, n) for (int i = a; i <= n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define ll long long
#define lli long long int
#define mem(a, k) memset(a, k, sizeof(a))
#define sp << " " <<
#define CASE(t, k) cout << "Case #" << (t) << ": " << (k) << endl
#define LIMIT 1000000
const int maxn = 200000 + 10;
const int mod = 1000000007;
double parallel(vector<string>);
vector<string> split(const string &s)
{
vector<string> v;
int x = 2;
stack<int> st;
forl(i, 2, s.length() - 1)
{
if (s[i] == '(')
st.push(1);
else if (s[i] == ')')
st.pop();
else if (s[i] == ',' && st.empty())
{
v.pb(s.substr(x, i - x));
x = i + 1;
}
}
v.pb(s.substr(x, s.length() - 1 - x));
return v;
}
double series(vector<string> pipes)
{
if (pipes.empty())
return 0;
double ans = 0;
for (string s : pipes)
{
if (int('9' - s[0]) <= 9 && int('9' - s[0]) >= 0)
ans += 1 / stod(s);
else if (s[0] == 'P' || s[0] == 'p')
ans += 1 / parallel(split(s));
else if (s[0] == 'S' || s[0] == 's')
ans += 1 / series(split(s));
}
return 1 / ans;
}
double parallel(vector<string> pipes)
{
if (pipes.empty())
return 0;
double ans = 0;
for (string s : pipes)
{
if (int('9' - s[0]) <= 9 && int('9' - s[0]) >= 0)
ans += stod(s);
else if (s[0] == 'P' || s[0] == 'p')
ans += parallel(split(s));
else if (s[0] == 'S' || s[0] == 's')
ans += series(split(s));
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
//Instructions for input
cout << "Enter sequence of pipes with their conductances(in L/s) in the following format" << endl;
cout << "(S-Series, P-Parallel): S(1.2,P(0.5,1,2),S(3.1,4))" << endl;
cout << "1. Remember to mention series or parallel before opening brackets." << endl;
cout << "2. Make sure brackets and commas are correctly placed." << endl;
cout << "3. Decimals <1 should have a 0 before decimal point." << endl;
cout << "4. Avoid any kind of spaces.\nInput:" << endl;
string s;
start:
cin >> s;
// Validating input
if (s.find(",.") != string::npos || s.find("(.") != string::npos)
{
cout << "Warning! Enter 0 before decimal point.\nInput:" << endl;
goto start;
}
stack<int> st;
forl(i, 0, s.length())
{
if (s[i] == '(')
{
st.push(1);
if (s[i - 1] != 'S' && s[i - 1] != 'P' && s[i - 1] != 's' && s[i - 1] != 'p')
{
cout << "Please mention series or parallel before bracket.\nInput:" << endl;
goto start;
}
}
else if (s[i] == ')' && !st.empty())
st.pop();
else if (s[i] == ')' && st.empty())
{
cout << "Warning! Recheck formatting.\nInput:" << endl;
goto start;
}
}
if (!st.empty())
{
cout << "Warning! Recheck fromatting.\nInput:" << endl;
goto start;
}
//Retrieving answer from string
double ans = 0;
if (s[0] == 'S' || s[0] == 's')
ans = series(split(s));
else if (s[0] == 'P' || s[0] == 'p')
ans = parallel(split(s));
//Outputs calculated answer
cout << "Total Conductance of Pipes: " << ans << " L/s" << endl;
return 0;
}