string - c++- a very long integer promblem -
this question has answer here:
- how implement big int in c++ 16 answers
i'm trying write program gets long number @ first keep string , supposed perform simple operators not know how i'm supposed store such long number these things
here's did, header:
class biginteger { private: string number; public: biginteger(); biginteger(int); biginteger(const char*); biginteger(const biginteger&); biginteger(const biginteger&&); //~biginteger(); void setvalue(int); void setvalue(const char*); //void setvalue(const biginteger&); string getnum(); static biginteger fromstring(const char*); const biginteger operator+(const biginteger&) const; const biginteger operator-(const biginteger&) const; const biginteger operator*(const biginteger&) const; const biginteger operator/(const biginteger&) const; }
main:
#include "biginteger.h" using namespace std; int main() { biginteger a("2837456897658923563425345"); biginteger b("23784623874623874682736478236"); biginteger c = + b; c /= "4237467864237846"; biginteger d = * b - c; }
thats main , how run, in implementation of functions, managed constractors work, dont know how build operators work because there no type can contain long numbers... can do?
just keep string , use schemes addition, subtraction, multiplication , division taught @ school. remember adding , multiplying numbers while 1 of them written under other one? have implement these algorithms process number digit digit process string number char char.
there more clever algorithms e.g. multiplication if search net alternatives, methods know school do.
see wikipedia article summarizes these basic methods:
https://en.wikipedia.org/wiki/elementary_arithmetic
edit:
addition example requested: let's want add 2 numbers converted strings , write result variable called std::string res
. if have 2 numbers converted string, e.g. "10"
, "18"
, , want add them, take last char of each of strings (i.e. rightmost digits of numbers - 0 , 8), add 0 + 8 , write last char of result string (i.e. rightmost digit), same next digits, i.e. 1 + 1, , write next digit. res[0] == 2
, res[1] == 8
, resulting string "28"
.
refer old school algorithms learn if numbers have different number of digits or result of digit addition greater 9.
Comments
Post a Comment