CPP

Reverse Numeral Algorithm in emacs C++

I got this from Minnesota... I take this question because I like Algorithm.. and like helping people.. just helped my bro.. from minnesota..
At First I write the code in Borland C++ until all question done. just need 1,5 hour. and then I translate that code into emacs C++ in linux..
I will give you and explain the source code.. check this out..

This is the Problem.. :

So This is the Solutions No 1 : (Emacs)

using namespace std;

#include<iostream>
#include<cstdlib>


//function to return digit from integer data type variable input
int digit(unsigned int x)
{
int retVal = 0;
int z;
while(1)
{
z = x%10;
x = (x-z)/10;
retVal++;
if(x==0) { break; }
}
return retVal;

}

//function to return reversed numeral..
unsigned int revNum(unsigned int n, unsigned int numDigit)
{
unsigned int retVal;

int realDigit = digit(n);


int *arr = 0;
int z;

for(int i=0; i<realDigit; i++)
{
z = n%10;
n = (n-z)/10;
arr[i] = z;
}


cout<<endl<<endl<<"arr : ";
unsigned int rev=0;
for(i=0; i<numDigit; i++)
{
if(i<realDigit)
{
int m = 1;
for(int j=i;j<realDigit-1;j++) { m*=10; }

int sum = (arr[i]) * m ;
rev+=sum;
cout<<endl<<"arr["<<i<<"] :"<<arr[i]<<" ; m : "<<m<<" ; sum : "<<sum<<" ; rev : "<<rev;
}
else
{

int multp = 1;
for(i=0;i<(numDigit-realDigit);i++) { multp*=10; }

cout<<endl<<endl<<"multp : "<<multp;
rev*=multp;
break;
}
}


if(realDigit>numDigit)
{
int div = 1;
for(i=0;i<(realDigit-numDigit);i++) { div*=10; }

cout<<endl<<endl<<"div : "<<div;
rev = rev / div;
}


cout<<endl<<endl<<rev;
cout<<endl<<endl;


return rev;
}

int main()
{
unsigned int a,b;


cout<<"Enter number: ";
cin>>a;
cout<<"Enter digit number: ";
cin>>b;
cout<<endl<<" Number : "<<a;
cout<<endl<<" Digit Number : "<<b;


cout<<endl<<endl;
cout<<"Reserve = "<<revNum(a,b)<<endl;

return 0;

}

this Just like one of hundred solutions.. maybe any efficient code to reverse numeral..

and then this is the solutions No. 2 : (Emacs)

using namespace std;

#include<iostream>
#include<cstdlib>


int digit(unsigned int x)
{
int retVal = 0;
int z;
while(1)
{
z = x%10;
x = (x-z)/10;
retVal++;
if(x==0) { break; }
}
return retVal;

}

unsigned int revNum(unsigned int n, unsigned int numDigit)
{
unsigned int retVal;

int realDigit = digit(n);


int *arr = 0;
int z;

for(int i=0; i<realDigit; i++)
{
z = n%10;
n = (n-z)/10;
arr[i] = z;
}

unsigned int rev=0;
for(i=0; i<numDigit; i++)
{
if(i<realDigit)
{
int m = 1;
for(int j=i;j<realDigit-1;j++) { m*=10; }

int sum = (arr[i]) * m ;
rev+=sum;
}
else
{

int multp = 1;
for(i=0;i<(numDigit-realDigit);i++) { multp*=10; }

rev*=multp;
break;
}
}


if(realDigit>numDigit)
{
int div = 1;
for(i=0;i<(realDigit-numDigit);i++) { div*=10; }

rev = rev / div;
}

return rev;
}

// return first digit
int first_digit(unsigned int n)
{
int numDigit = digit(n);
int div = 1;
for(int i=0;i<numDigit-1;i++) { div*=10; }

return (n-(n%div)) / div;
}

// return last digit
int last_digit(unsigned int n)
{
return (n%10);
}


int main()
{
unsigned int a;
unsigned int num=3;


cout<<"Enter a 3 digit number, with the first digit larger than the third: ";
cin>>a;


if(digit(a)!=3)
{

cout<<endl<<"wrong input.. Must be 3 digit.";
exit(27);
}
if( first_digit(a) <= last_digit(a) )
{
cout<<endl<<"wrong input.. the First Digit must be larger than the third.";
exit(27);
}


unsigned int rev_1 = revNum(a,num);

cout<<endl<<endl<<"Reserve and Subtract :";

cout<<endl<<endl<<a;
cout<<endl<<rev_1<<" -"<<endl;
cout<<"----"<<endl;

int val_1 = a-rev_1;

int zero = 0;
// for show and calculate total zero number if the value digit smaller than num digit
if(digit(val_1)<num)
{
for(int i = 0; i < (num-digit(val_1)); i++ )
{
cout<<"0";
zero++;
}
}
cout<<val_1;


cout<<endl<<endl<<"Reserve and Add :";

unsigned int rev_2 = revNum(val_1,digit(a));
if(zero>0)
{
int div = 1;
for(int i=1;i<zero;i++) { div*=10; }
rev_2*=div;
}


cout<<endl<<endl;
for(int i=0 ; i<zero ; i++) { cout<<"0"; }
cout<<val_1<<endl;
cout<<rev_2<<" +";
cout<<endl<<"----";
cout<<endl<<(val_1+rev_2);

cout<<endl<<endl<<"The answer is "<<val_1+rev_2;

return 0;

}

Download Solution Reverse Numeral No. 1
Download Solution Reverse Numeral No. 2

Hope can Be Useful..
Best Regard,

Yupi Sugianto



Related posts

Leave a Comment