Wednesday, 14 September 2011

Operators and Expressions

Expression:

An expression is An expression is a sequence of operands and operators that reduces to a single value.


C Programming Provides its own rules of Expression, whether it is legal expression or illegal expression.

For example, in the C language x+5 is a legal expression.

Every expression consists of at least one operand and can have one or more operators.
Operands are values and Operators are symbols that represent particular actions.


Operators and Expressions are a part of C Tokens(words). C contains a rich set of operators.
defn:
  An Opertor in a program language specifies an operation to be performed that gives a result(value).

Category of Operators
The Operators are classified into three types:
1. Unary Opertors
2. Binary Operators
3. Ternary Operators


Unary Operators
A unary operator is an operator, which operates on one operand.

Binary Operators
A binary operator is an operator, which operates on two operands

Ternary Operators
A ternary operator is an operator, which operates on three operands.



These Operators depending on whether they operate one, two, or three operands respectively
(Operands means constants and variables together are called operands)

                                                  Different types of Operators:


1. Arithmetic Operator
2. Relational Operators
3. Logical Operators
4. Assignment Operator
5. Increment/Decrement Operators
6. Conditional Operators
7. Bitwize Operators
8. Special Operators

1. Arithmetic Operator

The arithmetic operator is a binary operator, which needs two operands to perform its operation. Following are the arithmetic operators

Operator       Meaning
+                   Addition (or) unary plus
-                    subraction (or) unary Minus
*(asterisk)     multiplication
/                    division
%                  modulus(remainder)



2. Relational Operators

Relational operators compare between two operands and returns  true or false i.e. 1 or 0. In C and many other languages a true value is denoted by the integer 1 and a false value is denoted by the integer 0. Relational operators are used in conjunction with logical operators and conditional & looping statements.

Following are the various relational operators
Operator       Meaning

 <                    Less than
<=                  Less than or equal to
>                    Greater than
>=                 Greater than or equal to

==               Equal to
!=               Not equal to


3. Logical Operators

A logical operator is used to compare or evaluate logical and relational expressions. There are three logical operators available

Operator     Meaning
&&             Logical AND
||             Logical OR
!            Logical NOT



Truth table for Logical AND

1 1 1
1 0 0
0 1 0
0 0 0

Truth table for Logical OR

1 1 1
1 0 1
0 1 1
0 0 0



The Logical NOT operator (!)
The ! (Logical NOT) operator is a unary operator. This operator opposite(inverses) a result.


2. Assignment Operator


An assignment operator (=) is used to assign a constant or a value of one variable to left handside variable.

(a)Multiple assignments:

You can use the assignment for multiple assignments as follows:

a = b= c = 5;
At the end of this expression all variables a, b and c will have the value 10. Here the order of evaluation is from right to left. First 10 is assigned to c, hence the value of c now becomes 10. After that, the value of c (which is now 10) is assigned to b, hence the value of b becomes 10. Finally, the value of b (which is now 10) is assigned to a, hence the value of a becomes 10.

(b)Arithmetic Assignment Operators

Arithmetic Assignment operators are a combination of arithmetic and the assignment operator. With this operator, the arithmetic operation happens first and then the result of the operation is assigned.

Operators
+=
-=
*=
/=
%=

6. Conditional Operator(?:)

A conditional operator checks for an expression, which returns either a true or a false value. If the condition evaluated is true, it returns the value of the true section of the operator, otherwise it returns the value of the false section of the operator.

Precedence of Operators
precedence of operators in C
Operator                                        Associavity

Unary                                        Right to Left
Arithmetic Operators                Left to Right
Relational Operator                        Left to Right
Equality Operator                        Left to Right
Logical Operator                        Left to Right
Conditional Operator                Right to Left
Assignment Operator                Right to Left
Comma operator                        Right to Left

Differentiate between ‘=’ and’==’ in C language.

               =    
              ==
      1. It is a assignment operator                   
     1.it is a relational operator
      2. It is is used to assign the value         
       to the variable
     Eg: int a=10;
     2. It is used to compare the value to the left hand side variable.
     Eg : int a=5;
            a=10;
     3.The value 10 is string into the   variable a
     3.The value 10 is compare to the left hand side variable.



Related Videos:
 (1.) Arithmetic Operators https://youtu.be/6whfA-ARyMQ
    (2.) 
Relational Operator https://youtu.be/N-rWkVn9Fqs
    (3.) 
Logical Operators https://youtu.be/SQxypOkKWyM
    (4.) 
Assignment operators https://youtu.be/kyh9qQbf_uk
    (5.) 
Special Operators https://youtu.be/kyh9qQbf_uk
    (6.) 
Bitwise Operators https://youtu.be/xBNdZ2ytTiU
    Operator Precedence https://youtu.be/q8jc3s7Nmhk



Monday, 29 August 2011

Student data base using Single Linked List

/*Title : Creating Student Database Using Singly linked list */


