add

Thursday 16 February 2017

event driven programming with examples



Event Driven Programming


In computer programming, event-driven programming is a programming paradigm in which the flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or messages from other programs/threads. Event-driven programming is the dominant paradigm used in graphical user interfaces and other applications (e.g. JavaScript web applications) that are centered on performing certain actions in response to user input.



In an event-driven application, there is generally a main loop that listens for events, and then triggers a callback function when one of those events is detected. In embedded systems the same may be achieved using hardware interrupts instead of a constantly running main loop. Event-driven programs can be written in any programming language, although the task is easier in languages that provide high-level abstractions, such as closures

Event-driven Program
Definition:

An event-driven program is one that largely responds to user events or other similar input. The concept of event-driven programming is an important one in application development and other kinds of programming, and has spawned the emergence of event handlers and other resources.

Event-driven application:



An event-driven application is a computer program that is written to respond to actions generated by the user or the system. In a computing context, an event is any identifiable occurrence that has significance for system hardware or software. As such, events include both user-generated actions like mouse clicks and keystrokes and system-generated events such as program loading

Exception handlers:

In PL/1, even though a program itself may not be predominantly event-driven, certain abnormal events such as a hardware error overflow or "program checks" may occur that possibly prevent further processing. Exception handlers may be provided by "ON statements" in (unseen) callers to provide housekeeping routines to clean up afterwards before termination.

Creating event handlers:


The first step in developing an event-driven program is to write a series of subroutines, or methods, called event-handler routines. These routines handle the events to which the main program will respond. For example, a single left-button mouse-click on a command button in a GUI program may trigger a routine that will open another window, save data to a database or exit the application. Many modern-day programming environments provide the programmer with event templates, allowing the programmer to focus on writing the event code.





The second step is to bind event handlers to events so that the correct function is called when the event takes place. Graphical editors combine the first two steps: double-click on a button, and the editor creates an (empty) event handler associated with the user clicking the button and opens a text window so you can edit the event handler.

The third step in developing an event-driven program is to write the main loop. This is a function that checks for the occurrence of events, and then calls the matching event handler to process it. Most event-driven programming environments already provide this main loop, so it need not be specifically provided by the application programmer. RPG, an early programming language from IBM, whose 1960s design concept was similar to event-driven programming discussed above, provided a built-in main I/O loop (known as the "program cycle") where the calculations responded in accordance to 'indicators' (flags) that were set earlier in the cycle

A trivial event handler:


Because the code for checking for events and the main loop do not depend on the application, many programming frameworks take care of their implementation and expect the user to provide only the code for the event handlers. In this simple example there may be a call to an event handler called OnKeyEnter() that includes an argument with a string of characters, corresponding to what the user typed before hitting the ENTER key. To add two numbers, storage outside the event handler must be used. The implementation might look like below.



Code:

Globally declare the countrer k and the integer t.

Onkey enter ( character C )

{

Convert c to a number N

If K is zero store N in T and Increment K

Otherwise add N to T , print the result and reset K to zero

}




Thursday 9 February 2017

c++ circular link list source code

 C++ Circular link List source code

Dear readers  this code is simplest form of  circular link list, copy paste the code in your  IDE then you try to understand if you get any trouble to understanding the code comment me I give you answer for make you understand.

#include<iostream>
#include <stdlib.h>
using namespace std;
void welcome();
void welcome()
{
 system("color 1A");
 cout<<"\n\n\n";
 cout<<"\t\t\t\t\t*******************************************   W E L L    C O M E    ******************************************"<<endl;
 cout<<"\t\t\t\t\t|!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!|"<<endl;
 cout<<"\t\t\t\t\t|                                              | title |                                           |"<<endl;
 cout<<"\t\t\t\t\t|                                             *------------------*                                           |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t|    Topic    :_)                   C I R C U L A R        L I N K E D      L I S T                          |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t|                                                ( STUDENT INFORMATION )                                     |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t|                                                    XXXXXXXXX                                         |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t|                                                   REG NO:     XXXXXX                                        |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t|                                                    SEMISER       xxxxxxx                                        |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t|                                                    DATE          10-02-017                                |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t|                    S U B M I T T E D       T O     xxxxxxxxxxxxxxxxxxxx                    |"<<endl;
 cout<<"\t\t\t\t\t|                                                                                                            |"<<endl;
 cout<<"\t\t\t\t\t**************************************************************************************************************"<<endl;
  cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t\t\t\t";
 system("pause");
  system("cls");


}



      class  list
         {
         struct node
         {
         int values;
         node *next;
         };
                               
                               
          public:
         list()
           {
           head=NULL;
           }
         
                                  
          void create(int data)
            {
            node *temp,*temp1;
            if(head==NULL)
               {
               temp=new node;
               temp->values=data;
               head=temp;
               temp->next=head;
               }
           else
               {
            temp=head;
            while(temp->next!=head)
               temp=temp->next;
               temp1=new node;
               temp1->values=data;
               temp1->next=head;
               temp->next=temp1;
             }
              }
             
             
             
             
          void beg_insert(int data)
               {
                node *temp,*temp1;
                 temp=new node;
                 temp1=head;
              if(temp1==NULL)
                cout<<"\n Firstely create using 1 , list not exist";
              else
                 {
                 while(temp1->next!=head)
                   temp1=temp1->next;

                 temp->values=data;
                 temp->next=head;
                 head=temp;
                 temp1->next=head;
                }

                }
               
               
               
            void in_insert(int pos,int data)
                  {
                  node *temp,*t;
                  int a;
                  a=pos;
                  temp=head;
                  while(pos>1)
                {
                temp=temp->next;
                pos--;
                }
               if(temp==NULL)
                   cout<<"\n\t firstely create using 1 , List not exist ";
              else  if(a==0)
                beg_insert(data);

                 else
                   {
                   t=new node;
                   t->values=data;
                   t->next=temp->next;
                   temp->next=t;
                   }
                }
               
               
               
               
               
            void del( )
            {
            node *temp,*t;
            temp=head;
            if(temp==NULL)
              cout<<"\n \tList not Exists ";
            else if(temp->next==head)
                {
                delete temp;
                head=NULL;
                }
            else
              {
              t=head->next;
              while(t->next!=head)
                 {
                 t=t->next;
                 temp=temp->next;
                 }
               // t=temp->next->next;
                temp->next=head;
                delete t;
                }
             }


              void beg_del()
             {
              node *temp,*temp1;
              temp=head;
              if(head==NULL)
                cout<<"\n\t Link list has no nodes";
              else
               {
                temp1=head;
                while(temp1->next!=head)
                   temp1=temp1->next;
              head=temp->next;
              temp1->next=head;
              delete temp;
               }
              }
             
             
              void mid_del(int pos)
              {
              node *temp,*t;
                int a=pos;
                temp=head;
               while(pos>1)
                  {
                  temp=temp->next;
                   pos--;
                   }
             if(a==1 )
                   beg_del();

             else  if(temp==NULL)
                 cout<<"\n\t List not Exist ..";
              else
               {
               t=temp->next;
               temp->next=temp->next->next;
               delete t;
               }
              }
             
             
             
             
             

            void show()
              {
              node *temp;
              temp=head;
              cout<<"\n\t";
              if(temp==NULL)
             cout<<"\n \tList not exist";
              cout<<temp->values;
              temp=temp->next;
              while(temp!=head)
                 {
                 cout<<" psition no"<<" "<<temp->values;
                 temp=temp->next;
                 }
               }
              
              
              
                private:
                                node *head;
             };
            
            
int main() {
               
               
               
                system("mode 1000");
    system("color 4E");
    welcome();
    list l;
            int choice,position;
            while(1)
              {
              cout<<"\n\t\t\t\t\t    | E N T E R     Y O U R      C H O I S E |"<<endl;
              cout<<"\n\t\t\t\t\t    *----------------------------------------*"<<endl;
              cout<<"\n\n\n\t\t\t\t\t 1: To create a  Node";
                                                  cout<<"\n\t\t\t\t\t 2: To insert at Begining";
                                                  cout<<"\n\t\t\t\t\t 3: To insert at any position";
              cout<<"\n\t\t\t\t\t 4: To delete";
                                                  cout<<"\n\t\t\t\t\t 5: To delete at begining";
                                                  cout<<"\n\t\t\t\t\t 6: To Delete at any position ";
              cout<<"\n\t\t\t\t\t 7: To Display";
                                                  cout<<"\n\t\t\t\t\t 8: To exit";
              cout<<"\n\n";
              cin>>choice;
              if(choice==1)
              {
              cout<<"\n\t Enter data to Node: ";
              cin>>choice;
              l.create(choice);
              }
             else if(choice==2)
               {
               cout<<"\n\t Enter element to insert at begining : " ;
               cin>>choice;
               l.beg_insert(choice);
               }
              else if(choice==3)
            {
            cout<<"\nEnter element to insert at specific position  :  ";
            cin>>choice;
            cout<<"\n Enter position  : ";
            cin>>position;
               l.in_insert(position,choice);
               }
          else if(choice==4)
             l.del();
          else if (choice==5)
            l.beg_del();
          else if(choice==6)
          {
            cout<<"\n Enter position where u have to delete";
            cin>>position;
            l.mid_del(position);
              }
           else if(choice==7)
               l.show();
             
            else if(choice==8)
              {
              cout<<"\n Press Enter to Exit";
              break;
              }
             else
               cout<<"\n\t Wrong Choice   ";
            }
  
    system("pause");

}

