You are on page 1of 34

UNIT VI Applications Case study of simple bank transactions and program development.

t. Preprocessor directives Macro expansion - file inclusion - condition compilation - miscellaneous directives. Graphics in C Line drawing - Rectangle - ellipse - working with image - move to function - graphic related library functions

simple bank transactions Case study: Objective is to maintain a banks account information. Some of the important tasks are: -Updating existing account - Adding new accound - Deleting account - storing list of all accounts in a text file for printing The following sample data shows the record structure.

The Program has Five options: 1. store formatted list of accounts in a text file called accounts.txt 2. calls the function updaterecord to update an account. 3. calls the function newrecord to add a new account to the file. 4. calls the function deleterecord to delete a record from the file. 5. Terminates program execution.

Record Structure typedef struct { int accno; char fname[15]; char lname[15]; double amount; }cust;
void displaydata(FILE *dp) { FILE *rp; cust c1; rewind(dp); rp = fopen("result.dat","w"); fprintf(rp,"\n%-6s%-16s%-16s%10s","AccNo","First Name","Last Name","Amount"); while(!feof(dp)) { fread(&c1,sizeof(cust),1,dp); if(c1.accno != 0) { fprintf(rp,"\n%-6d%-16s%-16s%10f", c1.accno,c1.fname,c1.lname,c1.amount); printf("\n%-6d%-16s%-16s%10f", c1.accno,c1.fname,c1.lname,c1.amount); } } fclose(rp); }

void addrecord(FILE *ap) { cust c1,r1; rewind(ap); printf("\n Enter the record to add :"); scanf("%d%s%s%lf",&c1.accno,c1.fname,c1.lname, &c1.amount); fseek(ap,sizeof(cust) *(c1.accno - 1),SEEK_SET); fread(&r1,sizeof(cust),1,ap); if(r1.accno != 0) printf("\n Record already exists "); else { fseek(ap,sizeof(cust) *(c1.accno - 1),SEEK_SET); fwrite(&c1,sizeof(cust),1,ap); printf("\n Record added"); } }

void updaterecord(FILE *up) { int rno; cust c1; double tamt; printf("\n Enter the record no to update :"); scanf("%d",&rno); fseek(up,sizeof(cust) *(rno - 1),SEEK_SET); fread(&c1,sizeof(cust),1,up); printf("\n The custormer details are :\n"); printf("Accno = %d fname = %s amount = %lf", c1.accno,c1.fname,c1.amount); printf("\n Enter transaction amount (- for withdra):"); scanf("%lf",&tamt); c1.amount += tamt; fseek(up,sizeof(cust) *(rno - 1), SEEK_SET); fwrite(&c1,sizeof(cust),1,up); printf("\n Record updated"); }

void deleterecord(FILE *delp) { cust c1 = {0,"","",0}; int rno; printf("\n Enter Record No to delete :"); scanf("%d",&rno); fseek(delp,sizeof(cust) * (rno - 1),SEEK_SET); fwrite(&c1,sizeof(cust),1,delp); printf("\n Record deleted"); }

Order of executing the files. 1. First execute recfmt.c to create the file cust.dat which holds NULL records for 100 customers. 2. Test the above file structure by inputting sample records using disrec.c 3.Finally Execute the application cstudy.c. Final data can be viewed in the text file result.dat.

Preprocessor directives
#include Preprocessor Directive - Causes a copy of specified file to be included in place of the directive - The two forms of the #include directive are: #include <filename> #include filename - The difference between these is the location the preprocessor begins searches for the file to be included. #include<filename> - Used for Standard library headers - The search is performed in a implementation-dependent manner - normally through predesignated compiler and system directories #include filename - The preprocessor starts searches in the same directory as the file being compiled and may search other locations as well. - Normally used to include programmer-defined headers. A header containing declarations common to the separate program Files is often created and included in the file. Ex: structures, unions, function prototypes, enumerationsetc

Conditional Compilation
- Enables to control the execution of preprocessor directives and the compilation of program code. - Each of the conditional preprocessor directives evaluates a constant integer expression. - Cast expressions, sizeof expressions and enumeration constants can not be evaluated in preprocessor direcives. #if !defined PI #define PI 3.14 #endif - Also used to prevent portions of code from being compiled #if 0 code prevented from compiling #endif - Commonly used as a debugging aid. #ifdef DEBUG printf (Variable x = %d\n,x); #endif Example program : concomp.c If the code contains comments, /* and */ cannot be used to accomplish This task

Miscellaneous directives
Stringizing Operator(#) - Can be used to convert macro parameters to quoted strings. #define DEBUG_VALUE(v) printf(#v is equal to %d.\n,v) In program, check the value of a variable by invoking DEBUG_VALUE macro int x = 20; DEBUG_VALUE(x); Concatenation Operator (##) - Is used to concatenate (combine) two separate strings into one single string. #define SORT(x) sort_function ## x void main(void) { char *array; int elements, elemnt_size; . SORT(3)(array,elements, elements_size); } sort_function3(array,elements,element_size);

Dynamic Memory Allocation


Dynamic memory allocation Obtain and release memory during execution malloc Takes number of bytes to allocate Use sizeof to determine the size of an object Returns pointer of type void * A void * pointer may be assigned to any pointer If no memory available, returns NULL newPtr = malloc( sizeof( struct node ) ); free Deallocates memory allocated by malloc Takes a pointer as an argument free (newPtr);

Self-Referential Structures
Self-referential structures Structure that contains a pointer to a structure of the same type Can be linked together to form useful data structures such as lists, queues, stacks and trees Terminated with a NULL pointer (0) Two self-referential structure objects linked together 15 10

Data member and pointer

NULL pointer (points to nothing)

Linked List
A list refers to a set of items organized sequentially linked list is a linear collection of self-referential structures, called nodes, connected by pointer links Each node consists of two fields
First field contains information or data item. Second field contains the address of next node or next element.

Data stored dynamically each node is created as necessary Length of a list can increase or decrease
Accessed via a pointer to the first node of the list Subsequent nodes are accessed via the link-pointer member stored in each node Link pointer in the last node is set to null to mark the end of list Becomes full only when the system has insufficient memory to satisfy dynamic storage allocation requests

Types of linked lists:


singly linked list Begins with a pointer to the first node. Terminates with a null pointer Only traversed in one direction doubly linked list Two start pointers- first element and last element Each node has a forward pointer and a backward pointer Allows traversals both forwards and backwards circular, singly linked Pointer in the last node points back to the first node circular, doubly linked list Forward pointer of the last node points to the first node and backward pointer of the first node points to the last node

Creating memory for the node


Explanation: sizeof: it returns the number of bytes needed for the snode. malloc:it allocates block of memory return void pointer. In this block we can store any type of data. (snode *): the created block is type-casted to be of type node. The members of a structure can be accessed by using pointer variable throw arrow operator.

2000
newnode

data

next

10

NULL

2000 typedef struct node snode; snode *newnode; newnode = (snode*) malloc (sizeof(snode)); newnode -> data = 10; newnode -> next = NULL;

The diagrammatic representation of a single linked list:


info 30 start next 60 20 10 NULL

The above list is of 4 nodes. Each node is of two fields, info and next. The first one contains the data and second one contains the address of next node. The elements stored in the list are 30,60,20,10. The next fields contains the address of the next nodes. The nodes in the list can be accessed through pointer variable, which contains the address of the first node. Last node contains the address null.

Operations on single linked list:


Inserting node into the list Deleting node from the list. Display the contents of list. Searching the element in the list.

Inserting a node at the beginning


Create a node that is to be inserted

2500
data 10 newnode next

NULL

2500
If the list is empty

If (!head) head = newnode; else { newnode -> next = head; head = newnode; }

NULL
head If the list is not empty head

2500
10

1200
head

newnode

5 NULL 2500

1300

1330

1400

8 NULL
1400

1200

1300

1330

head 1500

Inserting a node at the end

10 1500

1800

20 1800

1200

30 1200

1400

40 1400

2000 NULL

temp 1400 1800 1500 1200

50 2000

NULL

snode *newnode = getnode(); snode *temp = head; while(temp -> next) temp =temp-> next; temp-> next = newnode;

newnode 2000

Inserting a node at the given position head 1500 temp 1800 1500 Insert position : 3

10 1500

1800

20 1800

1200 2000

30 1200

1400

40 1400

NULL

50 newnode 2000

NULL 1200 2000

snode *newnode = getnode(); snode *temp = head; for(i=1;i<pos-1;i++) temp = temp -> next; newnode -> next = temp -> next; temp -> next = newnode;

Deleting a node at the beginning


head 1800 1500 p 1500

10 1500

1800

20 1800

1200

30 1200

1400

40 1400

NULL

if (!head) printf(\nList Empty); else { snode *deletenode = head; head = head -> next; free(deletenode); }

head 1500

Deleting a node at the end

10 1500

1800

20 1800

1200

30 1200

NULL 1400

40 1400

NULL

p 1800 1500 1200

q 1400

snode *deletenode,*temp = head; while(temp -> next -> next) temp = temp -> next; deletenode = temp -> next; temp -> next = NULL; Free(deletenode);

head 1500

Deleting a node at the given position q 1200 1800 1500 delete position : 3

10 1500

1800

20 1800

1400 1200

30 1200

1400

40 1400

NULL

snode *deletenode,*temp = head ; if(pos==1) { deletenode = head; head = head -> next; free(deletenode);} else{ for(i=1;i<pos-1;i++) temp = temp -> next; deletenode = temp -> next; temp -> next = deletenode-> next; free(deletenode);

Single Linked List ADT


Data
struct node { int data; struct node *next; }; typedef struct node snode;

Operations
snode* insertBeg(*head,ele); snode* insertEnd(*head,ele); snode* insertPos(*head,ele,pos); snode* delBeg(*head); snode* delEnd(*head); snode* delPos(*head,pos);
snode* create(snode *head);

void display(head); int locate(head,ele);

slladt.h
#include "stdio.h" #include "conio.h struct node { int data; struct node *next; }; typedef struct node snode; snode* create(snode *head) { int ele; printf("\n Enter data (-1 to stop):"); while(1) { scanf("%d",&ele); if(ele == -1) break; else { snode *newnode = getnode(ele); snode* getnode(int ele) if(head) { { snode *temp = head; snode *newnode=(snode*)malloc(sizeof(snode)); while(temp->next) if(newnode) { temp = temp -> next; newnode -> data = ele; temp -> next = newnode; newnode -> next = NULL; } return newnode; } else head = newnode; else { } } printf("\n Unable to create the node"); return head; } getch(); exit(0); } return 0; }

snode* insbeg(snode *head,int ele) { snode *newnode = getnode(ele); if(head) { newnode -> next = head; head = newnode; } else head = newnode; return head; } snode* insend(snode *head,int ele) { snode *newnode = getnode(ele); if(head) { snode *temp = head; while(temp->next) temp = temp -> next; temp -> next = newnode; } else head = newnode; return head; }

snode* inspos(snode *head,int ele,int pos) { int i; snode *newnode = getnode(ele); snode *temp = head; for(i = 1;i < pos-1; i++) temp = temp -> next; newnode -> next = temp -> next; temp -> next = newnode; return head; } snode* delbeg(snode *head) { if(head) { snode *temp = head; head = head -> next; free(temp); } else printf("\n List is empty "); return head; }

deletenode = temp -> next; snode *delend(snode *head) { temp -> next = deletenode -> next; if(head==NULL) printf("\n List is empty"); free(deletenode); } else if(head->next==NULL) return head; } head=head->next; else { void display(snode *head) { snode *deletenode; if(head) { snode *temp=head; snode *temp = head; while(temp -> next -> next) printf("\n List elements are :"); while(temp) { temp = temp -> next; printf("%5d",temp -> data); deletenode=temp->next; temp = temp -> next; } } temp -> next = NULL; else printf("\n List is empty "); } free(deletenode); } return head; } snode* delpos(snode *head,int pos) { int locate(snode *head,int ele) { int i; snode *temp = head; snode *deletenode,*temp = head; while(temp -> next) { if(pos==1) { if(temp -> data == ele) return 1; temp = temp -> next; } head = head -> next; free(temp); } else { return 0; } for(i = 1; i < pos - 1; i ++) temp = temp -> next;

sll.c
#include "slladt.h snode* insert(snode *head,int ele) int menu() { { int pos,ch = smenu(); int ch; clrscr(); switch(ch) printf("\n Linked List Operations :"); { printf("\n\t 1 -> create\n\t 2 -> insert \n\t case 1 : head = insbeg(head,ele); 3 -> Delete \n\t 4 -> display \n\t 5 -> search \n\t 6 -> exit"); break; printf("\n Enter your choice :"); case 2: head = insend(head,ele); scanf("%d",&ch); break; return ch; } case 3: printf("\nEnter position :"); int smenu() scanf("%d",&pos); { inspos(head,ele,pos); int ch; break; printf("\n\t 1 -> begining\n\t 2 -> end\n\t } 3 -> position "); return head; printf("\n\t Enter your choice :"); scanf("%d",&ch); } return ch; }

snode* delele(snode *head) { int pos,ch = smenu(); switch(ch) { case 1 : head = delbeg(head); break; case 2: head = delend(head); break; case 3: printf("\nEnter position :"); scanf("%d",&pos); head = delpos(head,pos); break; } return head; } void main() { int ch,ele; snode *head = NULL; while(1) { ch = menu(); switch(ch) { case 1: head =create(head);break;

case 2: printf("\n Enter element :"); scanf("%d",&ele); head = insert(head,ele); break; case 3: head = delele(head); break; case 4: display(head); break; case 5: printf("\n enter an element : "); scanf("%d",&ele); if(locate(head,ele)) printf("\n Element found"); else printf("\n Element not found"); break; case 6: exit(0); default : main(); } getch(); } }

Graphics in C
Text Mode Graphics Mode

Basic Unit is character

Basic Unit is pixel

80 columns, 50 rows

640 columns, 480 rows

To work with graphics in C, First initialize graphics drivers on the computer by calling initgraph method available in graphics.h headerfile. void initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver); *graphdriver -> Integer that specifies the graphics driver to be used

*graphmode Integer that specifies the initial graphics mode (unless *graphdriver = DETECT).

Drawing a line

Drawing a Circle
circle(320,240,100);

A Circle takes a total of 3 arguments. The first two arguments are used to define center of the circle in x and y co-ordinates. Since screen has a size of 640 pixels in x-axis, so 320 is the center of x-axis. And screen has the size of 480 pixels in y-axis, so 240 is the center of y-axis. Third argument of the circle is its radius in pixels. In our example the radius of the circle is 100 pixels.

Drawing a rectangle ractangle(left, top,right, bottom); Rectangle is used to draw an empty rectangle. It takes 4 arguments all of int type. First two arguments are left-top corner of the rectangle, and last two arguments are right bottom corner of the rectangle. ractangle(100,100,200, 200); Output: (100,100)

(200,200)

Drawing ellipse
ellipse(midx, midy,starting-angle,ending-angle,radius-x, radius-y);

Ellipse is used to draw an elliptical arc. Ellipse takes 6 arguments, all of the int type. First two arguments define the center of the ellipse to place on the screen.(ie x and y co-ordinates) Third and Fourth arguments are starting and ending angles of the ellipse. Fifth argument is the radius of the ellipse in x-axis, and sixth argument is the radius of the ellipse in yaxis.

ellipse(320,240,0,360,50,100);

ellipse(midx, midy,starting-angle,ending-angle,radius-x, radius-y);

Example1: ellipse(320,240,0,360,50,100);

www.sirjameel.com

ellipse(midx, midy,starting-angle,ending-angle,radius-x, radius-y);

Example2: ellipse(320,240,0,360,100,100);

www.sirjameel.com

ellipse(midx, midy,starting-angle,ending-angle,radius-x, radius-y);

Example3: ellipse(320,240,0,360,100,50);

www.sirjameel.com

You might also like