Tuesday 5 November 2013

Auto light switching arduino program based on bidirectional counter











Here I have used common cathode configuration , 7-segment pins a,b.c...g  are connected to 4,5,6...10 digital pins of arduino board.

int pd1=2;   //photodiode
 int pd2=3;
//Photodiode to digital pin 2 and pin 3
 int led=13;          //led to digital pin 9
 int pd1Read=0;      //Readings from sensor to analog pin 0                  
 int pd2Read=1;
 int limit1=975;//Threshold range of an obstacle
 int limit2=975;
 int flag=0;

 int count=0;
const int segment[]={4,5,6,7,8,9,10};

 void setup()  
 {   for(int i=0;i<7;i++)
     {
    pinMode(segment[i],OUTPUT);

     }
 
   pinMode(pd1,OUTPUT);
   pinMode(pd2,OUTPUT);
  pinMode(led,OUTPUT);
  digitalWrite(pd1,HIGH);   //supply 5 volts to photodiode
  digitalWrite(pd2,HIGH);
  digitalWrite(led,LOW);      //set the led in off mode (initial condition)
  Serial.begin(9600);          //setting serial monitor at a default baund rate of 9600
 }
 void loop()
 {
  int val1=analogRead(pd1Read);  //variable to store values from the photodiode  1
  int val2=analogRead(pd2Read);
  // const byte segmentnumbers[]={B1111110,B0110000,B1101101,B1111001,B0110011,B1011011,B1011111,B1110000,B1111111,B1111011};

  if(val1<=limit1)
  {count=count+1;
  //digitalWrite(led,HIGH);
  }
  delay(500);
  if(val2<=limit2)
  {count=count-1;
   if(count==-1)
   count=0;
  }
 
    for(int i=0;i<7;i++)
    {
    digitalWrite(segment[i],LOW);
    }
         
// delay(100);

   switch(count)
      {case 0:
         for(int i=0;i<6;i++)
         digitalWrite(segment[i],HIGH);
          break;
        case 1:
          digitalWrite(segment[1],HIGH);
          digitalWrite(segment[2],HIGH);
          break;
       
         case 2:
       
       digitalWrite(segment[0],HIGH);
       digitalWrite(segment[1],HIGH);
       digitalWrite(segment[3],HIGH);
       digitalWrite(segment[4],HIGH);
       digitalWrite(segment[6],HIGH);
       break;
             
     case 3:
      digitalWrite(segment[0],HIGH);
       digitalWrite(segment[1],HIGH);
       digitalWrite(segment[3],HIGH);
       digitalWrite(segment[2],HIGH);
       digitalWrite(segment[6],HIGH);
       break;
   
       
         case 4:
       digitalWrite(segment[2],HIGH);
       digitalWrite(segment[1],HIGH);
       digitalWrite(segment[5],HIGH);
       digitalWrite(segment[6],HIGH);
       break;
         case 5:
          digitalWrite(segment[0],HIGH);
       digitalWrite(segment[2],HIGH);
       digitalWrite(segment[3],HIGH);
       digitalWrite(segment[5],HIGH);
       digitalWrite(segment[6],HIGH);
       break;
       
         case 6:
          for(int i=0;i<7;i++)
            if(i!=1)
              digitalWrite(segment[i],HIGH);
                   break;
               
         case 7: digitalWrite(segment[0],HIGH);
       digitalWrite(segment[1],HIGH);
       digitalWrite(segment[2],HIGH);
        break;
       
         case 8:
         for(int i=0;i<7;i++)
         digitalWrite(segment[i],HIGH);
          break;
         case 9:
         for(int i=0;i<7;i++)
           if(i!=4)
           digitalWrite(segment[i],HIGH);
          break;
      }

  if(count==0)
   {digitalWrite(led,LOW);
 
  }
else
 {
  digitalWrite(led,HIGH);
 }
  delay(50);

 }


Sunday 11 August 2013

Indentation of program by c++

#include<iostream>
#include<fstream>
#include<stack>
using namespace std;
class Indentation
{ char store[500000];
  int numofchar;
    public:
         void readFile(ifstream &inp);
         void indentData();
         void printSpace(int &s);
};
int main()
{Indentation f;
ifstream f1("filetobeindented.txt");
f.readFile(f1);
f.indentData();
return 0;
}


 void Indentation::readFile(ifstream &inp)
    {int i;
    inp.unsetf(ios::skipws); //include white space in read
        for(i=0;!inp.eof();i++)
        inp>>store[i];
        numofchar=i-1;
    }
    void Indentation::printSpace(int &s)
    {
        for(int k=0;k<s;k++) cout<<"   ";
    }
void Indentation::indentData()
{ stack<char> symbol;

        stack<int> symbolspacegap;
    int cntsymbolgap=0,s=0,k;
    for(int i=0;i<numofchar;i++)
    {  cntsymbolgap++;
        switch(store[i])
          {case '#' :{ for(;store[i]!='>';i++)
                        cout<<store[i];
                        cout<<">\n";
                        break;
                     }
           case '\n':  printSpace(s);cntsymbolgap=0;break;
           case ';' :  cout<<store[i]<<endl; printSpace(s);break;
           case '{' :{ cout<<endl;
                    printSpace(s);
                    cout<<store[i]<<endl;
                     printSpace(s);
                    symbol.push(store[i]);
                    symbolspacegap.push(s);s++;
                    break;
                     }
            case '}':{cout<<endl;
                  printSpace(symbolspacegap.top());
                  cout<<store[i]<<endl;
                  symbol.pop();symbolspacegap.pop();
                  break;
                  }
          case '(' :{  for(;store[i]!=')';i++)
                            cout<<store[i];cout<<")";break;
                    }

         default:{cout<<store[i];}
        }
    }

}

converting infix to postfix expressions using stack

#include<iostream>
#include<stack>
#include<string>
#include<cstdlib>

using namespace std;

class Infix2postfix
{char infix[50];
 char postfix[50];


public:
    void readData()
    {
        cout<<"Enter infix epression";
        cin.getline(infix,50);
    }
    int priorityOf(char sym)
    {
        switch(sym)
        {
        case '*':
        case '/':return 2; break;
        case '+':
        case '-':return 1; break;
        case '(':return 0;break;
        default: cout<<"Unknown symbol  "<<sym;exit(1);
        }
    }

    void infix2Postfix()
    { stack<char> s;

    int i,p=0;char c;
     for(i=0;infix[i]!='\0';i++)
     {
         while(infix[i]==' ' || infix[i]=='\t') i++;

        if(isdigit(infix[i]))   postfix[p++]=infix[i];
        else if(infix[i]=='(')  s.push(infix[i]);
        else if(infix[i]==')')
        {    if(s.empty())
              {
                  cout<<"There is no ( corresponding to ) in infix expression";exit(1);
              }
            while(s.top()!= '(' )
            {
                postfix[p++]=s.top();
                s.pop();
            }
            s.pop();

        }
        else if (infix[i]=='*' ||infix[i]=='/' ||infix[i]=='+' ||infix[i]=='-' )
        {    if(s.empty()) s.push(infix[i]);
              else if(priorityOf(infix[i])>priorityOf(s.top()))
            s.push(infix[i]);
            else
                {
                     while(priorityOf(s.top())>=priorityOf(infix[i]))
                    {
                    postfix[p++]=s.top();
                    s.pop();

                    if(s.empty()) break;

                    }
                     s.push(infix[i]);
                }
        }

    }
    while(!s.empty())
    { postfix[p++]=s.top();
    s.pop();

    }
cout<<"\nEquivalent postfix expresion  : ";
     for(int k=0;k<p;k++)
        cout<<postfix[k]<<" ";
    }
};
int main()
{Infix2postfix e;
e.readData();
e.infix2Postfix();
return 0;
}

Evaluating postfix expressions using stack

#include<iostream>
#include<cstdlib>
#include<stack>
using namespace std;
class Postfix
{public:

    char exp[50];
    int char2integer(char x)
    {
        return(x-'0');  //he character encodings for digits in AscII are all in order from 48 (for '0') to 57 (for '9').
    }
    void inputdata()
    { cout<<"Enter postfix expression\n";
     cin.getline(exp,50);
    }
    void evaluatepostfix()
    {  stack<int> store;
        int j=0,num,first,second,result;



        for(;exp[j]!='\0';j++)
        {while( exp[j]==' ' || exp[j]=='\t') j++;

            if(exp[j]>='0'  && exp[j]<='9')
            {
                num =char2integer(exp[j]);
                store.push(num);
            }
            else
            {
                first=store.top();store.pop();
                second=store.top();store.pop();
                switch(exp[j])
                {
                case '*' :result=second * first; store.push(result);break;
                case '/' :result=second / first; store.push(result);break;
                case '+' :result=second + first; store.push(result);break;
                case '-' :result=second - first; store.push(result);break;
                default :cout<<"unknown operator \n";exit(1);
                }

           }

       }
       cout<<"After evaluating postfix expression, result is :"<<store.top();
    }
};
int main()
{Postfix ex;
ex.inputdata();
ex.evaluatepostfix();
return 0;
}



Monday 5 August 2013

Graphical representation of frequency of data contained in a file

If sourcefile.txt contains the following data
 a 2 ab 10 2 ab 10 2 2 10 8

then the graph in graph.txt file would be

*
* *
*   *
 *  *   *   * *
 a 2 ab 10 8


#include<iostream>
#include<fstream>

using namespace std;
class Graphview
{public:
    int big;
    int biggest(int num[],int numofelements);
    void showfreqgraph(string store[],int storefreq[],int numofelements,ofstream &output);
    void computefrequency(ifstream &input,string store[],int storefreq[],int &numofelements);



};
int main()
{ ifstream input("sourcefile.txt");
ofstream output("graph.txt");
  string store[100];int storefreq[100];

   int numofelements=0;
       Graphview one;
       one.computefrequency(input,store,storefreq,numofelements);
       one.showfreqgraph(store,storefreq,numofelements,output);
     return 0;
}
int Graphview::biggest(int num[],int numofelements)
{ int big=0;
    for(int i=0;i<numofelements;i++)
    {
        if(num[i]>big)
            big=num[i];
    }
    return big;
}
 void Graphview:: showfreqgraph(string store[],int storefreq[],int numofelements,ofstream &output)
    { for(int k=0;k<numofelements;k++)
            cout<<store[k]<<"\t---->    "<<storefreq[k]<<endl;
            cout<<endl<<endl;
    int i;
    big=biggest(storefreq,numofelements);
    int freqprintstart[big];
    for(i=0;i<numofelements;i++)
      freqprintstart[i]=big-storefreq[i];


    for(int line=0;line<big;line++)
       {
       for(int k=0;k<numofelements;k++)
       {
           if(line>=freqprintstart[k])
          {cout<<"*\t";
           output<<"*\t";
          }
           else
            {cout<<"\t";
            output<<"\t";
            }
       }
        cout<<endl;
        output<<endl;

       }

       for(i=0;i<numofelements;i++)
       { cout<<store[i]<<"\t";
         output<<store[i]<<"\t";


       }
    }

     void Graphview::computefrequency(ifstream &input,string store[],int storefreq[],int &numofelements)
     { int i,j,k,c;
        string x[50];
        for(i=0;!input.eof();i++)
        input>>x[i];
        cout<<"number of elements from file:" <<i-1<<endl;

        for(j=0;j<i;j++)
        {c=1;int remember;
           for(k=j+1;k<i;k++)
           {   if(x[j]==x[k])
               {c++;i--;remember=k;
                  while(remember<i)
                   { x[remember]=x[remember+1];
                       remember++;
                   }
                   k--;
               }
           }
           store[j]=x[j];
           storefreq[j]=c;
        }
numofelements=j-1;


     }

intersection of data contained in 2 files

If set1.txt contain 5 6 7 9 4 10 3 2  and set2.txt contain 89 8 10 45 11 5
then Intersection.txt would contain 5  10


#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cmath>
using namespace std;
class Intersection
{public:
    void  Read2Files(ifstream &input1 ,ifstream &input2, string store1[],string store2[] ,int &storecount1,int &storecount2 );
    void copy2fileintersection(string store1[],string store2[],int &storecount1,int &storecount2, ofstream &output);  //set has no repition of elements

};

int main()
{

int storecount1=0,storecount2=0,k;
string store1[50],store2[50];

    ifstream f1("set1.txt"),f2("set2.txt");
    ofstream f3("Intersection.txt");
    if(f3.fail())
    {
        cout<"The file to be written not accessed";
        exit(1);
    }
Intersection group;


 group.Read2Files(f1 ,f2,store1,store2,storecount1,storecount2 );

 group.copy2fileintersection(store1,store2,storecount1,storecount2,f3);




    return 0;
}

void Intersection::Read2Files(ifstream &input1 ,ifstream &input2, string store1[],string store2[] ,int &storecount1,int &storecount2 )
    {
        if(input1.fail()|| input2.fail())
        {
        cout<<"Files to be read accessing failed";
        exit(1);
         }

    int i,j;string element;
    for(i=0;!input1.eof();i++)
    {input1>>element;
        store1[i]=element;

    }
        for(j=0;!input2.eof();j++)
        {input2>>element;
        store2[j]=element;

        }
        storecount1=i-1; storecount2=j-1;

    }


    void Intersection::copy2fileintersection(string store1[],string store2[],int &storecount1,int &storecount2, ofstream &output)  //set has no repition of elements
    {
int i,j;
       for(i=0;i<storecount1;i++)
         {
           for(j=0;j<storecount2;j++)
           {
               if (store1[i]==store2[j])
                {cout<<store1[i]<<"  ";
                 output<<store1[i]<<"  ";
                 break;
                }

           }

         }
          cout<<"\nIntersection of elements copied to file";
    }

Union of data contained in 2 files

if set1.txt file contains 2 3 56 4 5 7 and set2.txt contains 8 5 4 9 7 85
Union.txt file would contain  2 3 56 4 5 7 8 9 85



#include<iostream>
#include<fstream>
#include<cmath>
#include<cstdlib>
using namespace std;
class Unionofelements
{public:
    string  element;
    void  read2files(ifstream &input1 ,ifstream &input2, string store[] ,int &storecount );
    void copyunion2file(string  store[],int &storecount, ofstream &output);  //set has no repition of elements

};

int main()
{Unionofelements group;
string  store[100]; int storecount=0;
    ifstream f1("set1.txt"),f2("set2.txt");
    ofstream f3("Union.txt");
    if(f3.fail())
    {
        cout<"The file to be written not accessed";
        exit(1);
    }
    group.read2files(f1,f2,store,storecount);
    group.copyunion2file(store,storecount,f3);
    return 0;

}

 void Unionofelements::read2files(ifstream &input1 ,ifstream &input2, string store[] ,int &storecount )
    { int i;
    for(i=0;!input1.eof();i++)
        {input1>>element;
        store[i]=element;
        }
        i--;  //note this
        for(;!input2.eof();i++)
        {input2>>element;
        store[i]=element;
        }
        i--;
        storecount=i;
        for(int k=0;k<i;k++)  //displaying elements from both sets
            cout<<store[k]<<" ";
        cout<<"\nnumber of elements :  "<<i<<endl;
    }

    void Unionofelements::copyunion2file(string  store[],int &storecount, ofstream &output)  //set has no repition of elements
    {int i,j,remember;
       for(i=0;i<storecount;i++)
       {   for(j=i+1;j<storecount;j++)
            {if (store[i]==store[j])
               {remember=j;storecount--;
                  for(;remember<storecount;remember++)
                    store[remember]=store[remember+1];
                  j--;
                }

             }
          cout<<store[i] <<"  ";
          output<<store[i]<<" ";
       }

cout<<"\nUnion of elements copied.....";

    }