Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************
- * 请编程实现求两个超长非负数之和(限定使用C/C++或Java,不可使用类
- * 似 BigDecimal等类库).
- *
- * 要求
- *
- * ·不损失精度
- * ·首尾均不出现无意义的0
- * ·不出现无意义的小数点(比如1.00应该输出1)
- * ·不得使用除字符操作外的函数
- *
- * 输入描述
- *
- * 输入为两个非负数,每个数满足下面要求
- *
- * 仅包含0-9的数字,0或1个小数点“.”。
- * 长度不大于200
- * 不需判错
- *
- * 输出描述
- *
- * 输出为两数之和
- ******************************************************/
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- // 模拟整数十进制全加器
- int _add(int n1, int n2, const int in_carrier, int& out_carrier) {
- int sum = n1 + n2 + in_carrier;
- if (sum >= 10) {
- // carries
- out_carrier = 1;
- sum %= 10;
- }
- else {
- out_carrier = 0;
- }
- return sum;
- }
- string add(string &a1, string &a2)
- {
- // please write your code here.
- vector<int> carriers; // 进位标志位
- vector<int> x_sums; // 整数各位相加的结果
- vector<int> y_sums; // 小数个各位相加的结果
- string x_total = "";
- string y_total = "";
- string total = "";
- // 有没有小数点?
- int dot1_pos = a1.find(".");
- int dot2_pos = a1.find(".");
- string x1, x2; // 整数部分
- string y1, y2; // 小数部分
- bool have_dot = false;
- bool have_two_dots = false;
- if (dot1_pos > 0 && dot2_pos > 0) {
- // 两个都有小数点
- have_dot = true;
- have_two_dots = true;
- string trailing_zeros = "";
- x1 = a1.substr(0, dot1_pos);
- y1 = a1.substr(dot1_pos+1, a1.size() - dot1_pos - 1);
- x2 = a2.substr(0, dot2_pos);
- y2 = a2.substr(dot2_pos+1, a2.size() - dot2_pos - 1);
- // 补零
- if (y1.size() > y2.size()) {
- int num_zeros = y1.size() - y2.size();
- for (int i =0; i< num_zeros; i++) {
- trailing_zeros += "0";
- }
- y2 = y2 + trailing_zeros;
- }
- else if (y1.size() < y2.size()) {
- int num_zeros = y2.size() - y1.size();
- for (int i =0; i< num_zeros; i++) {
- trailing_zeros += "0";
- }
- y1 = y1 + trailing_zeros;
- }
- else {
- // 故意留空 nothing to do here
- }
- }
- else if (dot1_pos < 0 && dot2_pos < 0) {
- // 两个都没有小数点
- have_dot = false;
- x1 = a1.substr(0, dot1_pos);
- x2 = a2.substr(0, dot2_pos);
- }
- else
- {
- // 其中一个有小数点,另一个没有小数点
- have_dot = true;
- if (dot1_pos < 0 ) {
- y_total += y1;
- x1 = a1.substr(0, dot1_pos);
- }
- else {
- // dot2_pos < 0
- y_total += y2;
- x2 = a2.substr(0, dot2_pos);
- }
- }
- string leading_zeros = "";
- // 补零
- if (x1.size() > x2.size()) {
- int num_zeros = x1.size() - x2.size();
- for (int i =0; i< num_zeros; i++) {
- leading_zeros += "0";
- }
- x2 = leading_zeros + x2;
- }
- else if (x1.size() < x2.size()) {
- int num_zeros = x2.size() - x1.size();
- for (int i =0; i< num_zeros; i++) {
- leading_zeros += "0";
- }
- x1 = leading_zeros + x1;
- }
- else {
- // 故意留空 nothing to do here
- }
- // 从低位往高位加
- // 小数部分
- int this_in_carrier = 0; // 当前的输入进位位
- int this_out_carrier = 0; // 当前的输出进位位
- if (have_two_dots) {
- for (int i = y1.size()-1; i >= 0; i--) {
- int this_sum;
- int n1 = (char) y1[i] - '0';
- int n2 = (char) y2[i] - '0';
- this_sum = _add(n1, n2, this_in_carrier, this_out_carrier);
- y_sums.push_back(this_sum);
- this_in_carrier = this_out_carrier;
- }
- }
- // 整数部分
- for (int i = x1.size()-1; i >= 0; i--) {
- int this_sum;
- int n1 = (char) x1[i] - '0';
- int n2 = (char) x2[i] - '0';
- this_sum = _add(n1, n2, this_in_carrier, this_out_carrier);
- x_sums.push_back(this_sum);
- this_in_carrier = this_out_carrier;
- }
- // 整理结果并输出
- // 如果最高位的输出进位位是1,在就在输出字符串先加一个1
- if (this_out_carrier == 1) {
- x_total += "1";
- }
- // 从高位往低位输出
- for (int i = x_sums.size()-1; i >= 0; i--) {
- x_total += std::to_string(x_sums[i]);
- }
- // 判断是否要加小数点
- if (have_dot) {
- have_dot = false;
- for (int i = y_sums.size()-1; i >= 0; i--) {
- if (y_sums[i] != 0) {
- have_dot = true;
- }
- y_total += std::to_string(y_sums[i]);
- }
- }
- if (have_dot) {
- total = x_total + "." + y_total;
- }
- else {
- total = x_total;
- }
- return total;
- }
- int main(void){
- std::cout << "enter two nums:\n";
- ////////////////////////////////////////////
- string a1, a2, a3;
- cin >> a1 >> a2;
- a3 = add(a1, a2);
- cout<< a3<< endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement