home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l180 / 1.ddi / FRACTION.BAS < prev    next >
Encoding:
BASIC Source File  |  1989-02-07  |  13.5 KB  |  372 lines

  1.   ' ************************************************
  2.   ' **  Name:          FRACTION                   **
  3.   ' **  Type:          Toolbox                    **
  4.   ' **  Module:        FRACTION.BAS               **
  5.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  6.   ' ************************************************
  7.   '
  8.   ' Demonstrates a collection of functions and subprograms
  9.   ' for working with fractions.
  10.   '
  11.   ' USAGE:           No command line parameters
  12.   ' .MAK FILE:       (none)
  13.   ' PARAMETERS:      (none)
  14.   ' VARIABLES:       a          Structure of type Fraction
  15.   '                  b          Structure of type Fraction
  16.   '                  c          Structure of type Fraction
  17.   '                  f$         Input string for fraction problems
  18.   '                  fa$        First fraction in string format
  19.   '                  fb$        Second fraction in string format
  20.   '                  operator$  Function indicator
  21.   '                  fc$        Resultant fraction in string output form
  22.   
  23.   ' Data structure definitions
  24.     TYPE Fraction
  25.         Num AS LONG
  26.         Den AS LONG
  27.     END TYPE
  28.   
  29.   ' Subprograms
  30.     DECLARE SUB FractionReduce (a AS Fraction)
  31.     DECLARE SUB String2Fraction (f$, a AS Fraction)
  32.     DECLARE SUB FractionAdd (a AS Fraction, b AS Fraction, c AS Fraction)
  33.     DECLARE SUB FractionDiv (a AS Fraction, b AS Fraction, c AS Fraction)
  34.     DECLARE SUB FractionMul (a AS Fraction, b AS Fraction, c AS Fraction)
  35.     DECLARE SUB FractionSub (a AS Fraction, b AS Fraction, c AS Fraction)
  36.     DECLARE SUB SplitFractions (f$, fa$, operator$, fb$)
  37.   
  38.   ' Functions
  39.     DECLARE FUNCTION Fraction2String$ (a AS Fraction)
  40.     DECLARE FUNCTION GreatestComDiv& (n1&, n2&)
  41.     DECLARE FUNCTION LeastComMul& (n1&, n2&)
  42.   
  43.   ' Data structures
  44.     DIM a AS Fraction
  45.     DIM b AS Fraction
  46.     DIM c AS Fraction
  47.   
  48.   ' Demonstrate the LeastComMul& function
  49.     CLS
  50.     PRINT "LeastComMul&(21&, 49&)    =", LeastComMul&(21&, 49&)
  51.     PRINT
  52.   
  53.   ' Demonstrate the GreatestComDiv& function
  54.     PRINT "GreatestComDiv&(21&, 49&) =", GreatestComDiv&(21&, 49&)
  55.     PRINT
  56.   
  57.   ' Demonstrate the fraction routines
  58.     DO
  59.         PRINT
  60.         PRINT "Enter a fraction problem, or simply press Enter"
  61.         PRINT "Example: 2/3 + 4/5"
  62.         PRINT
  63.         LINE INPUT f$
  64.         IF INSTR(f$, "/") = 0 THEN
  65.             EXIT DO
  66.         END IF
  67.         SplitFractions f$, fa$, operator$, fb$
  68.         String2Fraction fa$, a
  69.         String2Fraction fb$, b
  70.         SELECT CASE operator$
  71.         CASE "+"
  72.             FractionAdd a, b, c
  73.         CASE "-"
  74.             FractionSub a, b, c
  75.         CASE "*"
  76.             FractionMul a, b, c
  77.         CASE "/"
  78.             FractionDiv a, b, c
  79.         CASE ELSE
  80.             BEEP
  81.         END SELECT
  82.         fc$ = Fraction2String$(c)
  83.         PRINT "Result (reduced to lowest terms) is "; fc$
  84.     LOOP
  85.  
  86.   ' ************************************************
  87.   ' **  Name:          Fraction2String$           **
  88.   ' **  Type:          Function                   **
  89.   ' **  Module:        FRACTION.BAS               **
  90.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  91.   ' ************************************************
  92.   '
  93.   ' Converts a type Fraction variable to a string.
  94.   '
  95.   ' EXAMPLE OF USE:  fa$ = Fraction2String$(a)
  96.   ' PARAMETERS:      a          Structure of type Fraction
  97.   ' VARIABLES:       (none)
  98.   ' MODULE LEVEL
  99.   '   DECLARATIONS:  TYPE Fraction
  100.   '                     Num AS LONG
  101.   '                     Den AS LONG
  102.   '                  END TYPE
  103.   '
  104.   '                  DECLARE FUNCTION Fraction2String$ (a AS Fraction)
  105.   '
  106.     FUNCTION Fraction2String$ (a AS Fraction) STATIC
  107.         Fraction2String$ = STR$(a.Num) + "/" + STR$(a.Den)
  108.     END FUNCTION
  109.  
  110.   ' ************************************************
  111.   ' **  Name:          FractionAdd                **
  112.   ' **  Type:          Subprogram                 **
  113.   ' **  Module:        FRACTION.BAS               **
  114.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  115.   ' ************************************************
  116.   '
  117.   ' Adds two fractions and reduces the result to lowest terms.
  118.   '
  119.   ' EXAMPLE OF USE:  FractionAdd a, b, c
  120.   ' PARAMETERS:      a          First fraction to add
  121.   '                  b          Second fraction to add
  122.   '                  c          Resulting fraction
  123.   ' VARIABLES:       (none)
  124.   ' MODULE LEVEL
  125.   '   DECLARATIONS:  TYPE Fraction
  126.   '                     Num AS LONG
  127.   '                     Den AS LONG
  128.   '                  END TYPE
  129.   '
  130.   '     DECLARE SUB FractionReduce (a AS Fraction)
  131.   '     DECLARE SUB FractionAdd (a AS Fraction, b AS Fraction, c AS Fraction)
  132.   '     DECLARE FUNCTION GreatestComDiv& (n1&, n2&)
  133.   '
  134.     SUB FractionAdd (a AS Fraction, b AS Fraction, c AS Fraction)
  135.         c.Num = a.Num * b.Den + a.Den * b.Num
  136.         c.Den = a.Den * b.Den
  137.         FractionReduce c
  138.     END SUB
  139.  
  140.   ' ************************************************
  141.   ' **  Name:          FractionDiv                **
  142.   ' **  Type:          Subprogram                 **
  143.   ' **  Module:        FRACTION.BAS               **
  144.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  145.   ' ************************************************
  146.   '
  147.   ' Divides two fractions and reduces the result to
  148.   ' lowest terms.
  149.   '
  150.   ' EXAMPLE OF USE:  FractionDiv a, b, c
  151.   ' PARAMETERS:      a          First fraction
  152.   '                  b          Fraction to divide into first
  153.   '                  c          Resulting fraction
  154.   ' VARIABLES:       (none)
  155.   ' MODULE LEVEL
  156.   '   DECLARATIONS:  TYPE Fraction
  157.   '                     Num AS LONG
  158.   '                     Den AS LONG
  159.   '                  END TYPE
  160.   '
  161.   '     DECLARE SUB FractionReduce (a AS Fraction)
  162.   '     DECLARE SUB FractionDiv (a AS Fraction, b AS Fraction, c AS Fraction)
  163.   '     DECLARE FUNCTION GreatestComDiv& (n1&, n2&)
  164.   '
  165.     SUB FractionDiv (a AS Fraction, b AS Fraction, c AS Fraction)
  166.         c.Num = a.Num * b.Den
  167.         c.Den = a.Den * b.Num
  168.         FractionReduce c
  169.     END SUB
  170.  
  171.   ' ************************************************
  172.   ' **  Name:          FractionMul                **
  173.   ' **  Type:          Subprogram                 **
  174.   ' **  Module:        FRACTION.BAS               **
  175.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  176.   ' ************************************************
  177.   '
  178.   ' Multiplies two fractions and reduces the result to
  179.   ' lowest terms.
  180.   '
  181.   ' EXAMPLE OF USE:  FractionMul a, b, c
  182.   ' PARAMETERS:      a          First fraction to multiply
  183.   '                  b          Second fraction to multiply
  184.   '                  c          Resulting fraction
  185.   ' VARIABLES:       (none)
  186.   ' MODULE LEVEL
  187.   '   DECLARATIONS:  TYPE Fraction
  188.   '                     Num AS LONG
  189.   '                     Den AS LONG
  190.   '                  END TYPE
  191.   '
  192.   '     DECLARE SUB FractionReduce (a AS Fraction)
  193.   '     DECLARE SUB FractionMul (a AS Fraction, b AS Fraction, c AS Fraction)
  194.   '     DECLARE FUNCTION GreatestComDiv& (n1&, n2&)
  195.   '
  196.     SUB FractionMul (a AS Fraction, b AS Fraction, c AS Fraction)
  197.         c.Num = a.Num * b.Num
  198.         c.Den = a.Den * b.Den
  199.         FractionReduce c
  200.     END SUB
  201.  
  202.   ' ************************************************
  203.   ' **  Name:          FractionReduce             **
  204.   ' **  Type:          Subprogram                 **
  205.   ' **  Module:        FRACTION.BAS               **
  206.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  207.   ' ************************************************
  208.   '
  209.   ' Reduces a fraction to its lowest terms.
  210.   '
  211.   ' EXAMPLE OF USE:  FractionReduce a
  212.   ' PARAMETERS:      a          Fraction to reduce
  213.   ' VARIABLES:       d&         Greatest common divisor of the numerator and
  214.   '                             denominator
  215.   ' MODULE LEVEL
  216.   '   DECLARATIONS:  TYPE Fraction
  217.   '                     Num AS LONG
  218.   '                     Den AS LONG
  219.   '                  END TYPE
  220.   '
  221.   '                  DECLARE SUB FractionReduce (a AS Fraction)
  222.   '                  DECLARE FUNCTION GreatestComDiv& (n1&, n2&)
  223.   '
  224.     SUB FractionReduce (a AS Fraction)
  225.         d& = GreatestComDiv&(a.Num, a.Den)
  226.         a.Num = a.Num / d&
  227.         a.Den = a.Den / d&
  228.     END SUB
  229.  
  230.   ' ************************************************
  231.   ' **  Name:          FractionSub                **
  232.   ' **  Type:          Subprogram                 **
  233.   ' **  Module:        FRACTION.BAS               **
  234.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  235.   ' ************************************************
  236.   '
  237.   ' Subtracts two fractions and reduces the result to
  238.   ' lowest terms.
  239.   '
  240.   ' EXAMPLE OF USE:  FractionSub a, b, c
  241.   ' PARAMETERS:      a          First fraction
  242.   '                  b          Fraction to subtract from the first
  243.   '                  c          Resulting fraction
  244.   ' VARIABLES:       (none)
  245.   ' MODULE LEVEL
  246.   '   DECLARATIONS:  TYPE Fraction
  247.   '                     Num AS LONG
  248.   '                     Den AS LONG
  249.   '                  END TYPE
  250.   '
  251.   '     DECLARE SUB FractionReduce (a AS Fraction)
  252.   '     DECLARE SUB FractionSub (a AS Fraction, b AS Fraction, c AS Fraction)
  253.   '     DECLARE FUNCTION GreatestComDiv& (n1&, n2&)
  254.   '
  255.     SUB FractionSub (a AS Fraction, b AS Fraction, c AS Fraction)
  256.         c.Num = a.Num * b.Den - a.Den * b.Num
  257.         c.Den = a.Den * b.Den
  258.         FractionReduce c
  259.     END SUB
  260.  
  261.   ' ************************************************
  262.   ' **  Name:          GreatestComDiv&            **
  263.   ' **  Type:          Function                   **
  264.   ' **  Module:        FRACTION.BAS               **
  265.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  266.   ' ************************************************
  267.   '
  268.   ' Returns the greatest common divisor of two long integers.
  269.   '
  270.   ' EXAMPLE OF USE:  gcd& = GreatestComDiv& (n1&, n2&)
  271.   ' PARAMETERS:      n1&        First long integer
  272.   '                  n2&        Second long integer
  273.   ' VARIABLES:       ta&        Working copy of n1&
  274.   '                  tb&        Working copy of n2&
  275.   '                  tc&        Working variable
  276.   ' MODULE LEVEL
  277.   '   DECLARATIONS:  DECLARE FUNCTION GreatestComDiv& (n1&, n2&)
  278.   '
  279.     FUNCTION GreatestComDiv& (n1&, n2&)
  280.         ta& = n1&
  281.         tb& = n2&
  282.         DO
  283.             tc& = ta& MOD tb&
  284.             ta& = tb&
  285.             tb& = tc&
  286.         LOOP WHILE tc&
  287.         GreatestComDiv& = ta&
  288.     END FUNCTION
  289.  
  290.   ' ************************************************
  291.   ' **  Name:          LeastComMul&               **
  292.   ' **  Type:          Function                   **
  293.   ' **  Module:        FRACTION.BAS               **
  294.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  295.   ' ************************************************
  296.   '
  297.   ' Returns the least common multiple of two long integers.
  298.   '
  299.   ' EXAMPLE OF USE:  lcm& = LeastComMul& (n1&, n2&)
  300.   ' PARAMETERS:      n1&         First long integer
  301.   '                  n2&         Second long integer
  302.   ' VARIABLES:       (none)
  303.   ' MODULE LEVEL
  304.   '   DECLARATIONS:  DECLARE FUNCTION LeastComMul& (n1&, n2&)
  305.   '
  306.     FUNCTION LeastComMul& (n1&, n2&)
  307.         LeastComMul& = ABS(n1& * n2& / GreatestComDiv&(n1&, n2&))
  308.     END FUNCTION
  309.  
  310.   ' ************************************************
  311.   ' **  Name:          SplitFractions             **
  312.   ' **  Type:          Subprogram                 **
  313.   ' **  Module:        FRACTION.BAS               **
  314.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  315.   ' ************************************************
  316.   '
  317.   ' Splits an input fraction problem string into
  318.   ' three strings representing each of the two
  319.   ' fractions and a one-character string of the
  320.   ' operation given.
  321.   '
  322.   ' EXAMPLE OF USE: SplitFractions f$, fa$, operator$, fb$
  323.   ' PARAMETERS:     f$         Input string from the FRACTIONS demonstration
  324.   '                            program
  325.   '                 fa$        First fraction, extracted from f$
  326.   '                 operator$  Mathematical operation symbol, from f$
  327.   '                 fb$        Second fraction, extracted from f$
  328.   ' VARIABLES:      i%         Looping index
  329.   '                 ndx%       Index to mathematical operation symbol
  330.   ' MODULE LEVEL
  331.   '   DECLARATIONS: DECLARE SUB SplitFractions (f$, fa$, operator$, fb$)
  332.   '
  333.     SUB SplitFractions (f$, fa$, operator$, fb$)
  334.         fa$ = ""
  335.         fb$ = ""
  336.         operator$ = ""
  337.         FOR i% = 1 TO 4
  338.             ndx% = INSTR(f$, MID$("+-*/", i%, 1))
  339.             IF ndx% THEN
  340.                 IF i% = 4 THEN
  341.                     ndx% = INSTR(ndx% + 1, f$, "/")
  342.                 END IF
  343.                 fa$ = LEFT$(f$, ndx% - 1)
  344.                 fb$ = MID$(f$, ndx% + 1)
  345.                 operator$ = MID$(f$, ndx%, 1)
  346.                 EXIT FOR
  347.             END IF
  348.         NEXT i%
  349.     END SUB
  350.  
  351.   ' ************************************************
  352.   ' **  Name:          String2Fraction            **
  353.   ' **  Type:          Subprogram                 **
  354.   ' **  Module:        FRACTION.BAS               **
  355.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  356.   ' ************************************************
  357.   '
  358.   ' Converts a string to a type Fraction variable.
  359.   '
  360.   ' EXAMPLE OF USE: String2Fraction f$, a
  361.   ' PARAMETERS:     f$         String representation of a fraction
  362.   '                 a          Structure of type Fraction
  363.   ' VARIABLES:      (none)
  364.   ' MODULE LEVEL
  365.   '   DECLARATIONS:  DECLARE SUB String2Fraction (f$, a AS Fraction)
  366.   '
  367.     SUB String2Fraction (f$, a AS Fraction)
  368.         a.Num = VAL(f$)
  369.         a.Den = VAL(MID$(f$, INSTR(f$, "/") + 1))
  370.     END SUB
  371.  
  372.