Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. /******************************************************
  2. * 请编程实现求两个超长非负数之和(限定使用C/C++或Java,不可使用类
  3. * 似 BigDecimal等类库).
  4. *
  5. * 要求
  6. *
  7. * ·不损失精度
  8. * ·首尾均不出现无意义的0
  9. * ·不出现无意义的小数点(比如1.00应该输出1)
  10. * ·不得使用除字符操作外的函数
  11. *
  12. * 输入描述
  13. *
  14. * 输入为两个非负数,每个数满足下面要求
  15. *
  16. * 仅包含0-9的数字,0或1个小数点“.”。
  17. * 长度不大于200
  18. * 不需判错
  19. *
  20. * 输出描述
  21. *
  22. * 输出为两数之和
  23. ******************************************************/
  24.  
  25.  
  26. #include <iostream>
  27. #include <string>
  28. #include <vector>
  29.  
  30. using namespace std;
  31.  
  32. // 模拟整数十进制全加器
  33. int _add(int n1, int n2, const int in_carrier, int& out_carrier) {
  34. int sum = n1 + n2 + in_carrier;
  35. if (sum >= 10) {
  36. // carries
  37. out_carrier = 1;
  38. sum %= 10;
  39. }
  40. else {
  41. out_carrier = 0;
  42. }
  43. return sum;
  44. }
  45.  
  46. string add(string &a1, string &a2)
  47. {
  48. // please write your code here.
  49.  
  50. vector<int> carriers; // 进位标志位
  51. vector<int> x_sums; // 整数各位相加的结果
  52. vector<int> y_sums; // 小数个各位相加的结果
  53. string x_total = "";
  54. string y_total = "";
  55. string total = "";
  56.  
  57. // 有没有小数点?
  58. int dot1_pos = a1.find(".");
  59. int dot2_pos = a1.find(".");
  60.  
  61. string x1, x2; // 整数部分
  62. string y1, y2; // 小数部分
  63. bool have_dot = false;
  64. bool have_two_dots = false;
  65. if (dot1_pos > 0 && dot2_pos > 0) {
  66. // 两个都有小数点
  67. have_dot = true;
  68. have_two_dots = true;
  69. string trailing_zeros = "";
  70. x1 = a1.substr(0, dot1_pos);
  71. y1 = a1.substr(dot1_pos+1, a1.size() - dot1_pos - 1);
  72. x2 = a2.substr(0, dot2_pos);
  73. y2 = a2.substr(dot2_pos+1, a2.size() - dot2_pos - 1);
  74.  
  75. // 补零
  76. if (y1.size() > y2.size()) {
  77. int num_zeros = y1.size() - y2.size();
  78. for (int i =0; i< num_zeros; i++) {
  79. trailing_zeros += "0";
  80. }
  81. y2 = y2 + trailing_zeros;
  82. }
  83. else if (y1.size() < y2.size()) {
  84. int num_zeros = y2.size() - y1.size();
  85. for (int i =0; i< num_zeros; i++) {
  86. trailing_zeros += "0";
  87. }
  88. y1 = y1 + trailing_zeros;
  89. }
  90. else {
  91. // 故意留空 nothing to do here
  92. }
  93.  
  94. }
  95. else if (dot1_pos < 0 && dot2_pos < 0) {
  96. // 两个都没有小数点
  97. have_dot = false;
  98. x1 = a1.substr(0, dot1_pos);
  99. x2 = a2.substr(0, dot2_pos);
  100. }
  101. else
  102. {
  103. // 其中一个有小数点,另一个没有小数点
  104. have_dot = true;
  105. if (dot1_pos < 0 ) {
  106. y_total += y1;
  107. x1 = a1.substr(0, dot1_pos);
  108. }
  109. else {
  110. // dot2_pos < 0
  111. y_total += y2;
  112. x2 = a2.substr(0, dot2_pos);
  113. }
  114. }
  115.  
  116. string leading_zeros = "";
  117.  
  118. // 补零
  119. if (x1.size() > x2.size()) {
  120. int num_zeros = x1.size() - x2.size();
  121. for (int i =0; i< num_zeros; i++) {
  122. leading_zeros += "0";
  123. }
  124. x2 = leading_zeros + x2;
  125. }
  126. else if (x1.size() < x2.size()) {
  127. int num_zeros = x2.size() - x1.size();
  128. for (int i =0; i< num_zeros; i++) {
  129. leading_zeros += "0";
  130. }
  131. x1 = leading_zeros + x1;
  132. }
  133. else {
  134. // 故意留空 nothing to do here
  135. }
  136.  
  137. // 从低位往高位加
  138. // 小数部分
  139. int this_in_carrier = 0; // 当前的输入进位位
  140. int this_out_carrier = 0; // 当前的输出进位位
  141. if (have_two_dots) {
  142. for (int i = y1.size()-1; i >= 0; i--) {
  143. int this_sum;
  144. int n1 = (char) y1[i] - '0';
  145. int n2 = (char) y2[i] - '0';
  146. this_sum = _add(n1, n2, this_in_carrier, this_out_carrier);
  147. y_sums.push_back(this_sum);
  148. this_in_carrier = this_out_carrier;
  149. }
  150. }
  151.  
  152.  
  153. // 整数部分
  154. for (int i = x1.size()-1; i >= 0; i--) {
  155. int this_sum;
  156. int n1 = (char) x1[i] - '0';
  157. int n2 = (char) x2[i] - '0';
  158. this_sum = _add(n1, n2, this_in_carrier, this_out_carrier);
  159. x_sums.push_back(this_sum);
  160. this_in_carrier = this_out_carrier;
  161. }
  162.  
  163.  
  164. // 整理结果并输出
  165. // 如果最高位的输出进位位是1,在就在输出字符串先加一个1
  166. if (this_out_carrier == 1) {
  167. x_total += "1";
  168. }
  169.  
  170. // 从高位往低位输出
  171. for (int i = x_sums.size()-1; i >= 0; i--) {
  172. x_total += std::to_string(x_sums[i]);
  173. }
  174.  
  175. // 判断是否要加小数点
  176. if (have_dot) {
  177. have_dot = false;
  178. for (int i = y_sums.size()-1; i >= 0; i--) {
  179. if (y_sums[i] != 0) {
  180. have_dot = true;
  181. }
  182. y_total += std::to_string(y_sums[i]);
  183. }
  184. }
  185.  
  186. if (have_dot) {
  187. total = x_total + "." + y_total;
  188. }
  189. else {
  190. total = x_total;
  191. }
  192.  
  193. return total;
  194. }
  195.  
  196.  
  197. int main(void){
  198. std::cout << "enter two nums:\n";
  199. ////////////////////////////////////////////
  200. string a1, a2, a3;
  201. cin >> a1 >> a2;
  202. a3 = add(a1, a2);
  203. cout<< a3<< endl;
  204. return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement