Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- string binary(string x)
- {
- string res="";
- int len=x.length();
- for(int i=0;i<len;i++)
- {
- if(x[i]=='1')
- res+="0001";
- else if(x[i]=='2')
- res+="0010";
- else if(x[i]=='3')
- res+="0011";
- else if(x[i]=='4')
- res+="0100";
- else if(x[i]=='5')
- res+="0101";
- else if(x[i]=='6')
- res+="0110";
- else if(x[i]=='7')
- res+="0111";
- else if(x[i]=='8')
- res+="1000";
- else if(x[i]=='9')
- res+="1001";
- else if(x[i]=='A')
- res+="1010";
- else if(x[i]=='B')
- res+="1011";
- else if(x[i]=='C')
- res+="1100";
- else if(x[i]=='D')
- res+="1101";
- else if(x[i]=='E')
- res+="1110";
- else if(x[i]=='F')
- res+="1111";
- }
- // cout<<res<<endl;
- int len1=res.length();
- int n=13-len1;
- string ans="";
- for(int i=0;i<n;i++)
- ans+='0';
- ans+=res;
- // cout<<ans<<endl;
- return ans;
- }
- int decimal(string x)
- {
- int len =x.length();
- int base=1;
- int res=0;
- for (int i=len-1; i>=0; i--)
- {
- if (x[i]>='0' && x[i]<='9')
- {
- res+=(x[i]-48)*base;
- base=base*16;
- }
- else if (x[i]>='A' && x[i]<='F')
- {
- res+=(x[i]-55)*base;
- base=base*16;
- }
- }
- return res;
- }
- int main()
- {
- int ans,c,d,e,f,t;
- char ch;
- string a,b;
- cin>>t;
- while(t--)
- {
- cin>>a>>ch>>b;
- string bina=binary(a);
- string binb=binary(b);
- int decia=decimal(a);
- int decib=decimal(b);
- if(ch=='+')
- {
- ans=decia+decib;
- }
- else
- {
- ans=decia-decib;
- }
- cout<<bina<<" "<<ch<<" "<<binb<<" = "<<ans<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement