fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void Multiplication(int (&Multiplicand)[64],int (&Multiplier)[32],int (&Product)[64],int Exponent1,int Exponent2,int RemExponent1,int RemExponent2);
  6. int ConvertToBinary_Multiplicand(unsigned long long first,int (&Multiplicand)[64],int &Exponent);
  7. int ConvertToBinary_Multiplier(unsigned long long Second,int (&Multiplier)[32],int &Exponent);
  8. int Convert_Fraction_Part_To_Binary(double remainder,int (&Goku)[5],int &RemainderExp);
  9. void UpdateMultiplicand(int (&DecimalPart)[64],int (&FractionPart)[5],int Exponent,int RemainderExp,double &remainder);
  10. void UpdateMultiplier(int (&DecimalPart)[32],int (&FractionPart)[5],int Exponent,int RemainderExp,double &remainder);
  11.  
  12.  
  13. int main()
  14. {
  15. int MutiplicandDecimalPart[64]={},MutiplicandFractionPart[5]={},MutiplierDecimalPart[32]={},MutiplierFractionPart[5]={},Product[64]={},Multiplicand_Decimal_Exponent=0,Multiplier_Decimal_Exponent=0,Multiplicand_Remainder_Exponent=0,Multiplier_Remainder_Exponent=0;
  16. unsigned long long first,second;
  17. double a,b,MultiplicandRemainder,MultiplierRemainder;
  18.  
  19. cin>> a;
  20. cin>>b;
  21. first=(unsigned long long) a;
  22. second=(unsigned long long) b;
  23.  
  24. MultiplicandRemainder=a-first;
  25. MultiplierRemainder=b-second;
  26.  
  27.  
  28.  
  29. ConvertToBinary_Multiplicand(first,MutiplicandDecimalPart,Multiplicand_Decimal_Exponent);
  30.  
  31. if(MultiplicandRemainder>0.0)
  32. {
  33. Convert_Fraction_Part_To_Binary(MultiplicandRemainder,MutiplicandFractionPart,Multiplicand_Remainder_Exponent); // 0.5 ki trha k numbers ka binary find kre ga
  34. }
  35.  
  36.  
  37. ConvertToBinary_Multiplier(second,MutiplierDecimalPart,Multiplier_Decimal_Exponent);
  38.  
  39. if(MultiplierRemainder>0.0)
  40. {
  41. Convert_Fraction_Part_To_Binary(MultiplierRemainder,MutiplierFractionPart,Multiplier_Remainder_Exponent);
  42. }
  43.  
  44. UpdateMultiplicand(MutiplicandDecimalPart,MutiplicandFractionPart,Multiplicand_Decimal_Exponent,Multiplicand_Remainder_Exponent,MultiplicandRemainder);
  45.  
  46. UpdateMultiplier(MutiplierDecimalPart,MutiplierFractionPart,Multiplier_Decimal_Exponent,Multiplier_Remainder_Exponent,MultiplicandRemainder);
  47.  
  48.  
  49. Multiplication(MutiplicandDecimalPart,MutiplierDecimalPart,Product,Multiplicand_Decimal_Exponent,Multiplier_Decimal_Exponent,Multiplicand_Remainder_Exponent,Multiplier_Remainder_Exponent);
  50.  
  51. return 0;
  52. }
  53.  
  54. int ConvertToBinary_Multiplicand(unsigned long long first,int (&Multiplicand)[64],int &Exponent)
  55. {
  56. // converts the decimal part of multiplicand into binary
  57.  
  58. for(Exponent=0;first!=0;Exponent++)
  59. {
  60. if(first & 1)
  61. Multiplicand[Exponent]=1;
  62. else
  63. Multiplicand[Exponent]=0;
  64.  
  65. first>>=1;
  66. }
  67. }
  68.  
  69.  
  70. int ConvertToBinary_Multiplier(unsigned long long Second,int (&Multiplier)[32],int &Exponent)
  71. {
  72. // converts decimal part of multiplier into binary
  73.  
  74. for(Exponent=0;Second!=0;Exponent++)
  75. {
  76. if(Second & 1)
  77. Multiplier[Exponent]=1;
  78. else
  79. Multiplier[Exponent]=0;
  80. Second>>=1;
  81. }
  82. }
  83.  
  84.  
  85. int Convert_Fraction_Part_To_Binary(double remainder,int (&Goku)[5],int &RemainderExp)
  86. {
  87. // converts fraction parts in to binary
  88.  
  89. int x=0;
  90. for(int i=0;i<5;i++)
  91. {
  92. remainder*=2;
  93.  
  94. if(remainder>1.0)
  95. {
  96. remainder-=1.0;
  97. Goku[x]=1;
  98. x++;
  99. }
  100. else
  101. if(remainder<1.0)
  102. {
  103. Goku[x]=0;
  104. x++;
  105. }
  106. else
  107. if(remainder==1.0)
  108. {
  109. Goku[x]=1;
  110. break;
  111. }
  112.  
  113. }
  114.  
  115. RemainderExp=x;
  116. }
  117.  
  118.  
  119. void UpdateMultiplicand(int (&DecimalPart)[64],int (&FractionPart)[5],int Exponent,int RemainderExp,double &remainder)
  120. {
  121. // Copies the binary bits of fraction part in DecimalPart array
  122.  
  123. // so that I won't have to handle two arrays separatly for multplication
  124.  
  125. if(RemainderExp==5)
  126. RemainderExp-=1;
  127.  
  128. int p=Exponent+RemainderExp;
  129.  
  130. if(remainder>0.0)
  131. {
  132. for(int i=p-2;i>=0;i--)
  133. {
  134. DecimalPart[i+1+RemainderExp]=DecimalPart[i];
  135. }
  136.  
  137. for(int i=0;i<=RemainderExp;i++)
  138. {
  139. DecimalPart[i]=FractionPart[i];
  140. }
  141.  
  142. }
  143. }
  144.  
  145. void UpdateMultiplier(int (&DecimalPart)[32],int (&FractionPart)[5],int Exponent,int RemainderExp,double &remainder)
  146. {
  147. // Copies the binary bits of fraction part in DecimalPart array
  148.  
  149. // so that I won't have to handle two arrays separatly for multplication
  150.  
  151. if(RemainderExp==5)
  152. RemainderExp-=1;
  153.  
  154. int p=Exponent+RemainderExp;
  155.  
  156. if(remainder>0.0)
  157. {
  158. for(int i=p-2;i>=0;i--)
  159. {
  160. DecimalPart[i+1+RemainderExp]=DecimalPart[i];
  161. }
  162.  
  163. for(int i=0;i<=RemainderExp;i++)
  164. {
  165. DecimalPart[i]=FractionPart[i];
  166. }
  167. }
  168.  
  169. }
  170.  
  171.  
  172. void Multiplication(int (&Multiplicand)[64],int (&Multiplier)[32],int (&Product)[64],int Exponent1,int Exponent2,int RemExponent1,int RemExponent2)
  173. {
  174. // Multiplies binary forms of Multiplicand and multiplier
  175.  
  176. int i=0,carry=0;bool OneOccuredBefore=false;
  177.  
  178. while(true)
  179. {
  180. if(Multiplier[i]!=0)// multiplier
  181. for(int j=0;j<64;j++)
  182. {
  183.  
  184. const int sum = Product[j] + Multiplicand[j] + carry;
  185. Product[j]=sum % 2;
  186. carry=sum / 2;
  187.  
  188. }
  189.  
  190.  
  191. for(int h=63;h>=0;h--)
  192. {
  193. if(h==0)
  194. {
  195. Multiplicand[0]=0; // shifting left
  196. }
  197. else
  198. {
  199. Multiplicand[h]=Multiplicand[h-1];
  200. Multiplicand[h-1]=0;
  201. }
  202.  
  203. }
  204.  
  205. if(i==31)
  206. break;
  207.  
  208. i++;
  209. }
  210.  
  211. bool oneOccured=false;
  212. // then I display the Product array on screen which contains the product.
  213.  
  214. for(int i=63;i>=0;i++)
  215. {
  216. if(Product[i]==1 && oneOccured==false)
  217. cout<<Product[i];
  218. else
  219. if(oneOccured==true)
  220. cout<<Product[i];
  221. }
  222.  
  223. }
Runtime error #stdin #stdout 0.01s 2728KB
stdin
Standard input is empty
stdout

Standard output is empty