Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class IEE754
  6. {
  7. public:
  8. IEE754(bool si, int pow, float mant);
  9. IEE754(float number);
  10. int get_power();
  11. float calculate_iee754();
  12. int potega(int power);
  13. void operator_convert(float number);
  14. void operator+= (IEE754 a1)
  15. {
  16.  
  17. float x = calculate_iee754()+a1.calculate_iee754();
  18. // Przerobić na IEEE754.
  19. this->operator_convert(x);
  20.  
  21. }
  22. void operator-= (IEE754 a1)
  23. {
  24. float x = calculate_iee754()-a1.calculate_iee754();
  25. this->operator_convert(x);
  26. }
  27. private:
  28. bool sign;
  29. int power;
  30. float mantysa;
  31. };
  32. void IEE754::operator_convert(float number)
  33. {
  34. if (number > 0)
  35. sign = 0;
  36. else
  37. {
  38. sign = 1;
  39. number = -number;
  40. }
  41.  
  42. int a = 0, temp_int = number;
  43. while (temp_int >= 2)
  44. {
  45. temp_int /= 2;
  46. a++;
  47. }
  48. power = a;
  49. temp_int = potega(power);
  50. mantysa = number / temp_int;
  51. }
  52. IEE754 operator+ (IEE754 a1, IEE754 a2)
  53. {
  54. return IEE754(a1.calculate_iee754() + a2.calculate_iee754());
  55. }
  56.  
  57. IEE754 operator- (IEE754 a1, IEE754 a2)
  58. {
  59. return IEE754(a1.calculate_iee754() - a2.calculate_iee754());
  60. }
  61.  
  62. ostream & operator<< (ostream & stream, IEE754 a)
  63. {
  64. return stream << a.calculate_iee754();
  65. }
  66.  
  67. IEE754::IEE754(float number)
  68. {
  69. if (number > 0)
  70. sign = 0;
  71. else
  72. {
  73. sign = 1;
  74. number = -number;
  75. }
  76.  
  77. int a = 0, temp_int = number;
  78. while (temp_int >= 2)
  79. {
  80. temp_int /= 2;
  81. a++;
  82. }
  83. power = a;
  84. temp_int = potega(power);
  85. mantysa = number / temp_int;
  86.  
  87. }
  88.  
  89. IEE754::IEE754(bool si, int pow, float mant) :
  90. sign(si), power(pow), mantysa(mant)
  91. {
  92.  
  93. }
  94.  
  95. float IEE754::calculate_iee754()
  96. {
  97. int a;
  98. if(sign==0)
  99. a=1;
  100. else
  101. a=-1;
  102. float x;
  103. x=a*potega(power)*this->mantysa;
  104. return x;
  105. }
  106.  
  107. int IEE754::get_power()
  108. {
  109. return power;
  110. }
  111.  
  112. int IEE754::potega(int power)
  113. {
  114. if (power == 0) return 1;
  115. else
  116.  
  117. return power = 2 * potega(power-1);
  118. }
  119.  
  120. void read_float(float &number)
  121. {
  122. bool load = true;
  123. while (load)
  124. {
  125. cin >> number;
  126. if (cin.fail())
  127. {
  128. cin.clear();
  129. cin.ignore(1000000, '\n');
  130. }
  131. else
  132. {
  133. load = false;
  134. }
  135. }
  136. }
  137. void read_number(string &x, bool &sign, int &power, float &mantysa)
  138. {
  139. bool load = true;
  140. while (load)
  141. {
  142. cin >> x;
  143. if (x.length() == 1)
  144. {
  145. if (x[0] == '-')
  146. {
  147. sign = true;
  148. load = false;
  149. }
  150. else if (x[0] == '+')
  151. {
  152. sign = false;
  153. load = false;
  154. }
  155. }
  156. }
  157. bool load1 = true;
  158. while (load1)
  159. {
  160. cin >> power;
  161. if (cin.fail())
  162. {
  163. cin.clear();
  164. cin.ignore(1000000, '\n');
  165. }
  166. else
  167. {
  168. power-=127;
  169. load1 = false;
  170. }
  171. }
  172. bool load2 = true;
  173. while (load2)
  174. {
  175. cin >> mantysa;
  176. if (cin.fail())
  177. {
  178. cin.clear();
  179. cin.ignore(1000000, '\n');
  180. }
  181. else
  182. {
  183. load2 = false;
  184. }
  185. }
  186.  
  187. }
  188.  
  189. int main()
  190. {
  191.  
  192. string x;
  193. bool sign;
  194. int power;
  195. float mantysa;
  196. read_number(x, sign, power, mantysa);
  197. IEE754 x0(sign, power, mantysa);
  198. read_number(x, sign, power, mantysa);
  199. IEE754 x1(sign, power, mantysa);
  200. float number;
  201. read_float(number);
  202. IEE754 x2(number);
  203. read_float(number);
  204. IEE754 x3(number);
  205. x0 += x1;
  206. x3 -= x2;
  207. IEE754 x4(0);
  208. x4 = x1 + x3;
  209. IEE754 x5(0);
  210. x5 = x4 - x2;
  211. cout << x0 << " ";
  212. cout << x1 << " ";
  213. cout << x2 << " ";
  214. cout << x3 << " ";
  215. cout << x4 << " ";
  216. cout << x5 << " ";
  217.  
  218.  
  219. return 0;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement