Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. //#include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <sstream>
  8. using namespace std;
  9.  
  10. #pragma region operations
  11. void add(int &firstValueRe, int &secondValueRe, int firstValueIm, int secondValueIm)
  12. {
  13. int sumRe = 0;
  14. int sumIm = 0;
  15. char plusOrMinus = '+';
  16. char charI = 'i';
  17.  
  18.  
  19. /*if (firstValueIm == 0)
  20. {
  21. firstValueIm = 1;
  22. }
  23. if (secondValueIm == 0)
  24. {
  25. secondValueIm = 1;
  26. }*/
  27. sumRe = firstValueRe + secondValueRe;
  28. sumIm = firstValueIm + secondValueIm;
  29.  
  30.  
  31. if (sumIm < 0)
  32. {
  33. char plusOrMinus = '-';
  34. sumIm *= -1;
  35. char charI = 'i';
  36. cout << "Answer: " << sumRe << plusOrMinus << sumIm << charI << endl;
  37. }
  38. else if (sumIm > 0)
  39. {
  40. char plusOrMinus = '+';
  41. char charI = 'i';
  42. cout << "Answer: " << sumRe << plusOrMinus << sumIm << charI << endl;
  43. }
  44. else if (sumIm == 1)
  45. {
  46. cout << "Answer: " << sumRe << plusOrMinus << charI << endl;
  47. }
  48. else if (sumIm == 0 && sumRe != 0)
  49. {
  50. cout << "Answer: " << sumRe << endl;
  51. }
  52. else if (sumRe == 0 && sumIm != 0)
  53. {
  54. cout << "Answer: " << sumIm << charI << endl;
  55. }
  56. else if (sumRe == 0 && sumIm == 0)
  57. {
  58. cout << "Answer: 0" << endl;
  59. }
  60.  
  61.  
  62. }
  63. void sub(int &firstValueRe, int &secondValueRe, int &firstValueIm, int &secondValueIm)
  64. {
  65. int difRe;
  66. int difIm;
  67. char plusOrMinus = '+';
  68. char charI = 'i';
  69. difRe = firstValueRe - secondValueRe;
  70. difIm = firstValueIm - secondValueIm;
  71. cout << difIm << endl;
  72.  
  73. if (difIm < 0 && difIm != -1)
  74. {
  75. char plusOrMinus = '-';
  76. difIm *= -1;
  77. char charI = 'i';
  78. }
  79. else if (difIm > 0 && difIm != 1)
  80. {
  81. char plusOrMinus = '+';
  82. char charI = 'i';
  83. }
  84. else if (difIm == 1)
  85. {
  86. cout << "Answer: " << difRe << plusOrMinus << charI << endl;
  87. }
  88. else if (difIm == -1)
  89. {
  90. difIm *= -1;
  91. cout << "Answer: " << difRe << plusOrMinus << charI << endl;
  92. }
  93. else if (difIm == 0 && difRe != 0)
  94. {
  95. cout << "Answer: " << difRe << endl;
  96. }
  97. else if (difRe == 0 && difIm != 0)
  98. {
  99. cout << "Answer: " << difIm << charI << endl;
  100. }
  101. else if (difRe == 0 && difIm == 0)
  102. {
  103. cout << "Answer: 0" << endl;
  104. }
  105. //cout << "Answer: " << difRe << plusOrMinus << difIm << charI << endl;
  106. }
  107. void mul(int &firstValueRe, int &secondValueRe, int &firstValueIm, int &secondValueIm)
  108. {
  109. int proRe;
  110. int proIm;
  111. char plusOrMinus = '+';
  112. char charI = 'i';
  113. proRe = (firstValueRe*secondValueRe) - (firstValueIm*secondValueIm);
  114. proIm = (firstValueRe*secondValueIm) + (firstValueIm*secondValueRe);
  115. if (proIm < 0)
  116. {
  117. char plusOrMinus = '-';
  118. proIm *= -1;
  119. char charI = 'i';
  120. }
  121. else if (proIm > 0)
  122. {
  123. char plusOrMinus = '+';
  124. char charI = 'i';
  125. }
  126. else if (proIm == 1)
  127. {
  128. cout << "Answer: " << proRe << plusOrMinus << charI << endl;
  129. }
  130. else if (proIm == 0 && proRe != 0)
  131. {
  132. cout << "Answer: " << proRe << endl;
  133. }
  134. else if (proRe == 0 && proIm != 0)
  135. {
  136. cout << "Answer: " << proIm << charI << endl;
  137. }
  138. else if (proRe == 0 && proIm == 0)
  139. {
  140. cout << "Answer: 0" << endl;
  141. }
  142. cout << "Answer: " << proRe << plusOrMinus << proIm << charI << endl;
  143. }
  144. void div(int &firstValueRe, int &secondValueRe, int &firstValueIm, int &secondValueIm)
  145. {
  146. double quoRe;
  147. double quoIm;
  148. char plusOrMinus = '+';
  149. char charI = 'i';
  150. quoRe = ((firstValueRe*secondValueRe) + (firstValueIm*secondValueIm)) / (pow(secondValueRe, 2) + pow(secondValueIm, 2));
  151. quoIm = ((firstValueIm*secondValueRe) - (firstValueRe*secondValueIm)) / (pow(secondValueRe, 2) + pow(secondValueIm, 2));
  152. if (quoIm < 0)
  153. {
  154. char plusOrMinus = '-';
  155. quoIm *= -1;
  156. char charI = 'i';
  157. }
  158. else if (quoIm > 0)
  159. {
  160. char plusOrMinus = '+';
  161. char charI = 'i';
  162. }
  163. else if (quoIm == 1)
  164. {
  165. cout << "Answer: " << quoRe << plusOrMinus << charI << endl;
  166. }
  167. else if (quoIm == 0 && quoRe != 0)
  168. {
  169. cout << "Answer: " << quoRe << endl;
  170. }
  171. else if (quoRe == 0 && quoIm != 0)
  172. {
  173. cout << "Answer: " << quoIm << charI << endl;
  174. }
  175. else if (quoRe == 0 && quoIm == 0)
  176. {
  177. cout << "Answer: 0" << endl;
  178. }
  179. cout << "Answer: " << quoRe << plusOrMinus << quoIm << charI << endl;
  180. }
  181. #pragma endregion
  182.  
  183. int main()
  184. {
  185. bool tr;
  186. tr = true;
  187. while (tr == true)
  188. {
  189. string firstValue;
  190. int firstValueRe;
  191. string firstValueImInput;
  192. int firstValueIm = 0;
  193. string secondValue;
  194. int secondValueRe;
  195. string secondValueImInput;
  196. int secondValueIm = 0;
  197. char inputOperation;
  198. double finalValue;
  199. stringstream ss;
  200. stringstream sn;
  201.  
  202.  
  203.  
  204. cout << "Please choose one of the following operations:" << endl;
  205. cout << "======[Operations]======" << endl;
  206. cout << "=|+|=============|plus|=" << endl;
  207. cout << "=|-|============|minus|=" << endl;
  208. cout << "=|*|=========|multiply|=" << endl;
  209. cout << "=|/|===========|divide|=" << endl;
  210. cout << "======[Operations]======" << endl;
  211. cout << endl;
  212. cout << "... or press 'x' to exit. ";
  213. cin >> inputOperation;
  214.  
  215. if (inputOperation == 'x')
  216. {
  217. tr = false;
  218. }
  219. else
  220. {
  221. cout << "Enter first complex value (a+bi): ";
  222. cin >> firstValueRe;
  223. cin >> firstValueImInput;
  224.  
  225. if (firstValueImInput == "0i")
  226. {
  227. firstValueIm = 0;
  228. int sumIm;
  229. sumIm += 1;
  230. }
  231. else
  232. {
  233. ss << firstValueImInput;
  234. ss >> firstValueIm;
  235. }
  236.  
  237. cout << "Enter second value's real part (c+di): ";
  238. cin >> secondValueRe;
  239. cin >> secondValueImInput;
  240.  
  241. if (secondValueImInput == "0i")
  242. {
  243. secondValueIm = 0;
  244. }
  245. else
  246. {
  247. ss << secondValueImInput;
  248. ss >> secondValueIm;
  249. }
  250.  
  251. if (inputOperation == '+')
  252. {
  253. add(firstValueRe, secondValueRe, firstValueIm, secondValueIm);
  254. cout << endl;
  255. }
  256. else if (inputOperation == '-')
  257. {
  258. sub(firstValueRe, secondValueRe, firstValueIm, secondValueIm);
  259. cout << endl;
  260. }
  261. else if (inputOperation == '*')
  262. {
  263. mul(firstValueRe, secondValueRe, firstValueIm, secondValueIm);
  264. cout << endl;
  265. }
  266. else if (inputOperation == '/')
  267. {
  268. (double)firstValueIm;
  269. (double)secondValueIm;
  270. if (secondValueRe == 0 && secondValueIm == 0)
  271. {
  272. cout << "{begin: Error Message} \\ This input equation is not registered == Please enter other input equation for successful calculation \\ {end: Error Message}" << endl;
  273. }
  274. else
  275. {
  276. div(firstValueRe, secondValueRe, firstValueIm, secondValueIm);
  277. cout << endl;
  278. }
  279.  
  280. }
  281.  
  282. }
  283.  
  284. }
  285.  
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement