Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I want this type of output on C++/C Program if I multiply 2 numbers??


827395
x 26153
-----------
2482185
4136975
827395
4964370
1654790
-----------
21638861435

Please help me out?
Posted
Comments
walterhevedeich 24-Aug-11 19:38pm    
Please do not repost questions. Doing it here in Q&A is often considered as rude.
Stefan_Lang 25-Aug-11 4:59am    
I dunno. Is it? I suppose it would be nice to point to the other post or in some other way notify the readers of the fact that you posted the question in different locations, but the people looking in one place may not be the same looking in the other place, so I don't find it unreasonable to post in both places. Or did I miss some CP rule?

P.S.: sorry, did you meanm reposting repeatedly in one forum, i. e. in Q&A? If so, I hadn't noticed it was. I thought you were referring to reposts in several different forums (one per forum)

You would have to create source that takes calculates each item manually as though you were doing it by hand.
If we use your example, you would have to calculate how many rows of longhand you require and then calculate for each of the rows. The first longhand row is simply 827395 x 3, the second is 827395 x 5 offset by one column, etc.
 
Share this answer
 
Comments
Trick7 24-Aug-11 22:50pm    
Ya i got u but what about the formatting to get the desired output...
In honor to Emilio Garavaglia ive replaced the original code by a more c++ like solution. But im afraid there is no perfect solution.

content of CNumber.h:
C++
#ifndef __CNUMBER_H
#define __CNUMBER_H

// this: CNumber.h
// impl: CNumber.cpp

#include <tchar.h>

template <class TI> const TI& myminimum(const TI& a,const TI& b){ return a<b?a:b; }
template <class TI> const TI& mymaximum(const TI& a,const TI& b){ return a<b?b:a; }

class bNumber
{
public:
  bNumber();
  ~bNumber();

protected:
  void            d(const unsigned int n);
  void            t(const unsigned int n);

protected:
  unsigned int    _ndigits;
  unsigned char*  _adigits;
  TCHAR*          _tmpout;

};

class CNumber : bNumber
{
public:
  CNumber();
  CNumber(CNumber& n);
  CNumber(const unsigned int z);
  CNumber(const TCHAR* s);
  ~CNumber();
  
  operator const TCHAR*  ();

  CNumber&  operator = (CNumber& n);
  CNumber&  operator = (const TCHAR* s);
  CNumber&  operator = (const unsigned int z);

  CNumber    operator *= (const unsigned char m);
  CNumber&  operator += (CNumber& add);

  CNumber    operator * (const unsigned char m);
  CNumber    operator * (CNumber& mul);

  CNumber&      exp(unsigned int e);

  unsigned char  operator [] (const unsigned int ix);

  unsigned int  length();
  const TCHAR*  pad(const unsigned int n,const TCHAR c);

private:
  inline
  unsigned int    m10(unsigned int d,unsigned char& c){ c=d%10; return d/10; }
  void            trim();
};


#endif // __CNUMBER_H


content of CNumber.cpp:
C++
#include "CNumber.h"

////////////////////////////////
// bNumber
bNumber::bNumber()
{
  _ndigits = 0;
  _adigits = 0;
  _tmpout  = 0;
}

bNumber::~bNumber()
{
  if(_adigits) delete [] _adigits;
  if(_tmpout) delete [] _tmpout;
}

void bNumber::d(const unsigned int n)
{
  unsigned char*  p = new unsigned char[n];
  unsigned int    i,m;
  if(_adigits)
  {
    for(i=0,m=myminimum(n,_ndigits);i<m;i++) p[i] = _adigits[i];
    delete [] _adigits;
  }
  _adigits = p;
}

void bNumber::t(const unsigned int n)
{
  TCHAR*  p = new TCHAR[n];
  if(_tmpout) delete [] _tmpout;
  _tmpout = p;
}

////////////////////////////////
// CNumber
CNumber::CNumber()
{
}

CNumber::CNumber(CNumber& n)
{
  operator = (n);
}

CNumber::CNumber(const unsigned int z)
{
  operator = (z);
}

CNumber::CNumber(const TCHAR* s)
{
  operator = (s);
}

CNumber::~CNumber()
{
}

CNumber::operator const TCHAR*  ()
{
  return pad(0,' ');
}

CNumber& CNumber::operator = (CNumber& n)
{
  unsigned int  i;
  d(_ndigits=n._ndigits);
  for(i=0;i<_ndigits;i++) _adigits[i] = n._adigits[i];
  return *this;
}

CNumber& CNumber::operator = (const TCHAR* s)
{
  unsigned int i,n = _tcslen(s);
  d(_ndigits=n);
  for(i=0;i<n;i++) _adigits[i] = s[i]-'0';
  return *this;
}

CNumber& CNumber::operator = (const unsigned int z)
{
  unsigned int i,r = z;
  d(_ndigits=10);
  for(i=0;i<10;i++){ _adigits[9-i]=r%10; r/=10; }
  return *this;
}

CNumber CNumber::operator * (const unsigned char m)
{
  return CNumber(*this) *= m;
}

CNumber CNumber::operator *= (const unsigned char m)
{
  unsigned int  i,r=0;
  d(++_ndigits);
  for(i=_ndigits-1;i;i--)
  {
    r += (unsigned int)m * (unsigned int)_adigits[i-1];
    r = m10(r,_adigits[i]);
  }
  _adigits[i] = (unsigned char)r;
  trim();
  return *this;
}

CNumber& CNumber::operator += (CNumber& add)
{
  unsigned int  i,n,r;

  n = mymaximum(_ndigits,add._ndigits);
  d(1+n);
  for(r=i=0;i<n;i++)
  {
    r += operator[](i) + add[i];
    r = m10(r,_adigits[n-i]);
  }
  for(;i<_ndigits;i++)
  {
    r += operator[](i);
    r = m10(r,_adigits[n-i]);
  }
  for(;i<add._ndigits;i++)
  {
    r += add[i];
    r = m10(r,_adigits[n-i]);
  }

  _adigits[0] = r;
  _ndigits = 1 + n;
  trim();

  return *this;
}

CNumber CNumber::operator * (CNumber& mul)
{
  CNumber        n,m;
  unsigned int  i;
  for(i=0;i<mul.length();i++)
  {
    m = (*this) * mul[i];
    m.exp(i);
    n += m;
  }
  return n;
}

unsigned char CNumber::operator [] (const unsigned int ix)
{
  return ix<_ndigits?_adigits[_ndigits-ix-1]:0;
}

CNumber& CNumber::exp(unsigned int e)
{
  if(_ndigits)
  {
    d(e+=_ndigits);
    for(;_ndigits<e;_ndigits++) _adigits[_ndigits]=0;
  }
  return *this;
}

unsigned int CNumber::length()
{
  return _ndigits;
}

const TCHAR* CNumber::pad(const unsigned int n,const TCHAR c)
{
  unsigned int i,o,x;

  for(i=0;(i<_ndigits)&&(0==_adigits[i]);i++);
  x = mymaximum(n,_ndigits-i); t(1+x);
  for(o=0;o<(x-(_ndigits-i));o++) _tmpout[o] = c;
  for(;i<_ndigits;i++) _tmpout[o++] = '0' + _adigits[i];
  _tmpout[o] = 0;

  return _tmpout;
}

// private
void CNumber::trim()
{
  unsigned int  i,o;
  for(i=0;(i<_ndigits)&&(0==_adigits[i]);i++);
  if(i)
  {
    for(o=0;i<_ndigits;i++) _adigits[o++]=_adigits[i];
    _ndigits = o;
  }
}


content of main.cpp:
C++
#pragma once
#include <stdio.h>
#include "CNumber.h"

int _tmain(int argc, _TCHAR* argv[])
{
  {
    enum{ PAD = 40, };

    CNumber        a = _T("905427184367094827395");
    CNumber        b = _T("4838203673416526153");
    CNumber        c;
    unsigned int  i;

    if(1<argc) a = argv[1];
    if(2<argc) b = argv[2];

    _tprintf(__T("%s x %s = \r\n"),(const TCHAR*)a,(const TCHAR*)b);
    _tprintf(__T("-----------------------------\r\n"));

    for(i=0;i<b.length();i++)
    {
      c = a * b[i];
      c.exp(i);
      if(c.length()) _tprintf(__T("%s\r\n"),c.pad(PAD,' '));
    }

    _tprintf(__T("-----------------------------\r\n"));
    c = a * b;
    _tprintf(__T("%s\r\n"),c.pad(PAD,' '));
    _tprintf(__T("=============================\r\n"));

    _gettch();
  }
  return 0;
}


Best regards to the useful hints of Emilio Garavaglia.
 
Share this answer
 
v2
Comments
Emilio Garavaglia 25-Aug-11 4:11am    
This solution is C++ed C#, with a lot of plain C. Not C++
#defina max is ridiculous, there is std::max, that is a function (not a macro!)
Constructor cannot call each other explicitly (it's a feature introduced with C++0x, but doesn't work the way you did: see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1986.pdf.)
Use of malloc/realloc is an history relict. There are std::vector for dynamic containment.
I can continue, but frankly, before posting this things as a solution, dust off your C++ skill!
Stefan_Lang 25-Aug-11 5:06am    
I totally agree with everything you said.

I'd like to add that #define max(...) is not just ridiculous, but it actually breaks the STL! E. g. try including valarray after that define - it will break with incomprehensible compiler errors, because there is a method std::valarray::max(...) . Now guess what happens when you try to use this class...
mbue 25-Aug-11 10:04am    
using std is the real c++ skill (lol). show me anything in the code above that wont work. its on you to use std::vector, there is no reason to say malloc is obsolet. You say i cant do calling a default contructor: but i do that cause it worked all times. Take a look to the mfc-sources and you will see much more like this (ie. CArray element constructor and destructor). so far to your skill.
Regards.
Emilio Garavaglia 26-Aug-11 2:57am    
It's not C++. It's C. It works because is very good C skill, with a very few of C++. Your code is 80% C, 20% C++. Your very good skill must be re-proportioned to 20% of weight.
The way you use constructors is WRONG not because I said it, but because the C++ language specifications said that. It works only because the compiler you used was "permissive". That practice, if used with more complex objects, may result in double allocation/creation of leaked resources.
So, please, stop LOL-lling (There is absolutely nothing to laugh here) and study the language specification.

Consider this as a refuse to hire.
mbue 26-Aug-11 4:40am    
where is your 100% c++ compliant solution? im waiting...
nobody has yearned a 100% c++ solution - that was your imagination.
read that thread again and again and again, if you cant remember.
little hint: take a look at the ops question tags.
have a nice day.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900