-
quick question
i read that you guys dont like answering hw questions but ive been stuck on this for a while... im a 2nd semester c++ programmer so i dont know all that much...
im writing a function which takes in a long int.
i want to break up that int into its digits and put them in to my array digits[40] .. could you please tell me the best way to go about doing this..
i want to thank you in advance for any inconvienience.
-
use the function itoa
char *_itoa( int value, char *string, int radix );
value
Number to be converted
string
String result
radix
Base of value; must be in the range 2 – 36
bases are as follows
Binary : 2
Octal : 8
Decimal :10
Hex :16
Code:
#include <stdlib.h>
int main(void)
{
char digits[40];
long thenum = 120;
itoa(thenum,digits,10);
cout << thenum << endl;
return 0;
}
-
im not sure if that will work for what im doing....
im doing a class HugeInteger....
which is in essence creating a new data type that will store 40 digit integers.
the array that im using is digits[40];
there is a call for the function setValue(-2000000000);
the prototype is
void HugeInteger::setValue(long n){
}
what i need to do is take that -200000000
and store the digits into my array as
000000000000000000000000000000-2000000000
will what you said work for this?
-
give me some time to think on this... im not sure off hand how you would implement such a thing.
BTW: i deleted the other posts cause they were ineffective and over complicated
-
um......wow... i have absolutely no idea what you are talkin about so i doubt that is the way my professor intended it to be done...
but, thank you very much for your time nonetheless
-
yeah thats why i figure im missing something...
-
would showing you some code help at all... our professor provided us with three files..
HugeInteger.h
HugeInteger.cpp - used to define the functions
P1B.cpp - the driver which he wrote so it has to do
pretty much exactly what he wants
-
-
here is HugeInteger.h
-----------------------------------------------------------------------------------//Document this file!!!
//Revision 09/09/2001 tsm
// Changed the word "typecast" to "conversion" in the comments associated
// with two of the constructors. This is a terminology correction.
// Also moved the location of a poorly placed comment near the bottom of the file.
// The comment is: //Hint - write two of these, then use them to implement the others
#ifndef HugeInteger_h
#define HugeInteger_h
#include <iostream>
using namespace std;
const int HUGE_INTEGER_MAX_DIGITS = 40;
class HugeInteger{
private:
int digits [HUGE_INTEGER_MAX_DIGITS];
char sign;
public:
HugeInteger(void); //default constructor creates HugeInteger 0
HugeInteger(long); //conversion constructor creates HugeInteger equal to argument
HugeInteger(const char *); //conversion constructor creates HugeInteger equal to argument
//see setValue (below) for a string argument for details - in fact, simply call it.
//In case the argument is illegal, you should set the HugeInteger to zero.
HugeInteger(const HugeInteger &); //copy constructor
bool inputHugeInteger(istream &); //Input from the stream passed as an argument.
//skip leading whitespace and leading zeros.
//A leading + or - sign is optional. Accept digits until a non-digit is
//encountered - leave that character in the stream (do not extract it)
//Watch out for end of file. Return true if a valid number was extracted,
//false otherwise. Watch out for overflow (more than 40 significant digits)
//and return false in this case also (but still extract digits until a non-digit
//is encountered). The number created when any error condition occurs is
//unspecified (anything is OK)
void setValue(long); //set the huge integer to the argument
bool setValue(const char *); //set the huge integer to the value indicated by the string.
//The string may not contain anything other than a sign and digits. Return
//false if it is illegal or too big to store.
void outputHugeInteger(ostream &)const; //Output to the stream passed as the argument
//Print a leading -, but not a leading +. Never print leading zeros.
//Do not print a newline or any spaces. Yes - leave the const in the header -
//its meaning will be explained later.
bool addHugeInteger(const HugeInteger &); //Form the sum of the HugeInteger in
//the base object and the HugeInteger passed as an argument. The sum should
//replace the base integer. Be careful if the signs are different!
//Return true if the answer is legal, Return false if overflow occurs (this
//can only happen if the signs are the same). The answer in case of overflow
//is simply the actual answer mod 10^HUGE_INTEGER_MAX_DIGITS (before adding the sign)
//Think about what happens if you do x.addHugeInteger(x); - be sure this works!
bool subtractHugeInteger(const HugeInteger &); //Form the difference of the HugeInteger in
//the base object and the HugeInteger passed as an argument. (base-parameter).
//Replace the base integer with the answer. You can do this by changing the sign of
//the second number and using the add method (you will have to change the sign
//of a copy since the original is protected as const). Return the result returned by add.
//Think about what happens if you do x.subtractHugeInteger(x); - be sure this works!
bool isEqualTo(const HugeInteger &)const; //True if the two numbers are the same
bool isNotEqualTo(const HugeInteger &)const; //True if the two numbers are NOT the same
//Hint - write two of these, then use them to implement the others
bool isGreaterThan(const HugeInteger &)const; //True if the base > argument
bool isGreaterThanOrEqualTo(const HugeInteger &)const; //True if the base >= argument
bool isLessThan(const HugeInteger &)const; //True if the base < argument
bool isLessThanOrEqualTo(const HugeInteger &)const; //True if the base <= argument
bool isZero(void)const; //True if the base is zero
};
#endif
-------------------------------------------------------------------------
-
here is P1B.cpp
-----------------------------------------------------------------------//Test driver for HugeInteger
//Written by Tim Margush Sept 4, 2001
//
//Revision 09/09/2001 tsm
// Added a clear to the inTemp object before reopening it to reread the file
// The added statenment is: inTemp.clear();
//
//Feel free to add or subtract. Your program may be tested with a different driver.
//Consider the tests here as minimal - your program must do all these things correctly.
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include "HugeInteger.h"
using namespace std;
void printHugeInteger(HugeInteger z);
void compare(const HugeInteger & a, const HugeInteger & b);
void pause(void);
const int FILE_ENTRIES = 10;
const char * FILENAME = "tempHugeInteger.dat";
int main(){
HugeInteger x; //test default constructor
cout << "Default HugeInteger: ["; x.outputHugeInteger(cout); cout << ']' << endl;
x.setValue(-2000000000); //setValue with long argument
cout << "Negative 2 billion: ["; x.outputHugeInteger(cout); cout << ']' << endl;
/* x.setValue("+1234567890123456789012345"); //setValue with string argument
cout << "A large positive HugeInteger: ["; x.outputHugeInteger(cout); cout << ']' << endl;
x.setValue("-9876543210987654321098765432109876543210"); //another string argument
cout << "A 40-digit negative HugeInteger: ["; x.outputHugeInteger(cout); cout << ']' << endl;
//Checking the isZero method
cout << "x is zero (true or false): " << (x.isZero() ? "true":"false") << endl;
cout << "setting x to zero...\n";
x.setValue(int(0));
cout << "x is zero (true or false): " << (x.isZero() ? "true":"false") << endl;
x.setValue("40404040404040403131313131313131");
//Testing copy constructor (used to create a copy of x when argument is passed by value)
printHugeInteger(x);
HugeInteger y = "40404040404040403131313131313131"; //conversion constructor
//Testing the comparison methods
compare(x, y);
x.setValue("40404040404040403131313131313132");
compare(x, y);
x.setValue(-31);
y.setValue(-2);
compare(x, y);
compare(y, x);
//Testing subtraction
x.setValue("12345");
y.setValue("54321");
x.outputHugeInteger(cout);
cout << " - ";
y.outputHugeInteger(cout);
cout << " = ";
cout << (x.subtractHugeInteger(y)?"(success) ":"(fail) ");
x.outputHugeInteger(cout);
cout << endl << "Computing x - x, result is: ";
cout << (x.subtractHugeInteger(x)?"(success) ":"(fail) ");
x.outputHugeInteger(cout);
cout << endl;
//Leading zeros in strings should not be stored - they should be discarded
HugeInteger z ("000000000000000000000000000000000000000000000000 00000000001");
cout << "Testing leading zero removal: ";
z.outputHugeInteger(cout);
cout << "\nTesting leading zero removal (again): ";
x.setValue ("-00000000000000000000000000000000000000000000000000 000000001");
x.outputHugeInteger(cout);
cout << endl;
//Another subtraction
x.outputHugeInteger(cout);
cout << " - ";
y.outputHugeInteger(cout);
cout << " = ";
cout << (x.subtractHugeInteger(y)?"(success) ":"(fail) ");
x.outputHugeInteger(cout);
cout << endl;
pause();
//testing that you detect illegal arithmetic operations
cout << "Testing illegal addition...\n";
x.setValue("99999999999999999999999999999999999999 99");
cout << (x.addHugeInteger( (long)1 ) ? "Oops - Overflow did not occur\n":"Good - Overflow detected\n");
x.outputHugeInteger(cout);
cout << "Testing illegal subtraction...\n";
x.setValue("99999999999999999999999999999999999999 99");
y.setValue("-1");
cout << (y.subtractHugeInteger(x) ? "Oops - Overflow did not occur\n":"Good - Overflow detected\n");
y.outputHugeInteger(cout);
cout << endl;
//Create a file of random integers
ofstream outTemp(FILENAME);
if (!outTemp) cout << "Output file not created" << endl;
else {
srand( (unsigned)time( NULL ) );
for (int i=0; i<FILE_ENTRIES; i++){
x.setValue(rand());
x.outputHugeInteger(outTemp);
x.setValue((unsigned) rand()); //concat more digits
x.outputHugeInteger(outTemp);
x.setValue((unsigned) rand());
x.outputHugeInteger(outTemp);
x.setValue((unsigned) rand());
x.outputHugeInteger(outTemp);
outTemp << endl; //end of number
}
outTemp.close();
//read from the file and compute the sum
ifstream inTemp(FILENAME);
HugeInteger z = (long)0;
if (!inTemp) cout << "Input file not found" << endl;
else {
while (y.inputHugeInteger(inTemp)) {
z.addHugeInteger(y);
}
inTemp.close();
cout << "Sum: ";
z.outputHugeInteger(cout);
cout << endl;
}
//read from the file and reduce previous sums to get 0 (we hope)
inTemp.clear();
inTemp.open(FILENAME);
if (!inTemp) cout << "Input file not found" << endl;
else {
while (y.inputHugeInteger(inTemp)) {
z.subtractHugeInteger(y);
}
inTemp.close();
cout << "Result after subtracing all entries from total: ";
z.outputHugeInteger(cout);
cout << endl;
}
}
//Interactive testing
do {
cout << "Enter a HugeInteger for testing the input method (0 to end)" << endl;
cout << (x.inputHugeInteger(cin) ? "success: " : "fail: ");
x.outputHugeInteger(cout);
cout << endl;
} while (!x.isZero());*/
cout << "\nTest concluded" << endl;
return 0;
}
/*void printHugeInteger(HugeInteger z){ //copy constructor used for argument
cout << "The HugeInteger passed by value is: [";
z.outputHugeInteger(cout);
cout << "]" << endl;
}
void compare(const HugeInteger & a, const HugeInteger & b){
//compare a to b and display true relationships
a.outputHugeInteger(cout);
cout << " :?: ";
b.outputHugeInteger(cout);
cout << " ====> ";
if (a.isEqualTo(b)) cout << " == ";
if (a.isGreaterThan(b)) cout << " > ";
if (a.isGreaterThanOrEqualTo(b)) cout << " >= ";
if (a.isLessThan(b)) cout << " < ";
if (a.isLessThanOrEqualTo(b)) cout << " <= ";
if (a.isNotEqualTo(b)) cout << " != ";
cout << " <====" << endl;
}
void pause(void){
cout << "Press Enter to Continue" << endl;
while (cin.get() != '\n'); //do nothing else
}*/
-------------------------------------------------------------------
alot is commented out due to testing i was doing
-
here is HugeInteger.cpp
basically all i have done is the default constructor, and the output function...
------------------------------------------------------------------------------------
//Document this file!
#include "HugeInteger.h"
#include <iostream>
#include <cmath>
using namespace std;
//Document each method explaining your implementation
HugeInteger::HugeInteger(void){
for (int i=0; i<HUGE_INTEGER_MAX_DIGITS; i++)
digits [i] = 0;
}
void HugeInteger::outputHugeInteger(ostream & f)const{
int firstNonZero, i;
for (i=0;digits[i]==0 && i<HUGE_INTEGER_MAX_DIGITS ; i++)
firstNonZero = i;
if (firstNonZero == HUGE_INTEGER_MAX_DIGITS -1)
f << "0";
else
for (i=firstNonZero +1; i <HUGE_INTEGER_MAX_DIGITS; i++)
f << digits[i];
}
void HugeInteger::setValue(long){
-
so basically he gave us most of the code, we just have to write the function definitions.... im having trouble with the setValue functions as well as the conversion constructor...
again, thank you so much for your time
-
if you want to you can instant message me on aim with specifics
SN : Legend LP 869
-
oh i get it now... hah i though you were actually having to implement a huge int:D whew... you just need to implement the framework. i see...
then the itoa() will convert the long to a char array
look setValue(long l) could be implemented as follows
Code:
#include <stdlib.h>
#include <string.h>
void HugeInteger::setValue(long l)
{
char p[HUGE_INTEGER_MAX_DIGITS];
memset(p,'\0',HUGE_INTEGER_MAX_DIGITS);
itoa(l,p,10);
int nzeros = HUGE_INTEGER_MAX_DIGITS - strlen(p);
memset(digits,'0',HUGE_INTEGER_MAX_DIGITS);
for(int i = nzeros;i < HUGE_INTEGER_MAX_DIGITS ;i++)
{
digits[i] = p[i-nzeros];
}
}
most of the conversions can work this way.
i don't have aim.
-
ummmm.... would you mind explaining that code...
im not familiar with the itoa or memset functions....
you have to understand.. im just beginning my 2nd semester of programing...:)
thank you