题干
C++实现
# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <map>
# include <string>
using namespace std; string GetInteger ( string a) { return a. substr ( 0 , a. find ( '.' ) ) ;
} string GetFraction ( string a) { return a. substr ( a. find ( '.' ) + 1 , a. size ( ) - a. find ( '.' ) ) ;
}
void FractionPlus ( string& res, int & carry, string fa, string fb) { int size = max ( fa. size ( ) , fb. size ( ) ) ; while ( fa. size ( ) < size) { fa. push_back ( '0' ) ; } while ( fb. size ( ) < size) { fb. push_back ( '0' ) ; } res. resize ( size) ; carry = 0 ; for ( int i = size - 1 ; i >= 0 ; i-- ) { if ( fa[ i] + fb[ i] + carry - '0' > '9' ) { res[ i] = fa[ i] + fb[ i] + carry - '0' - 10 ; carry = 1 ; } else { res[ i] = fa[ i] + fb[ i] + carry - '0' ; carry = 0 ; } } return ;
} void IntegerPlus ( string& res, int carry, string ia, string ib) { res. clear ( ) ; for ( int i = ia. size ( ) - 1 , j = ib. size ( ) - 1 ; i >= 0 || j >= 0 || carry == 1 ; -- i, -- j) { if ( i >= 0 && j >= 0 ) { if ( ia[ i] + ib[ j] + carry - '0' > '9' ) { res. insert ( res. begin ( ) , ia[ i] + ib[ j] + carry - '0' - 10 ) ; carry = 1 ; } else { res. insert ( res. begin ( ) , ia[ i] + ib[ j] + carry - '0' ) ; carry = 0 ; } } else if ( i >= 0 && j < 0 ) { if ( ia[ i] + carry > '9' ) { res. insert ( res. begin ( ) , ia[ i] + carry - 10 ) ; carry = 1 ; } else { res. insert ( res. begin ( ) , ia[ i] + carry) ; carry = 0 ; } } else if ( i < 0 && j >= 0 ) { if ( ib[ j] + carry > '9' ) { res. insert ( res. begin ( ) , ib[ j] + carry - 10 ) ; carry = 1 ; } else { res. insert ( res. begin ( ) , ib[ j] + carry) ; carry = 0 ; } } else { res. insert ( res. begin ( ) , '1' ) ; carry = 0 ; } } return ;
} int main ( ) { char arra[ 1024 ] ; char arrb[ 1024 ] ; while ( scanf ( "%s%s" , arra, arrb) != EOF ) { string a = arra; string b = arrb; string ia = GetInteger ( a) ; string ib = GetInteger ( b) ; string fa = GetFraction ( a) ; string fb = GetFraction ( b) ; string res; int carry; FractionPlus ( res, carry, fa, fb) ; string ires; IntegerPlus ( ires, carry, ia, ib) ; printf ( "%s.%s\n" , ires. c_str ( ) , res. c_str ( ) ) ; } return 0 ;
}