Hello all,

Firstly let me say yes, i have searched the forums, secondly No! i am not asking for you to do my homework, i realize how much you all hate that =P

I am here for some guidance, i have exhausted my researching capabilities and am at the point of getting nowhere, yes i am a noob to this topic, however, i need to get it done, and i am acing the rest bar this section.

The goal is to build a calculator capable of inputing and outputting large integers (up to 20+ characters) I know there are a lot of different threads on this but they are either extremely complex or do not follow my scope.

I need to use arrays
I need to build a Bigint class
I need to do + / - and *
I need to make my own operators
I need Bigint.cpp and Bigint.h files.

I am only in the process of creating the basic files, not much actual code yet. I have not worked with c++ class code before, which is why i'm having trouble.

Questions i am asking:
1. Is my main method legal? can i create Bigint variables and call to them just as i have in my code? assuming i have created the operators << and >> for them?

2. I am definitely missing something in my class of Bigint, are these array declarations allowed?

3. Can i be pointed in the right direction of creating the Bigint constructor?

4. My main problem is with the << and >> operations, totally stuck here!!!

5. With the << >> operations successful, is it plausible that my getInt function, which is called as part of constructing Bigint, that it will gather the input and place them into an array such that:
a[0 0 0 0 0 0 0 0 0 9 8 7 6 5 4 3 2 1 0]?

Bigint.cpp

using namespace std;

int main(void){

    Bigint x, y;
    char op;
    
    cout << "Value x, y and operation?: ";
    cin >> x >> y >> op; //This will work when I write the code for the >> operator.

    switch(op){
        case '+':
            cout << "Result:\n" << x + y << "\n"; //These should work as I write the operators for + - / *.
            break;
        case '-':
            cout << "Result:\n" << x - y << "\n";
            break;
        case '/':
            cout << "Result:\n" << x / y << "\n";
            break;
        case '*':
            cout << "Result:\n" << x * y << "\n";
            break;
    }
    return(0);
}

Bigint.h file

using namespace std;

//Begin class
class Bigint {
    friend ostream& operator>> (std::ostream& in, Bigint& x);
    friend ostream& operator<< (std::ostream& out, const Bigint& x);
    friend Bigint operator+(const Bigint& x, const Bigint& y);
    friend Bigint operator-(const Bigint& x, const Bigint& y);
    friend Bigint operator/(const Bigint& x, const Bigint& y);
    friend Bigint operator*(const Bigint& x, const Bigint& y);

public:
    Bigint(long = 0);
    ~Bigint();
    void setInt(unsigned long long int);
    void getInt(unsigned long long int);

private:
    Bigint a[256];
    Bigint b[256];
    Bigint c[256];
//Need some variable array here?

};
//End class


//Begin constructor
Bigint::Bigint(long value) {
    getInt(value);
}

Bigint::~Bigint() {

}

void Bigint::getInt(unsigned long long int value){

    //How do i do this? do i need the in/out operators done first?
    //How would i test the code?
}
//End constructor


//Begin in/out operators
ostream &operator>>(std::ostream &input, const Bigint &x)
{

    //How do i do this?


    return input;
}

ostream &operator<<(std::ostream &output, const Bigint &x)
{


    //How do i do this?


    return output;
}
//End in/out operators


//Begin math operators
Bigint operator+(const Bigint &x, const Bigint &y) {

//    Make several variables to hold values
//    Make a temp array and carry array
//    Make a variable for the result
//
//    Begin writing psuedo code
    
}

Bigint operator*(const Bigint &x, const Bigint &y) {

//    Make several variables to hold values
//    Make a temp array and carry array
//    Make a variable for the result
//
//    Begin writing psuedo code

}

Bigint operator-(const Bigint &x, const Bigint &y) {

//    Make several variables to hold values
//    Make a temp array and carry array
//    Make a variable for the result
//
//    Begin writing psuedo code

}

Bigint operator/(const Bigint &x, const Bigint &y) {

//    Make several variables to hold values
//    Make a temp array and carry array
//    Make a variable for the result
//
//    Begin writing psuedo code

}
//End Math operators


#endif	/* _BIGINT_H */

Thank you for your replies it is much appreciated. I will be fine writing the calculator itself, its more getting the values into the array that is baffling me!!!

cheers,

Zea

Take it a step at a time.

Initially, forget the class itself.

How would you represent a Bigint value? As a long? As an array? What makes the most sense for 20 digits?
Conceptually, how would you do an operation on this value?
Now write an input and output function to test this design idea (with no operations, just I/O). Compile and test it. Does it work? Does it make sense?

Once that's done, how would you add two values? Write it and test it.

If that works, now write your class with these tests in mind. Input and Output first. Test. When it works, drop in the ADD operator. Test.

Continue from there...

I get you, so write the code ignoring the Bigint class yes?
Well the input should be placed into an array/string/vector then operations will be done on the indexes, this i understand and can probably write with ease.

However, i am not good with writing my own c++ classes (obviously), so im confused with getting 'input passed in' to become 'Bigint type' ... hmmmm

Thank you for your reply :)

There is an error to do with my >> operators in either:
a) class itself
b) constructor
c) >> operator function

Error:

//SHOULD be placing the value passed to the Bigint(num) string in reverse
    //as per the >> operator in Bigint.h (This is where my errors are recurring
    //well the FIRST error) cant compile any further.
    // -- error: no match for 'operator>>' in 'std::cin >> x' --
    cin >> x >> y >> op;

