home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 177.lha / Newton_v1.0 / io.c < prev    next >
C/C++ Source or Header  |  1988-04-28  |  4KB  |  176 lines

  1. /* io.c:    Input/Output functions.
  2.  *
  3.  * Written by Daniel Barrett.  100% PUBLIC DOMAIN. */
  4.  
  5. #include "decl.h"
  6.  
  7.  
  8. PrintComplex(comp)
  9. /* Print a complex number. */
  10. complex comp;
  11. {
  12.     double fabs();
  13.  
  14.     if (comp.n[REAL] >= 0)
  15.         printf(" ");
  16.     printf("%f%c%fi",    comp.n[REAL], 
  17.                 (comp.n[IMAG] < 0.0) ? '-' : '+',
  18.                 fabs(comp.n[IMAG]));
  19. }
  20.  
  21.     
  22. PrintPoly(poly, n)
  23. /* Print the polynomial in a nice format. */
  24. complex poly[];
  25. int n;
  26. {
  27.     int i;
  28.  
  29.     printf("\nYour polynomial is:\n");
  30.     printf("--------------------\n");
  31.     for (i=0; i<n-1; i++) {
  32.         PrintComplex(poly[i]);
  33.         printf("    * X^%d    +\n", n-i);
  34.     }
  35.     PrintComplex(poly[n-1]);
  36.     printf("    * X    +\n");
  37.     PrintComplex(poly[n]);
  38.     printf("\n\n");
  39. }
  40.  
  41.     
  42. ReadPoly(poly, degree, epsilon)
  43. /* Read all data from the user:  the degree of the polynomial, the
  44.  * accuracy desired, and the polynomial coefficients. */
  45. complex poly[];
  46. int *degree;
  47. double *epsilon;
  48. {
  49.     ReadDegree(degree);
  50.     ReadEpsilon(epsilon);
  51.     TellUserWhatToDo();
  52.     ReadCoefficients(poly, *degree);
  53. }
  54.  
  55.     
  56. ReadDegree(degree)
  57. /* Prompt user for the degree of the polynomial.  Read it. */
  58. int *degree;
  59. {
  60.     char buf[BUFSIZ];
  61.  
  62.     *degree = 0;
  63.     do {
  64.         printf("Degree of polynomial? (1..%d): ", MAX_DEGREE);
  65.         gets(buf);
  66.         *degree = atoi(buf);
  67.     }while (*degree <= 0 || *degree > MAX_DEGREE);
  68. }
  69.  
  70.     
  71. ReadEpsilon(epsilon)
  72. /* Prompt user for epsilon, the desired accuracy.  Read it.  If no
  73.  * data, set epsilon to the default value. */
  74. double *epsilon;
  75. {
  76.     char buf[BUFSIZ];
  77.     double atof();
  78.  
  79.     do {
  80.         *epsilon = EPSILON_DEFAULT;
  81.         printf("Accuracy desired? [<RETURN> = %1.7f]: ", *epsilon);
  82.         gets(buf);
  83.          if (strlen(buf))
  84.             *epsilon = atof(buf);
  85.     }while (*epsilon <= 0.0);
  86. }
  87.  
  88.     
  89. ReadCoefficients(poly, degree)
  90. /* Read in the coefficients of the polynomial "poly".  NOTE that
  91.  *  the array "poly" is backwards... poly[0] is the coefficient
  92.  *  of X^degree, and so on. 
  93.  * I use "strtok()", my version of the UNIX function, to get the input. */
  94. complex poly[];
  95. int degree;
  96. {
  97.     char buf[BUFSIZ], *tokeReal=NULL, *tokeImag=NULL, *strtok();
  98.     static char separators[] = " \t\n\r";
  99.     register int i;
  100.     double atof();
  101.  
  102.     for (i=0; i <= degree; i++) {
  103. Again:        printf("X^%d coefficient: ", degree-i);
  104.         gets(buf);
  105.         tokeReal = strtok(buf, separators);
  106.         if (!tokeReal)
  107.             goto Again;
  108.         else if (NeedsHelp(tokeReal)) {
  109.             Help();
  110.             goto Again;
  111.         }
  112.         tokeImag = strtok((char *)NULL, separators);
  113.         AssignComplex(&poly[i],    atof(tokeReal),
  114.                     tokeImag ? atof(tokeImag) : 0.0);
  115.     }
  116. }
  117.  
  118.  
  119. PrintRoot(root, i)
  120. /* Print the value of root "i" of the polynomial. */
  121. complex root;
  122. int i;
  123. {
  124.     printf("Root %d:  ", i);
  125.     if (i<10)
  126.         printf(" ");
  127.     PrintComplex(root);
  128.     printf("\n");
  129. }
  130.  
  131.     
  132. TellUserWhatToDo()
  133. {
  134.     printf("Enter the coefficients of your polynomial.  ");
  135.     printf("Type \"H\" for help.\n");
  136. }
  137.  
  138.     
  139. static char *helpMessage[] = {
  140. "",
  141. "Enter the coefficients of your polynomial, one coefficient per line.",
  142. "Note that \"X^n\" means \"X to the nth power\".",
  143. "If the coefficient is REAL, simply enter it and press <RETURN>.",
  144. "If the coefficient is COMPLEX, enter its real and imaginary terms,",
  145. " separated by a space.  (Do not use the letter \"i\".)",
  146. "",
  147. NULL
  148. };
  149.  
  150.     
  151. Help()
  152. /* Print the above help message. */
  153. {
  154.     char **s = helpMessage;
  155.     do
  156.         puts(*s);
  157.     while (*(s++));
  158. }
  159.  
  160.  
  161. NeedsHelp(s)
  162. /* Does the user need help? */
  163. char *s;
  164. {
  165.     return(EQUAL(s, "H") | EQUAL(s, "h") | EQUAL(s, "?"));
  166. }
  167.  
  168.     
  169. Version()
  170. {
  171.     printf("Newton V1.0");
  172.     printf(" by Daniel Barrett.  100%% PUBLIC DOMAIN.\n");
  173.     printf("Estimate the roots of a polynomial numerically.\n");
  174.     printf("Type ^C <RETURN> to abort this program.\n\n");
  175. }
  176.