|
/* Program for
Addition of two very large floating numbers. The Numbers may be
larger than the permissible float/double numbers */
#include<stdio.h>
# include<math.h>
# include <string.h>
# include <iostream.h>
# include <stdlib.h>
main ()
{
int
len1=0,len2=0;
int
len_mant_1=0,len_mant_2=0,len_mant=0;
/* 80 digits max */
char
num1[80] ="";
char
num2[80] ="";
char
*num3="eE";
char
key, *mants1,*exp1,*mants2,*exp2;
double mant;
/* Mantissa and
Exponents are separated and stored separately in strings variables
mants1, exp1,
mants2, exp2 respectively for the two numbers */
printf("\n\n\n");
printf("|
--------------------------------------------------------------------------|\n");
printf("|
Enter Two very large Floating Point
Numbers of 80 digits with exponentials |\n");
printf("| such as
1.23456788888E1234567 or 1.23444444444E+300001111 |\n");
printf("|
--------------------------------------------------------------------------|\n");
printf("\n\n");
printf("Enter
First Number : ");
gets(num1);
printf("Enter Second Number : ");
gets(num2);
mants1 =
strtok(num1,num3);
exp1 =
strtok(NULL,num3);
mants2
= strtok(num2,num3);
exp2 =
strtok(NULL,num3);
printf
("\nAs per this program Your's \n");
printf
("Number 1 -> mantissa = %s....exponential = %s\n",mants1,exp1 );
printf
("Nember 2 -> mantissa = %s....exponential = %s\n",mants2,exp2 );
printf
("\n\n\n");
printf
("---------------Results-------------------\n\n");
len_mant_1 = strlen(mants1);
len_mant_2 = strlen(mants2);
/* len_mant is the
number of required digits of mantissa and which shall be
equal to the largest
value of both the mantissa, so minimum value is fixed
as per the input of
user's
*/
if
(len_mant_1 > len_mant_2)
len_mant = len_mant_1;
else
len_mant = len_mant_2;
/* Check for the
validity of the Mantisaa and exponents.
They
should not be equal to zero . If so terminate the program. */
if(atof(mants1) == 0 )
{
printf ("** Mantissa Part for First number
is invalid **\n");
printf ("** Program Terminated Re-run the
program **\n");
goto stop;
}
else
if(atof(mants2) == 0 )
{
printf ("** Mantissa Part for Second
number is invalid **\n");
printf ("** Program Terminated Re-run the
program **\n");
goto stop;
}
/* Check
whether the Exponential is zero */
else if(atol(exp1) == 0 )
{
printf ("** Exponential Part for First
number is invalid.**\n");
printf ("** Program Terminated Re-run the
program **\n");
goto stop;
}
else if(atol(exp2) == 0 )
{
printf ("** Exponential Part for Second
number is invalid.**\n");
printf ("** Program Terminated Re-run the
program **\n");
goto stop;
}
else;
/* Normalise the
exponentials with respect to exp2 i.e, the output will be
Written with the
eponential of second Number
*/
{
mant =
atof(mants2)+atof(mants1)*(pow(10,(atol(exp1)-atol(exp2))));
gcvt( mant,
len_mant, mants1 );
printf( "
Addition= number 1 + number 2 = %sE%s \n",mants1,exp2);
printf
("\n\n\n");
printf( "< To
quit type 'q' or 'Q' or to add more number press Any Other Key
>");
key = getchar();
if (key =='q' ||
key== 'Q')
goto
stop;
else
main
();
}stop:
return
0;
} |