⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 decimal.cpp

📁 《c++大学教程实验指导书》源码
💻 CPP
字号:
// Chapter 8 of C++ How to Program
// Debugging Problem (decimal.cpp)

#include <iostream>

using std::cout;
using std::cin;

#include <cmath>

#include "decimal.h"

// constructor
Decimal::Decimal( double n )
{
   decimal = modf( n, &integer );

} // end class Decimal constructor

// function operator<< definition
friend ostream & operator<<( const Decimal &d )
{
   double n = 0;

   n = floor( d.decimal * 100 );

   if ( n < 0 )
      n = 0 - dec;

   if ( d.decimal != 0 ) {
      output << floor( d.integer ) << ".";

      if ( n > 10 )
         output << n;

      else
         output << "0" << n;

   } // end if 

   else
      output << d.integer;

} // end function operator<<

// function operator>> definition
friend istream operator>>( istream &input, const Decimal &d )
{
   double n;

   cout << "Enter a number: ";
   istream >> n;

   decimal = modf( n, &integer );

   return input;

} // end function operator>>

// function operator= definition
Decimal &Decimal::operator=( const Decimal d )
{
   integer = d.integer;
   decimal = d.decimal;

   return *this;

} // end function operator=

// function setDecimal definition
void Decimal::setDecimal( double d ) 
{
   decimal = d;

} // end function setDecimal

// function setInteger definition
void Decimal::setInteger( double i ) 
{
   integer = i;

} // end function setInteger

// function operator+ definition
Decimal Decimal::operator+( Decimal d )
{
   Decimal result;

   result.setDecimal( decimal + d.decimal );
   result.setInteger( integer + d.integer );

   if ( result.decimal >= 1 ) {
      result.decimal--;
      result.integer++;

   } // end if

   else if ( result.decimal <= -1 ) {
      result.decimal++;
      result.integer--;

   } // end if

   return result;

} // end function operator+

// function operator+= definition
Decimal Decimal::operator+=( Decimal d ) const
{
   *this = *this += d;
   return *this;

} // end function operator+=

// function operator++ definition
Decimal &Decimal::operator++() 
{
   integer++;
   return integer;

} // end function operator++

// function operator++ definition
Decimal Decimal::operator++( double )
{
   Decimal temp = *this;

   integer++;
   return *this;

} // end function operator++

// function operator== definition
bool Decimal::operator==( const Decimal d )
{
   return ( integer == d.integer && decimal == d.decimal );

} // end function operator==



/**************************************************************************
 * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice      *
 * Hall. All Rights Reserved.                                             *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -