C++大学教程

  • Z6_208430
    了解作者
  • 683.7KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-06 08:37
    上传日期
C++大学教程(第二版),ISBN:9787505367272,作者:(美)Harvey M.Deitel,(美)Paul James Deitel著;邱仲潘 等译
习题解答.zip
内容介绍
// 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++课件
      c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件c++课件
    • C++ PRrimer
      本书是久负盛名的C++经典教程,其内容是C++大师Stanley B. Lippman丰富的实践经验和C++标准委员会原负责人Josée Lajoie对C++标准深入理解的完美结合,已经帮助全球无数程序员学会了C++。本版对前一版进行了彻底的...
    • C++
      C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++C++
    • Effective C++
      学习c++的经典书籍!每一个学习c++的人有时间最好看看!你会获得很大收获!
    • c++information
      c++c++c++c++c++c++c++c++c++c++c++c++
    • SourceStyler C++
      用DEV-C++写代码很方便,就是不能格式化有点郁闷 c++格式化的好工具 效率高
    • c++yuyanbiancheng
      这是C和C++集成的编程环境!这是C和C++集成的编程环境!这是C和C++集成的编程环境!这是C和C++集成的编程环境!这是C和C++集成的编程环境!
    • C++ primier
      学习C++的经典之作,希望大家学习愉快,快速掌握C++的各种知识。多下哈
    • effective C++
      有关C++编程方面的检验性介绍,对由C转向C++,和有C++编程基础的程序员有帮助,不过是英文版
    • effective c++
      C++经典书籍Effective C++ By Scott Meyers, 已译成中文,chm格式,方便检阅 Uploaded by save (chaisave@sohu.com)