Sunday 18 November 2012

Converting Infix expression to Prefix expression



#include<iostream>

#include<string>

#include<stack>

#include<cstdio>



using namespace std;



bool precedence(string a, string b);

void infixToPrefix(string s);



int main()
{

        string s;

        cin>>s;



        infixToPrefix(s);



        return 0;

}

void infixToPrefix(string s)

{

        stack<string> operator1;        //operator stack

        stack<string> op;               //operand stack



        for(int i=0; i<s.size(); i++){

                string s6="";

                s6+=s[i];



                if(s[i]!='+' && s[i]!='-' && s[i]!='*' && s[i]!='/' && s[i]!='(' && s                   [i]!=')' && s[i]!='$'){

                        string s5;

                        s5=s[i];

                        op.push(s5);

                }else if(s[i]==')'){  //if right parentheses

                 //continue to pop operator nd operand stack

                 //till left parentheses is found

                        while(operator1.top()!="("){

                                string a=operator1.top();

                                operator1.pop();
                                string b=op.top();

                                op.pop();

                                string c=op.top();

                                op.pop();

                                string s2;

                                s2=a+c+b;

                                op.push(s2);

                        }

                        //pop the left parentheses

                        operator1.pop();





                }else if(s[i]=='(' || operator1.empty() || (!operator1.empty() &&                 
      precedence(s6,operator1.top())) ){

                        string s5;

                        s5=s[i];

                        operator1.push(s5);

                }else{

                

                        while((!operator1.empty()) && !(precedence(s6, operator1.top                        ()))){

                                string a=operator1.top();

                                operator1.pop();

                                string b=op.top();

                                op.pop();

                                string c=op.top();

                                op.pop();

                                string s2;

                                s2=a+c+b;

                                op.push(s2);

                        }

                        string s5="";

                        s5+=s[i];

                        operator1.push(s5);

                }



        }

        /*If the stack is not empty,

        continue to pop operator and operand stacks building

        prefix expressions until the operator stack is empty.*/

        while(!operator1.empty()){

                string a=operator1.top();

                operator1.pop();

                string b=op.top();

                op.pop();

                string c=op.top();

                op.pop();

                string s2;

                s2=a+c+b;

                op.push(s2);

        }



        while(!op.empty()){

                cout<<op.top()<<" ";

                op.pop();

        }

        cout<<endl;



       }



// returns true if precedence of a is more than b

// else return false    

bool precedence(string a, string b)

{

        int aa=0;

        int bb=0;

        if(a=="("){

                return false;

        }else if(b=="("){

                return true;

        }else if(b==")"){

                return true;

        }else if(a==")"){

                printf("undefined operation\n");

                return 0;

        }



        if(a=="+" || a=="-") aa=1;

        if(a=="*" || a=="/") aa=2;

        if(b=="+" || b=="-") bb=1;

        if(b=="*" || b=="/") bb=2;
        if(a=="$") aa=3;

        if(b=="$") bb=3;

        if(aa > bb) return true;

        return false;

}

Searching in Link List


#include<iostream>
#include<process.h>
using namespace std;
class Chain;
class Node
{
private:
int info;
Node *link;
friend class Chain;
};

class Chain
{
private:
Node * first;
public:
Chain()
{
first = NULL;
}
void head_insert(int val)
{
Node *temp;
temp = new Node;
temp -> info = val ;
temp -> link = first;
first = temp;
}

void display()
{
Node *temp;
temp = first;
while(temp != NULL )
{
cout<<temp -> info<<endl;
temp = temp -> link;
}
}

Node *search(int key)
{
bool found = false;
if(first == NULL)
{
cout<<"there is nothing to search, searching is impossible. . "<<endl;
exit(1);;
}
Node * temp;
temp = new Node;
temp = first ;
while( temp != NULL)
{
if ( temp ->info == key )
return temp;
else
temp = temp ->link;
}
if(found == true)
cout<<"Key is found. . ."<<endl;
else
cout<<"Key is not Found. . ."<<endl;

}

};


void main()
{
Chain ch;
ch.head_insert(45);
ch.head_insert(15);
ch.head_insert(75);
ch.head_insert(5);
ch.display();
//ch.search(5);
//ch.display();
Node * t = search(t);
//ch.display();
}

Insertion in Singly Link List


#include<iostream>
using namespace std;
template<class T>
class Chain;
template<class T>
class Node
{
private:
T info;
Node *link;
friend class Chain<T>;

};

