// Exercise 11.9 Solution: Hugeint.cpp
// HugeInt member-function and friend-function definitions.
#include <iostream>
#include <cctype> // isdigit function prototype
#include "Hugeint.h" // HugeInt class definition
using namespace std;
// default constructor; conversion constructor that converts
// a long integer into a HugeInt object
HugeInt::HugeInt( long value )
{
// initialize array to zero
for ( int i = 0; i < digits; i++ )
integer[ i ] = 0;
// place digits of argument into array
for ( int j = digits - 1; value != 0 && j >= 0; j-- )
{
integer[ j ] = value % 10;
value /= 10;
} // end for
} // end HugeInt default/conversion constructor
// conversion constructor that converts a character string
// representing a large integer into a HugeInt object
HugeInt::HugeInt( const string &number )
{
// initialize array to zero
for ( int i = 0; i < digits; i++ )
integer[ i ] = 0;
// place digits of argument into array
int length = number.size();
for ( int j = digits - length, k = 0; j < digits; j++, k++ )
if ( isdigit( number[ k ] ) )
integer[ j ] = number[ k ] - '0';
} // end HugeInt conversion constructor
// get function calculates length of integer
int HugeInt::getLength() const
{
int i;
for ( i = 0; i < digits; i++ )
if ( integer[ i ] != 0 )
break; // break when first digit is reached
return digits - i; // length is from first digit (at i) to end of array
} // end function getLength
// addition operator; HugeInt + HugeInt
HugeInt HugeInt::operator+( const HugeInt &op2 ) const
{
HugeInt temp; // temporary result
int carry = 0;
for ( int i = digits - 1; i >= 0; i-- )
{
temp.integer[ i ] = integer[ i ] + op2.integer[ i ] + carry;
// determine whether to carry a 1
if ( temp.integer[ i ] > 9 )
{
temp.integer[ i ] %= 10; // reduce to 0-9
carry = 1;
} // end if
else // no carry
carry = 0;
} // end for
return temp; // return copy of temporary object
} // end function operator+
// addition operator; HugeInt + int
HugeInt HugeInt::operator+( int op2 ) const
{
// convert op2 to a HugeInt, then invoke
// operator+ for two HugeInt objects
return *this + HugeInt( op2 );
} // end function operator+
// addition operator;
// HugeInt + string that represents large integer value
HugeInt HugeInt::operator+( const string &op2 ) const
{
// convert op2 to a HugeInt, then invoke
// operator+ for two HugeInt objects
return *this + HugeInt( op2 );
} // end function operator+
// equality operator; HugeInt == HugeInt
bool HugeInt::operator==( const HugeInt &op2 ) const
{
for ( int i = 0; i < digits; i++ ) // compare each element
if ( op2.integer[ i ] != integer[ i ] )
return false; // return false if mismatch found
return true; // all elements match, return true
} // end function operator==
// inequality operator; HugeInt != HugeInt
bool HugeInt::operator!=( const HugeInt &op2 ) const
{
return !( *this == op2 ); // return opposite of ==
} // end function operator!=
// less than operator; HugeInt < HugeInt
bool HugeInt::operator<( const HugeInt &op2 ) const
{
for ( int i = 0; i < digits; i++ ) // compare each element
{
if ( integer[ i ] == op2.integer[ i ] )
continue; // test next element
else if ( integer[ i ] > op2.integer[ i ] )
return false; // first element larger
else
return true; // first element smaller
} // end for
return false; // if reached this point, objects are equal
} // end function operator<
// less than or equal operator; HugeInt <= HugeInt
bool HugeInt::operator<=( const HugeInt &op2 ) const
{
return !( *this > op2 );
} // end function operator<=
// greater than operator; HugeInt > HugeInt
bool HugeInt::operator>( const HugeInt &op2 ) const
{
return op2 < *this;
} // end function operator>
// greater than or equal operator; HugeInt >= HugeInt
bool HugeInt::operator>=( const HugeInt &op2 ) const
{
return !( *this < op2 );
} // end function operator>=
// overloaded output operator
ostream& operator<<( ostream &output, const HugeInt &num )
{
int i;
for ( i = 0; i < HugeInt::digits && num.integer[ i ] == 0; i++ )
; // skip leading zeros
if ( i == HugeInt::digits )
output << 0;
else
for ( ; i < HugeInt::digits; i++ )
output << num.integer[ i ];
return output;
} // end function operator<<
// subtraction operator, subtract op2 from (*this)
HugeInt HugeInt::operator-( const HugeInt &op2 ) const
{
// return if first value is smaller; we are not handling negatives
if ( op2 > *this )
{
cout << "Error: Tried to subtract larger value from smaller value."
<< endl;
return HugeInt( "0" );
} // end if
HugeInt result( "0" ); // final result currently 0
// used to determine if previous digit had 10 added to it;
// if true, current digit needs to be reduced by 1
bool minusOne = false;
// for each digit in both arrays,
// subtract digit of smaller int from digit of larger int
for ( int i = digits - 1; i >= 0; i-- )
{
// find digits we will currently be subtracting
int topValue = this->integer[ i ];
int bottomValue = op2.integer[ i ];
// if previous topValue had 10 added to it;
// subtract one from current topValue
if ( minusOne )
{
if ( topValue == 0 ) // topValue cannot become -1
// set to 9 but keep minusOne true for next digit
topValue = 9;
else
{
topValue -= 1; // subtract from top value
minusOne = false; // minusOne is handled, return to false
} // end else
} // end outer if
if ( topValue >= bottomValue )
// if topValue larger, perform subtraction and store result
result.integer[ i ] = topValue - bottomValue;
else
{
topValue += 10; // if bottomValue larger, add 10 to topValue
minusOne = true; // next digit must be decreased
// topValue is now larger, perform subtraction and store result
result.integer[ i ] = topValue - bottomValue;
} // end else
} // end for
return result; // return final result
} // end function operator-
// multiplication operator; multiply op2 with (*this)
HugeInt HugeInt::operator*( const HugeInt &op2 ) const
{
int carryOver = 0; // carry value when previous digits are multiplied
HugeInt total( "0" ); // result currently 0
// find the smaller int
HugeInt smaller = ( *this < op2 ) ? *this : op2;
HugeInt larger = ( *this > op2 ) ? *this : op2;
// determine index of larger's first digit; used to determine
// when to stop multiplying
int x;
for ( x = 0; ( x < digits ) && ( larger.integer[ x ] == 0 ); x++ )
;
int indexOfFirstDigitForLarger = x;
// for each digit in smaller, multiply by each digit in larger
for ( int i = digits; i > digits - smaller.getLength(); i-- )
{
// currentInt stores result of current digit in
// smaller multiplied by digits in larger
HugeInt currentInt( "0" );
// currentIntFrontHandle used to keep track of
// index of first digit in currentInt
int currentIntFrontHandle = i - 1;
// multiply each digit in larger with the current digit in smaller;
// go backward from last digit in larger to first digit
for ( int j = digits; j > digits - larger.getLength(); j-- )
{
// perform multiplication;
// add carryOver from previous multiplications
int currentResult = carryOver +
( larger.integer[ j - 1 ] * smaller.integer[ i - 1 ] );
没有合适的资源?快使用搜索试试~ 我知道了~
C++大学教程(第七版)].(美)戴特尔课后习题答案
共540个文件
cpp:393个
h:127个
dat:10个
需积分: 9 33 下载量 118 浏览量
2018-09-23
21:04:07
上传
评论 4
收藏 688KB RAR 举报
温馨提示
C++大学教程(第七版)]. (美)戴特尔 课后习题答案, txt文本
资源推荐
资源详情
资源评论
收起资源包目录
C++大学教程(第七版)].(美)戴特尔课后习题答案 (540个子文件)
List.h.bak 11KB
Ex21_26.cpp.bak 3KB
Listnode.h.bak 2KB
Ex23_09.cpp.bak 2KB
Ex23_04.cpp.bak 2KB
Ex23_04.cpp.bak 2KB
Ex23_07.cpp.bak 1KB
Hugeint.cpp 12KB
ex17_12.cpp 10KB
Polynomial.cpp 9KB
ex08_19.cpp 9KB
HugeInteger.cpp 8KB
Date.cpp 7KB
DoubleSubscriptedArray.cpp 7KB
Ex08_17.cpp 7KB
Ex06_60.cpp 6KB
Ex07_27.cpp 6KB
Ex06_49.cpp 6KB
ex05_22.cpp 6KB
GradeBook.cpp 6KB
Ex07_28.cpp 6KB
Ex07_38.cpp 6KB
Ex07_41.cpp 6KB
Ex08_16.cpp 6KB
Package.cpp 5KB
Package.cpp 5KB
ex17_10.cpp 5KB
Ex07_23.cpp 5KB
Ex18_11.cpp 5KB
Ex06_47.cpp 5KB
Ex06_59.cpp 5KB
Ex07_24.cpp 5KB
DateAndTime.cpp 5KB
ex17_07.cpp 5KB
Ex08_38.cpp 5KB
Ex06_58.cpp 5KB
Rectangle.cpp 5KB
Ex07_24.cpp 5KB
Ex14_04.cpp 5KB
Ex18_28.cpp 5KB
Ex21_12.cpp 5KB
RationalNumber.cpp 5KB
Ex07_25.cpp 5KB
BasePlusCommissionEmployee.cpp 4KB
Ex07_30.cpp 4KB
Ex07_25.cpp 4KB
BucketSort.cpp 4KB
ex05_30.cpp 4KB
Date.cpp 4KB
ex05_21.cpp 4KB
ex13_12.cpp 4KB
Hand.cpp 4KB
TicTacToe.cpp 4KB
Ex07_20.cpp 4KB
ex21_31.cpp 4KB
Ex09_14.cpp 4KB
BinarySearch.cpp 4KB
ex17_15.cpp 4KB
IntegerSet.cpp 4KB
Ex07_19.cpp 4KB
ex17_13.cpp 4KB
Ex19_10.cpp 4KB
CommissionEmployee.cpp 4KB
Ex14_06.cpp 4KB
Ex08_39.cpp 4KB
ex11_07.cpp 4KB
Ex08_15.cpp 4KB
ex12_09.cpp 4KB
Rational.cpp 4KB
Time.cpp 4KB
PhoneNumber.cpp 4KB
Ex18_13.cpp 4KB
Time.cpp 3KB
Ex08_12.cpp 3KB
Invoice.cpp 3KB
Ex06_35.cpp 3KB
Ex06_57.cpp 3KB
Ex22_13.cpp 3KB
ex12_10.cpp 3KB
Complex.cpp 3KB
Ex06_14.cpp 3KB
ex05_14.cpp 3KB
Ex22_14.cpp 3KB
Time.cpp 3KB
ex17_08.cpp 3KB
ex05_28.cpp 3KB
Ex14_03.cpp 3KB
Ex21_07.cpp 3KB
Rectangle.cpp 3KB
ex13_16.cpp 3KB
Ex21_13.cpp 3KB
Ex06_12.cpp 3KB
ex11_10.cpp 3KB
Ex09_06.cpp 3KB
ex13_15.cpp 3KB
ex03_17.cpp 3KB
Ex06_26.cpp 3KB
Date.cpp 3KB
Complex.cpp 3KB
Employee.cpp 3KB
共 540 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
JEFYCG
- 粉丝: 1
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功