Jump to content
Sign in to follow this  
tuan_oxford

Coding

Recommended Posts

saya di beri tugasan untuk buat coding ni...

ada sape2 leh bantu x????

A machine with 32-bit integers can represent integers in the range of approximately 2 billion to +2 billion. This fixed-size restriction is rarely troublesome, but there are applications in which we would like to be able to use a much wider range of integers, such as factorial. This is what C++ was built to do, namely, create powerful new data types.

Create class HugeInt that is capable of representing positive (non-negative) 30-digit decimal values (this will be able to represent values from 0 to 1030). The 30-digit number can be represented internally as array of integer whereby each digit takes values from zero to nine. The class must have conversion constructor that convert int or char value to HugeInt. Then overload the following operators

a. Stream insertion (<<) and extraction (>>) operators

b. Arithmetic operators (+,-,/,*). Each operation must also support operation with mix operands (integer and HugeInt objects)

c. Relational operators (==,<=,>=,!=).Each operation must also support operation with mix operands (integer and HugeInt objects)

d. Define a function factorial(X) that calculate factorial of X, whereby X is a HugeInt object.*

The program must follow good programming practices such as information hiding, principle of least privilege, etc.

Your program must compile successfully for the following driver program.

int main()

{

HugeInt a; //HugeInt object initialized to zero

HugeInt b(12345);

HugeInt c(“100200101002005550â€);

HugeInt result;

cin >> a;

result = a+b;

cout << a << “+†<< b << “ = “ << result << endl;

result = c – b;

cout << result << endl;

result = c / b;

cout << result << endl;

result = c * b;

cout << result << endl;

if (a == B)

cout << “Equal†<< endl;

else

cout << “Not Equal†<< endl;

if (a >= B)

cout << “Greater†<< endl;

else

cout << “Less†<< endl;

if (a <= B)

cout << “Less†<< endl;

else

cout << “Greater†<< endl;

if (a != B)

cout << “Not Equal†<< endl;

else

cout << “Equal†<< endl;

factorial(B); //will output the result of b factorial

return 0;

}

Share this post


Link to post
Share on other sites

Lecturer awak memang best sebab bagi soalan esenmen yang mencabar.

Untuk selesaikan masalah tersebut, pastikan awak reti pasal teori nombor sikit2. Sebabnya awak kena memanipulasi register 32bit utk mengira nombor yang lebih besar. Lepas tu kena reti la utk merekabentuk Class dan tau macam mana nak guna operator overloading.

Hint: http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic

Share this post


Link to post
Share on other sites

Lecturer awak memang best sebab bagi soalan esenmen yang mencabar.

Untuk selesaikan masalah tersebut, pastikan awak reti pasal teori nombor sikit2. Sebabnya awak kena memanipulasi register 32bit utk mengira nombor yang lebih besar. Lepas tu kena reti la utk merekabentuk Class dan tau macam mana nak guna operator overloading.

Hint: http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic

ni coding yang saya buat tapi ada masalah la.x paham

#include <iostream>

#include <cctype>

#include <cstring>

using namespace std;

class HugeInt

{

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

friend istream &operator>>(istream &, const HugeInt &);

friend void factorial(HugeInt X);

public :

HugeInt(long = 0); //default constructor

HugeInt(const char *); //conversion constructor

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

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

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

HugeInt operator- (const HugeInt & ) const; //subtraction operator; HugeInt - HugeInt

HugeInt operator- (int) const; //subtraction operator; HugeInt - int

HugeInt operator- (const char *) const; //subtraction operator;HugeInt - string that represent large integer value

HugeInt operator* (const HugeInt & ) const; //multiplication operator; HugeInt * HugeInt

HugeInt operator* (int) const; //multiplication operator; HugeInt * int

HugeInt operator* (const char *) const; //multiplication operator;HugeInt * string that represent large integer value

HugeInt operator/ (const HugeInt & ) const; //division operator; HugeInt / HugeInt

HugeInt operator/ (int) const; //division operator; HugeInt / int

HugeInt operator/ (const char *) const; //division operator;HugeInt / string that represent large integer value

bool operator==(HugeInt &);

bool operator==(char*);

bool operator==(int);

bool operator<=(HugeInt &);

bool operator<=(char*);

bool operator<=(int);

bool operator>=(HugeInt &);

bool operator>=(char*);

bool operator>=(int);

bool operator!=(HugeInt &);

bool operator!=(char*);

bool operator!=(int);

private:

short integer[30];

};

//converts a long integer into a HugeInt object

HugeInt::HugeInt (long value)

{

for(int i = 0; i <= 29; i++)

integer = 0;

for(int j = 29; value != 0 && j >= 0; j--)

{

integer[j] = value % 10;

value /= 10;

}

}

//converts a character string into a HugeInt object

HugeInt::HugeInt(const char *string)

{

for(int i = 0; i <= 29; i++)

integer = 0;

int length = strlen(string);

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

if(isdigit(string[k]))

integer[j] = string[k] - '0';

}

//addition operator; HugeInt + HugeInt

HugeInt HugeInt::operator +(const HugeInt &op2) const

