Friday, November 23, 2012

My Sudoko Solver



Sudoko Solver i wrote using C#. It's unfinished. But it solves sudoko puzzles.

If anyone wants to try and help me in findings errors/bugs here is the link : http://uploaded.net/file/42s0tnka


Tuesday, June 12, 2012

A new Project

Soon I will be uploading my UI project... It's ambitious but I will have time. (29 card game next, need help, any one volunteering? (Sriram Dash is helping already) ).

Here are some of the pics.






Saturday, May 12, 2012

Sum of Subsets (find all sums for given Number set)



Sample Output is shown above. This program will run in windows only. For Linux you must write your own gotoxy function.

#include<stdio.h>
#include<stdlib.h>

#define MAX_PRINT_LEVEL 5

#include <windows.h>

void gotoxy(int col, int row)     //starting from 0
{
    COORD coord;
    coord.X = col;
    coord.Y = row;

    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

struct tree_node
{
       struct tree_node *left,*right;
       int data;
};

typedef struct tree_node TNODE;

TNODE* create_tree(int *input,int num);
void add_num(TNODE *node,int *input,int num);
void print_tree(TNODE *root,int num,int col,int row);
int print_subsets(TNODE *root,int sum,int *input,int *subsets,int index,int num);
int pow(int a, int b);

//array to store all possible sums, remove if not neeeded
/*int *sums,sum_count=0;*/

int main()
{
       int sum;

       int *input,num;
       int *subsets;
       TNODE *root=NULL;



       int i;

       printf("\nHOW MANY NUMBERS: ");
       scanf("%d",&num);
       if(num<0)
       {
              printf("\nERROR: Invalid num value...\n\n");
              exit(1);
       }
       if( (input = (int*)malloc(num*sizeof(int))) == NULL)
       {
              printf("\n\nERROR: MEMORY NOT ALLOCATED\n\n");
              exit(1);
       }
       if( (subsets = (int*)malloc(num*sizeof(int))) == NULL)
       {
              printf("\n\nERROR: MEMORY NOT ALLOCATED\n\n");
              exit(1);
       }
       printf("\nEnter numbers: \n");
       for(i=0;i<num;i++)
       {
              printf("NUM[%d] : ",i);
              scanf("%d",&input[i]);
       }

       //array to store all possible sums, remove if not neeeded
       /*if( (sums = (int*)malloc(pow(2,num)*sizeof(int))) == NULL)
       {
              printf("\n\nERROR: MEMORY NOT ALLOCATED\n\n");
              exit(1);
       }*/

       root = create_tree(input,num);

       system("cls");
       print_tree(root,num,0,0);
       printf("\n\nINPUT ARRAY :");
       for(i=num-1;i>=0;i--)
              printf(" %d",input[i]);
      
       printf("\n\n");

       //print all sums possible
       /*for(i=sum_count-1;i>=0;i--)
       {
              if(print_subsets(root,sums[i],input,subsets,num,num))
                     printf("\n********** SUBSETS WITH SUM = %d **********",sums[i]);
       }*/


       //print  a particular sum
       printf("\n\nENTER SUM: ");
       scanf("%d",&sum);
       printf("\n********** SUBSETS WITH SUM = %d **********",sum);
       if(!print_subsets(root,sum,input,subsets,num,num))
              printf("\n\nOOPS : NO SUBSET CAN BE FOUND");

       printf("\n\n\n");
       return 0;
}

int print_subsets(TNODE *node,int sum,int *input,int *subsets,int index,int num)
{
       int i,found=0,a,b;

       if(node->left==NULL && node->right == NULL)
       {
              if(node->data == sum)
              {
                     printf("\n{ ");
                     for(i=0;i<num;i++)
                           if(subsets[i] == 1)
                           {
                                  found = 1;
                                  printf("%d,",input[i]);
                           }

                     if(found)
                           printf("\b } ");
                     else
                           printf("NULL SET }");

                     return 1;
              }
              else
                     return 0;
       }
       if(node->left)
       {
              subsets[index-1] = 1;
              a=print_subsets(node->left,sum,input,subsets,index-1,num);
       }
       if(node->right)
       {
              subsets[index-1] = 0;
              b=print_subsets(node->right,sum,input,subsets,index-1,num);
       }

       return (a||b);
}

void print_tree(TNODE *root,int level,int col,int row)
{
       int gap,pos,slash;
       int i;
       int bcol,brow;

       if(level>MAX_PRINT_LEVEL)
       {
              printf("\nTree cant be displayed for level > %d...",MAX_PRINT_LEVEL);
              return;
       }
       gap = pow(2,level) - 1;
       bcol = col;
       brow = row;

       col=col+gap;
       gotoxy(col,row);

       printf("%d",root->data);
       if(root->left)
       {
              slash = pow(2,level-1);
              for(i=0;i<slash;i++)
              {
                     gotoxy(--col,++row);
                     printf("/");
              }
              print_tree(root->left,level-1,bcol,row+1);
       }
       if(root->right)
       {
              col = bcol+gap;
              row = brow;
              slash = pow(2,level-1);
              for(i=0;i<slash;i++)
              {
                     gotoxy(++col,++row);
                     printf("\\");
              }
              if(level>1)
                     print_tree(root->right,level-1,bcol+gap+1,row+1);
              else
                     print_tree(root->right,level-1,bcol+gap+1,row+2);
       }
}

int pow(int a, int b)
{
       int ans=1;

       if(b==0)
              return 1;

       while(b>0)
       {
              ans = ans * a;
              b--;
       }

       return ans;
}

TNODE* create_tree(int *input,int num)
{
       TNODE *temp;
       int i;

       temp = (TNODE*)malloc(1*sizeof(TNODE));
       temp->data=0;
       temp->left=NULL;
       temp->right=NULL;

       add_num(temp,input,num);

       return temp;
}

void add_num(TNODE *node,int *input,int num)
{
       if(num == 0)
       {
              //array to store all possible sums, remove if not neeeded
              /*sums[sum_count++] = node->data;*/

              return;
       }

       node->left=(TNODE*)malloc(1*sizeof(TNODE));
       node->right=(TNODE*)malloc(1*sizeof(TNODE));

       node->left->data = node->data + input[num-1];
       node->left->left = NULL;
       node->left->right = NULL;

       node->right->data = node->data + 0;
       node->right->left = NULL;
       node->right->right = NULL;

       add_num(node->left,input,num-1);
       add_num(node->right,input,num-1);
}

Recursive Decent Parser


Sample Output when syntax is correct

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

#define MAX   100

#define ID           261           //identifier
#define ARRAY        262           //array
#define COMMA        265           //','
#define SMCOLON      266           //';'
#define PNTR         267           //pointer
#define OBRCS        268           //'{'
#define CBRCS        269           //'}'
#define INVALID      270           //not a valid type for our tokens

#define FAILURE      0
#define SUCCESS      1

//this program will now detect Structure declaration like " struct abc{int a,*v,p[]; float e,*o,y[]; char r,*t,w[];}; "
//grammer:
//     str_stmt      ->     struct sname{ dec_list };
//     dec_list      ->     dec_stmt rest_stmts
//     rest_stmts    ->     dec_stmt rest_stmts | NULL
//     dec_stmt      ->     type var_list;
//     type          ->     int | float | char
//     var_list      ->     var rest_list
//     rest_list     ->     , var_list | NULL
//     sname         ->     ID
//     var                  ->     ID
//                                |ARRAY
//                                |PNTR

char str[MAX];             //our input string
char token[MAX];           //contains a token
int token_type;            //the type of token
char *s;                   //pointer to str
void get_token();          //function to get next token

int str_stmt();            //struct statement
int dec_list();            //declaration of struct members
int rest_stmts();          //rest of dec_stmts if there
int dec_stmt();            //function to verify declaration syntax
int type();                //function to check type
int var_list();            //function to check varaible syntax
int rest_list();           //fuction to check variable list,rest_list and var_list work               recursively and call each other

int main()
{
       while(1)
       {
              s=str;
              printf("\nENTER DECLARTATION SYNTAX: ");
              gets(s);

              if( str_stmt() )
                     printf("\n\n***** SYNTAX CORRECT *****\n\n");
              else
                     printf("\n\n***** SYNTAX INCORRECT *****\n\n");       
       }

       return 0;
}

int str_stmt()
{
       get_token();
       if(strcmp(token,"struct") == 0)
       {
              printf("\tMATCHED KEYWORD    : %s",token);
              get_token();
              if(token_type == ID)
              {
                     printf("\tMATCHED IDENTIFIER : %s",token);
                     get_token();
                     if(token_type == OBRCS)
                     {
                           printf("\tMATCHED OBRCS      : %s",token);
                           if(dec_list())
                                  return SUCCESS;
                     }
                     else
                           printf("\tExpecting '{'");
              }
              else
                     printf("\tExpecting struct name.");
       }
       else
              printf("\tExpecting struct keyword");

       return FAILURE;
}

int dec_list()
{
       get_token();
       if(dec_stmt())
       {
              printf("\n\t\t\t\t\t-> MATCHED DEC_STMT");
              if(rest_stmts())
                     return SUCCESS;
       }
       else
              printf("\n\t\t\t\t\t-> DEC_STMT INCORRECT");
       return FAILURE;
}

int rest_stmts()
{
       get_token();
       if(token_type == CBRCS)
       {
              printf("\tMATCHED CBRCS      : %s",token);
              get_token();
              if(token_type == SMCOLON)
              {
                     printf("\tMATCHED SMCOLON    : %s",token);
                     return SUCCESS;
              }
              else
                     printf("\tExpecting ';'");
       }
       else if(dec_stmt())
       {
              printf("\n\t\t\t\t\t-> MATCHED DEC_STMT");
              if(rest_stmts())
                     return SUCCESS;
       }
       else
              printf("\n\t\t\t\t\t->Expecting valid dec_stmt or '}'.");

       return FAILURE;
}

int dec_stmt()
{
       if( (strcmp(token,"int") == 0) ||(strcmp(token,"float") == 0) || (strcmp(token,"char") == 0) )
       {
              printf("\tMATCHED TYPE       : %s",token);
              if(var_list() )
                     return SUCCESS;
       }
       else
              printf("\tExpecting correct TYPE");

       return FAILURE;
}

int var_list()
{
       get_token();
       if(token_type == ID)
       {
              printf("\tMATCHED IDENTIFIER : %s",token);
              if( rest_list() )
                     return SUCCESS;
       }
       else if(token_type == ARRAY)
       {
              printf("\tMATCHED ARRAY-ID   : %s",token);
              if( rest_list() )
                     return SUCCESS;
       }
       else if(token_type == PNTR)
       {
              printf("\tMATCHED POINTER    : %s",token);
              if( rest_list() )
                     return SUCCESS;
       }
       else
              printf("\tExpecting an ID, POINTER or ARRAY");

       return FAILURE;
}

int rest_list()
{
       get_token();

       if(token_type == COMMA)
       {
              printf("\tMATCHED COMMA      : %s",token);
              if( var_list() )
                     return SUCCESS;
       }
       else if(token_type == SMCOLON)
       {
              printf("\tMATCHED SMCOLON    : %s",token);
              return SUCCESS;
       }
       else
              printf("\tExpecting ',' or ';'");
      
       return FAILURE;
}

void get_token()
{
       //before writing this function, remember this is the base of whole program
       //have in mind what we need : ID ARRAY ( ) , ; =
       //rest are invalid for our needs right now
       char* tostr_tt(int token_type);

       int i = 0;                        //index for token string

       while(*s == ' ' || *s == '\t')    //eat white spaces
              s++;

       if(*s == '*')  //pointer
       {
              token[i++] = *s++;
              if(isalpha(*s) || *s == '_')
              {
                     token_type=PNTR;
                     while(isalnum(*s) || *s == '_')   //while token is a valid ID
                           token[i++] = *s++;
              }
              else
                     token_type=INVALID;

              token[i] = '\0';
       }
       else if( isalpha(*s) || *s == '_')       //if current token starts with an alphaber or _ it is an ID
       {
              token_type=ID;
              while(isalnum(*s) || *s == '_')   //while token is a valid ID
              {
                     token[i++] = *s++;
              }

              token[i] = '\0';
       }
       else
       {
              if(*s == ',')
                     token_type = COMMA;
              else if(*s == ';')
                     token_type = SMCOLON;
              else if(*s == '{')
                     token_type = OBRCS;
              else if(*s == '}')
                     token_type = CBRCS;
              else
                     token_type = INVALID;
              token[i++] = *s++;
              token[i] = '\0';
       }

       if(token_type == ID)  //array is ID[]
       {
              if(*s == '[')
              {
                     token_type = INVALID;
                     token[i++] = '[';
                     s++;
                     if(*s == ']')
                     {
                           token_type = ARRAY;
                           token[i++] = ']';
                           s++;
                     }
                     token[i] = '\0';
              }
       }

       printf("\nTOKEN : %s\t\tTYPE:%-10s",token,tostr_tt(token_type));
}

char* tostr_tt(int token_type)
{
       switch(token_type)
       {
              case ID:             return "ID";         break;
              case ARRAY:          return "ARRAY";      break;
              case COMMA:          return "COMMA";      break;
              case SMCOLON:        return "SMCOLON";    break;
              case PNTR:           return "POINTER";    break;
              case OBRCS:          return "OBRACES";    break;
              case CBRCS:          return "CBRACES";    break;
              case INVALID:        return "INVALID";    break;
              default:             return "ERROR";      break;

       }     
}