Hopefully som1 may assist, I have added comments to make the code a little more 'graceful'.

/* 
 * File:   Bigint.h
 * Author: Ollie
 *
 * Created on 26 March 2010, 3:58 PM
 */

#ifndef _BIGINT_H
#define	_BIGINT_H

#include <iostream>
#include <cctype>
#include <string>
#include "Bigint.h"
#include <vector>
#include <sstream>
using namespace std;

//////////////////////////////////////////////////////////////////////
//Begin class Bigint
//////////////////////////////////////////////////////////////////////
class Bigint {
    friend ostream& operator>> (std::ostream& in, Bigint& x);
    friend ostream& operator<< (std::ostream& out, const Bigint& x);
    friend Bigint operator+(const Bigint& x, const Bigint& y);
    friend Bigint operator-(const Bigint& x, const Bigint& y);
    friend Bigint operator/(const Bigint& x, const Bigint& y);
    friend Bigint operator*(const Bigint& x, const Bigint& y);


public:
    //Default constructor
    Bigint();
    //The constructors we want
    Bigint(const Bigint &x);
    Bigint(string str);

private:
    string num; // <-------- Hold all Bigint's
};
//End class

//////////////////////////////////////////////////////////////////////
//Begin constructor
//////////////////////////////////////////////////////////////////////
Bigint::Bigint(){

    this->num;
}

Bigint::Bigint(string str){
    for(int i = 0; i < str.length(); i++){
        num.push_back((str[i]));
    }
}

Bigint::Bigint(const Bigint& x){
////////////////////////////////////////////////////////
    //Something major missing here, this should be the initial constructor
    //for an unsigned Bigint variable x? I think!!
    //as in Bigint x, y; in Bigint.cpp
    
    this->num = "";


}
//End constructor



//////////////////////////////////////////////////////////////////////
//Begin << and >> operators
//////////////////////////////////////////////////////////////////////
ostream &operator>>(std::ostream &input, Bigint &x)//Inputs the string backwards to be calculated on.
{
    long i = 0;
    for(i = x.num.size(); i > 0; --i){
        input << x.num[i-1];
    }
    return input;
}

//Something very wrong here? or possibly in the class itself.
ostream &operator<<(ostream &output, const Bigint &x)//Outputs the string backwards (correctly).
{
	long i = 0;
	for(i = x.num.size();i > 0; --i){
		output << x.num[i-1];
	}
	return output;
}
//End in/out operators



//////////////////////////////////////////////////////////////////////
//Begin arithmetric operators
//////////////////////////////////////////////////////////////////////
Bigint operator+(const Bigint &x, const Bigint &y) {

    //still to do (testing)
    return x;
}

Bigint operator*(const Bigint &x, const Bigint &y) {

    return x;
}

Bigint operator-(const Bigint &x, const Bigint &y) {

    return y;
}

Bigint operator/(const Bigint &x, const Bigint &y) {

    return y;
}
//End Math operators


#endif	/* _BIGINT_H */

Any help is appreciated.

All line numbers refer to post #4.

Bigint::Bigint()

Is the default constructor. I'd use it to to set num to be an empty string. Line 63 is an example of how to do that.

As it stands now:

Bigint::Bigint(string str){    
   for(int i = 0; i < str.length(); i++)
    {        
        num.push_back((str[i]));

This is a constructor to convert a string into a Bigint. Since num is a string, just assign str to num.

Bigint::Bigint(const Bigint& x){

This is a copy constructor. Just assign x.num to num. Since num is declared with private access you will need a public accessor function to place x.num into num.

However, you don't have to decide how you want to do things as regards to num. Will it hold the Bigint with lowest value digit to the right, as we routinely write numerals, or will it have the lowest value to the left. Given one desireable trait in any code is a user friendly interface I would suggest that you have num be with lowest value digit to right. Internally then you would need to reverser num or plan to do your manipulations starting at the highest index of num and work backward. I believer there is a string method that will reverse STL strings for you, if you want. Otherwise, then for loop on line 87 will reverse it.

Now to the >> operator. Have user input the digits in Bigint using the routine syntax with lowest value digit to the right. It could go directly into num or into a temporary holding string and reversed before putting it into num. Your call. But I encourage you to be user friendly and internally consistent.

commented: This member was very helpful! +0

Thank you for taking the time to explain this, it was very helpful.

I thought that my Bigint::Bigint(string str) constructor IS taking str (the string passed in) and placing it in num, and in reverse? How would i make the string a set length, say 30 char's long, fill the string from back to front then fill the rest with zero's? is this done here or in my << operator?

With Bigint::Bigint(const Bigint& x) would i just use a command like:
x.num = num; or do i need to do something special here? You mentioned it being private, but its still a part of Bigint?

The way i think i understand it:
When you have say Bigint x, y; in your main function.
That will call the default Bigint::Bigint(const Bigint&x) to make an instance of num, just an empty string.
Then when u call cin << x; in the main function, that will call:
Bigint::Bigint(string str); passing the input given, so it will place the given value into a string backwards.? Am i on the right track here? or do i do all this in my << operator?

Bleh!

Again thank you i'm slowely getting there and understand a bit more as i go. Much appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.