Hello I'm trying to implement a simple calculator supporting variables.
And my question is the LValue can be int or var, and if it's unused the initial value should be 0. How can I make that happen, thank you so much.
The format is defined below:
Int : 32-bit signed integer
Var : Name of a variable.
A legal name is a string containing at least 1 and at most 20 characters.
RValue : Int
LValue : Int or Var
The calculator should support the input below:
print RValue : print RValue.
add RValue LValue1 LValue2 : RValue = LValue1 + LValue2, and print RValue.
sub RValue LValue1 LValue2 : RValue = LValue1 - LValue2, and print RValue.
mul RValue LValue1 LValue2 : RValue = LValue1 * LValue2, and print RValue.
div RValue LValue1 LValue2 :
RValue = LValue1 // LValue2, and print RValue. (for python user)
RValue = LValue1 / LValue2, and print RValue. (for c++ user)
The initial value of any unused variable is 0
Input Format
The first line contains an integer T.
In the following T lines, each line contains a calculator command.
Sample Input:
6
print a
add a 123 456
sub b a 777
sub a 356 b
print b
mul c a b
Sample Output:
0
579
-198
554
-198
-109692
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
|
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int T;
cin>>T;
int val1,val2;
int a,b,c=0;
vector<pair<string,int>>queries(T);
for(auto& s: queries)
{
cin>>get<0>(s)>>get<1>(s);
if(get<0>(s)=="print")
{cout<<get<1>(s)<<"\n";}
else if(get<0>(s)=="add")
{ cin>>val1>>val2;
get<1>(s)=val1+val2;
cout<<get<1>(s)<<"\n";
}
else if(get<0>(s)=="sub")
{ cin>>val1>>val2;
get<1>(s)=val1-val2;
cout<<get<1>(s)<<"\n";
}
else if(get<0>(s)=="mul")
{ cin>>val1>>val2;
get<1>(s)=val1*val2;
cout<<get<1>(s)<<"\n";
}
else if(get<0>(s)=="div")
{ cin>>val1>>val2;
get<1>(s)=val1/val2;
cout<<get<1>(s)<<"\n";
}
}
return 0;
}
|