Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: None  |  size: 2.90 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.   Evaluates a parenthesized boolean function of at most 9 variables.
  3. */
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include <stdarg.h>
  7.  
  8. #define ANDSYMBOL (char)'*'
  9. #define ORSYMBOL (char)'+'
  10. #define XORSYMBOL (char)'@'
  11. #define NOTSYMBOL (char)'~'
  12.  
  13. #define Error -999
  14. #define MAXVARIABLE 9
  15.  
  16. int level1(char *,int *,int *),
  17.     level2(char *,int *,int *),
  18.     level3(char *,int *,int *),
  19.     level4(char *,int *,int *);
  20.  
  21. main()
  22. {
  23.   int cnt1,cnt2,cnt3,cnt4,cnt5,cnt6,cnt7,cnt8,cnt9,cnt10;
  24.  
  25.   for(cnt1=0;cnt1<2;cnt1++)
  26.     for(cnt2=0;cnt2<2;cnt2++)
  27.       for(cnt3=0;cnt3<2;cnt3++)  {
  28.         cnt4=cnt1;
  29.         cnt5=cnt2;
  30.         cnt6=cnt3;
  31.         printf("x=(%d,%d,%d) ",cnt4,cnt5,cnt6);
  32.         for(cnt7=0;cnt7<4;cnt7++)  {
  33.           printf("F(X)=%d%d%d ",
  34.             cnt8=evaluate("1+(2*(~3)))",3,cnt4,cnt5,cnt6),
  35.             cnt9=evaluate("1*(~3)",3,cnt4,cnt5,cnt6),
  36.             cnt10=evaluate("2",3,cnt4,cnt5,cnt6));
  37.           cnt4=cnt8;
  38.           cnt5=cnt9;
  39.           cnt6=cnt10;
  40.         }
  41.         puts("");
  42.       }
  43. }
  44.  
  45. /* evaluate returns the answer to the given boolean function of
  46.    complexity n, that is there are n variables. The function uses
  47.    variable argument lists, so n must be included.
  48.    Example :
  49.      evaluate("1+(2*(~3))",3,1,1,0) returns 1+(1*(~0)) = 1 */
  50. int evaluate(function,n)
  51. char *function;
  52. int n;
  53. {
  54.   int *x,cnt,answer;
  55.   va_list argptr;
  56.  
  57. /* Initialize argptr */
  58.   va_start(argptr,n);
  59.   if(n>0&&n<10)  {
  60. /* Allocate space for x. */
  61.     x=(int *)malloc(n*sizeof(int));
  62.     for(cnt=0;cnt<n;cnt++)
  63.       x[cnt]=va_arg(argptr,int);
  64.     cnt=0;
  65.     answer=level1(function,&cnt,x);
  66.     free(x);
  67.   }
  68.   else
  69.     answer=-1;
  70. /* Close down variable arguments */
  71.   va_end(argptr);
  72.   return answer;
  73. }
  74.  
  75. int level1(str,strpos,x)
  76. char str[];
  77. int *strpos,*x;
  78. {
  79.   int num1,num2;
  80.  
  81.   if((num1=level2(str,strpos,x))<0)
  82.     return -1;
  83.   if(str[*strpos]==ANDSYMBOL)  {
  84.     (*strpos)++;
  85.     if((num2=level2(str,strpos,x))<0)
  86.       return -1;
  87.     num1=(num1&&num2);
  88.   }
  89.   return num1;
  90. }
  91.  
  92. int level2(str,strpos,x)
  93. char str[];
  94. int *strpos,*x;
  95. {
  96.   int num1,num2;
  97.  
  98.   if((num1=level3(str,strpos,x))<0)
  99.     return -1;
  100.   if(str[*strpos]==XORSYMBOL)  {
  101.     (*strpos)++;
  102.     if((num2=level3(str,strpos,x))<0)
  103.       return -1;
  104.     num1=!(num1==num2);
  105.   }
  106.   return num1;
  107. }
  108.  
  109. int level3(str,strpos,x)
  110. char str[];
  111. int *strpos,*x;
  112. {
  113.   int num1,num2;
  114.  
  115.   if((num1=level4(str,strpos,x))<0)
  116.     return -1;
  117.   if(str[*strpos]==ORSYMBOL)  {
  118.     (*strpos)++;
  119.     if((num2=level4(str,strpos,x))<0)
  120.       return -1;
  121.     num1=(num1||num2);
  122.   }
  123.   return num1;
  124. }
  125.  
  126. int level4(str,strpos,x)
  127. char str[];
  128. int *strpos,*x;
  129. {
  130.   int num1;
  131.  
  132.   switch(str[*strpos])  {
  133.     case NOTSYMBOL :
  134.       (*strpos)++;
  135.       if((num1=!level4(str,strpos,x))<0)
  136.         return -1;
  137.       break;
  138.     case '(' :
  139.       (*strpos)++;
  140.       if((num1=level1(str,strpos,x))<0)
  141.         return -1;
  142.       if(str[*strpos]!=')')
  143.         return -1;
  144.       (*strpos)++;
  145.       break;
  146.     default :
  147.       if(isdigit(str[*strpos]))
  148.         if((int)(str[*strpos]-'0')>0)  {
  149.           num1=x[(int)(str[*strpos]-'0')-1];
  150.           (*strpos)++;
  151.         }
  152.       break;
  153.   }
  154.   return num1;
  155. }