Guest User

Untitled

a guest
Aug 16th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. /****************************************************************************
  2. FileName [ largeNum.cpp ]
  3. PackageName [ addlargeNum ]
  4. Synopsis [ Define main() function ]
  5. Author [ (Jacky) Chang ]
  6. ****************************************************************************/
  7.  
  8. #include <iostream>
  9. #include <vector>
  10. #include <algorithm>
  11. #include <cstdlib>
  12. #include <cstring>
  13.  
  14. using namespace std;
  15.  
  16. bool enterNum(vector<char> &, short &);
  17. void addSum(vector<char> &, vector<char> &, const short, const short);
  18. int compare(const vector<char>, const vector<char>);
  19.  
  20. int main()
  21. {
  22. vector<char> num_x;
  23. vector<char> num_y;
  24. //---------------------------
  25. // +1:positive, -1:negative
  26. //---------------------------
  27. short signed_x = 1;
  28. short signed_y = 1;
  29.  
  30. bool INPUTVALID = 0;
  31.  
  32. cout << "Please enter first number:" << endl;
  33. INPUTVALID = enterNum(num_x, signed_x);
  34.  
  35. if ( INPUTVALID )
  36. {
  37. cout << "Please enter second number:" << endl;
  38. INPUTVALID = enterNum(num_y, signed_y);
  39. }
  40.  
  41. if (INPUTVALID)
  42. addSum(num_x, num_y, signed_x, signed_y);
  43.  
  44. return 0;
  45. }
  46.  
  47. //--------------------------------------------------------
  48. // Function: read input from user keyboard.
  49. //--------------------------------------------------------
  50. bool enterNum(vector<char>& num, short& signedbit)
  51. {
  52. bool is_First = 1;
  53. bool is_Valid = 1;
  54. char input = 0;
  55.  
  56. while (1)
  57. {
  58. input = cin.get();
  59.  
  60. if ( input == '\n' )
  61. break;
  62. else if ( input < 48 || input > 57 )
  63. {
  64. if ( is_First == 1 && input == 45)
  65. ;
  66. else
  67. {
  68. is_Valid = 0;
  69. break;
  70. }
  71. }
  72. else
  73. {
  74. num.push_back(input);
  75. }
  76.  
  77. if ( input == '0' && num.size() == 1 )
  78. {
  79. is_Valid = 0;
  80. break;
  81. }
  82.  
  83. if (is_First)
  84. {
  85. if ( input == 45 )
  86. {
  87. signedbit = -1;
  88. }
  89. else if ( input == '0' && num.size() != 1 )
  90. {
  91. is_Valid = 0;
  92. break;
  93. }
  94. is_First = 0;
  95. }
  96. }
  97.  
  98. if (is_Valid)
  99. return 1;
  100. else
  101. {
  102. cout << "Invalid input!!" << endl;
  103. return 0;
  104. }
  105. }
  106.  
  107. //--------------------------------------------------------
  108. // Function: add two number and print the result.
  109. //--------------------------------------------------------
  110. void addSum(vector<char>& num_x, vector<char>& num_y, const short signed_x, const short signed_y)
  111. {
  112. int ii = num_x.size() - 1;
  113. int jj = num_y.size() - 1;
  114. int over = 0;
  115. int tmp = 0;
  116. int borrow = 0;
  117. bool has_Negative = 0;
  118. vector<char> result;
  119.  
  120. //-------------------------------------------------------
  121. // case 1: num_x > 0, num_y < 0
  122. // (a) |num_x| > |num_y|
  123. // (b) |num_x| < |num_y|
  124. // case 2: num_x < 0, num_y > 0
  125. // (a) |num_x| > |num_y|
  126. // (a) |num_x| < |num_y|
  127. // case 3: num_x > 0, num_y > 0 || num_x < 0, num_y < 0
  128. //-------------------------------------------------------
  129.  
  130. while (1)
  131. {
  132. if ( signed_x == 1 && signed_y == -1 )
  133. {
  134. if ( compare(num_x, num_y) == 1 )
  135. {
  136. if ( ii >= 0 && jj >= 0)
  137. tmp = (num_x[ii]-48) - (num_y[jj]-48) - borrow;
  138. else if ( ii >= 0 && jj < 0 )
  139. tmp = (num_x[ii]-48) - borrow;
  140. else
  141. break;
  142.  
  143. if ( tmp < 0 )
  144. {
  145. tmp = tmp + 10;
  146. borrow = 1;
  147. }
  148. else
  149. borrow = 0;
  150.  
  151. result.push_back(tmp%10+48);
  152. }
  153. else if ( compare(num_x, num_y) == -1 )
  154. {
  155. if ( ii >= 0 && jj >= 0)
  156. tmp = (num_y[jj]-48) - (num_x[ii]-48) - borrow;
  157. else if ( ii < 0 && jj >= 0)
  158. tmp = (num_y[jj]-48) - borrow;
  159. else
  160. break;
  161.  
  162. if ( tmp < 0 )
  163. {
  164. tmp = tmp + 10;
  165. borrow = 1;
  166. }
  167. else
  168. borrow = 0;
  169.  
  170. result.push_back(tmp%10+48);
  171. has_Negative = 1;
  172. }
  173. else if ( compare(num_x, num_y) == 0 )
  174. {
  175. result.push_back(48);
  176. break;
  177. }
  178. }
  179. else if ( signed_x == -1 && signed_y == 1 )
  180. {
  181. if ( compare(num_x, num_y) == 1 )
  182. {
  183. if ( ii >= 0 && jj >= 0)
  184. tmp = (num_x[ii]-48) - (num_y[jj]-48) - borrow;
  185. else if ( ii >= 0 && jj < 0 )
  186. tmp = (num_x[ii]-48) - borrow;
  187. else
  188. break;
  189.  
  190. if ( tmp < 0 )
  191. {
  192. tmp = tmp + 10;
  193. borrow = 1;
  194. }
  195. else
  196. borrow = 0;
  197. result.push_back(tmp%10+48);
  198. has_Negative = 1;
  199. }
  200. else if ( compare(num_x, num_y) == -1 )
  201. {
  202. if ( ii >= 0 && jj >= 0)
  203. tmp = (num_y[jj]-48) - (num_x[ii]-48) - borrow;
  204. else if ( ii < 0 && jj >= 0)
  205. tmp = (num_y[jj]-48) - borrow;
  206. else
  207. break;
  208.  
  209. if ( tmp < 0 )
  210. {
  211. tmp = tmp + 10;
  212. borrow = 1;
  213. }
  214. else
  215. borrow = 0;
  216.  
  217. result.push_back(tmp%10+48);
  218. }
  219. else if ( compare(num_x, num_y) == 0 )
  220. {
  221. result.push_back(48);
  222. break;
  223. }
  224. }
  225. else
  226. {
  227. if ( ii >= 0 && jj >= 0 )
  228. tmp = (num_x[ii]-48) + (num_y[jj]-48) + over;
  229. else if ( ii >= 0 && jj < 0 )
  230. tmp = (num_x[ii]-48) + over;
  231. else if ( ii < 0 && jj >= 0 )
  232. tmp = (num_y[jj]-48) + over;
  233. else
  234. break;
  235.  
  236. if ( tmp > 9 )
  237. {
  238. result.push_back(tmp%10+48);
  239. over = tmp/10;
  240. }
  241. else
  242. {
  243. result.push_back(tmp+48);
  244. over = 0;
  245. }
  246.  
  247. }
  248. --ii; --jj;
  249. }
  250.  
  251. reverse(result.begin(), result.end());
  252.  
  253. if ( signed_x == -1 && signed_y == -1 )
  254. has_Negative = 1;
  255.  
  256. if ( has_Negative )
  257. cout << "-";
  258.  
  259. if ( over != 0 )
  260. cout << over;
  261.  
  262. cout << "The sum is:" << endl;
  263. for (unsigned short loop = 0; loop < result.size(); ++loop)
  264. cout << result[loop];
  265. cout << endl;
  266. }
  267.  
  268. //--------------------------------------------------------
  269. // Function: compare the value of two numbers.
  270. // Output : 1: a>b, 0: a=b, -1: a<b.
  271. //--------------------------------------------------------
  272. int compare(const vector<char> num_x, const vector<char> num_y)
  273. {
  274. int loop = 0;
  275. if ( num_x.size() > num_y.size() )
  276. return 1;
  277. else if ( num_x.size() < num_y.size() )
  278. return -1;
  279. else
  280. {
  281. while (1)
  282. {
  283. if ( num_x[loop] > num_y[loop] )
  284. return 1;
  285. else if ( num_x[loop] < num_y[loop] )
  286. return -1;
  287. else if ( num_x[loop] == num_y[loop] )
  288. {
  289. if ( loop == num_x.size() )
  290. return 0;
  291. }
  292. ++loop;
  293. }
  294. }
  295. }
Add Comment
Please, Sign In to add comment