Saturday 30 July 2016

  • ADDING TWO NUMBERS UP TO 10000 DIGITS

    /*We know that by using int ( integer type ) variables we can store it a number up to 9 or 10 digits.Even long int type can store up to 18 or 19 digits.So we can not store or add two numbers greater than this digits.No problem.Here is a simple program that could add two numbers up to 10000 digits.*/

     #include <iostream>

    using namespace std;

    int main()
    {
        string  x,y;

        int a[10000],b[10000],C[10000],k = 0;

        int counter,counter2;

        cin >> x >> y;

        if( x.length() >= y.length() ){

        for( counter = 0; counter < x.length(); counter++ )

            a[counter] = x[counter] - 48;

        for( counter2 = 0; counter2 < y.length(); counter2++ )

            b[counter2] = y[counter2] - 48;
        }

        if( x.length() < y.length() ){

        for( counter = 0; counter < y.length(); counter++)

            a[counter] = y[counter] - 48;

        for( counter2 = 0; counter2 < x.length(); counter2++)

            b[counter2] = x[counter2] - 48;
        }
        int rem = 0;

        for( int i = counter-1, j = counter2-1; i >=0 || j>=0; i--, j--, k++ )
        {
            if( i>=0 && j>=0 )
            {
                int c = a[i]+b[j] + rem;

                if(c < 10)
                {
                        rem = 0;
                        C[k] = c;
                }
                else
                {
                        c = c-10;
                        rem = 1;
                       C[k] = c;

                }


            }
        else
        {
            int c = a[i]+rem;
              if(c<10)
                {
                        rem = 0;
                        C[k] = c;
                }
                else
                {
                        c = c-10;
                        rem = 1;
                       C[k] = c;

                }


        }

        }
        if(rem == 1)
            C[k++] = rem;
            for(int i = k-1; ;i--)
            {
                if(C[i] == 0){
                    k--;
                }
                else
                {
                    break;
            }
            }
        for(int i = k-1; i >= 0; i--)

            cout << C[i];

            cout << endl;

    return 0;
    }

     



Thursday 21 July 2016

  • OBJECT ORIENTED PROGRAMMING EXAMPLE

    Basic logic gates operations in C++

    // there is a simple object oriented program that will read the input values of different logic gates and show their output values.

    #include <iostream>

    using namespace std;

    class basic_gate
    {
        //member variables
        bool input1;
        bool input2;
        bool output;

    public :


        basic_gate()//base class constructor for initializing member variables to false
        {
            input1 = false;
            input2 = false;
            output = false;
        }

        void set_input(bool a,bool b)//for setting the input
        {
            input1 = a;
            input2 = b;
        }

        void set_input(bool a)//overloading set_input()
        {
            input1 = a;
        }

        void set_output(bool x)//for setting the output
        {
            output = x;
        }

        bool get_input1()//for returning 1st input
        {
            return input1;
        }

        bool get_input2()//for returning 2nd input
        {
            return input2;
        }

        bool get_output()//for returning the output
        {
            return output;
        }

        virtual void operation() = 0;//declaring a pure virtual member function

    };


    class AND_gate : virtual public basic_gate//here base class is inherited as virtual
    {

    public :

        void operation()//a new definition of operation function
        {
            bool o;

            o = this -> get_input1() * this -> get_input2();//processing output

            this -> set_output(o);//setting the output
        }
    };


    class OR_gate : virtual public basic_gate//here the base class is inherited as virtual
    {

    public :

        void operation()//another definition of operation function
        {
            bool o;

            o = this -> get_input1() + this -> get_input2();//processing output

            this -> set_output(o);//setting the output
        }
    };

    class NOT_gate : virtual public basic_gate//base class is inherited as virtual
    {

    public :

        void operation()//another definition of operation function
        {
            if(this -> get_input1() == false)

                this -> set_output(true);//setting the output

            else

                this -> set_output(false);
        }
    };

    class NAND_gate : public AND_gate , public NOT_gate//this derived class derived from two base classes
    {

        void operation()//another definition of operation function
        {
            AND_gate :: operation();
            this -> set_input( this -> get_output() );
            NOT_gate :: operation();
        }
    };

    class NOR_gate : public OR_gate , public NOT_gate//this derived class also derived from two base classes
    {

        void operation()
        {
            OR_gate :: operation();
            this -> set_input( this -> get_output() );
            NOT_gate::operation();
        }
    };


    int main()
    {
        bool X,Y;

        basic_gate *ptr;//declare a base type pointer

        AND_gate A;

        ptr = &A;//point the derived class

        cout << "Enter the inputs of AND gate:";
        cin >> X >> Y;

        ptr -> set_input(X,Y);
        ptr -> operation();
        cout << ptr -> get_output() << endl;

        OR_gate B;
        ptr = &B;//point the derived class

        cout << "Enter the inputs of OR gate:";
        cin >> X >> Y;

        ptr -> set_input(X,Y);
        ptr -> operation();
        cout << ptr -> get_output() << endl;

        NOT_gate c;
        ptr = &c;//point the derived class

        cout << "Enter the inputs of NOT gate:";
        cin >> X;

        ptr -> set_input(X);
        ptr -> operation();
        cout << ptr -> get_output() << endl;

        NAND_gate r;
        ptr = &r;//point the derived class

        cout << "Enter the inputs of NAND gate:";
        cin >> X >> Y;

        ptr -> set_input(X,Y);
        ptr -> operation();
        cout << ptr->get_output() << endl;

        NOR_gate z;
        ptr = &z;//point the derived class

        cout << "Enter the inputs of NOR gate:";
        cin >> X >> Y;

        ptr -> set_input(X,Y);
        ptr -> operation();
        cout << ptr -> get_output() << endl;

        return 0;
    }
     



Monday 4 July 2016

Introduction:

                                   What is Computer Programming?This is the burning question for all who are interested in the fact Computer.Textbook definition of programming is that "some finite sets of work for doing a particular job".But I dont think so.Before learning programming you have to learn about the history of computer,how it works,how it is related to humanlife,mainly you have to understand Computer.Programming is not only for solving some problems on different online judges or participating in programming contestes or like that.I believe that programming is like talking to a computer.So,programmers forget about the definition of programming,sense it,realize it,love it and start programming.

Kruskal Algorithm

Minimum Cost Spanning Tree By Kruskal Algorithm using priority_queue in c++ #include<bits/stdc++.h> using namespace std; typedef p...