/home/toolbox/public_html/solutions/4/424/e.cpp
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <stdint.h>
8 #include <math.h>
9 #include <stdlib.h>
10
11 #define TRUE (1 == 1)
12 #define FALSE (1 != 1)
13
14 #define DEBUG if (FALSE)
15
16 /*
17 * Author: Isaac Traxler
18 * Date: 2015 02 11
19 * Purpose:
20 * Problem: 424
21 */
22
23 /*
24 * This template reads data until a terminating value is reached.
25 */
26
27
28 #define LN_MAX_DIGITS 1025
29 #define MAX_LINE (LN_MAX_DIGITS+25)
30
31 typedef struct LARGE_NUMBER_STRUCT
32 {
33 unsigned short int array[LN_MAX_DIGITS]; /* might consider unsigned char here */
34 /* to reduce storage by 50% */
35 signed short int sign;
36 signed long int exponent;
37 signed short int digitCount;
38 }
39 LARGE_NUMBER_STRUCT;
40
41 char buffer[MAX_LINE];
42 LARGE_NUMBER_STRUCT tot;
43 LARGE_NUMBER_STRUCT num;
44
45
46 //
47 // LargeNumber library
48 //
49 // authors: Isaac Traxler & students
50 //
51 // This is a project started back in 2000 that was not finished
52 // and forgotten. It has now been resurrected.
53 //
54 // The goal of this project is to produce a usable c source library
55 // for large number arithmetic.
56 //
57 // Students in my Psychology of Programming class in 2000 contributed.
58 // Since I have largely restructured it, individual names have been
59 // removed from the individual routines. So here are the names of the
60 // original contributing students:
61 // - Paul Miller
62 // - Wojciech Stryjewski
63 // - Phil Bordelon
64 // - John Walker
65 // - Erin Valentine
66 // - Jay Ponville
67 // - Jamie Ahmad
68 // - Helen
69 //
70 // Contributed to revamped version:
71 // - Josh Abadie
72 //
73 //
74 // Make sure that LN_MAX_DIGITS is large enough to handle worst case.
75 // Be aware that as many as 5 LN instances might exist in a routine.
76 // Be aware that excessively large values will result in CPU consumption
77 // in certain situations (like division of repeating decimals).
78 //
79
80 //
81 // ASSUMPTIONS:
82 // 1) That no value sent to the library will exceed bounds
83 // a) Exponent will fit into a signed long at ALL times
84 // b) Significant digit count will not exceed LN_MAX_DIGITS
85 //
86 // 2) LN instances are ALWAYS normalized when passed into a routine
87 //
88 // 3) Adding or subtracting two 501 digit numbers can exceed 1000 digits
89 //
90 // 4) No bounds checking is done against LN_MAX_DIGITS
91 //
92 // 5) Normalization does the following:
93 // a) Adjusts exponent, adjusts digitCount, and removes leading zeroes
94 // b) Adjusts digitCount and removes trailing zeroes
95 // c) Leaves number so that a decimal point is implied before the first digit
96 //
97 //
98 // ********************************************************************************
99 // rules:
100 // - all routines return 0 if everything is okay, a different positive number
101 // for each different error
102 // - Any result passed back (except from LNshift) is normalized
103 // - LNadd adds any two numbers with same sign (calls subtract if signs differ)
104 // - LNsubtract subtracts any two numbers with same sign (calls add if signs differ)
105 //
106 // a = 1
107 // b = -1
108 // add(a,b,c) ==> subtract(a,(-b),c)
109 // subtract(a,b,c) ==> add(a,(-b),c)
110 //
111 // - 0 ALWAYS has a + sign and an exponent of 0
112 //
113 //
114
115 void LNprint(LARGE_NUMBER_STRUCT *n)
116 // simple routine to print large number
117 {
118 // BEGIN function LNprint -- (200302)
119 char str[LN_MAX_DIGITS+25];
120
121 DEBUG printf("LNprint: 1\n");
122 DEBUG printf("LNprint: digitCount=%d\n", n->digitCount);
123 DEBUG printf("LNprint: exponent=%d\n", n->exponent);
124 DEBUG printf("LNprint: size=%d\n", sizeof(str));
125 LNarr2strInt(n, str);
126 DEBUG printf("LNprint: 2\n");
127 printf("%s\n", str);
128 DEBUG printf("LNprint: 3\n");
129 } // END function LNprint -- (200302)
130
131 int LNnegate(LARGE_NUMBER_STRUCT *ln1)
132 // changes sign of ln1
133 {
134 // BEGIN function LNnegate -- (200302)
135 if (0 != ln1->digitCount)
136 ln1->sign *= -1;
137 return 0;
138 } // END function LNnegate -- (200302)
139
140 int LNcompare(LARGE_NUMBER_STRUCT *ln1, LARGE_NUMBER_STRUCT *ln2)
141 // compares to see if a<b (-1), a=b (0), a>b (1); assumes both numbers
142 // are normalized (have first digit non-zero and no trailing zeros)
143 {
144 // BEGIN function LNcompare -- (200303)
145 int toReturn = 0;
146
147 // XOR the two, if same 0 (false) results, if different -2 (true)
148 if (ln1->sign ^ ln2->sign) // signs are same, result is 0
149 {
150 // have different signs
151 toReturn = (0 > ln1->sign) ? -1 : 1;
152 } // have different signs
153 else
154 {
155 // have same sign
156 if (ln1->exponent != ln2->exponent)
157 {
158 // exponents not equal, return 1 (1st one) or -1 (2nd one) to show larger
159 toReturn = (ln1->exponent > ln2->exponent) ? 1 : -1;
160 } // exponents not equal, return 1 (1st one) or -1 (2nd one) to show larger
161 else
162 {
163 // numbers have same exponent
164 int i, t;
165
166 t = (ln1->digitCount > ln2->digitCount) ? ln2->digitCount : ln1->digitCount;
167 for (i=0; (i<t) && (0 == toReturn) ; i++)
168 {
169 // for - compare each digit
170 if (ln1->array[i] != ln2->array[i])
171 toReturn = (ln1->array[i] > ln2->array[i]) ? ln1->sign : - ln2->sign;
172 } // for - compare each digit
173 // digits identical, but one number has extra digits, so number with extra digits
174 // is bigger if positive, smaller if negative.
175 if ((0 == toReturn) && (ln1->digitCount != ln2->digitCount))
176 toReturn = (ln1->digitCount > ln2->digitCount) ? ln1->sign : - ln2->sign;
177 } // numbers have same exponent
178 } // have same sign
179 return toReturn;
180 } // END function LNcompare -- (200303)
181
182 int LNstr2arr(char str[], LARGE_NUMBER_STRUCT *ln)
183 // convert from string into LN data structure
184 // assume no invalid characters in string (but skip over leading and trailing blanks)
185 //
186 // State diagram:
187 //
188 // state | val | whitespace | - | + | . | digit | Ee | end of string`
189 // ------+-----+------------+------+------+------+-------+------+--------------
190 // start | 1 | start | num1 | num1 | dec1 | num2 | err | 0
191 // num1 | 2 | err | err | err | dec1 | num2 | err | err
192 // num2 | 3 | normalize | err | err | dec2 | num2 | exp1 | normalize
193 // dec1 | 4 | err | err | err | err | dec2 | err | err
194 // dec2 | 5 | normalize | err | err | err | dec2 | exp1 | normalize
195 // exp1 | 6 | err | exp2 | exp2 | err | exp3 | err | err
196 // exp2 | 7 | err | err | err | err | exp3 | err | err
197 // exp3 | 8 | normalize | err | err | err | exp3 | err | normalize
198 {
199 // BEGIN function LNstr2arr -- (200302)
200 #define START 1
201 #define NUM1 2
202 #define NUM2 3
203 #define DEC1 4
204 #define DEC2 5
205 #define EXP1 6
206 #define EXP2 7
207 #define EXP3 8
208 //
209 // status - (succes/failure) flag (init to SUCCESS)
210 // done - are we done yet? (init to NO)
211 // dpflag - decimal point flag - Have we encountered one yet? (init to NO)
212 // numflag - number flag - Have we encountered any NON-ZERO digits yet? (init to NO)
213 // n - counter/subscript for large number (init to start)
214 // ch - point to main string
215 // These next two need not be used if their values are stored directly into large number.
216 // sign
217 // exp
218 //
219 int state = START, done = 0, n = 0;
220 signed short int dpflag = 0, numflag = 0, sign = 1;
221 signed long int exp;
222 char *ch = str;
223
224 // skip leading blanks and leading zeroes (start < > start)
225 while ( (isspace(*ch)) || ('0' == *ch) ) ch++;
226
227 if (0 != *ch) // is it end of string?
228 {
229 // not empty string
230 // do we have a sign (must be first char)
231 if ('-' == *ch)
232 {
233 // negative - state is NUM1
234 sign = -1;
235 ch++;
236 } // negative - state is NUM1
237 else
238 // Check for plus sign: Not really necessary...(these 4 lines may
239 // be omitted) if numbers are not going to have unary pluses
240 if (*ch == '+')
241 {
242 // positive (the default) - state is NUM1
243 ch++;
244 } // positive (the default) - state is NUM1
245
246 // now get digits until the decimal point or exponent
247 while (isdigit(*ch))
248 {
249 // process digits -- into state NUM2
250 state = NUM2;
251 ln->array[n++] = *ch - '0';
252 ch++; // move to next char in string
253 } // process digits -- into state NUM2
254 exp = n;
255
256 if ('.' == *ch)
257 {
258 // decimal point
259 // set state to DEC1 if no digits, DEC2 if digits already
260 state = (NUM2 != state) ? DEC1 : DEC2;
261 ch++;
262
263 // now get digits after the decimal point
264 if (isdigit(*ch)) state = DEC2;
265 while (isdigit(*ch))
266 {
267 // process digits -- into state NUM2
268 ln->array[n++] = *ch - '0';
269 ch++; // move to next char in string
270 } // process digits -- into state NUM2
271 } // decimal point
272
273 // only things left are Ee (exponent), whitespace and end of string
274 if (('E' == *ch) || ('e' == *ch))
275 {
276 // okay we have an exponent
277 signed long int texp;
278
279 state = EXP2;
280 ch++;
281 sscanf(ch, "%d", &texp);
282 exp += texp;
283 } // okay we have an exponent
284 } // not empty string
285
286 if ((EXP2 == state) || (DEC2 == state) || (NUM2 == state))
287 {
288 // normalize number
289 ln->sign = sign;
290 ln->exponent = exp;
291 ln->digitCount = n;
292 state = 0;
293 } // normalize number
294 return state;
295 } // END function LNstr2arr -- (200302)
296
297 int LNicnvrt(long int number, LARGE_NUMBER_STRUCT *n)
298 // convert a long int to Large Number
299 {
300 // BEGIN function LNicnvrt -- (200302)
301 int idx, digit, j;
302
303 // do sign
304 n->sign = (number < 0) ? -1 : 1;
305
306 // set mantissa and exponent
307 n->exponent = 0;
308 idx = LN_MAX_DIGITS;
309 while (number > 0)
310 {
311 // while
312 digit = number % 10;
313 n->array[--idx] = digit;
314 number /= 10;
315 } // while
316 n->exponent = LN_MAX_DIGITS - idx;
317 n->digitCount = n->exponent;
318 for (j=0; LN_MAX_DIGITS > (j + idx); j++)
319 {
320 // for
321 n->array[j] = n->array[idx+j];
322 } // for
323 return 0;
324 } // END function LNicnvrt -- (200302)
325
326 int LNarr2strExp(LARGE_NUMBER_STRUCT *ln, char str[])
327 // convert the ln struct to a string in scientific notation
328 {
329 // BEGIN function LNarr2strExp -- (200302)
330 int i, cnt = 0;
331
332 if (0 == ln->digitCount)
333 {
334 // handle zero
335 strncpy("+0E0", str, LN_MAX_DIGITS);
336 } // handle zero
337 else
338 {
339 // handle non-zero
340 str[cnt++] = (0 > ln->sign) ? '-' : '+';
341 str[cnt++] = '0' + ln->array[0];
342 str[cnt++] = '.';
343 for (i = 1; i < ln->digitCount; i++)
344 str[cnt++] = '0' + ln->array[i];
345 str[cnt++] = 'E';
346 sprintf(&(str[cnt]), "%+ld", (ln->exponent-1));
347 } // handle non-zero
348 return 0;
349 } // END function LNarr2strExp -- (200302)
350
351 int LNarr2strInt(LARGE_NUMBER_STRUCT *ln, char str[])
352 // convert the ln struct to a string n integer format with truncate
353 {
354 // BEGIN function LNarr2strInt -- (200302)
355 int i, cnt = 0;
356
357
358 DEBUG printf("LNarr2strInt->exponent=%d\n", ln->exponent);
359 DEBUG printf("LNarr2strInt->digitCount=%d\n", ln->digitCount);
360 if ((0 == ln->exponent) && (0 == ln->digitCount))
361 {
362 // abs(number) less than 0 or number is zero
363 str[0]='0';
364 str[1]=0;
365 } // abs(number) less than 0 or number is zero
366 else if ((0 > ln->exponent) || (0 >= ln->digitCount))
367 {
368 // abs(number) less than 0 or number is zero
369 strncpy("+0", str, LN_MAX_DIGITS);
370 } // abs(number) less than 0 or number is zero
371 else
372 {
373 // something to convert
374 int x;
375
376 // str[cnt++] = (0 > ln->sign) ? '-' : '+';
377 x = (ln->exponent > ln->digitCount) ? ln->digitCount : ln->exponent;
378 for (i = 0; i < x; i++)
379 str[cnt++] = '0' + ln->array[i];
380 x = ln->exponent - ln->digitCount;
381 for (i=0; i < x; i++)
382 str[cnt++] = '0';
383 str[cnt] = 0;
384 } // something to convert
385 return 0;
386 } // END function LNstrInt -- (200302)
387
388 int LNln2strFloat(LARGE_NUMBER_STRUCT *ln, char str[])
389 // convert the ln struct to a string in float format
390 {
391 // BEGIN function LNln2strFloat -- (200302)
392 int i, cnt = 0;
393
394 if (0 == ln->digitCount)
395 {
396 // hadle 0
397 strncpy("+0.0", str, LN_MAX_DIGITS);
398 } // hadle 0
399 else
400 {
401 // not zero
402 int x1, x2;
403
404 str[cnt++] = (0 > ln->sign) ? '-' : '+';
405 x1 = (ln->exponent > ln->digitCount) ? ln->digitCount : ln->exponent;
406 for (i = 0; i < x1; i++)
407 str[cnt++] = '0' + ln->array[i];
408 x2 = ln->exponent - ln->digitCount;
409 if (0 > x2)
410 {
411 // digitCount larger
412 str[cnt++] = '.';
413 for (i = x1; i < ln->digitCount; i++)
414 str[cnt++] = '0' + ln->array[i];
415 } // digitCount larger
416 else
417 {
418 // exponent larger
419 for (i=0; i < x2; i++)
420 str[cnt++] = '0';
421 str[cnt++] = '.';
422 str[cnt++] = '0';
423 } // exponent larger
424 str[cnt++] = 0;
425 } // not zero
426 return 0;
427 } // END function LNstrFloat -- (200302)
428
429 int LNshift(LARGE_NUMBER_STRUCT *ln, int cnt)
430 // shift the implied decimal point
431 {
432 // BEGIN function LiNshift -- (200302)
433 // errorLevel - Errorlevel to return.
434 // i - Loop variable.
435 int i, errorLevel = 0;
436
437 if (0 < ln->digitCount)
438 if (0 < cnt)
439 {
440 // non-negative - do the shift
441 for ((i = ln->digitCount - 1); i >= 0 ; i --)
442 ln->array[i + cnt] = ln->array[i];
443 for (i = 0; i < cnt; i ++)
444 ln->array[i] = 0;
445 ln->exponent += cnt;
446 ln->digitCount += cnt;
447 } // non-negative - do the shift
448 else if (0 != cnt)
449 {
450 // can't do it
451 fprintf (stderr, "Error: Cannot shift negative bits.\n");
452 errorLevel = 1;
453 } // can't do it
454 return errorLevel;
455 } // END function LNshift -- (200302)
456
457 int LNnormalize(LARGE_NUMBER_STRUCT *ln)
458 // fix LN so that arr[0] <> 0 (if possible)
459 {
460 // BEGIN function LNnormalize -- (200302)
461 // errorLevel - Errorlevel to return.
462 // zeroCount - How many zeroes?
463 // i - Loop variable
464 int i, errorLevel = 0, zeroCount = 0;
465
466 if (0 < ln->digitCount)
467 {
468 // digits to process
469 while ((ln->array[zeroCount] == 0) && (zeroCount < ln->digitCount))
470 {
471 // count leading zeroes
472 zeroCount ++;
473 } // count leading zeroes
474 if (0 < zeroCount)
475 {
476 // found leading zeroes
477 if (zeroCount == ln->digitCount)
478 {
479 // entire number is zero
480 ln->exponent = 0;
481 ln->digitCount = 0;
482 ln->sign = 1;
483 } // entire number is zero
484 else
485 {
486 // digits to shift
487 ln->digitCount -= zeroCount;
488 ln->exponent -= zeroCount;
489 for (i = 0; i < ln->digitCount; i++)
490 {
491 // shift them
492 ln->array[i] = ln->array[i + zeroCount];
493 } // shift them
494 } // digits to shift
495 } // found leading zeroes
496 // handle railing zeroes
497 i = ln->digitCount - 1;
498 while ((i >= 0) && (0 == ln->array[i]))
499 {
500 // while
501 ln->digitCount--;
502 i--;
503 } // while
504 if (0 == ln->digitCount)
505 {
506 // entire number is zero
507 ln->exponent = 0;
508 ln->sign = 1;
509 } // entire number is zero
510 } // digits to process
511 return (errorLevel);
512 } // END function LNnormalize -- (200302)
513
514 int LNadd(LARGE_NUMBER_STRUCT *lna, LARGE_NUMBER_STRUCT *lnb,
515 LARGE_NUMBER_STRUCT *ln3)
516 // ln3=ln1+ln2
517 {
518 // BEGIN function LNadd -- (200302)
519 int i, cnt, state = 0, carry = 0;
520 LARGE_NUMBER_STRUCT ln1, ln2;
521
522 if (0 == lna->digitCount)
523 *ln3 = *lnb;
524 else if (0 == lnb->digitCount)
525 *ln3 = *lna;
526 else
527 {
528 // add non-zero numbers
529 ln1 = *lna;
530 ln2 = *lnb;
531 if (ln1.sign != ln2.sign)
532 {
533 // signs different
534 LNnegate(&ln2);
535 state = LNsubtract(&ln1, &ln2, ln3);
536 } // signs different
537 else
538 {
539 // signs are same -- continue adding
540 if (ln1.exponent > ln2.exponent)
541 {
542 // ln1 is biger
543 LNshift(&ln1, 1);
544 LNshift(&ln2, (ln1.exponent - ln2.exponent));
545 } // ln1 is biger
546 else
547 {
548 // ln2 is biger
549 LNshift(&ln2, 1);
550 LNshift(&ln1, (ln2.exponent - ln1.exponent));
551 } // ln2 is biger
552
553 if (ln1.digitCount > ln2.digitCount)
554 {
555 // 1 has more digits
556 for (i = ln1.digitCount - 1; i >= ln2.digitCount; i--)
557 ln3->array[i] = ln1.array[i];
558 cnt = ln2.digitCount;
559 ln3->digitCount = ln1.digitCount;
560 } // 1 has more digits
561 else
562 {
563 // 2 has more digits
564 for (i = ln2.digitCount - 1; i >= ln1.digitCount; i--)
565 ln3->array[i] = ln2.array[i];
566 cnt = ln1.digitCount;
567 ln3->digitCount = ln2.digitCount;
568 } // 2 has more digits
569 // now actually add
570 ln3->exponent = ln1.exponent;
571 for (i = cnt - 1; i >= 0; i--)
572 {
573 // for
574 ln3->array[i] = ln1.array[i] + ln2.array[i] + carry;
575 if (ln3->array[i] > 9)
576 {
577 // set carry
578 carry = 1;
579 ln3->array[i] -= 10;
580 } // set carry
581 else
582 {
583 // reset carry
584 carry = 0;
585 } // reset carry
586 } // for
587 LNnormalize(ln3);
588 } // signs are same -- continue adding
589 } // add non-zero numbers
590 return state;
591 } // END function LNadd -- (200302)
592
593 int LNsubtract(LARGE_NUMBER_STRUCT *lna, LARGE_NUMBER_STRUCT *lnb,
594 LARGE_NUMBER_STRUCT *ln3)
595 // ln3=ln1-ln2
596 {
597 // BEGIN function LNsubtract -- (200303)
598 int toReturn = 0;
599 LARGE_NUMBER_STRUCT ln1, ln2;
600
601 ln1 = *lna;
602 ln2 = *lnb;
603
604 if (0 == lna->digitCount)
605 {
606 // first number is zero
607 *ln3 = *lnb;
608 LNnegate(ln3);
609 } // first number is zero
610 else if (0 == lnb->digitCount)
611 *ln3 = *lna;
612 else
613 {
614 // subtract non-zero numbers
615 if (ln1.sign != ln2.sign)
616 {
617 // signs different
618 LNnegate(&ln2);
619 toReturn = LNadd(&ln1, &ln2, ln3);
620 } // signs different
621 else
622 {
623 // signs are same - so subtract
624 int t, i, tmp, borrow = 0;
625
626 t = LNcompare(&ln1, &ln2);
627 if (0 == t)
628 {
629 // same number - return 9
630 ln3->sign = 1;
631 ln3->exponent = 0;
632 ln3->digitCount = 0;
633 } // same number - return 9
634 else if (0 > t)
635 {
636 // first number smaller
637 toReturn = LNsubtract(&ln2, &ln1, ln3);
638 ln3->sign = -1 * ln3->sign;
639 } // first number smaller
640 else
641 {
642 // everything okay - actually subtract
643 ln3->sign = ln1.sign;
644 ln3->exponent = ln1.exponent;
645 LNshift(&ln2, (ln1.exponent - ln2.exponent));
646 if (ln1.digitCount != ln2.digitCount)
647 if (ln1.digitCount > ln2.digitCount)
648 {
649 // add zeroes to ln2
650 for (i=ln2.digitCount; i < ln1.digitCount; i++)
651 ln2.array[i] = 0;
652 ln2.digitCount = ln1.digitCount;
653 } // add zeroes to ln2
654 else
655 {
656 // add zeroes to ln1
657 for (i=ln1.digitCount; i < ln2.digitCount; i++)
658 ln1.array[i] = 0;
659 ln1.digitCount = ln2.digitCount;
660 } // add zeroes to ln1
661
662 for (i = ln1.digitCount - 1; i >= 0; i--)
663 {
664 // loop through each digit
665 tmp = borrow + ln2.array[i];
666 if (ln1.array[i] > tmp)
667 {
668 // no borrow
669 ln3->array[i] = ln1.array[i] - tmp;
670 borrow = 0;
671 } // no borrow
672 else
673 {
674 // have to borrow
675 ln3->array[i] = (10 + ln1.array[i]) - tmp;
676 borrow = 1;
677 } // have to borrow
678 } // loop through each digit
679 LNnormalize(ln3);
680 } // everything okay - actually subtract
681 } // signs are same - so subtract
682 } // subtract non-zero numbers
683 return toReturn;
684 } // END function LNsubtract -- (200303)
685
686 int LNmultiply(LARGE_NUMBER_STRUCT *ln1, LARGE_NUMBER_STRUCT *ln2,
687 LARGE_NUMBER_STRUCT *ln3)
688 // ln3=ln1*ln2
689 {
690 // BEGIN function LNmultiply -- (200303)
691 if ((0 == ln1->digitCount) || (0 == ln2->digitCount))
692 {
693 // multiply by 0 and get 0
694 ln3->digitCount = 0;
695 ln3->sign = 0;
696 ln3->exponent = 0;
697 ln3->array[0] = 0;
698 } // multiply by 0 and get 0
699 else
700 {
701 // guess we have to multiply
702 int i1, i2, i3, dig, tmp, carry;
703
704 ln3->sign = ln1->sign * ln2->sign;
705 ln3->exponent = ln1->exponent + ln2->exponent;
706 ln3->digitCount = ln1->digitCount + ln2->digitCount;
707 dig = ln3->digitCount - 1;
708 // zero out ln3 so that it can be summed into
709 for (i3=0; i3 <= dig; i3++)
710 ln3->array[i3] = 0;
711 for (i2=ln2->digitCount - 1; i2 >= 0; i2--)
712 {
713 // loop through the second numbers digits
714 carry = 0;
715 i3 = dig--;
716 for (i1=ln1->digitCount - 1; i1 >= 0; i1--)
717 {
718 // process first number
719 tmp = (ln1->array[i1] * ln2->array[i2]) + carry + ln3->array[i3];
720 if (tmp > 9)
721 {
722 // deal with overflow
723 carry = tmp / 10;
724 ln3->array[i3] = (tmp % 10);
725 } // deal with overflow
726 else
727 {
728 // no overflow
729 carry = 0;
730 ln3->array[i3] = tmp;
731 } // no overflow
732 i3--;
733 } // process first number
734 ln3->array[i3] = ln3->array[i3] + carry;
735 } // loop through the second numbers digits
736 LNnormalize(ln3);
737 } // guess we have to multiply
738 return 0;
739 } // END function LNmultiply -- (200303)
740
741 int LNdivide(LARGE_NUMBER_STRUCT *ln1, long int i2, LARGE_NUMBER_STRUCT *ln3)
742 // ln3=ln1/i2
743 {
744 // BEGIN function LNdivide -- (200303)
745 int i, tot = 0;
746
747 ln3->exponent = ln1->exponent;
748 ln3->sign = ln1->sign * ((0 < i2) ? 1 : -1);
749 for (i=0; i < ln1->digitCount; i++)
750 {
751 // for each digit
752 tot += ln1->array[i];
753 if (tot >= i2)
754 {
755 // we can divide
756 ln3->array[i] = tot / i2;
757 tot = (tot % i2) * 10;
758 } // we can divide
759 else
760 {
761 // can't divide yet, so add a zero
762 ln3->array[i] = 0;
763 tot *= 10;
764 } // can't divide yet, so add a zero
765 } // for each digit
766
767 ln3->digitCount = i;
768 while ((LN_MAX_DIGITS > ln3->digitCount) && (0 < tot))
769 {
770 // add zeroes on right
771 if (tot >= i2)
772 {
773 // we can divide
774 ln3->array[ln3->digitCount] = tot / i2;
775 tot = (tot % i2) * 10;
776 } // we can divide
777 else
778 {
779 // can't divide yet, so add a zero
780 ln3->array[ln3->digitCount] = 0;
781 tot *= 10;
782 } // can't divide yet, so add a zero
783 ln3->digitCount++;
784 } // add zeroes on right
785 LNnormalize(ln3);
786 return 0;
787 } // END function LNdivide -- (200303)
788
789 int LNdcnvrt(double d, LARGE_NUMBER_STRUCT *n)
790 // convert a double to Large Number
791 {
792 // BEGIN function LNdcnvrt -- (200303)
793 char str[LN_MAX_DIGITS];
794
795 sprintf(str, "%ld:", d);
796 return LNstr2arr(str, n);
797 } // END function LNdcnvrt -- (200303)
798
799 int LNcopy(LARGE_NUMBER_STRUCT *ln1, LARGE_NUMBER_STRUCT *ln2)
800 // copy from ln1 to ln2
801 {
802 // BEGIN function LNcopy -- (200302)
803 int i;
804
805 ln2->sign = ln1->sign;
806 ln2->exponent = ln1->exponent;
807 ln2->digitCount = ln1->digitCount;
808 for (i=0; i < ln1->digitCount; i++)
809 ln2->array[i] = ln1->array[i];
810 return 0;
811 } // END function LNcopy -- (200302)
812
813
814 void init()
815 {
816 /* FUNCTION init */
817 buffer[0] = '0';
818 buffer[1] = 0;
819 LNstr2arr(buffer, &tot);
820 } /* FUNCTION init */
821
822 int getInput()
823 {
824 /* FUNCTION getInput */
825 int dataReadFlag;
826 int i;
827 int top;
828
829 scanf(" %s ", buffer);
830 if (0 == strcmp("0", buffer))
831 {
832 /* end of file reached */
833 dataReadFlag = FALSE;
834 } /* end of file reached */
835 else
836 {
837 /* read in something */
838 LNstr2arr(buffer, &num);
839 } /* read in something */
840 return (dataReadFlag);
841 } /* FUNCTION getInput */
842
843 void process()
844 {
845 /* FUNCTION process */
846 LNadd(&num, &tot, &tot);
847 } /* FUNCTION process */
848
849 int main()
850 {
851 /* main */
852 int moreToDo;
853 int gotInput = FALSE;
854
855 DEBUG printf("Here 1\n");
856 init();
857 DEBUG printf("Here 2\n");
858 DEBUG printf("tot=");
859 DEBUG LNprint(&tot);
860 DEBUG printf("Here 3\n");
861
862 moreToDo = getInput();
863 while (moreToDo)
864 {
865 /* while */
866 gotInput = TRUE;
867 DEBUG printf("tot=");
868 DEBUG LNprint(&tot);
869 DEBUG printf("num=");
870 DEBUG LNprint(&num);
871 process();
872 moreToDo = getInput();
873 } /* while */
874 // if (gotInput) { LNprint(&tot); }
875 LNprint(&tot);
876
877 return EXIT_SUCCESS;
878 } /* main */
879