Compiler Library/XL Reference Manual (32650-90029)

4-: 16
Sample Program
This section contains a sample program to demonstrate how to call the
packed-decimal library routines. The program takes three large numbers
(U.S. federal government annual appropriations for the years 1977 through
1979), converts them from ASCII to packed-decimal, adds them, converts
the sum to ASCII, and prints the answer.
There are four versions of the program. The first example is written in
HP C/XL, the second in HP Pascal/XL, the third in HP FORTRAN 77/XL, and
the fourth in SPL/V, using ASSEMBLE statements. The SPL/V example is
included to show how the procedure calls would have been done on an MPE V
based HP 3000. This may be helpful to users converting SPL code to C,
Pascal, or FORTRAN.
Program Output
All four versions of the program produce the following output:
1977 046655980996406
1978 050778229148999
1979 056396083378825
Total 153830293524230
HP C/XL Example
/*
* This program calculates total appropriations by the
* United States federal government for the years 1977-1979
*/
#pragma intrinsic HPPACADDD
#pragma intrinsic HPPACCVAD
#pragma intrinsic HPPACCVBD
#pragma intrinsic HPPACCVDA
typedef struct {char c [15]} ascii_num_15; /* 15-digit numbers in ASCII */
typedef struct {char c [8]} packed_num_15; /* 8-character field holds */
/* 15 digits and sign
static ascii_num_15 yearly [3] = /* Annual appropriations, in cents */
{"046655980996406",
"050778229148999",
"056396083378825"};
main ()
{
short int zero=0;
int year;
packed_num_15 packed_sum, packed_temp;
ascii_num_15 ascii_sum;
HPPACCVBD (&packed_sum, 15, &zero, 1); /* Initialize sum */
for (year=1977; year<=1979; year++)
{
printf ("%d %.15s\n", /* Echo annual data */
year, &yearly [year-1977]);
HPPACCVAD (&packed_temp, 15, /* Convert ASCII to decimal */
&yearly [year-1977], 15);
HPPACADDD (&packed_sum, 15, /* Add to sum */
&packed_temp, 15);
}
HPPACCVDA (&ascii_sum, 15, /* Convert sum to ASCII, */
&packed_sum, 1); /* suppressing plus sign */
printf ("Total %.15s\n", &ascii_sum);
}