Advertisement
Farjana_akter

Untitled

Dec 29th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. string binary(string x)
  4. {
  5. string res="";
  6. int len=x.length();
  7. for(int i=0;i<len;i++)
  8. {
  9. if(x[i]=='1')
  10. res+="0001";
  11. else if(x[i]=='2')
  12. res+="0010";
  13. else if(x[i]=='3')
  14. res+="0011";
  15. else if(x[i]=='4')
  16. res+="0100";
  17. else if(x[i]=='5')
  18. res+="0101";
  19. else if(x[i]=='6')
  20. res+="0110";
  21. else if(x[i]=='7')
  22. res+="0111";
  23. else if(x[i]=='8')
  24. res+="1000";
  25. else if(x[i]=='9')
  26. res+="1001";
  27. else if(x[i]=='A')
  28. res+="1010";
  29. else if(x[i]=='B')
  30. res+="1011";
  31. else if(x[i]=='C')
  32. res+="1100";
  33. else if(x[i]=='D')
  34. res+="1101";
  35. else if(x[i]=='E')
  36. res+="1110";
  37. else if(x[i]=='F')
  38. res+="1111";
  39. }
  40. // cout<<res<<endl;
  41. int len1=res.length();
  42. int n=13-len1;
  43. string ans="";
  44. for(int i=0;i<n;i++)
  45. ans+='0';
  46. ans+=res;
  47. // cout<<ans<<endl;
  48. return ans;
  49. }
  50. int decimal(string x)
  51. {
  52. int len =x.length();
  53. int base=1;
  54. int res=0;
  55. for (int i=len-1; i>=0; i--)
  56. {
  57. if (x[i]>='0' && x[i]<='9')
  58. {
  59. res+=(x[i]-48)*base;
  60. base=base*16;
  61. }
  62. else if (x[i]>='A' && x[i]<='F')
  63. {
  64. res+=(x[i]-55)*base;
  65. base=base*16;
  66. }
  67. }
  68.  
  69. return res;
  70. }
  71.  
  72.  
  73. int main()
  74. {
  75. int ans,c,d,e,f,t;
  76. char ch;
  77. string a,b;
  78. cin>>t;
  79. while(t--)
  80. {
  81. cin>>a>>ch>>b;
  82. string bina=binary(a);
  83. string binb=binary(b);
  84. int decia=decimal(a);
  85. int decib=decimal(b);
  86. if(ch=='+')
  87. {
  88. ans=decia+decib;
  89. }
  90. else
  91. {
  92. ans=decia-decib;
  93. }
  94.  
  95. cout<<bina<<" "<<ch<<" "<<binb<<" = "<<ans<<endl;
  96. }
  97. return 0;
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement