[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


/* I want to overload (+,-,%,*,/) to the class mult*/ ...

<< Back to: comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)

Question by kimo3333
Submitted on 6/14/2004
Related FAQ: comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)
Rating: Rate this question: Vote
/* I want to overload (+,-,%,*,/) to the class mult*/
#include<iostream.h>
#include<conio.h>
#include<string.h>

#define TRUE 1
#define FALSE 0

#define NEXT TRUE
#define LAST FALSE

char *chstr(char ,char*);

class digit;
typedef unsigned char byte;
typedef digit *dgtp;

class digit
{
friend class mult;
friend class multscan;
private:
     static int radix;//to be used by the all classes
     byte dgt;
     dgtp next;
     dgtp last;

public:
     digit (int);
     friend ostream  &operator<<(ostream &s, const digit &d);//overloading of iostream to be used with the class/
     friend istream  &operator>>(istream &s,  digit &d);
};
int digit ::radix=256;

digit::digit(int i)
{
        dgt =(byte)i ;
        next=last=0 ;
}

  ostream &operator<<(ostream &s, const digit &d)
        {
        s << int(d.dgt);
        return s;
        }
        istream &operator>>(istream &s, digit &d)
        {   int i;
           s >> i;
          if (i<0 ||i >= digit::radix)
            //"enter a byte in op>>(os&,dig&)");
            d.dgt = (byte)i ;
            return s;
        };
    const int POSV=+1;
    const int NEGV=-1;
  class mult
          {
          friend class multscan;
          private:
                    int sgn;
                    dgtp head;
                    dgtp tail;
                    int numdgt;
              void  clearmult();
         public:
                    mult ()   {sgn=POSV;head= tail=0;numdgt=0;}
                    mult (int);
                    mult(mult&);
                    ~mult()   {clearmult(); }  ;
         void setsgn (char);
         char getsign ();
         void chsgn();
         void prepend(int);
         void append(int);
         int gethead();
         int gettail();
         int lastdec();
         mult& subdec(int);
         mult& divten ();
         char *decimalize();
         mult &operator= ( mult &) ;

};
void mult::clearmult()
{
  if (!head)return;
  dgtp dp =head;
  while (dp)
    {
      dgtp ddp =dp;
      if ((dp=dp->next) !=0)dp->last= 0;
      delete ddp;
    }   ;
  head=tail=0;
  numdgt=0;
}
mult::mult(int i)
{
  if (i<0||i>=digit::radix) {
  //  error ("enter a byte!");
  sgn=POSV;
  head=tail=new digit(i);
  numdgt=1;};
}
void mult::setsgn(char ch)
{
  if (ch == '+') sgn = POSV;
  else if (ch == '-') sgn = NEGV;
//else cerr("enter '+' or '-'only!");
}
char mult::getsign(){return (sgn==POSV) ?'+' :'-';}
void mult::chsgn() {sgn =(sgn ==POSV) ?NEGV :POSV;}
/*the isertion function are for head insertion*/
void mult::prepend (int i)
{
  dgtp p=new digit(i);
if (!p)   // error("allocation error in mult::prepend (int)");
if(!head)head=tail=p;else
{ head->last=p;
  p->next=head;
  head=p;
} ;
  ++numdgt;
}
/*and for tail isertion*/
void mult::append(int i)
{
  dgtp p=new digit(i);
  //if (!p)
  //error("allocation error in mult::append (int)");
  if (!tail)
     head =tail=p;
  else
    {
      tail->next=p;
      p->last=tail;
      tail=p;
    }
    ++numdgt;

}

