示例#1
0
Token Instruction::eval(const std::string& expression)
{
	// 1. Split string into a token list
	tokenization(expression);

	// 2. Convert tokens from infix notation to postfix notation
	infix_to_postfix();

	// 3. Eval postfixed expression
	return eval_postfix();
}
示例#2
0
int truth_table(char ipstr[], int fnvalues[], int *no_of_variables){
     int i, j, output, len;
	  
     remove_spaces(ipstr);
     
     if(check_for_valid_characters(ipstr)){
	  printf("ERROR! Use only valid characters - ~&|() a-z A-Z\n");
	  return 1;
     }

     set_precedence(precedence,  ipstr);
     get_variables(listvar, &numvar, precedence);

     if(conv_to_postfix(ipstr, opstr, precedence) == 1){
	  printf("\nError in input\n");
	  return 1;
     }

     len = strlen(ipstr);
     /* Truth-table Printing */
     printf("\n");
     for(i=0; i<numvar; i++){
	  printf("%c  ", listvar[i]);
     }
     printf("%s ", ipstr);
     
     for(i=0; i<(1<<numvar); i++){
	  /* For each permutation of bits */
	  printf("\n");
	  compute_val_var_from_perm(i, valvar, numvar);
	  for(j=0; j<numvar; j++)
	       printf("%d  ", valvar[j]);
	  output = eval_postfix(opstr, valvar, numvar, listvar);
	  if(output == 1){
	       printf("Error : eval - not enuff operands\n");
	       return 1;
	  }
	  fnvalues[i] = output - '0';
	  printf("%*c ", len/2 + 1, output);
     }

     *no_of_variables = numvar;
     printf("\n");
     return 0;
}
示例#3
0
int main()
{
    char *exp = "231*+9-";
    printf("Postfix exp: %s is evaluated to %d\n", exp, eval_postfix(exp));
    return 0;    
}
示例#4
0
/**
This Compares the given expression value to the the value current log , Whether it's conforming that expression or not.
*/
boolean isValideLog(char *expression)
{
   int i=0,j,k=0,key=1;
   j=0;
   char compchar='\0';
   char newstr[N],keystr[N],valuestr[N];
   while(expression[i]!='\0')
   {
      if(expression[i]==' ')
      {
         i++;
         continue;
       }
     else if(expression[i]=='='||expression[i]=='>'||expression[i]=='<')
     {
         compchar=expression[i];
         keystr[j]='\0';
         key=0;
         j=0;
     }
     else if(expression[i]=='&'||expression[i]=='|'||expression[i]=='!'||expression[i]=='('||expression[i]==')'
     ||expression[i]=='!')
    {
           if(expression[i]=='('||expression[i]=='!')
           {
               newstr[k++]=expression[i];
            }
           else if((i-1)>=0&&(expression[i]!=')'||expression[i]==')')&&expression[i-1]==')')
           {
                newstr[k++]=expression[i];

            }
           else{
            valuestr[j]='\0';
            Tries *node;
            node=find(&root,keystr);
            if(node==NULL)
            {
               newstr[k++]='0';
            }
            else if(!strcmp(keystr,s[1]))
            {
               int t=0;
               switch(compchar)
               {
                  case '=':
                           if(atoi(node->value)==atoi(valuestr))
                             t=1;
                            break;

                  case '<':if(atoi(node->value)<atoi(valuestr))
                             t=1;
                            break;
                  case '>':   if(atoi(node->value)>atoi(valuestr))
                             t=1;
                }
               if(t==1)
                newstr[k++]='1';
               else
                newstr[k++]='0';
            }

          else  if(!strcmp(keystr,s[2]))
            {
               int t=0;
               switch(compchar)
               {
                  case '=':
                           if(atoi(node->value)==atoi(valuestr))
                             t=1;
                            break;

                  case '<':if(atoi(node->value)<atoi(valuestr))
                             t=1;
                            break;
                  case '>':   if(atoi(node->value)>atoi(valuestr))
                             t=1;
                }
               if(t==1)
                newstr[k++]='1';
               else
                newstr[k++]='0';
            }

            else if(strcmp(node->value,valuestr))
            {
               newstr[k++]='0';
            }
            else
            {
                newstr[k++]='1' ;
            }
            newstr[k++]=expression[i];
            keystr[0]=valuestr[0]='\0';
            key=1;
            j=0;
         }
   }

      else
      {
			 if(key==1)
			 {
		       keystr[j++]=expression[i];
			 }
			  else
			  {
				 valuestr[j++]=expression[i];
			  }
		 }

         i++;
   }
     if(keystr[0]!='\0'&&valuestr[0]!='\0')
     {
            valuestr[j]='\0';
            Tries *node;
            node=find(&root,keystr);
            if(node==NULL)
               newstr[k++]='0';
            else if(!strcmp(keystr,s[1]))
            {
               int t=0;
               if(node){
               switch(compchar)
               {
                  case '=':
                           if(atoi(node->value)==atoi(valuestr))
                             t=1;
                            break;

                  case '<':if(atoi(node->value)<atoi(valuestr))
                             t=1;
                            break;
                  case '>':   if(atoi(node->value)>atoi(valuestr))
                             t=1;
                }
               }
               if(t==1)
                newstr[k++]='1';
               else
                newstr[k++]='0';
            }


          else  if(!strcmp(keystr,s[2]))
            {
               int t=0;
               if(node){
               switch(compchar)
               {
                  case '=':
                           if(atoi(node->value)==atoi(valuestr))
                             t=1;
                            break;

                  case '<':if(atoi(node->value)<atoi(valuestr))
                             t=1;
                            break;
                  case '>':   if(atoi(node->value)>atoi(valuestr))
                             t=1;
                }
               }
               if(t==1)
                newstr[k++]='1';
               else
                newstr[k++]='0';
            }

            else if(strcmp(node->value,valuestr))
            {
               newstr[k++]='0';
            }
            else
            {
                newstr[k++]='1' ;
            }
            newstr[k++]=expression[i];
            keystr[0]=valuestr[0]='\0';
            key=1;
            j=0;
     }
     newstr[k]='\0';
    char postfix[N];
    infix_to_postfix(newstr,postfix);
    if(eval_postfix(postfix))
    {
        return 1;
    }
    else
      return 0;
}