Patricia Shanahan said:
Infix arithmetic operator version: (n * (2*a1 + (n-1)*d))/2.
This version is a very simple transformation of the mathematical notation.
Anyone up to writing this out in prefix method call notation?
On can always use a program to transform an infix expression like
BigDecimal( "2" )+ BigDecimal( "3" )* BigDecimal( "4" )
to a prefix expression like
( new java.math.BigDecimal( "2" ) ).add( ( new java.math.BigDecimal( "3" ) ).multiply( new java.math.BigDecimal( "4" ) ) )
with a program like
#include <string>
#include <iostream>
#include <ostream>
class BigDecimal;
::std:
stream& operator<<( ::std:
stream& os, const BigDecimal& number );
class BigDecimal
{ ::std::string const value_;
BigDecimal const * const left_;
BigDecimal const * const right_;
public:
explicit BigDecimal( ::std::string const value ):
value_( value ), left_( 0 ), right_( 0 ){}
explicit BigDecimal
( ::std::string const op,
BigDecimal const * const left,
BigDecimal const * const right ):
value_( op ), left_( left ), right_( right ){}
::std:
stream& Print( ::std:
stream& os ) const
{ if( value_ == "+" )
return os << "( " << *left_ << " ).add( " << *right_ << " )";
else if( value_ == "*" )
return os << "( " << *left_ << " ).multiply( " << *right_ << " )";
else
return os << "new java.math.BigDecimal( \"" << value_ << "\" )"; }};
const BigDecimal operator+
( const BigDecimal left, const BigDecimal right )
{ BigDecimal temp( "+", &left, &right );
return temp; }
const BigDecimal operator*
( const BigDecimal left, const BigDecimal right )
{ BigDecimal temp( "*", &left, &right );
return temp; }
::std:
stream& operator<<( ::std:
stream& os, const BigDecimal& number )
{ return number.Print( os ); }
int main()
{ ::std::cout << BigDecimal( "2" )+ BigDecimal( "3" )* BigDecimal( "4" )<< '\n'; }
.
Disclaimer: I do not use C++ that often, and therefore I am not
sure if I got the lifetime of all temporaries right.