int mult::gethead ()
{
//if (!head)
  // error ("can't get from empty list in mult::gethead()");
  dgtp p=head;
  int i=int(p->dgt);
  if ((head=head->next)!=0)
  head->last=0;
  delete p;
  --numdgt;
  return i;
};
int mult::gettail()
{
if (!tail)
   cout<<"can't get from empty list in mult::gettail()";
  dgtp p=tail;
  int i=int(p->dgt);
  if ((tail=tail->last)!=0)tail->next=0;

  delete p;
  --numdgt;
  return i;
}
/*the copy constructor may be written in terms of prepend or append;we choose the latter*/
mult::mult (mult &m)
{
  head=tail=0;
  sgn=m.sgn;
  numdgt=0;
  while (m.head) append(m.gethead());
}
/*next we need input and output functions;these needn't be member functions.the input function is one which assemblesa list of digits*/
void makemult(mult &m)
  {
  char ch;
  cout<<"enter sign: ";
  cin>>ch;
  m.setsgn(ch);
  int n;
  cout<<"number of bytes? : ";
  cin>>n;
  for (int i=0;i<n;++i)
    {
     int b;
     cout<<"enter byte: ";
     cin>>b;
     m.append(b);
    };
  } ;

  class multscan
{
private:
        mult *mp;
        dgtp dp;
        bool dir;
public:
        multscan(mult&,bool);
        dgtp operator()();
};
/*ere sep is defined in standard.h to be the string constant", "and '\b'representsthebackspace character.
the heart of the problem of representing large integers as lists of bytes ,however,is the conversion of the multiple
byte integer so constructed into a normal integer>>>>>>>>>>>>>>*/
int mult::lastdec()
{
  int d;
  int B = digit::radix;
  int c = B%10;
  multscan thisptr(*this,NEXT);
  d=0;
  dgtp p;
  while ((p = thisptr())!= 0)
       d =(c*d + (p->dgt%10))%10;
  return d;
}
mult& mult::subdec(int i)
{
  int B = digit::radix;
  bool carry=FALSE;
  multscan thisptr (*this,LAST);
  dgtp p=thisptr();
  if (!p)
     cout<<"cann'tsubtract from empty list"<<"\n";
  int j=int(p->dgt)-i;
  if (j<0)
    {
     j +=B;
     carry=TRUE;
    };
  p->dgt=(byte)j;
  while((p = thisptr()) !=0)
    {
     j=int (p->dgt);
     if (carry)
       {
        --j;
        carry =FALSE;
       };
     if (j<0)
       {
        j +=B;
        carry =TRUE;
       };
     p->dgt =(byte)j;
    }
    if (!head->dgt) gethead();
    return *this;
}
/*we may now divide by 10.knowing that there will be no overall remainder,carrying the stepwise remainder,and deleting the head digit if its data value is zero*/
  mult& mult::divten()
   {
    int B=digit::radix;
    int c=0;
    multscan thisptr(*this,NEXT);
    dgtp p;
    while ((p =thisptr()) !=0)
      {
       int i = int (p->dgt);
       i +=(c*B);
       p->dgt =(byte)(i/10);
       c =i %10;
      }  ;
      if (!head->dgt) gethead();
      return *this;
   }

/*printing out the string which results yields decimal digits in the normal order with the aid of the dewcimal output member function (which also procedes the number which its sign):*/
char* mult::decimalize()
{
  char *number = "";
  while (head)
       {
           int d = lastdec();
           char ch = char(d+ '0');
           number = chstr(ch,number);
           subdec(d);
           if (head) divten() ;
          } ;
    char s = (sgn == POSV) ? '+' : '-';
    number = chstr(s,number);
    return number;

}

multscan::multscan (mult &m ,bool direction)
{
       mp = &m;
       dir=direction;
       dp=dir ? m.head : m.tail;
}
dgtp multscan::operator()()
{
  dgtp p=dp;
  if (dir) dp = p ? dp->next:0;
  else  dp = p ? dp->last:0;
  return p;
}

/*and the ouput function should be one which prints out a multiple byte integer in some suitable format*/
void printmult (mult &m)
{
  multscan thisptr(m,NEXT);
  cout<<"("<<m.getsign();
  dgtp p;
  while ((p=thisptr()) !=0)
     cout<<*p<<", ";
  cout<<"\b\b)";
  } ;
/*here sep is defined in standard.h to be the string constant", "and '\b'representsthebackspace character.
the heart of the problem of representing large integers as lists of bytes ,however,is the conversion of the multiple
byte integer so constructed into a normal integer>>>>>>>>>>>>>>*/
/*if we now repeat the process we obtain the second to last decimal digit:and so on .
However,if we print out the decimals as they arrive,they will be in inverse order.It is therfore necessary to ouput the character value of each decimal digit in turnk to the head of a character string. For a C string:*/
char *chstr(char ch,char *str)
   {
    int n = strlen(str);
    char *newstr = new char[n+2];
    newstr[0] = ch;
    newstr[1]='\0';
    str = strcat(newstr,str);
    //newstr = 0;
    return str;
   } ;

mult &mult::operator=(mult &m)
{
clearmult();
while (m.tail)append(m.gethead()) ;

return *this;
}
void main()
{
  mult m;
  makemult(m);
  printmult(m);
  cout<<"\n";
  cout<<m.decimalize()<<"\n";
  getch();
}


Answer by car insurance
Submitted on 6/14/2006
Rating: Not yet rated Rate this answer: Vote
<a href='http://www.yahoo.com'></a>Thanks! http://www.insurance-top.com/auto/ <a href='http://www.insurance-top.com'>auto insurance</a>. <a href="http://www.insurance-top.com ">Insurance car</a>: The autos insurance company, compare car insurance, auto insurance. Also [url]http://www.insurance-top.com/car/[/url] and [link=http://www.insurance-top.com]insurance quote[/link] from site .

 

Answer by insurance auto
Submitted on 6/14/2006
Rating: Not yet rated Rate this answer: Vote
Thanks!!! http://www.insurance-top.com/company/ car site insurance. [URL=http://www.insurance-top.com]home insurance[/URL]: The autos insurance company, compare car insurance, auto insurance. Also [url=http://www.insurance-top.com]cars insurance[/url] from website .

 

Answer by insurance auto
Submitted on 6/14/2006
Rating: Not yet rated Rate this answer: Vote
Hi! http://www.insurance-top.com/company/ car site insurance. The autos insurance company, compare car insurance, auto insurance. from website .

 

Answer by oxgu3zf@google.com
Submitted on 6/14/2006
Rating: Not yet rated Rate this answer: Vote
ringtones free

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.