home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_09 / 8n09055a < prev    next >
Text File  |  1990-07-24  |  8KB  |  296 lines

  1.  
  2.  
  3. Listing 7:
  4.  
  5. /**************************************************
  6. **                                               **
  7. **          Input Impedance Calculation          **
  8. **                                               **
  9. **         Maynard A. Wright, P. E. 1990         **
  10. **                                               **
  11. **************************************************/
  12.  
  13. /* Stored as "zin.c."  Algorithm used here de-
  14.    scribed in M. A. Wright, "Computation of In-
  15.    put Impedance Using RPN Calculators," IEEE
  16.    1979 Region VI Conference Proceedings. This
  17.    version of the program uses functions from
  18.    the Microsoft graphics library and must there-
  19.    fore be compiled using Microsoft C or QuickC.
  20.    The program also uses function prototypes and
  21.    constant definitions from include file cmplx.h
  22.    and must be linked to library cmplxlib.lib. */
  23.  
  24. #include <graph.h>
  25. #include <math.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <local\cmplx.h>
  31.  
  32. #define DBSCALE 8.68588963  /* convert dB to nepers */
  33. #define PHASCALE 57.29577951 /* convert degrees to
  34.                                 radians */
  35. #define MINVAL 1e-100  /* minimum value of double
  36.                           input */
  37. #define NW '\xC9' /* northwest graphics corner */
  38. #define NE '\xBB' /* northeast graphics corner */
  39. #define SW '\xC8' /* southwest graphics corner */
  40. #define SE '\xBC' /* southeast graphics corner */
  41. #define HZ '\xCD' /* horizontal graphics line */
  42. #define VR '\xBA' /* vertical graphics line */
  43.  
  44. double get_real(char*, short, short);
  45.  
  46. void main(void)
  47.  
  48. {
  49.    /* declarations */
  50.  
  51.    char decision, counter;
  52.    short linectr;
  53.    double atten, phase, length;
  54.    struct cmplx_nmbr calcz, termz, linez, inputz;
  55.  
  56.    /* screen header */
  57.  
  58.    _settextcolor(14);
  59.    _clearscreen(_GCLEARSCREEN);
  60.    _settextposition(3,20);
  61.    printf("%c", NW);
  62.    for(counter = 1; counter <= 37; ++counter)
  63.       printf("%c", HZ);
  64.    printf("%c", NE);
  65.    _settextposition(4,20);
  66.    printf("%c     INPUT IMPEDANCE CALCULATION     %c",
  67.          VR, VR);
  68.    _settextposition(5,20);
  69.    printf("%c", SW);
  70.    for(counter = 1; counter <= 37; ++counter)
  71.       printf("%c", HZ);
  72.    printf("%c", SE);
  73.  
  74.    /* input data from keyboard */
  75.  
  76.    termz.real = get_real("ENTER REAL PART OF
  77. TERMINATING IMPEDANCE IN OHMS:", 8, 17);
  78.    if(termz.real == 0.) /* make input nonzero to
  79.                            avoid sub- */
  80.       termz.real = MINVAL; /* sequent math errors */
  81.    termz.imag = get_real("ENTER IMAGINARY PART OF
  82. TERMINATING IMPEDANCE IN OHMS:", 9, 12);
  83.    if(termz.imag == 0.)
  84.       termz.imag = MINVAL;
  85.    do
  86.  
  87.    {
  88.       linez.real = get_real("ENTER REAL PART OF
  89. CHARACTERISTIC IMPEDANCE OF LINE IN OHMS:", 10, 6);
  90.       if(linez.real == 0.)
  91.          linez.real = MINVAL;
  92.       linez.imag = get_real("ENTER IMAGINARY PART OF
  93. CHARACTERISTIC IMPEDANCE OF LINE IN OHMS:", 11, 1);
  94.       if(linez.imag == 0.)
  95.          linez.imag = MINVAL;
  96.       atten = get_real("ENTER ATTENUATION PER UNIT
  97. LENGTH OF LINE:", 12, 24);
  98.       if(atten == 0.)
  99.          atten = MINVAL;
  100.       _settextposition(13,22);
  101.       printf("ATTENUATION IN DECIBELS OR NEPERS? ");
  102.       printf("(D OR N):");
  103.       do
  104.  
  105.       {
  106.          fflush(stdin);
  107.          decision = getche();
  108.          if(decision != 'D' && decision != 'd' &&
  109.                decision != 'N' && decision != 'n')
  110.          {
  111.             _settextposition(14,22);
  112.             printf("      !!! RESPONSE MUST BE D OR 
  113.                    N !!!");
  114.             printf("          ");
  115.             _settextposition(13,66);
  116.          }
  117.       }
  118.  
  119.       while(decision != 'D' && decision != 'd' &&
  120.             decision != 'N' && decision != 'n');
  121.       if(decision == 'D' || decision == 'd')
  122.          atten /= DBSCALE;
  123.       phase = get_real("ENTER PHASE SHIFT PER UNIT 
  124. LENGTH OF LINE:", 14, 24);
  125.       if(phase == 0.)
  126.          phase = MINVAL;
  127.       _settextposition(15,22);
  128.       printf("PHASE SHIFT IN DEGREES OR RADIANS? ");
  129.       printf("(D OR R):");
  130.       do
  131.  
  132.       {
  133.          fflush(stdin);
  134.          decision = getche();
  135.          if(decision != 'D' && decision != 'd' &&
  136.                decision != 'R' && decision != 'r')
  137.          {
  138.             _settextposition(16,22);
  139.             printf("      !!! RESPONSE MUST BE D OR 
  140.                    R !!!");
  141.             printf("          ");
  142.             _settextposition(15,66);
  143.          }
  144.       }
  145.  
  146.       while(decision != 'D' && decision != 'd' &&
  147.             decision != 'R' && decision != 'r');
  148.       if(decision == 'D' || decision == 'd')
  149.          phase /= PHASCALE;
  150.       length = get_real("                    ENTER 
  151. LENGTH OF LINE:", 16, 25);
  152.       _settextposition(17,22);
  153.       printf("                                    
  154.                   ");
  155.       if(length == 0.)
  156.          length = MINVAL;
  157.  
  158.       /* calculate attenuation and phase shift of line */
  159.  
  160.       atten *= length;
  161.       phase *= length;
  162.  
  163.       /* calculate termz / linez */
  164.  
  165.       calcz = cmplxdiv(termz, linez);
  166.  
  167.       /* handle case of angle = 0 */
  168.  
  169.       if(calcz.imag == 0.)
  170.          calcz.imag = 1e-100;
  171.  
  172.       /* calculate inverse complex hyperbolic tangent */
  173.  
  174.       calcz = catnh(calcz);
  175.  
  176.       /* add in line constants */
  177.  
  178.       calcz.real += atten;
  179.       calcz.imag += phase;
  180.  
  181.       /* calculate complex hyperbolic tangent */
  182.  
  183.       calcz = ctanh(calcz);
  184.  
  185.       /* denormalize solution to obtain input impedance */
  186.  
  187.       inputz = cmplxmul(calcz, linez);
  188.  
  189.       /* print results */
  190.  
  191.       for(linectr = 19; linectr <= 20; ++linectr)
  192.  
  193.       {
  194.          _settextposition(linectr, 51);
  195.          printf("                              ");
  196.       }
  197.  
  198.       _settextposition(19,20);
  199.       printf("REAL PART OF INPUT ");
  200.       printf("IMPEDANCE = %+lG OHMS",inputz.real);
  201.       _settextposition(20,15);
  202.       printf("IMAGINARY PART OF INPUT ");
  203.       printf("IMPEDANCE = %+lG OHMS\n\n",inputz.imag);
  204.  
  205.       /* loop for another section or terminate execution */
  206.  
  207.       _settextposition(22,4);
  208.       printf("USE INPUT IMPEDANCE AS TERMINAL IMPED");
  209.       printf("ANCE FOR ANOTHER SECTION?  ");
  210.       _settextposition(22,67);
  211.       decision = 0;
  212.       fflush(stdin);
  213.       decision = getche();
  214.       if(decision == 'Y' || decision == 'y')
  215.  
  216.       {
  217.          termz.real = inputz.real;
  218.          termz.imag = inputz.imag;
  219.  
  220.          /* set up screen for new input */
  221.  
  222.          for(linectr = 8; linectr <= 16; ++linectr)
  223.  
  224.          {
  225.             _settextposition(linectr, 66);
  226.             printf("               ");
  227.          }
  228.  
  229.          _settextposition(19,20);
  230.          printf("                         ");
  231.          printf("                         ");
  232.          _settextposition(20,15);
  233.          printf("                              ");
  234.          printf("                         ");
  235.          _settextposition(8,66);
  236.          printf("%+lG", inputz.real);
  237.          _settextposition(9,66);
  238.          printf("%+lG", inputz.imag);
  239.       }
  240.    }
  241.  
  242.    while(decision == 'Y' || decision == 'y');
  243.    printf("\n");
  244. }
  245.  
  246. double get_real(char *num_string, short x, short y)
  247.  
  248. {
  249.    struct rccoord rcoord;
  250.    double value;
  251.    char *phlag = '\0', *remainder, string[20];
  252.    short inrow, incol;
  253.  
  254.    do
  255.  
  256.    {
  257.       if(phlag)
  258.  
  259.       {
  260.          _settextposition((x + 1), 1);
  261.          printf("                                  
  262.                       ");
  263.          printf("                                  
  264.                      ");
  265.          _settextposition((x + 1), 25);
  266.          printf("!!! ERROR IN INPUT: PLEASE REENTER
  267.                 !!!");
  268.       }
  269.  
  270.       _settextposition(x, y);
  271.       printf("%s", num_string);
  272.       printf("          ");
  273.       rcoord = _gettextposition();
  274.       inrow = rcoord.row;
  275.       incol = rcoord.col + strlen(num_string);
  276.       _settextposition(inrow, incol);
  277.       ++phlag;
  278.       fflush(stdin);
  279.    }
  280.  
  281.    while((!(value = strtod((gets(string)),\
  282.          &remainder)) && *remainder) || value ==
  283.          HUGE_VAL);
  284.    _settextposition(inrow, incol);
  285.    printf("%G         ", value);
  286.    if(phlag)
  287.  
  288.    {
  289.       _settextposition((x+1), 25);
  290.       printf("                                      ");
  291.    }
  292.  
  293.    return(value);
  294. }
  295.  
  296.