template<class T>
class Chain
{
private:
Node<T> *first;
public:
Chain()
{
first = NULL;
}
void head_insert(T val)
{
Node<T> *temp;
temp = new Node<T>;
temp -> info = val ;
temp -> link = first;
first = temp;
}

void display()
{
Node<T> *temp;
temp = first;
while(temp != NULL )
{
cout<<temp -> info<<endl;
temp = temp -> link;
}
}
};

void main()
{
Chain<int> ch;
ch.head_insert(5);
ch.head_insert(10);
ch.head_insert(12);
cout<<"the info part of the nodes are : "<<endl;
ch.display();
}

Insertion and Deletion in Arrays


#include <iostream.h>
const int SIZE = 10;
class Array
{
private:
int a[SIZE];
int n;
public:
Array()
{
for (int i=0;i<SIZE;i++)
a[i] = 0;
n = 0;
}
void read()
{
int num;
cout<<"how many values you want to enter ? "<<endl;
cin>>num;
for(int i = 0; i<num; i++)
{
cout<<"enter values "<<endl;
cin>>a[i];
}
n = num;
}
void display()
{
for (int i=0;i<n;i++)
cout << a[i];
cout << endl;
}
void del()
{
int index;
cout << "Enter index";
cin >> index;
for (int i=index;i<n-1;i++)
a[i] = a[i+1];
n--;
display();
}
void insert()
{
int index;
cout << "Enter index";
cin >> index;
for (int i=n;i>index;i--)
a[i] = a[i-1];
cout << "Enter Value";
cin >> a[i];
n++;
display();
}


};
void main()
{
Array itm, a2, a3;
itm.read();
itm.display();
a2.read();
a2.display();
itm.del();
itm.insert();
}

Merging Two Arrays in C++


#include <iostream.h>
const int SIZE = 10;
class Array
{
private:
int a[SIZE];
int n;
public:
Array()
{
for (int i=0;i<SIZE;i++)
a[i] = 0;
n = 0;
}
void read()
{
int num;
cout<<"how many values you want to enter ? "<<endl;
cin>>num;
for(int i = 0; i<num; i++)
{
cout<<"enter values: "<<endl;
cin>>a[i];
}
n = num;
}
void display()
{
for (int i=0;i<n;i++)
cout << a[i];
cout << endl;
}
void merge(Array a1, Array a2)
{
int i=0,j=0;

while (i < a1.n)
{
a[n] = a1.a[i];
i++; n++;
}
while (j < a2.n)
{
a[n] = a2.a[j];
j++; n++;
}
}
};
void main()
{
Array itm, a2, a3;
itm.read();
itm.display();
cout<<"for 2nd array ";
a2.read();
a2.display();
a3.merge(itm,a2);
a3.display();

}

Average of Arrays


#include<iostream.h>
const int size = 10;
class array
{
private:
int a[size];
int n;
public:
array()
{
for ( int i=0; i<size; i++ )
{
a[i];
n = 0;
}
}
void read()
{
int num;
cout<<"how many values you want to insert ?? "<<endl;
cin>>num;
for ( int i = 0; i<num; i++)
{
cout<<"Enter value: "<<endl;
cin>>a[i];
}
n = num;

}
void display ()
{
for (int i = 0; i<n; i++ )
cout<<a[i]<<endl;
}
void average()
{
int av = 0;
for ( int j = 0; j<size; j++ )
av = av + a [j];
int avg = av / size ;
cout<<avg;
}


};
void main()
{
array arr;
arr.read();
cout<<"The entered values are: "<<endl;
arr.display();
cout<<"the average of arrays : "<<endl;
arr.average();

}

Array Reading and Displaying


#include<iostream.h>
const int size = 10;
class array
{
private:
int a[size];
int n;
public:
array()
{
for ( int i=0; i<size; i++ )
{
a[i];
n = 0;
}
}
void read()
{
int num;
cout<<"how many values you want to insert ?? "<<endl;
cin>>num;
for ( int i = 0; i<num; i++)
{
cout<<"Enter value: "<<endl;
cin>>a[i];
}
n = num;

}
void display ()
{
for (int i = 0; i<n; i++ )
cout<<a[i]<<endl;
}

};
void main()
{
array arr;
arr.read();
cout<<"The entered values are: "<<endl;
arr.display();
}