#include<stdio.h>
#include<conio.h>

struct Student
{
int roll;
char name[30];
float marks;
struct Student *next; //Self referential pointer...

};

typedef struct Student Node;


void Linkfloat()
{
float a=0,*b; //To create link of float to some compiler ...
b=&a;
a=*b;

}

void Display(Node *head)
{

Node *p;
int i;

if(head==NULL)
{
printf("

There is no records in database.

");

}

else
{
p=head;
for(i=0;i<80;i++)
{

printf("-");

}
printf("

Updated Student Database

");
for(i=0;i<80;i++)
{

printf("-");

}
printf("
");
printf("Roll No. Name Marks
");
for(i=0;i<80;i++)
{

printf("-");

}
printf("
");
while(p!=NULL)
{

printf("%d %s %0.2f",p->roll,p->name,p->marks);
printf("

");
for(i=0;i<80;i++)
{

printf("-");

}
printf("
");

flushall();
p=p->next; //Go to next node...
}

}

}

void DReverse(Node * head)
{
Node *p;
int i;
if(head==NULL)
{
printf("

There is no records in database.

");

}

else
{
p=head;

if(p->next!=NULL)
{
DReverse(p->next); //Recursive call...

}

printf("%d %s %0.2f",p->roll,p->name,p->marks);
printf("
");
for(i=0;i<80;i++)
{

printf("-");

}
printf("
");

flushall();

printf("

");
}
}

Node* Create(Node *head)
{
int n,i;
Node *nn,*p;

printf("

How many Entries to Create Database???

");
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(head==NULL)
{

nn=(Node*)malloc(sizeof(Node)); //Creating first node...
printf("

Enter Roll No. , Name & Marks

");
scanf("%d",&(nn->roll));
flushall();

gets(nn->name);

scanf("%f",&(nn->marks));

nn->next=NULL; //Set next to NULL...

head=nn;

}
else
{
p=nn=head;

while(nn->next!=NULL)
{
nn=nn->next;
}
nn->next=(Node*)malloc(sizeof(Node)); //Creating further nodes...
nn=nn->next;

printf("

Enter Roll NO.,Name & Marks

");
scanf("%d",&(nn->roll));
flushall();

gets(nn->name);
scanf("%f",&(nn->marks));

nn->next=NULL; 
nn=p;
}

}

return head;

}

Node* Insert(Node *head)
{
int ch,r;
char ans;
Node *p,*nn,*q;
do
{

printf("

Whwre do you want to enter new entry???

");

printf("
1.At the Begining
2.At the middle
3.At the end

");
printf("
Enter your choice:
");
scanf("%d",&ch);
switch(ch)
{
case 1: 
/* Insert at Begining */
p=head;

nn=(Node*)malloc(sizeof(Node));

printf("

Enter Roll NO., Name & Marks

");
scanf("%d",&(nn->roll));
flushall();

gets(nn->name);
scanf("%f",&(nn->marks));

nn->next=NULL;
nn->next=p;
head=nn; //set first node as head...

printf("
Entry is Created successfully.

");
Display(head);

break;

case 2:
/* Insert at Middle */

if(head==NULL)
{
printf("
Yet database is not created.");
printf("
Database is empty.
");
printf("

First Create Database.
");


}

else
{

printf("

After which Roll NO. You want to insert new Data???

");
scanf("%d",&r);

p=head;

while(p->roll!=r && p->next!=NULL)
{
p=p->next; //Go upto that roll no....

}

if(p->roll!=r)
{
printf("

There is no such entry.

");

}

else
{

nn=(Node*)malloc(sizeof(Node));

printf("

Enter Roll NO.,Name & Marks

");
scanf("%d",&(nn->roll));
flushall();
gets(nn->name);

scanf("%f",&(nn->marks));
nn->next=NULL;
q=p->next;
p->next=nn;
nn->next=q;

printf("
Entry is Created successfully.

");
Display(head);
}
}

break;

case 3:
/* Insert at end */
if(head==NULL)
{
printf("
Yet database is not created.");
printf("
Database is empty.
");
printf("

First Create Database.
");


}

else
{

p=head;

nn=(Node*)malloc(sizeof(Node));

printf("

Enter Roll NO.,Name & Marks

");
scanf("%d",&(nn->roll));
flushall();
gets(nn->name);
scanf("%f",&(nn->marks));

nn->next=NULL;
while(p->next!=NULL)
{
p=p->next; //Go upto last node...

}

p->next=nn;

printf("
Entry is Created successfully.

");
Display(head);
}

break;
}
printf("

Do you want to Insert more data(Y/N)???");
flushall();
scanf("%c",&ans);

}while(ans=='y' ans=='Y');

return head;
}


Node* Delete(Node* head)
{
Node *p,*q,*r;
char ans;
int ch,n;

do{
printf("

Which Entry you want to Delete???

");
printf("
1.First

2.Middle

3.End
");
scanf("%d",&ch);

if(head==NULL)
{
printf("
Yet database is not created.");
printf("
Database is empty.
");
printf("

First Create Database.
");


}

else
{
switch(ch)
{
case 1:

/*Delete first node */
p=head;

head=head->next; //Set second node as head...
free(p);

printf("

First entry is deleted.
");

Display(head);

break;

case 2:
/*Delete middle Node*/ 

p=head;
printf("
Enter roll no. which you want to delete:

");
scanf("%d",&n);

while((p->next)->roll!=n && p->next->next!=NULL)
{
p=p->next; //Go upto -1 node which you want to delete...

}

if(p->next->next==NULL)
{
printf("

There is no such entry.

");

}

else
{
q=p->next;
r=q->next;
p->next=r;
free(q); //Delete that node...

printf("

Entry is deleted.
");
Display(head);
}
break;

case 3:
/* Delete last node */
p=head;
while(p->next->next!=NULL)
{
p=p->next; //Go upto -1 node which you want to delete...

}
q=p->next;
free(q); //Delete last node...

p->next=NULL;
printf("

Last entry is deleted.
");
Display(head);

break;

}

}

printf("
Do you want to delete more data(Y/N)???
");
flushall();
scanf("%c",&ans);

}while(ans=='y' ans=='Y');


return head;

}

Search(Node *head)
{

Node *p;
int r,cnt=0;
if(head==NULL)
{
printf("
Yet database is not created.");
printf("
Database is empty.
");
printf("

First Create Database.
");
}

else
{
p=head;
printf("

Enter roll no. which you want to Search:

");
scanf("%d",&r);

while(p->roll!=r && p->next!=NULL) //Search for roll no...
{
p=p->next;
cnt++;
}

if(p->roll!=r)
printf("
There is no such entry.

");
else
{
printf("
Roll NO. %d is at %d th Position.",r,(cnt+1));
printf("

Roll No. Name Marks

");
printf("%d %s %0.2f",p->roll,p->name,p->marks);
}
}

}

Modify(Node * head)
{
Node *p;
int r;

if(head==NULL)
{
printf("
Yet database is not created.");
printf("
Database is empty.
");
printf("

First Create Database.
");

}
else
{
p=head;
printf("

Enter the Roll no. whose data you want to modify:

");
scanf("%d",&r);

while(p->roll!=r && p->next!=NULL)
{
p=p->next;
}

if(p->roll!=r)
{
printf("

Thre is no such record in Database.

");

}
else
{
printf("
Entered roll no's Data is:
");
printf("Roll No. Name Marks
"); //Displaying Data who is going to modify....
printf("%d %s %f",p->roll,p->name,p->marks);

printf("
Enter New roll no ,New name & Marks for this entry:

");
scanf("%d",&p->roll);
flushall();
gets(p->name); //Enter new data... 

scanf("%f",&(p->marks));

printf("
Entered New Data is:
");
printf("Roll No. Name Marks
");
printf("%d %s %f",p->roll,p->name,p->marks);
Display(head);

}

}

}

Count(Node *head)
{
Node *p;
int cnt=0;

if(head==NULL)
{
printf("
Yet database is not created.");
printf("
Database is empty.
");
printf("

First Create Database.
");
printf("
There are 0 records in Database.

");
}

else
{

p=head;
while(p->next!=NULL)
{
p=p->next;
cnt++; //Counting records...

}

printf("

There are %d records in Database.

",(cnt+1));

}

}


void main()
{
int ch,i;
char op;
Node *head;
head=NULL;
printf("

*----------Studednt Database-----------*

");
do
{
printf("

Menu

1.Create Database

2.Insert

3.Delete

4.Search

5.Modify

6.Display

7.Display Reverse

8.Count Records

9.Exit

");

printf("

Enter your choice

");
scanf("%d",&ch);

switch(ch)
{
case 1:
head=Create(head); //Call to Create...

break;

case 2:

head=Insert(head); //Call to Insert...

break;

case 3:

head=Delete(head); //Call to Delete...

break;

case 4:

Search(head); //Call to Search...

break;

case 5:

Modify(head); //Call to Modify...
break;

case 6:
Display(head); //Call to Display...
break;

case 7:

for(i=0;i<80;i++)
{

printf("-");

}
printf("

Updated Student Database

");
for(i=0;i<80;i++)
{

printf("-");

}
printf("
");

printf("Roll No. Name Marks
");
for(i=0;i<80;i++)
{

printf("-");

}
printf("
");
DReverse(head); //Call to displaying reversre...

break;

case 8:
Count(head); //Call to counting records...
break;

case 9:
exit(); //Exit...

default :
printf("

You entered wrong choice.

");

}

printf("

Do you want to Exit(Y/N)???

");
flushall();
scanf("%c",&op);

}while(op=='n' op=='N');

}



What is  Linked list
 Some terminology of Linked List programs