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
|
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <iostream>
#include <bits/stdc++.h>
#include <unordered_map>
#include <algorithm>
#include <stack>
using namespace std;
class Solution {
public:
int calculate(string s) {
vector<int> nums;
char op = '+';
long unsigned int cur = 0;
long unsigned int pos = 0;
while (pos < s.size()) {
if (s[pos] == ' ') {
++pos;
continue;
}
while (isdigit(s[pos]) && pos < s.size())
cur = cur * 10 + (s[pos++] - '0');
if (op == '+' || op == '-') {
nums.push_back(cur * (op == '+' ? 1 : -1));
} else if (op == '*') {
nums.back() *= cur;
} else if (op == '/') {
nums.back() /= cur;
}
cur = 0;
op = s[pos++];
}
return accumulate(begin(nums), end(nums), 0);
}
};
void findAndReplaceAll(std::string & data, std::string toSearch, std::string replaceStr)
{
size_t pos = data.find(toSearch);
// Repeat till end is reached
while( pos != std::string::npos)
{
data.replace(pos, toSearch.size(), replaceStr);
pos =data.find(toSearch, pos + replaceStr.size());
}
}
int main() {
string str,res;
int t,a,b,c,d,e;
cin>>str>>t;
while(t--)
{
cin>>a>>b>>c>>d>>e;
findAndReplaceAll(str, "A", "a");
findAndReplaceAll(str, "B", "b");
findAndReplaceAll(str, "C", "c");
findAndReplaceAll(str, "D", "d");
findAndReplaceAll(str, "E", "e");
Solution sol;
cout<<sol.calculate(str);
}
return 0;
}
|