الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مساعده

مغلق
بدأه alr7al في 28 يونيو 2003 · 10 رد · 797 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

اذا امكن مساعدة في الحل

You are to implement a bigint class. The following operators are to be overloaded:

bigint * bigint

bigint > int

bigint > bigint

bigint = int

bigint = bigint

ostream << bigint

istream >> bigint

bigint--

In addition you may find the following public member functions useful:

getSize //which gives the number of digits in the bigint.

getInts //which returns a copy of the array holding your integer representation.

You should then use your class to write a program that computes factorial (hope you saw this coming). You should prompt the user for a number. Read the number in to a bigint value. Compute and display factorial as follows:

bigint x;

cin >> x;

bigint fact = 0;

for(bigint i = x; i > 0; i--){

fact = fact * i;

}

cout << x "! = " << fact;

Your program should only compute one factorial and exit.

. A bigint object should allow an arbitrary length string to be converted to an bigint value. Your bigint's should not overflow

اتمنى ان احصل على الحل وشكرا

#2

حاول توضح مشكلتك اكثر و ياريت بالعربي

اللهم انا نسألك علما نافعا ونعوذ بك من حماقة الجهلاء
#3

السلام عليكم

في الحقيقة البرنامج يتكون نت headerfile اسمه bigint.h و bigint.cpp

وايضا bigfact.cpp هذا الملف الاساسي

نقوم بكتابة class بحيث ان تكون

bigint * bigint

bigint > int

bigint > bigint

bigint = int

bigint = bigint

ostream << bigint

istream >> bigint

bigint--

overloaded

و نستخدم هذه الاشياء ك member function

getSize //which gives the number of digits in the bigint.

getInts //which returns a copy of the array holding your integer representation

بعد ذلك نقوم بكتابة برنامج باستخدام class يقوم بحساب المضروب

عن طريق اخذ قيمة من المستخدم ثم يقوم البرنامج بقرائتها كقيمة لbigint

ثم يحسب المضروب مثل

bigint x;
    cin >> x;
    bigint fact = 0;
    for(bigint i = x; i > 0; i--){
        fact = fact * i;

    cout << x "! = " << fact;

يقوم البرنامج بحساب المضروب ثم يخرج

البرنامج والclass شبيه بهذا المثال

// HugeInt class definition.
#ifndef HUGEINT1_H
#define HUGEINT1_H

#include 

using std::ostream;

class HugeInt {
   friend ostream &operator<<( ostream &, const HugeInt & );

public:
   HugeInt( long = 0 );      // conversion/default constructor
   HugeInt( const char * );  // conversion constructor

   // addition operator; HugeInt + HugeInt
   HugeInt operator+( const HugeInt & );

   // addition operator; HugeInt + int
   HugeInt operator+( int );            

   // addition operator; 
   // HugeInt + string that represents large integer value
   HugeInt operator+( const char * );    

private:
   short integer[ 30 ];

}; // end class HugeInt

#endif

//  hugeint1.cpp 
// HugeInt member-function and friend-function definitions.

#include       // isdigit function prototype
#include      // strlen function prototype

#include "hugeint1.h"  // HugeInt class definition

// 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 <= 29; i++ )
      integer[ i ] = 0;   

   // place digits of argument into array 
   for ( int j = 29; 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 char *string )
{
   // initialize array to zero
   for ( int i = 0; i <= 29; i++ )
      integer[ i ] = 0;

   // place digits of argument into array
   int length = strlen( string );

   for ( int j = 30 - length, k = 0; j <= 29; j++, k++ )

      if ( isdigit( string[ k ] ) )
         integer[ j ] = string[ k ] - '0';

} // end HugeInt conversion constructor

// addition operator; HugeInt + HugeInt
HugeInt HugeInt::operator+( const HugeInt &op2 )
{
   HugeInt temp;   // temporary result
   int carry = 0;

   for ( int i = 29; 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

      // no carry 
      else
         carry = 0;
   }

   return temp;  // return copy of temporary object

} // end function operator+

// addition operator; HugeInt + int
HugeInt HugeInt::operator+( int op2 )
{ 
   // 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 char *op2 )
{ 
   // convert op2 to a HugeInt, then invoke 
   // operator+ for two HugeInt objects
   return *this + HugeInt( op2 ); 

} // end operator+

// overloaded output operator
ostream& operator<<( ostream &output, const HugeInt &num )
{
   int i;

   for ( i = 0; ( num.integer[ i ] == 0 ) && ( i <= 29 ); i++ )
      ; // skip leading zeros

   if ( i == 30 )
      output << 0;
   else

      for ( ; i <= 29; i++ )
         output << num.integer[ i ];

   return output;

} // end function operator<<

// HugeInt test program.
#include 

using std::cout;
using std::endl;

#include "hugeint1.h"

int main()
{
   HugeInt n1( 7654321 );
   HugeInt n2( 7891234 );
   HugeInt n3( "99999999999999999999999999999" );
   HugeInt n4( "1" );
   HugeInt n5;

   cout << "n1 is " << n1 << "nn2 is " << n2
        << "nn3 is " << n3 << "nn4 is " << n4
        << "nn5 is " << n5 << "nn";

   n5 = n1 + n2;
   cout << n1 << " + " << n2 << " = " << n5 << "nn";

   cout << n3 << " + " << n4 << "n= " << ( n3 + n4 ) 
        << "nn";

   n5 = n1 + 9;
   cout << n1 << " + " << 9 << " = " << n5 << "nn";

   n5 = n2 + "10000";
   cout << n2 << " + " << "10000" << " = " << n5 << endl;

   return 0;

} // end main
#4

:'(:'(:'(:'(:'(:'(:'(:'(:'(

#5

أنا أرى أن المشكلة محلولة في المثال الذي وضعته أنت فاستفد مما كُتب في المثال وسوف أساعد عندما تُواجهك مشكلة, لكن كتابة برنامج كامل فأرجو أن تعذرني لأني مشغول ولا أستطيع التفرغ لبرنامج كامل (f)(f)(f)

#6

شكرا لك اخي الكريم

:):):):):):):):):):)

#7

المساعدة يا اخوان :'(:'(:'(:'(:'(:'(:'(:'(:'(:'(:'(:'(:'(

#8

هذا الذي فهمته ولكن لا اعرف هل صح

class:

#ifndef Bigint_H
#define Bigint_H

#include 

usnig namespace std;

class Bigint {
   friend ostream &operator<<( ostream &, const Bigint & );
   friend istream &operator>>( istream &, const Bigint & );
public:


   Bigint operator*( const Bigint & );
   Bigint operator>( const Bigint & );            
   Bigint operator>( int );
   Bigint operator==( int ); 
   Bigint operator==( const Bigint & );  
   Bigint operator--( const Bigint & )

getSize();
getInts();

}; 

#endif
#10

سوال

اماناتج 0*1*2*3 تساوي صفر

وهذا ماتفعله د1لة المضروب في برنامجك

هيء المتغير ب 1 وليس 0

اللهم انا نسألك علما نافعا ونعوذ بك من حماقة الجهلاء
#11

:'(:'(:'(:'(:'(:'(:'(:'(:'(

هذا الموضوع مغلق.

مواضيع مشابهة