Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: August 2013

Thursday, August 29, 2013

Random Quotes

This is a command line utility that prints a "Maths Quote of the Day" on the user's terminal when run.
User's types "java MathQuoteOFDay" on the command line
System prints out a quote.
import java.util.*;
public class MathsQuoteOFDay
    {
    public static void main(String[] args)
        {
        Quotes qts = new Quotes();
            int n = (int)(Math.random()*10);
            System.out.println("");
            System.out.println(qts.getQuote(n));   
            System.out.println("");        
        }
    }
class Quotes
    {
    Quotes()
        {
        }
    public String getQuote(int n)
        {
        return (quotes[n]);
        }
    private static String quotes[]={"I like mathematics because it is not human and has nothing particular to do with this planet or with the whole accidental universe                         - because like Spinoza's God, it won't love us in return.  ~Bertrand Russell",
                    "If there is a God, he's a great mathematician.  ~Paul Dirac",
                    "Do not worry about your problems with mathematics, I assure you mine are far greater. ~Albert Einstein",
                    "There are things which seem incredible to most men who have not studied mathematics. ~ Aristotle",
                    "Mathematics is a game played according to certain simple rules with meaningless marks on paper. ~ David Hilbert",
                    "Go down deep enough into anything and you will find mathematics~ Dean Schlicter",
                    " Life is good for only two things, discovering mathematics and teaching mathematics. ~ Simeon Poisson",
                    "In mathematics you don't understand things. You just get used to them. ~ Johann von Neumann",
                    " Medicine makes people ill, mathematics make them sad and theology makes them sinful. ~ Martin Luther",
                    "A man whose mind has gone astray should study mathematics. ~ Francis Bacon"};
    }

Tuesday, August 27, 2013

Parentheses Checking

Given an expression string exp, write a program to examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[","]” are correct in exp. For example, the program should print true for exp = “[a+{b-(c-(d-e)*d)+w)}+q]” and false for exp = “[(])”



 import java.util.*;

public class Parentheses
   {
   public static void main(String[] args)
{
int n,i,pos,posm,t;
boolean flag=true;
char[] symb={'(','{','[',']','}',')'};
char tmp;
Scanner in = new Scanner(System.in);
String expression;
System.out.println(" Enter the expression:");
expression=in.nextLine();
System.out.println(" Entered expression is:"+expression);
n=expression.length();
Stack s = new Stack(n);
System.out.println("");
for(i=0;i<n;i++)
{
pos=0;
posm=0;
if(expression.charAt(i)==symb[0] || expression.charAt(i)==symb[1] || expression.charAt(i)==symb[2] )
{
s.push(expression.charAt(i));

}
if(expression.charAt(i)==symb[3] || expression.charAt(i)==symb[4] || expression.charAt(i)==symb[5] )
{
if(s.isempty()==true)
{
flag=false;
break;
}
else
{
tmp=s.pop();
for(t=0;t<3;t++)
if(symb[t]==tmp)
pos=t;
for(t=3;t<6;t++)
if(symb[t]==expression.charAt(i))
posm=t;
if(pos+posm!=5)
{
flag=false;
break;
}
}
}
 

}
if(s.isempty()==false)
flag=false;
if(flag==true)
System.out.println(" Valid Expression:");
else
System.out.println(" Invalid Expression:");
in.close();
}

  }
class Stack
{
public Stack(int a)
{
top=-1;
n=a;
stack = new char[n];
}
public void push(char x)
{
stack[++top]=x;
}
public char pop()
{
return (stack[top--]);
}
public char stacktop()
{
return (stack[top]);
}
public boolean isempty()
{
return ( top==-1 ? true: false);
}
private int top;
private int n;
private char[] stack;

}



Sunday, August 18, 2013

Saturday, August 10, 2013

n-Queen

n-queens problem of placing n queens on an n×n chessboard, where solutions exist for all natural numbers n with the exception of n=2 and n=3.


#include<stdio.h>
#include<malloc.h>
#define TRUE 1
#define FALSE 0

int BOARDSIZE;
int DIAGONAL;
int OFFSET;
void dispal(void);
void add_queen(void);
int *queencol;
int *colfree;
int *upfree;
int *downfree;
int Queenc=-1;
int main()
{
int i;
printf("\n Enter the boardsize: ");
scanf("%d",&BOARDSIZE);
DIAGONAL=2*BOARDSIZE-1;
OFFSET=BOARDSIZE-1;
queencol=(int *)malloc(BOARDSIZE*sizeof(int));
colfree=(int *)malloc(BOARDSIZE*sizeof(int));
upfree=(int *)malloc(DIAGONAL*sizeof(int));
downfree=(int *)malloc(DIAGONAL*sizeof(int));
for(i=0;i<BOARDSIZE;i++)
    colfree[i]=TRUE;
    //queencol[i]=-1;
for(i=0;i<DIAGONAL;i++)
    {
    upfree[i]=TRUE;
    downfree[i]=TRUE;
    }
add_queen();
return 0;
}
void add_queen(void)
    {
    int col;
    Queenc++;
    for(col=0;col<BOARDSIZE;col++)
        if(colfree[col] && upfree[Queenc+col] && downfree[Queenc-col+OFFSET])
            {
            queencol[Queenc]=col;
            colfree[col]=FALSE;
            upfree[Queenc+col]=FALSE;
            downfree[Queenc-col+OFFSET]=FALSE;
            if(Queenc==BOARDSIZE-1)
                display();
            else
                add_queen();
            colfree[col]=TRUE;
            upfree[Queenc+col]=TRUE;
            downfree[Queenc-col+OFFSET]=TRUE;
            }
        Queenc--;
    }
void display(void)
    {
    printf("\n");
    int i,j,tmp;
    for(i=0;i<BOARDSIZE;i++)
        {
        tmp=queencol[i];
        for(j=0;j<BOARDSIZE;j++)
            {
            if(j==tmp)
            printf(" Q ");
            else
            printf(" 0 ");
            }
        printf("\n");
        }
    printf("\n");
    return;
    }
 

Thursday, August 1, 2013

Uniform Symbol Table

