-
Notifications
You must be signed in to change notification settings - Fork 13
/
expression.cpp
executable file
·52 lines (51 loc) · 1.25 KB
/
expression.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
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t0;cin>>t0;
while(t0--)
{
string s;
cin>>s;
//getline(cin,s);
long long int a=0,b=0;
vector<long long int> arr;
vector<long long int> a1;
int len=s.length();
int f=0;
// cout<<len;
for(int i=0;i<=len;i++)
{
if(i!=len && s[i]>=48 && s[i]<=57 )
a=(a*10)+(s[i]-48);
else
{
if(f==1)a=-1*a;
arr.push_back(a);
if(s[i]=='-')f=1;
else f=0;
//cout<<"("<<a<<")";
a=0;
}
}
int j=0;
// for(int i=0;i<arr.size();i++)cout<<"("<<arr[i]<<")";
int fl=0;
a1.push_back(arr[0]);j++;
for(int i=0;i<len;i++)
{
if(s[i]=='*')
{ a1[((int)a1.size()-1)]*=arr[j];}
else if(s[i]=='+' || s[i]=='-')
{a1.push_back(arr[j]);}
if(s[i]=='+' || s[i]=='-'|| s[i]=='*'){j++;}
}
long long sum=0;
for(int i=0;i<a1.size();i++)
{
sum+=a1[i];
}
cout<<sum<<endl;
}
return 0;
}