home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_03 / 8n03073a < prev    next >
Text File  |  1990-03-20  |  14KB  |  609 lines

  1. *****Listing 1*****
  2.  
  3.  
  4.        /*************************************************************
  5.        *
  6.        *       file d:\tc\cujds.c
  7.        *
  8.        *       Functions: This file contains
  9.        *          main
  10.        *          display_belief_vector
  11.        *          clear_belief_vector
  12.        *          enter_belief_vector
  13.        *          combine_using_dempsters_rule
  14.        *
  15.        *       Purpose:
  16.        *          This program demonstrates how to implement Dempster's
  17.        *          rule of combination.
  18.        *
  19.        *       NOTE: This is written for Borland's Turbo C
  20.        *             Version 1.5.  This allows us to use some
  21.        *             nice user interface functions.  The actual
  22.        *             combination code is compiler independent.
  23.        *
  24.        *************************************************************/
  25.  
  26.  
  27. extern unsigned int _stklen = 40000;
  28.  
  29.  
  30. #include "d:\tc\include\stdio.h"
  31. #include "d:\tc\include\io.h"
  32. #include "d:\tc\include\fcntl.h"
  33. #include "d:\tc\include\dos.h"
  34. #include "d:\tc\include\math.h"
  35. #include "d:\tc\include\graphics.h"
  36. #include "d:\tc\include\conio.h"
  37. #include "d:\tc\include\sys\stat.h"
  38.  
  39. #define LENGTH_OF_BELIEF_VECTOR 8
  40.  
  41.  
  42.  
  43.  
  44. main()
  45. {
  46.  
  47.    char   response[80];
  48.  
  49.    int    choice,
  50.           i,
  51.           j,
  52.           not_finished;
  53.  
  54.    short  place;
  55.  
  56.  
  57.    float a[LENGTH_OF_BELIEF_VECTOR],
  58.          belief,
  59.          v[LENGTH_OF_BELIEF_VECTOR];
  60.  
  61.  
  62.  
  63.    textbackground(1);
  64.    textcolor(7);
  65.    clrscr();
  66.  
  67.    not_finished = 1;
  68.    while(not_finished){
  69.  
  70.       clrscr();
  71.       printf("\n> You may now either:");
  72.       printf("\n            1. Start the process");
  73.       printf("\n            2. Enter more assertions");
  74.       printf("\n            3. Exit program");
  75.       printf("\n        _\b");
  76.       get_integer(&choice);
  77.  
  78.       switch (choice){
  79.  
  80.          case 1:
  81.             clear_belief_vector(v);
  82.             clear_belief_vector(a);
  83.             clrscr();
  84.             enter_belief_vector(v, 1);
  85.             clrscr();
  86.             enter_belief_vector(a, 1);
  87.             clrscr();
  88.             printf("\n> Initial Belief Vector\n");
  89.             display_belief_vector(v);
  90.             printf("\n> Second Belief Vector\n");
  91.             display_belief_vector(a);
  92.             combine_using_dempsters_rule(v, a);
  93.             printf("\n> Resultant Belief Vector\n");
  94.             display_belief_vector(v);
  95.             break;
  96.  
  97.          case 2:
  98.             clrscr();
  99.             clear_belief_vector(a);
  100.             enter_belief_vector(a, 1);
  101.             clrscr();
  102.             printf("\n> Initial Belief Vector\n");
  103.             display_belief_vector(v);
  104.             printf("\n> Second Belief Vector\n");
  105.             display_belief_vector(a);
  106.             combine_using_dempsters_rule(v, a);
  107.             printf("\n> Resultant Belief Vector\n");
  108.             display_belief_vector(v);
  109.             break;
  110.  
  111.          case 3:
  112.             not_finished = 0;
  113.             break;
  114.  
  115.       }  /* ends switch choice  */
  116.    }     /* ends while not_finished  */
  117. }        /* ends main */
  118.  
  119.  
  120.  
  121. clear_belief_vector(v)
  122.    float  v[];
  123. {
  124.    int i;
  125.  
  126.    for(i=0; i<LENGTH_OF_BELIEF_VECTOR; i++)
  127.       v[i] = 0.0;
  128. }  /*  ends clear_belief_vector  */
  129.  
  130.  
  131.  
  132.  
  133. display_belief_vector(v)
  134.    float  v[];
  135. {
  136.    int i, j;
  137.    char response[80];
  138.  
  139.    j=1;
  140.    for(i=0; i<LENGTH_OF_BELIEF_VECTOR; i++){
  141.       if((j%5) == 0){
  142.          printf("\n");
  143.          j++;
  144.       }
  145.       if(v[i] > 0.0001){
  146.          printf("  [%3d]=%6f", i, v[i]);
  147.          j++;
  148.       }
  149.    }
  150.    printf("\n   Hit RETURN to continue");
  151.    read_string(response);
  152. }  /*  ends display_belief_vector  */
  153.  
  154.  
  155.  
  156.  
  157. enter_belief_vector(v, line)
  158.    float v[];
  159.    int   line;
  160. {
  161.    int   i,
  162.          not_finished,
  163.          y;
  164.    float value;
  165.    y = line;
  166.  
  167.    printf("\n> ENTER BELIEF VECTOR");
  168.  
  169.    printf("\n> Enter the place (RETURN) and value (RETURN)");
  170.    printf("\n> (Enter -1 for place when you're finished)");
  171.  
  172.    not_finished = 1;
  173.    while(not_finished){
  174.       printf("\n   [___]=______");
  175.       y = wherey();
  176.       gotoxy(5, y);
  177.       get_integer(&i);
  178.       gotoxy(10, y);
  179.       get_float(&value);
  180.  
  181.       if(i != -1){
  182.          v[i] = value;
  183.       }  /* ends if i 1+ -1  */
  184.       else
  185.          not_finished = 0;
  186.  
  187.    }  /* ends while not_finished  */
  188. }     /* ends enter_belief_vector  */
  189.  
  190.  
  191.  
  192.  
  193.        /************************************************************
  194.        *
  195.        *   This is the function that implements Demptser's rule
  196.        *   of combination.
  197.        *   vector1 holds the original beliefs and will hold the
  198.        *   result of the combination.
  199.        *
  200.        ************************************************************/
  201.  
  202. combine_using_dempsters_rule(vector1, vector2)
  203.    float vector1[LENGTH_OF_BELIEF_VECTOR],
  204.          vector2[LENGTH_OF_BELIEF_VECTOR];
  205. {
  206. float denominator,
  207.       sum_vector[LENGTH_OF_BELIEF_VECTOR];
  208.  
  209. int   a,
  210.       i,
  211.       place;
  212.  
  213. /* set the sums to zero */
  214. for(i=0; i<LENGTH_OF_BELIEF_VECTOR; i++)
  215.    sum_vector[i] = 0.0;
  216.  
  217. /* Now go through the intersection tableau.      */
  218. /* Look for the intersection of non-zero beliefs */
  219. /* and save their products.                      */
  220.  
  221. for(a=1; a<LENGTH_OF_BELIEF_VECTOR; a++){
  222.    if(vector2[a] > 0.0){
  223.       for(i=0; i<LENGTH_OF_BELIEF_VECTOR; i++){
  224.          place = i & a;
  225.          if(vector1[i] > 0.0)
  226.           sum_vector[place] = (vector1[i] * vector2[a]) + sum_vector[place];
  227.       }  /* ends loop over i */
  228.    }  /* ends if vector2[a] > 0.0 */
  229. }  /* ends loop over a */
  230.  
  231. denominator = 1.0 - sum_vector[0];
  232. for(i=1; i<LENGTH_OF_BELIEF_VECTOR; i++)
  233.    vector1[i] = sum_vector[i]/denominator;
  234.  
  235.  
  236. }  /* ends combine_using_dempsters_rule */
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.    /* The following functions are I-O  */
  244.  
  245.  
  246.  
  247. read_string(string)
  248.         char *string;
  249. {
  250.         int     eof,
  251.                 letter,
  252.                 no_error;
  253.  
  254.         eof = -1;
  255.         no_error = 0;
  256.  
  257.         while((letter = getchar()) != '\n' &&
  258.                letter !=  eof)
  259.            *string++ = letter;
  260.  
  261.         *string = '\0';
  262.  
  263.         return((letter == eof) ? eof : no_error);
  264.  
  265. }       /* ends read_string */
  266.  
  267.  
  268.  
  269. clear_buffer(string)
  270.    char string[];
  271. {
  272.    int i;
  273.    for(i=0; i<80; i++)
  274.       string[i] = ' ';
  275. }
  276.  
  277.  
  278.  
  279. long_clear_buffer(string)
  280.    char string[];
  281. {
  282.    int i;
  283.    for(i=0; i<300; i++)
  284.       string[i] = ' ';
  285. }
  286.  
  287.  
  288.  
  289.  
  290. #define is_digit(x) ((x >= '0' && x <= '9') ? 1 : 0)
  291.  
  292. #define is_blank(x) ((x == ' ') ? 1 : 0)
  293.  
  294. #define to_decimal(x) (x - '0')
  295.  
  296. #define NO_ERROR  0
  297. #define IO_ERROR -1
  298. #define NULL2   '\0'
  299.  
  300.  
  301. get_integer(n)
  302.    int *n;
  303. {
  304.    char string[80];
  305.  
  306.    read_string(string);
  307.    int_convert(string, n);
  308. }
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  int_convert (ascii_val, result)
  315.     char *ascii_val;
  316.     int *result;
  317.   {
  318.     int sign = 1;  /* -1 if negative */
  319.  
  320.     *result = 0;   /* value returned to the calling routine */
  321.  
  322.     /* read passed blanks */
  323.  
  324.     while (is_blank(*ascii_val))
  325.        ascii_val++;              /* get next letter */
  326.  
  327.     /* check for sign */
  328.  
  329.     if (*ascii_val == '-' || *ascii_val == '+')
  330.        sign = (*ascii_val++ == '-') ? -1 : 1;  /* find sign */
  331.  
  332.    /*
  333.     * convert the ASCII representation to the actual
  334.     * decimal value by subtracting '0' from each character.
  335.     *
  336.     * for example, the ASCII '9' is equivalent to 57 in decimal.
  337.     * by subtracting '0' (or 48 in decimal) we get the desired
  338.     * value.
  339.     *
  340.     * if we have already converted '9' to 9 and the next character
  341.     * is '3', we must first multiply 9 by 10 and then convert '3'
  342.     * to decimal and add it to the previous total yielding 93.
  343.     *
  344.     */
  345.  
  346.     while (*ascii_val)
  347.      if (is_digit(*ascii_val))
  348.        *result = *result * 10 + to_decimal(*ascii_val++);
  349.  
  350.      else
  351.        return (IO_ERROR);
  352.  
  353.  
  354.     *result = *result * sign;
  355.  
  356.     return (NO_ERROR);
  357.   }
  358.  
  359.  
  360.  
  361.  
  362.  
  363. get_short(n)
  364.    short *n;
  365. {
  366.    char string[80];
  367.  
  368.    read_string(string);
  369.    int_convert(string, n);
  370. }
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  short_convert (ascii_val, result)
  377.     char *ascii_val;
  378.     short *result;
  379.   {
  380.     int sign = 1;  /* -1 if negative */
  381.  
  382.     *result = 0;   /* value returned to the calling routine */
  383.  
  384.     /* read passed blanks */
  385.  
  386.     while (is_blank(*ascii_val))
  387.        ascii_val++;              /* get next letter */
  388.  
  389.     /* check for sign */
  390.  
  391.     if (*ascii_val == '-' || *ascii_val == '+')
  392.        sign = (*ascii_val++ == '-') ? -1 : 1;  /* find sign */
  393.  
  394.    /*
  395.     * convert the ASCII representation to the actual
  396.     * decimal value by subtracting '0' from each character.
  397.     *
  398.     * for example, the ASCII '9' is equivalent to 57 in decimal.
  399.     * by subtracting '0' (or 48 in decimal) we get the desired
  400.     * value.
  401.     *
  402.     * if we have already converted '9' to 9 and the next character
  403.     * is '3', we must first multiply 9 by 10 and then convert '3'
  404.     * to decimal and add it to the previous total yielding 93.
  405.     *
  406.     */
  407.  
  408.     while (*ascii_val){
  409.      if (is_digit(*ascii_val)){
  410.        *result = *result * 10 + to_decimal(*ascii_val++);
  411.        if( (sign == -1) && (*result > 0)) *result = *result * -1;
  412.      }
  413.      else
  414.        return (IO_ERROR);
  415.     }  /* ends while ascii_val  */
  416.  
  417.     return (NO_ERROR);
  418.   }
  419.  
  420.  
  421.  
  422.  
  423. get_long(n)
  424.    long *n;
  425. {
  426.    char string[80];
  427.  
  428.    read_string(string);
  429.    long_convert(string, n);
  430. }
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  long_convert (ascii_val, result)
  437.     char *ascii_val;
  438.     long *result;
  439.   {
  440.     int sign = 1;  /* -1 if negative */
  441.  
  442.     *result = 0;   /* value returned to the calling routine */
  443.  
  444.     /* read passed blanks */
  445.  
  446.     while (is_blank(*ascii_val))
  447.        ascii_val++;              /* get next letter */
  448.  
  449.     /* check for sign */
  450.  
  451.     if (*ascii_val == '-' || *ascii_val == '+')
  452.        sign = (*ascii_val++ == '-') ? -1 : 1;  /* find sign */
  453.  
  454.    /*
  455.     * convert the ASCII representation to the actual
  456.     * decimal value by subtracting '0' from each character.
  457.     *
  458.     * for example, the ASCII '9' is equivalent to 57 in decimal.
  459.     * by subtracting '0' (or 48 in decimal) we get the desired
  460.     * value.
  461.     *
  462.     * if we have already converted '9' to 9 and the next character
  463.     * is '3', we must first multiply 9 by 10 and then convert '3'
  464.     * to decimal and add it to the previous total yielding 93.
  465.     *
  466.     */
  467.  
  468.     while (*ascii_val)
  469.      if (is_digit(*ascii_val))
  470.        *result = *result * 10 + to_decimal(*ascii_val++);
  471.  
  472.      else
  473.        return (IO_ERROR);
  474.  
  475.  
  476.     *result = *result * sign;
  477.  
  478.     return (NO_ERROR);
  479.   }
  480.  
  481.  
  482.  
  483.  
  484. get_float(f)
  485.    float *f;
  486. {
  487.    char string[80];
  488.  
  489.    read_string(string);
  490.    float_convert(string, f);
  491. }
  492.  
  493.  
  494.  float_convert (ascii_val, result)
  495.     char *ascii_val;
  496.     float *result;
  497.   {
  498.     int count;            /* # of digits to the right of the
  499.                              decimal point. */
  500.     int sign = 1;         /* -1 if negative */
  501.  
  502.     double pow10();       /* Turbo C function */
  503.     float power();        /* function returning a value raised
  504.                              to the power specified. */
  505.  
  506.     *result = 0.0;        /* value desired by the calling routine */
  507.  
  508.     /* read passed blanks */
  509.  
  510.     while (is_blank(*ascii_val))
  511.        ascii_val++;              /* get the next letter */
  512.  
  513.     /* check for a sign */
  514.  
  515.     if (*ascii_val == '-' || *ascii_val == '+')
  516.        sign = (*ascii_val++ == '-') ? -1 : 1;  /* find sign */
  517.  
  518.  
  519.    /*
  520.     * first convert the numbers on the left of the decimal point.
  521.     *
  522.     * if the number is 33.141592  this loop will convert 33
  523.     *
  524.     * convert ASCII representation to the  actual decimal
  525.     * value by subtracting '0' from each character.
  526.     *
  527.     * for example, the ASCII '9' is equivalent to 57 in decimal.
  528.     * by subtracting '0' (or 48 in decimal) we get the desired
  529.     * value.
  530.     *
  531.     * if we have already converted '9' to 9 and the next character
  532.     * is '3', we must first multiply 9 by 10 and then convert '3'
  533.     * to decimal and add it to the previous total yielding 93.
  534.     *
  535.     */
  536.  
  537.     while (*ascii_val)
  538.      if (is_digit(*ascii_val))
  539.        *result = *result * 10 + to_decimal(*ascii_val++);
  540.  
  541.      else if (*ascii_val == '.')  /* start the fractional part */
  542.        break;
  543.  
  544.      else
  545.        return (IO_ERROR);
  546.  
  547.  
  548.    /*
  549.     * find number to the right of the decimal point.
  550.     *
  551.     * if the number is 33.141592 this portion will return 141592.
  552.     *
  553.     * by converting a character and then dividing it by 10
  554.     * raised to the number of digits to the right of the
  555.     * decimal place the digits are placed in the correct locations.
  556.     *
  557.     *     4 / power (10, 2) ==> 0.04
  558.     *
  559.     */
  560.  
  561.     if (*ascii_val != NULL2)
  562.      {
  563.         ascii_val++;   /* past decimal point */
  564.  
  565.         for (count = 1; *ascii_val != NULL2; count++, ascii_val++)
  566.  
  567.               /*********************************************
  568.               *
  569.               *   The following change was made 16 June 1987.
  570.               *   For some reason the power function below
  571.               *   was not working. Borland's Turbo C pow10
  572.               *   was substituted.
  573.               *
  574.               ***********************************************/
  575.          if (is_digit(*ascii_val)){
  576.            *result = *result + to_decimal(*ascii_val)/power(10.0,count);
  577.         /***********
  578.         *result = *result + to_decimal(*ascii_val)/((float)(pow10(count)));
  579.         ************/
  580.           }
  581.  
  582.          else
  583.           return (IO_ERROR);
  584.      }
  585.  
  586.     *result = *result * sign; /* positive or negative value */
  587.  
  588.     return (NO_ERROR);
  589.   }
  590.  
  591.  
  592. float power(value, n)
  593.    float value;
  594.    int n;
  595. {
  596.    int   count;
  597.    float result;
  598.  
  599.    if(n < 0)
  600.       return(-1.0);
  601.  
  602.    result = 1;
  603.    for(count=1; count<=n; count++){
  604.       result = result * value;
  605.    }
  606.  
  607.    return(result);
  608. }
  609.