{

HugeInt temp;

int carry = 0;

for(int i = 29; i >= 0; i--)

{

temp.integer = integer + op2.integer + carry;

if(temp.integer > 9)

{

temp.integer %= 10; //reduce to 0 - 9

carry = 1;

}

else

carry = 0;

}

return temp;

}

//addition operator; HugeInt + int

HugeInt HugeInt::operator +(int op2) const

{

return *this + HugeInt (op2);

}

//addition operator; HugeInt + string

HugeInt HugeInt::operator+( const char *op2) const

{

return *this + HugeInt( op2 );

}

//subtraction operator; HugeInt - HugeInt

HugeInt HugeInt::operator -(const HugeInt &op2) const

{

HugeInt temp;

HugeInt val;

for(int i = 29; i >= 0; i--)

val.integer = integer;

for(i = 29; i >= 0; i--)

{

if(val.integer - op2.integer < 0)

{

val.integer +=10;

temp.integer = val.integer - op2.integer ;

val.integer[i-1] -= 1;

}

else

{

temp.integer = integer - op2.integer;

}

}

return temp;

}

//addition operator; HugeInt - int

HugeInt HugeInt::operator -(int op2) const

{

return *this - HugeInt (op2);

}

//addition operator; HugeInt - string

HugeInt HugeInt::operator-( const char *op2) const

{

return *this - HugeInt( op2 );

}

//multiplication operator; HugeInt * HugeInt

HugeInt HugeInt::operator* ( const HugeInt &num ) const

{

HugeInt temp;

int Offset = 0;

HugeInt subTotal;

for (int i=10;i>=0;--i)

{

for (int j=10;j>=0;--j)

{

int nCarry = 0;

int nProd = integer[j] * num.integer;

if (nProd > 9)

{

nCarry += nProd / 10;

nProd -= nCarry * 10;

}

subTotal.integer[j-Offset] += nProd;

subTotal.integer[j-Offset-1] += nCarry;

if (subTotal.integer[j-Offset] > 9)

{

subTotal.integer[j-Offset] -= 10;

subTotal.integer[j-Offset-1] += 1;

}

if (num.integer[j] != 0)

{

temp.integer[j-Offset] += subTotal.integer[j-Offset];

}

Offset++;

}

}

return temp;

}

//multiplication operator; HugeInt * int

HugeInt HugeInt::operator*(int op2) const

{

return *this * HugeInt (op2);

}

//multiplication operator; HugeInt * string

HugeInt HugeInt::operator*( const char *op2) const

{

return *this * HugeInt( op2 );

}

bool HugeInt::operator==(HugeInt &h1)

{

HugeInt temp;

int i=29;

while(i>=0)

{

if(temp.integer==h1.integer)

i--;

else

return false;

}

return true;

}

bool HugeInt::operator<=(HugeInt &h1)

{

HugeInt temp;

int i=29;

while(i>=0)

{

if(temp.integer<=h1.integer)

i--;

else

return false;

}

return true;

}

bool HugeInt::operator>=(HugeInt &h1)

{

HugeInt temp;

int i=29;

while(i>=0)

{

if(temp.integer>=h1.integer)

i--;

else

return false;

}

return true;

}

bool HugeInt::operator!=(HugeInt &h1)

{

HugeInt temp;

int i=29;

while(i>=0)

{

if(temp.integer!=h1.integer)

i--;

else

return false;

}

return true;

}

//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;

}

//overloaded input operator

istream& operator>>(istream &input, const HugeInt &H)

{

for(int i=0;i<30;i++)

{

input >> H.integer;

}

return input;

}

void factorial(HugeInt X)

{

HugeInt result;

HugeInt temp(2);

while(temp != X)

{

result = result + X * temp;

temp = temp + 1;

}

cout << result << endl;

}

int main()

{

HugeInt a; //HugeInt object initialized to zero

HugeInt b(12345);

HugeInt c("100200101002005550");

HugeInt result;

cin >> a;

result = a + b;

cout << a << "+" << b << " = " << result << endl;

result = c - b;

cout << result << endl;

result = c / b;

cout << result << endl;

result = c * b;

cout << result << endl;

if (a == B)

cout << "Equal" << endl;

else

cout << "Not Equal" << endl;

if (a >= B)

cout << "Greater" << endl;

else

cout << "Less" << endl;

if (a <= B)

cout << "Less" << endl;

else

cout << "Greater" << endl;

if (a != B)

cout << "Not Equal" << endl;

else

cout << "Equal" << endl;

factorial(B); //will output the result of b factorial

return 0;

}

ni error dia:

pada line 121:using obsolete binding at ‘i’.

pada line 124 pulak, error is name lookup of ‘i’ changed for new ISO for scoping.

tolong....

Share this post


Link to post
Share on other sites

ni error dia:

pada line 121:using obsolete binding at ‘i’.

pada line 124 pulak, error is name lookup of ‘i’ changed for new ISO for scoping.

tolong....

i doubt u wrote this because the error quite apparent. please u proper compiler, eg. the latest visual c++ compiler

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...