A program to create a Terminal Table on the basis of the subsets of tokens
{ "if","for","while","int","float","char","+","-","*","/","=","<",">","{",",","(",")",";","?"} . Given a C statement the system program should create and display a uniform table that consists of full list of valid tokens. There must be two entries for each token in the Symbol Table, namely class and index (that is a pointer to a specific location of an appropriate table where the description of the token can be found. The index can be found to any one of the following tables: Identifier table, Literal Table and Terminal Table depending on the type of token. Each  of these three table would have its own relevant fields.

#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
int tokencount(char *str);
void  classification(char iden[],int *key,int *sym,int *ope,int *lit,int *ide,int *pkey,int *psym,int *pope);
void print_table(int,int,int);
#define TRUE 1
#define FALSE 0
typedef struct
    {
    int indx;
    char tok[20];
    int pos;
    }Terminal_table;
typedef struct
    {
    int indx;
    char tok[20];
    char t_nme[20];
    int pos;
    }Uniform_sym_table;
typedef struct
    {
    int indx;
    char lit[20];
    int pos;
    }Literal;
typedef struct
    {
    int indx;
    char iden[20];
    int pos;
    }Identifier;
Terminal_table *TT;
Uniform_sym_table *UST;
Literal *LT;
Identifier *IDE;
int main(void)
{
char *keyword1[]={ "if","for","while","int","float","char","+","-","*","/","=","<",">",",","(",")",";","?"};
char *str,ar[80],dig[2],sp[20];
char *token;
int FLAG=FALSE,count=0,n,len,i=0,tc=0,p=0,q=0,r=0,s=0,t=0,j=0;
int key=FALSE,sym=FALSE,ope=FALSE,lit=FALSE,ide=FALSE,lit_count=0,FLAG1=FALSE,iden_count=0;
int pkey,psym,pope;
printf("\n Enter the maximum length of the statement:");
scanf(" %d", &n);
str=(char *)malloc( (n+1)*sizeof(char));
fflush(stdin);
printf("\n Enter the C statement:");
scanf("%[^\n]",str);
len=strlen(str);
if(len>n)
{
printf("\n Length of the statement is greater than the input length:");
exit(1);
}
token=str;
tc=tokencount(str);
UST=(Uniform_sym_table *)malloc(tc*sizeof(Uniform_sym_table));
TT=(Terminal_table *)malloc(20*sizeof(Terminal_table));
LT=(Literal *)malloc(tc*sizeof(Literal));
IDE=(Identifier *)malloc(tc*sizeof(Identifier));
for(i=0;i<18;i++)
    {
    strcpy(TT[i].tok,keyword1[i]);
    TT[i].indx=i+1;
    TT[i].pos=i+1;
    }
tc=0;
while(1)
         {
         while(*token!= ' ')
         {
         if(*token== '\0')
            {
            FLAG=TRUE;
            break;
            }
    ar[count++]=*token;
    token++;
    }
    ar[count]='\0';
    classification(ar,&key,&sym,&ope,&lit,&ide,&pkey,&psym,&pope);
   if(key)
        {
        UST[tc].indx=tc+1;
        strcpy(UST[tc].tok,ar);
        strcpy(UST[tc].t_nme,"TRM");
        for(i=0;i<19;i++)
            {
            if((strcmp(TT[i].tok,ar))==0)
                break;
            }
        UST[tc].pos=TT[i].pos;
        tc++;
        }
    if(sym)
        {
        UST[tc].indx=tc+1;
        strcpy(UST[tc].tok,ar);
        strcpy(UST[tc].t_nme,"TRM");
        for(i=0;i<19;i++)
            {
            if((strcmp(TT[i].tok,ar))==0)
                break;
            }
        UST[tc].pos=TT[i].pos;
        tc++;
      }
    if(ope)
        {
      UST[tc].indx=tc+1;
        strcpy(UST[tc].tok,ar);
        strcpy(UST[tc].t_nme,"TRM");
        for(i=0;i<19;i++)
            {
            if((strcmp(TT[i].tok,ar))==0)
                break;
            }
        UST[tc].pos=TT[i].pos;
        tc++;
        }
    if(lit)
        {
        UST[tc].indx=tc+1;
        for(i=0;i<p;i++)
            {
            if((strcmp(LT[i].lit,ar))==0)
                {
                FLAG1=TRUE;
                s=i;
                break;
                }
            }
        if(FLAG1==0)
            {
            strcpy(UST[tc].tok,ar);
            strcpy(UST[tc].t_nme,"LTR");
            LT[p].indx=p+1;
            strcpy(LT[p].lit,ar);
            LT[p].pos=p+1;
            UST[tc].pos=LT[p].pos;
            p++;
            lit_count++;
            }
        else
        {
        strcpy(UST[tc].tok,ar);
        strcpy(UST[tc].t_nme,"LTR");
        UST[tc].pos=LT[s].pos;
        }
        tc++;
        }
    if(ide)
        {
        
      UST[tc].indx=tc+1;
        for(i=0;i<q;i++)
            {
            if((strcmp(IDE[i].iden,ar))==0)
                {
                FLAG1=TRUE;
                r=i;
                break;
                }
            }
        if(FLAG1==0)
            {
            strcpy(UST[tc].tok,ar);
            strcpy(UST[tc].t_nme,"IDE");
            IDE[q].indx=q+1;
            strcpy(IDE[q].iden,ar);
            IDE[q].pos=q+1;
            UST[tc].pos=IDE[q].pos;
            q++;
            iden_count++;
            }
        else
        {
        strcpy(UST[tc].tok,ar);
        strcpy(UST[tc].t_nme,"IDE");
        UST[tc].pos=IDE[r].pos;
        }
        tc++;

        }
/*    if(!key && !sym && !ope && !lit && !ide)
        {
        printf("\n Invalid tokens: %s ", ar);
      }*/
    if(FLAG==TRUE)
         break;
    while(*token==' ')
    {
         token++;
         if(*token=='\0')
           {
            FLAG=TRUE;
            break;
         }
      }
    if(FLAG==TRUE)
        break;
    count=0;
   FLAG1=FALSE;
    key=FALSE;
    sym=FALSE;
    ope=FALSE;
    lit=FALSE;
    ide=FALSE;
      }
print_table(tc,lit_count,iden_count);
printf("\n Enter a key");
getche();
return 0;
}
void print_table(int tc, int lit_count, int iden_count)
{
int i;
printf("\n");
printf("\n\t Uniform Symbol Table");
printf("\n---------------------------------------------------------");
printf("\n #Index\t\tTokens\t\tClass\t\tPosition");
printf("\n---------------------------------------------------------");
for(i=0;i<tc;i++)
    {
    printf("\n%d\t\t%s\t\t%s\t\t%d",UST[i].indx,UST[i].tok,UST[i].t_nme,UST[i].pos);
    }
printf("\n\tTerminal Table\n");
printf("---------------------------------");
printf("\nIndex\tKeywords\tPosition\n");
printf("---------------------------------");
for(i=0;i<18;i++)
{
printf("\n#%d\t%s\t %d",TT[i].indx,TT[i].tok,TT[i].pos);
}
printf("\n\n\n");
printf("\n\t Literal Table");
printf("\n---------------------------------------------------------");
printf("\n #Index\t\tTokens\t\tPosition");
printf("\n---------------------------------------------------------");
for(i=0;i<lit_count;i++)
    {
    printf("\n%d\t\t%s\t\t%d",LT[i].indx,LT[i].lit,LT[i].pos);
    }
printf("\n\n\n");
printf("\n\t Identifier Table");
printf("\n---------------------------------------------------------");
printf("\n #Index\t\tTokens\t\tPosition");
printf("\n---------------------------------------------------------");
for(i=0;i<iden_count;i++)
    {
    printf("\n%d\t\t%s\t\t%d",IDE[i].indx,IDE[i].iden,IDE[i].pos);
    }
return;
}

int tokencount(char *str)
{
char *token,ar[30];
int tc=0,count=0,FLAG=FALSE;
token=str;
while(1)
       {
       while(*token!= ' ')
       {
       if(*token== '\0')
         {
         FLAG=TRUE;
         break;
         }
    ar[count++]=*token;
    token++;
    }
    ar[count]='\0';
    tc++;
    if(FLAG==TRUE)
       break;
         while(*token== ' ')
       {
       token++;
       if(*token== '\0')
           {
            FLAG=TRUE;
              break;
        }
            
    }
    if(FLAG==TRUE)
    break;
    count=0;
    }
    return (tc);
}
void classification( char ar[],int *key,int *sym,int *ope,int *lit,int *ide,int *pkey,int *psym,int *pope)
{
int  FLAG=FALSE,len,count=0,flag=TRUE,numb=0,d,t=0,m=0;
char *keyword[]={ "if","for","while","int","float","char"};
char *operatr[]={ "+","-","*","/","=","<",">"};
char *symbols[]={ ",","(",")",";","?"};
int i=0;
len=strlen(ar);
for(i=0;i<len;i++)
    if(ar[i]==' ')
    t++;
if(t==len)
    return;
if(len>8)
FLAG=FALSE;
for(i=0;i<=5;i++)
{
    if(strcmp(keyword[i],ar)==0)
    {
  //      printf("\n%s\tis a Keyword:",ar);
        FLAG=TRUE;
        flag=FALSE;
        *key=TRUE;
      *pkey=i;
    }

}
i=0;
for(i=0;i<=6;i++)
    {
    if(strcmp(operatr[i],ar)==0)
        {
     //    printf("\n%s\tis a Operator:",ar);
        FLAG=TRUE;
        flag=FALSE;
        *ope=TRUE;
      *pope=i;
        }
    }
i=0;
for(i=0;i<5;i++)
    {
    if(strcmp(symbols[i],ar)==0)
        {
     //    printf("\n%s\tis a Symbol:",ar);
        FLAG=TRUE;
        flag=FALSE;
        *sym=TRUE;
      *psym=i;
        }
    }
if ( ((65 <= ar[0] && ar[0] <= 93 )|| ( 97 <= ar[0] && ar[0] <= 122)) && len <= 8 && FLAG==FALSE)
    {
  //    printf("\n%s\tis an Identifier:",ar);
   for(i=1;i<strlen(ar);i++)
        {
        if( (65 <= ar[i] && ar[i] <= 93 ) ||  ( 97 <= ar[i] && ar[i] <= 122)  || ( 48 <= ar[i] && ar[i] <= 57) || ar[i]==95 )
            {
             m++;
            }

        }
    if(m==strlen(ar)-1)
    {
  //    printf("\n%s\tis an Identifier:\n",ar);
    FLAG=TRUE;
    flag=FALSE;
    *ide=TRUE;
    }
  //    FLAG=TRUE;
  //    flag=FALSE;
  //    *ide=TRUE;
    }
for(i=0;i<len;i++)
    {
    if(48 <= ar[i] && ar[i] <=57 )
    count++;
    else
        continue;
    }
if(count==len && flag==TRUE && FLAG==FALSE)

    {
    numb=atoi(ar);
    if(numb==0 || numb >999)
     {printf("\n Invalid token: %s ", ar);}//printf("%s\tis not a valid Constant:",ar);
    else
    {*lit=TRUE;}//printf("\n%s\tis a Constant:",ar);
    FLAG=TRUE;

}
i=0;
if( FLAG==FALSE)
    {
    if(ar[0]=='-' || (48 <= ar[0] && ar[0] <=57))
    {printf("\n Invalid token: %s ", ar);}//printf("%s\tis not a valid Constant ",ar);
    else
    {printf("\n Invalid token: %s ", ar);}//printf("\n%s\tis an INVALID TOKEN:",ar);
    }
//printf("\n");
return;
}