Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include "stdafx.h"
- #include <algorithm>
- #include <iostream>
- #include <string>
- #include <cstring>
- #include <fstream>
- #include <cmath>
- #include <vector>
- #include <utility>
- #include <cstdlib>
- #include <deque>
- #include <queue>
- #include <iomanip>
- #include <numeric>
- #include <stack>
- #include <map>
- //#include <unordered_map>
- #include <set>
- //#include <unordered_set>
- #include <cmath>
- #include <list>
- #define mp make_pair
- #define pb push_back
- //#define x first
- //#define y second
- #define pnt pair<ld, ld>
- #define preturn(a) return cout << a << endl, 0
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef long double ld;
- typedef unsigned int uint;
- const int N = (int)250000, mdt = 1e7, limit = 1e8;
- const ld eps = 1e-8, eps1 = 1e-20, PI = 3.14159265358979323846264338, OOO = 1e100L, lim = sqrt(2e9 + 100);
- const ll OO = 1e18 * 8, mod = 1e9 + 7, md = 4294967291;
- map<char, int> a;
- string s;
- void wr()
- {
- int r = 5;
- for (; s[r] == ' ' || s[r] == '(' || s[r] == 39; r++);
- string g;
- for (; s[r] != ' ' && s[r] != ')' && s[r] != 39; g += s[r], r++);
- if (g.empty())
- {
- cout << ' ';
- return;
- }
- int e = (g[0] == '-');
- if((g[e] <= '9' && g[e] >= '0') || g[e] == '-')
- cout << g;
- else cout << a[g[e]] * ((e) ? -1 : 1);
- return;
- }
- string mul(string q, string w)
- {
- int r = 0, t = 0, p;
- string ans;
- bool b = 0;
- int i = 0;
- for (; q[i] == ' '; i++);
- if (!(q[i] <= '9' && q[i] >= '0'))
- r = a[q[i]];
- else for (int i = 0; i < q.size(); i++)
- if (q[i] != ' ') r = r * 10 + q[i] - '0';
- i = 0;
- for (; w[i] == ' '; i++);
- if (!(w[i] <= '9' && w[i] >= '0'))
- t = a[w[i]];
- else for (int i = 0; i < w.size(); i++)
- if(w[i] != ' ')
- t = t * 10 + w[i] - '0';
- p = r * t;
- b = (p < 0);
- p = abs(p);
- for (; p; ans = (char)(p % 10 + '0') + ans, p /= 10);
- if (ans == "")
- ans = "0";
- if (b)
- ans = '-' + ans;
- return ans;
- }
- string norm(string s)
- {
- for (int i = 0; i < s.size(); i++)
- if(s[i] == '*')
- {
- string s1, q, w;
- int l, r;
- for (l = i - 1; s[l] == ' ' || (s[l] <= '9' && s[l] >= '0') || (s[l] >= 'a' && s[l] <= 'z'); q = s[l] + q, l--);
- for (r = i + 1; s[r] == ' ' || (s[r] <= '9' && s[r] >= '0' || (s[r] >= 'a' && s[r] <= 'z')); w += s[r], r++);
- for (int i = 0; i < l + 1; i++)
- s1 += s[i];
- s1 += mul(q, w);
- for (int i = r; i < s.size(); i++)
- s1 += s[i];
- s = s1;
- i = 0;
- }
- return s;
- }
- int solve()
- {
- int r = 1;
- bool b = 1;
- for (; s[r] == ':' || s[r] == '=' || s[r] == ' '; r++);
- int res = 0, u;
- char t = '+';
- for(int i = r; s[i] != ';'; i++)
- if (s[i] != ' ')
- {
- if (b && s[i] == '-')
- {
- if (t == '+')
- t = '-';
- else t = '+';
- }
- else if (s[i] == '-' || s[i] == '+')
- t = s[i], b = 1;
- else if(s[i] <= '9' && s[i] >= '0')
- {
- u = 0;
- for (int j = i; s[j] <= '9' && s[j] >= '0'; j++)
- u = u * 10 + s[j] - '0', i = j;
- if (t == '-')
- res -= u;
- else if (t == '+')
- res += u;
- b = 0;
- }
- else
- {
- if (t == '-')
- res -= a[s[i]];
- else
- res += a[s[i]];
- b = 0;
- }
- }
- return res;
- }
- string pr(string s)
- {
- string s1;
- for (int i = 0; i < s.size(); i++)
- if (s[i] != ' ' || (i > 1 && i < s.size() - 1 && s[i - 1] == 39 && s[i + 1] == 39))
- s1 += s[i];
- return s1;
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie();
- cout.tie();
- //cout << (char)(39);
- for (; getline(cin, s);)
- {
- s = pr(s);
- if (s[0] == 'w' && s[1] == 'r')
- wr();
- else
- s = norm(s), a[s[0]] = solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement