Skip to content

Instantly share code, notes, and snippets.

@andrewrcollins
Created January 6, 2012 02:33
Show Gist options
  • Save andrewrcollins/1568659 to your computer and use it in GitHub Desktop.
Save andrewrcollins/1568659 to your computer and use it in GitHub Desktop.
#TJHSST ~ Evaluate parenthesized boolean function of at most 9 variables
/*
Evaluates a parenthesized boolean function of at most 9 variables.
*/
#include <stdio.h>
#include <ctype.h>
#include <stdarg.h>
#define ANDSYMBOL (char)'*'
#define ORSYMBOL (char)'+'
#define XORSYMBOL (char)'@'
#define NOTSYMBOL (char)'~'
#define Error -999
#define MAXVARIABLE 9
int level1(char *,int *,int *),
level2(char *,int *,int *),
level3(char *,int *,int *),
level4(char *,int *,int *);
main()
{
int cnt1,cnt2,cnt3,cnt4,cnt5,cnt6,cnt7,cnt8,cnt9,cnt10;
for(cnt1=0;cnt1<2;cnt1++)
for(cnt2=0;cnt2<2;cnt2++)
for(cnt3=0;cnt3<2;cnt3++) {
cnt4=cnt1;
cnt5=cnt2;
cnt6=cnt3;
printf("x=(%d,%d,%d) ",cnt4,cnt5,cnt6);
for(cnt7=0;cnt7<4;cnt7++) {
printf("F(X)=%d%d%d ",
cnt8=evaluate("1+(2*(~3)))",3,cnt4,cnt5,cnt6),
cnt9=evaluate("1*(~3)",3,cnt4,cnt5,cnt6),
cnt10=evaluate("2",3,cnt4,cnt5,cnt6));
cnt4=cnt8;
cnt5=cnt9;
cnt6=cnt10;
}
puts("");
}
}
/* evaluate returns the answer to the given boolean function of
complexity n, that is there are n variables. The function uses
variable argument lists, so n must be included.
Example :
evaluate("1+(2*(~3))",3,1,1,0) returns 1+(1*(~0)) = 1 */
int evaluate(function,n)
char *function;
int n;
{
int *x,cnt,answer;
va_list argptr;
/* Initialize argptr */
va_start(argptr,n);
if(n>0&&n<10) {
/* Allocate space for x. */
x=(int *)malloc(n*sizeof(int));
for(cnt=0;cnt<n;cnt++)
x[cnt]=va_arg(argptr,int);
cnt=0;
answer=level1(function,&cnt,x);
free(x);
}
else
answer=-1;
/* Close down variable arguments */
va_end(argptr);
return answer;
}
int level1(str,strpos,x)
char str[];
int *strpos,*x;
{
int num1,num2;
if((num1=level2(str,strpos,x))<0)
return -1;
if(str[*strpos]==ANDSYMBOL) {
(*strpos)++;
if((num2=level2(str,strpos,x))<0)
return -1;
num1=(num1&&num2);
}
return num1;
}
int level2(str,strpos,x)
char str[];
int *strpos,*x;
{
int num1,num2;
if((num1=level3(str,strpos,x))<0)
return -1;
if(str[*strpos]==XORSYMBOL) {
(*strpos)++;
if((num2=level3(str,strpos,x))<0)
return -1;
num1=!(num1==num2);
}
return num1;
}
int level3(str,strpos,x)
char str[];
int *strpos,*x;
{
int num1,num2;
if((num1=level4(str,strpos,x))<0)
return -1;
if(str[*strpos]==ORSYMBOL) {
(*strpos)++;
if((num2=level4(str,strpos,x))<0)
return -1;
num1=(num1||num2);
}
return num1;
}
int level4(str,strpos,x)
char str[];
int *strpos,*x;
{
int num1;
switch(str[*strpos]) {
case NOTSYMBOL :
(*strpos)++;
if((num1=!level4(str,strpos,x))<0)
return -1;
break;
case '(' :
(*strpos)++;
if((num1=level1(str,strpos,x))<0)
return -1;
if(str[*strpos]!=')')
return -1;
(*strpos)++;
break;
default :
if(isdigit(str[*strpos]))
if((int)(str[*strpos]-'0')>0) {
num1=x[(int)(str[*strpos]-'0')-1];
(*strpos)++;
}
break;
}
return num1;
}
/*
Evaluates a parenthesized boolean function of at most 9 variables.
*/
#include <stdio.h>
#include <ctype.h>
#include <stdarg.h>
#define ANDSYMBOL (char)'*'
#define ORSYMBOL (char)'+'
#define XORSYMBOL (char)'@'
#define NOTSYMBOL (char)'~'
#define Error -999
#define MAXVARIABLE 9
int level1(char *,int *,int *),
level2(char *,int *,int *),
level3(char *,int *,int *),
level4(char *,int *,int *);
main()
{
int cnt1,cnt2,cnt3,cnt4,cnt5,cnt6,cnt7,cnt8,cnt9,cnt10;
for(cnt1=0;cnt1<2;cnt1++)
for(cnt2=0;cnt2<2;cnt2++)
for(cnt3=0;cnt3<2;cnt3++) {
cnt4=cnt1;
cnt5=cnt2;
cnt6=cnt3;
printf("x=(%d,%d,%d) ",cnt4,cnt5,cnt6);
for(cnt7=0;cnt7<4;cnt7++) {
printf("F(X)=%d%d%d ",
cnt8=evaluate("1+(2*(~3)))",3,cnt4,cnt5,cnt6),
cnt9=evaluate("1*(~3)",3,cnt4,cnt5,cnt6),
cnt10=evaluate("2",3,cnt4,cnt5,cnt6));
cnt4=cnt8;
cnt5=cnt9;
cnt6=cnt10;
}
puts("");
}
}
/* evaluate returns the answer to the given boolean function of
complexity n, that is there are n variables. The function uses
variable argument lists, so n must be included.
Example :
evaluate("1+(2*(~3))",3,1,1,0) returns 1+(1*(~0)) = 1 */
int evaluate(function,n)
char *function;
int n;
{
int *x,cnt,answer;
va_list argptr;
/* Initialize argptr */
va_start(argptr,n);
if(n>0&&n<10) {
/* Allocate space for x. */
x=(int *)malloc(n*sizeof(int));
for(cnt=0;cnt<n;cnt++)
x[cnt]=va_arg(argptr,int);
cnt=0;
answer=level1(function,&cnt,x);
free(x);
}
else
answer=-1;
/* Close down variable arguments */
va_end(argptr);
return answer;
}
int level1(str,strpos,x)
char str[];
int *strpos,*x;
{
int num1,num2;
if((num1=level2(str,strpos,x))<0)
return -1;
if(str[*strpos]==ANDSYMBOL) {
(*strpos)++;
if((num2=level2(str,strpos,x))<0)
return -1;
num1=(num1&&num2);
}
return num1;
}
int level2(str,strpos,x)
char str[];
int *strpos,*x;
{
int num1,num2;
if((num1=level3(str,strpos,x))<0)
return -1;
if(str[*strpos]==XORSYMBOL) {
(*strpos)++;
if((num2=level3(str,strpos,x))<0)
return -1;
num1=!(num1==num2);
}
return num1;
}
int level3(str,strpos,x)
char str[];
int *strpos,*x;
{
int num1,num2;
if((num1=level4(str,strpos,x))<0)
return -1;
if(str[*strpos]==ORSYMBOL) {
(*strpos)++;
if((num2=level4(str,strpos,x))<0)
return -1;
num1=(num1||num2);
}
return num1;
}
int level4(str,strpos,x)
char str[];
int *strpos,*x;
{
int num1;
switch(str[*strpos]) {
case NOTSYMBOL :
(*strpos)++;
if((num1=!level4(str,strpos,x))<0)
return -1;
break;
case '(' :
(*strpos)++;
if((num1=level1(str,strpos,x))<0)
return -1;
if(str[*strpos]!=')')
return -1;
(*strpos)++;
break;
default :
if(isdigit(str[*strpos]))
if((int)(str[*strpos]-'0')>0) {
num1=x[(int)(str[*strpos]-'0')-1];
(*strpos)++;
}
break;
}
return num1;
}
#include <stdio.h>
main()
{
char str[20];
int answer,pos=0;
printf("Enter an expression\n");
scanf("%s",str);
printf("Your string is : %s\n",str);
answer=evalexp(str,&pos);
printf("The answer is %d\n",answer);
}
int evalexp(str,pos)
char str[];
int *pos;
{
int num1,num2;
printf("In evalexp : %d\n",*pos);
num1=evalterm(str,pos);
if(str[*pos]=='+') {
(*pos)++;
num2=evalexp(str,pos);
num1+=num2;
}
return num1;
}
int evalterm(str,pos)
char str[];
int *pos;
{
int num1,num2;
printf("In evalterm %d\n",*pos);
num1=evalfactors(str,pos);
if(str[*pos]=='*') {
(*pos)++;
num2=evalterms(str,pos);
num1*=num2;
}
return num1;
}
int evalfactors(str,pos)
char str[];
int *pos;
{
int num1;
printf("In evalfactor %d\n",*pos);
switch(str[*pos]) {
case '(' :
(*pos)++;
printf("Left Paren\n");
num1=evalexp(str,pos);
printf("Inside Paren %d\n",num1);
(*pos)++;
printf("Assume Right Paren\n");
break;
case '~' :
(*pos)++;
printf("Complement\n");
num1=-evalexp(str,pos);
printf("Complement value %d\n",num1);
break;
case 'A' :
(*pos)++;
num1=2;
printf("A = %d\n",num1);
break;
case 'B' :
(*pos)++;
num1=3;
printf("B = %d\n",num1);
break;
default :
num1=4;
printf("Default = %d\n",num1);
(*pos)++;
break;
}
return num1;
}
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#define ValueD 0xAAAA /* 1010 1010 1010 1010 */
#define ValueC 0xCCCC /* 1100 1100 1100 1100 */
#define ValueB 0xF0F0 /* 1111 0000 1111 0000 */
#define ValueA 0xFF00 /* 1111 1111 0000 0000 */
#define AUsed 2
#define BUsed 4
#define CUsed 8
#define DUsed 16
#define ANDSYMBOL '*'
#define ORSYMBOL '+'
#define XORSYMBOL '@'
#define NOTSYMBOL '~'
#define GetBit(number,bit) (((number)&(1<<(bit)))&&1)
#define Error -999
main()
{
char str[20];
int answer,strpos,varused;
while(1) {
strpos=varused=0;
menu();
scanf("%s",str);
if(isdigit(str[0]))
exit(1);
answer=level1(str,&strpos,&varused);
table(answer,varused);
}
}
int menu(void)
{
printf("+--------------------------------------+\n");
printf("| Recursive Descent Expression Parsing |\n");
printf("+--------------------------------------+\n");
printf("Enter an Expression using the following\n");
printf(" A,B,C,D : Variables\n");
printf(" ~,|,&,^ : Operations\n");
printf(" 0..9 : Quit\n");
printf("Expression ");
textattr(LIGHTRED);
putch('?');
putch(' ');
}
int table(answer,varused)
int answer,varused;
{
int cnt;
if(varused!=Error) {
if(varused&AUsed)
printf(" A");
if(varused&BUsed)
printf(" B");
if(varused&CUsed)
printf(" C");
if(varused&DUsed)
printf(" D");
printf(" | Answer\n");
for(cnt=0;cnt<16;cnt++) {
if(varused&AUsed)
printf(" %d",GetBit(ValueA,cnt));
if(varused&BUsed)
printf(" %d",GetBit(ValueB,cnt));
if(varused&CUsed)
printf(" %d",GetBit(ValueC,cnt));
if(varused&DUsed)
printf(" %d",GetBit(ValueD,cnt));
printf(" %d\n",GetBit(answer,cnt));
}
}
else
printf("error in expression\n");
}
int level1(str,strpos,varused)
char str[];
int *strpos,*varused;
{
int num1,num2;
num1=level2(str,strpos,varused);
if((*varused)==Error)
return;
if(str[*strpos]==ANDSYMBOL) {
(*strpos)++;
num2=level2(str,strpos,varused);
if((*varused)==Error)
return;
num1&=num2;
}
return num1;
}
int level2(str,strpos,varused)
char str[];
int *strpos,*varused;
{
int num1,num2;
num1=level3(str,strpos,varused);
if((*varused)==Error)
return;
if(str[*strpos]==XORSYMBOL) {
(*strpos)++;
num2=level3(str,strpos,varused);
if((*varused)==Error)
return;
num1^=num2;
}
return num1;
}
int level3(str,strpos,varused)
char str[];
int *strpos,*varused;
{
int num1,num2;
num1=level4(str,strpos,varused);
if((*varused)==Error)
return;
if(str[*strpos]==ORSYMBOL) {
(*strpos)++;
num2=level4(str,strpos,varused);
if((*varused)==Error)
return;
num1|=num2;
}
return num1;
}
int level4(str,strpos,varused)
char str[];
int *strpos,*varused;
{
int num1;
switch(str[*strpos]) {
case NOTSYMBOL :
(*strpos)++;
num1=~level4(str,strpos,varused);
if((*varused)==Error)
return;
break;
case '(' :
(*strpos)++;
num1=level1(str,strpos,varused);
if((*varused)==Error)
return;
if(str[*strpos]!=')') {
*varused=Error;
return;
}
(*strpos)++;
break;
case 'A' :
case 'a' :
(*strpos)++;
num1=ValueA;
(*varused)|=AUsed;
break;
case 'B' :
case 'b' :
(*strpos)++;
num1=ValueB;
(*varused)|=BUsed;
break;
case 'C' :
case 'c' :
(*strpos)++;
num1=ValueC;
(*varused)|=CUsed;
break;
case 'D' :
case 'd' :
(*strpos)++;
num1=ValueD;
(*varused)|=DUsed;
break;
default :
num1=0;
(*strpos)++;
break;
}
return num1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment