Displaying Source Code(s)
|
|
Addition of two very long positive integers(without using linked
list -saving memory which is consum
--------------------------------------------------------------------------------
Description : This code adds two very long integers. Here making
this the linked list has been avoided because linked list takes
extra memory in storing the address of next node. This code has
been implemented using a pointer to a character. The pointer to
a character is allocated memory using malloc() function here.
But as the length of number is increased
then the number is allocated more memory using realloc()
function.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
class long_add
{
char *s1; /* First number*/
char *s2; /* Second number*/
char *result; /* Final Result*/
public:
void getnum(); /*Function to enter 2 numbers */
char *long_add::enternum(); /*Function to put numbers to
strings*/
void long_add::addnum(); /*Function to add two numbers */
void display(); /*Function to display result of
Addition*/
};
void long_add::display() /*Function to display result of
Addition*/
{
cout<<"
Sum:";
if (*result=='0')
result++;
cout<<result;
}
void long_add::getnum() /*Function to enter 2 numbers */
{
cout<<"
Enter First number:";
s1=enternum();
cout<<"
Enter Second number:";
s2=enternum();
}
char *long_add::enternum() /*Function to put numbers to
strings*/
{
char p,*s,*t;
int i=0;
while(p!='
')
{
p=getche();
if (i==0)
{
s=(char *)malloc(sizeof(char)+1);
*s=p;
}
else
{
s=(char *)realloc(s,((i+1)*sizeof(char))+1);
t=s+i;
*t=p;
}
i++;
}
*(s+i)=' |
|
|