Monday 6 February 2017

C++ nested if-else statements with examples

Two-Way Selection
There are many programming situations in which you must choose between two alternatives. For example, if a part-time employee works overtime, the paycheck is calculated using the overtime payment formula; otherwise, the paycheck is calculated using the regular formula. This is an example of two-way selection. To choose between two alternatives—that is, to implement two-way selections—C++ provides the if. . .else statement. Two-way selection uses the following syntax:


if {
statement 1
   }
Else
{
Statement 2
}




Take a moment to examine this syntax. It begins with the reserved word if, followed by a logical expression contained within parentheses, followed by a statement, followed by the reserved word else, followed by a second statement. Statements 1 and 2 are any valid++ statements. Into-way selection, if the value of the expression is true, statement1 executes. If the value of the expression is false, statement2 executes.

Example:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
double wages, rate, hours;
cout << fixed << showpoint << setprecision(2);                     //Line 1
cout << "Line 2: Enter working hours and rate: ";                  //Line 2
cin >> hours >> rate;                                                                  //Line 3
if (hours > 40.0)                                                                          //Line 4
wages = 40.0 * rate +
1.5 * rate * (hours - 40.0);                                                           //Line 5
else                                                                                              //Line 6
wages = hours * rate;                                                                   //Line 7
cout << endl;                                                                                 //Line 8
cout << "Line 9: The wages are $" << wages
<< endl;                                                                                          //Line 9


Multiple Selections: Nested if

In the previous sections, you learned how to implement one-way and two-way selections in a program. Some problems require the implementation of more than two alternatives.
For example, suppose that if the checking account balance is more than $50,000, the interest rate is 7%; if the balance is between $25,000 and $49,999.99, the interest rate is
5%; if the balance is between $1,000 and $24,999.99, the interest rate is 3%; otherwise,
4Selection: if and if...else the interest rate is 0%. This particular problem has four alternatives—that is, multiple selection paths. You can include multiple selection paths in a program by using an if.. .else structure if the action statement itself is an if or if. . .else statement. When one control statement is located within another, it is said to be nested.

Example:
if (balance > 50000.00)                                                                                  //Line 1
interestRate = 0.07;                                                                                      //Line 2
else                                                                                                                //Line 3
if (balance >= 25000.00)                                                                               //Line 4
interestRate = 0.05;                                                                                     //Line 5
else                                                                                                                  //Line 6
if (balance >= 1000.00)                                                                                  //Line 7
interestRate = 0.03;                                                                                       //Line 8
else                                                                                                                   //Line 9
interestRate = 0.00;                                                                                       //Line 10
A nested if. . .else structure demands the answer to an important question: How do you know which else is paired with which if? Recall that in C++, there is no stand-alone else statement. Every else must be paired with an if. The rule to pair an else with an if is as follows:
Pairing an else with an if: In a nested if statement, C++ associates an else with the most recent incomplete if—that is, the most recent if that has not been paired with an else.

Comparing if...else Statements with a Series of if Statements

Consider the following C++ program segments, all of which accomplish the same task.

a
 if (month == 1)                                                                          //Line 1
cout << "January" << endl;                                                      //Line 2
else if (month == 2)                                                                   //Line 3
cout << "February" << endl;                                                     //Line 4
else if (month == 3)                                                                    //Line 5
cout << "March" << endl;                                                          //Line 6
else if (month == 4)                                                                     //Line 7
cout << "April" << endl;                                                                //Line 8
else if (month == 5)                                                                       //Line 9
cout << "May" << endl;                                                                //Line 10
else if (month == 6)                                                                     //Line 11
cout << "June" << endl;                                                              //Line 12

b  
 if (month == 1)
cout << "January" << endl;
if (month == 2)
cout << "February" << endl;
if (month == 3)
cout << "March" << endl;
if (month == 4)
cout << "April" << endl;
if (month == 5)
cout << "May" << endl;
if (month == 6)
cout << "June" << endl;

Program segment (a) is written as a sequence of if. . .else statements; program segment

(b) is written as a series of if statements. Both program segments accomplish the something. If month is 3, then both program segments output March. If month is 1, then in program segment (a), the expression in the if statement in Line 1 evaluates to true. The statement (in Line 2) associated with this if then executes; the rest of the structure, which is the else of this if statement, is skipped; and the remaining if statements are not evaluated. In program segment (b), the computer has to evaluate the expression in each if statement because there is no else statement. As a consequence, program segment (b) executes more slowly than does program segment (a).