0% found this document useful (0 votes)
245 views30 pages

Applications: Unit - Vi

The document discusses C programming concepts including: 1) A case study on simple bank transactions and program development using functions like update, add, and delete records from a file. 2) Preprocessor directives like macro expansion, file inclusion, and conditional compilation. 3) Graphics programming in C using functions for lines, rectangles, circles, and ellipses from the graphics library.

Uploaded by

Ravi Teja
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
245 views30 pages

Applications: Unit - Vi

The document discusses C programming concepts including: 1) A case study on simple bank transactions and program development using functions like update, add, and delete records from a file. 2) Preprocessor directives like macro expansion, file inclusion, and conditional compilation. 3) Graphics programming in C using functions for lines, rectangles, circles, and ellipses from the graphics library.

Uploaded by

Ravi Teja
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

UNIT – VI

Applications
– Case study of simple bank transactions and program
development.

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 bank’s account information.
Some of the important tasks are:
-Updating existing account
- Adding new account
- 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 void addrecord(FILE *ap)
{
typedef struct cust c1,r1;
{ rewind(ap);
int accno; printf("\n Enter the record to add :");
scanf("%d%s%s%lf",&c1.accno,c1.fname,c1.lname,
char fname[15]; &c1.amount);
char lname[15]; fseek(ap,sizeof(cust) *(c1.accno - 1),SEEK_SET);
double amount; fread(&r1,sizeof(cust),1,ap);
if(r1.accno != 0)
}cust; printf("\n Record already exists ");
void displaydata(FILE *dp) else
{ {
FILE *rp; fseek(ap,sizeof(cust) *(c1.accno - 1),SEEK_SET);
cust c1; fwrite(&c1,sizeof(cust),1,ap);
rewind(dp); printf("\n Record added");
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 updaterecord(FILE *up)
void deleterecord(FILE *delp)
{
{
int rno;
cust c1 = {0,"","",0};
cust c1;
int rno;
double tamt;
printf("\n Enter Record No to delete :");
printf("\n Enter the record no to update :");
scanf("%d",&rno);
scanf("%d",&rno);
fseek(delp,sizeof(cust) * (rno - 1),SEEK_SET);
fseek(up,sizeof(cust) *(rno - 1),SEEK_SET);
fwrite(&c1,sizeof(cust),1,delp);
fread(&c1,sizeof(cust),1,up);
printf("\n Record deleted");
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");
}
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, enumerations…etc
Conditional Compilation
- Enables to control the execution of preprocessor directives
and the compilation of program code. skip the some
statements by the compliler.
- 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.
#ifndef PI
#define PI 3.14
#endif
- Also used to prevent portions of code from being compiled
If the code contains
#if 0 comments,
code prevented from compiling /* and */ cannot be used
#endif to accomplish
This task
- Commonly used as a debugging aid.
#ifdef DEBUG
printf (“Variable x = %d\n”,x);
#endif
Example program : concomp.c
• #ifdef macroname
stat1;
stat2;
#endif
if macro name is defined in the program
stat1 and stat2 would be executed. Else
skip those statements.
#if and #elif directives
• #if directive is used to test whether an
expression evaluates to a nonzero or not.
these to codes are like else if block.
Miscellaneous Directives
• Two more preprocessor directives are
a) #undef
b) #pragma
#undef this is used to make the defined name to
the undefined
#pragma
a) #pragma startup and #pragma exit. These are
used to specify a function that is called before
main or before program exit.
• #pragma startup abc1
#pragma exit abc2
main()
{
printf(“in main\n”);
}//main
void abc1()
{
printf(“in abc1 functions\n”);
}/abc1;
void abc2()
{
printf(“in abc2 function\n”);
}//abc2
• #pragma warn:- this code is used to avoid
reporting the warnings by the compiler
such as function does not return a value,
assigning a value to an uninitialized
pointer.
Miscellaneous directives
Stringizing Operator(#)
- Can be used to convert macro parameters to quoted strings.
#define DEBUG_VALUE 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);
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.

• rectangle(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 y-
axis.

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
• /*
shapes.c
example 1.1
*/
#include<graphics.h>
#include<conio.h>
void main()
{
    int gd=DETECT, gm;
    int poly[12]={350,450, 350,410, 430,400, 350,350, 300,430, 350,450 };
    initgraph(&gd, &gm, "");
      
    circle(100,100,50);
    outtextxy(75,170, "Circle");
    rectangle(200,50,350,150);
    outtextxy(240, 170, "Rectangle");
    ellipse(500, 100,0,360, 100,50);
    outtextxy(480, 170, "Ellipse");
    line(100,250,540,250);
    outtextxy(300,260,"Line");
    sector(150, 400, 30, 300, 100,50);
    outtextxy(120, 460, "Sector");
    drawpoly(6, poly);
    outtextxy(340, 460, "Polygon");
    getch();
    closegraph();
}
Graphics library functions
• void far outtextxy(int x, int y, char *text);
displays the string in graphics mode at
specified position.
• void far outtext(char *text );- displays string
at the current position.
• void far setcolor(int color);
• void far setbkcolor(int color);
• BLACK:                  0
BLUE:                     1
GREEN:                  2
CYAN:                    3
RED:                       4
MAGENTA:            5
BROWN:                 6
LIGHTGRAY:         7
DARKGRAY:          8
LIGHTBLUE:           9
LIGHTGREEN:       10
LIGHTCYAN:         11
LIGHTRED:            12
LIGHTMAGENTA: 13
YELLOW:               14
WHITE:                   15
• gotoxy()
• This will initialize the graphics cursor to the
specified co-ordianate.In C gotoxy function
is used very frequently to locate the cursor
at different locations whenever as
necessary.
gotoxy(x,y);
• putpixel()
• It will colour the pixel specified by the co-
ordinates.
putpixel(x,y,white);
• lineto()
• Draws a line from its current location to the
co-ordinate(x,y)

• Lineto(x,y);
• settextstyle()
• The fonts available are :TRIPLEX_FONT,
SMALL_FONT, SANS_SERIE_FONT,
GOTHIC_FONT
• The direction can be changed as
HORIZ_DIR or VERT_DIR
• Settextstyle(GOTHIC_FONT,VERT_DIR)
;
Move to functions
• moverel(int x,int y);- this function is used
to move to cursor to a point a relative
distance away from the current CP
moveto(int x,int y)- move the cursor to the
specified location.
• Loading the header file of C graphics.h
• The C graphics is the header file which
should be included to initialize your
computer to start using graphics methods
and also to initialize the monitor. These
kinds of statements before the main
program are called preprocessor
dierctives. Its very simple as coded below.
• #INCLUDE<STDIO.H>   
• #INCLUDE<GRAPHICS.H>   
• Void main()
• {
- This initialization is done inside the main
program.The method initgraph()  is used for this
purpose there are three parameters for this , the
first two are of integer type and the next is the
path which you can leave blank in quotes to take
the default path.
Initializing 'Graphics Driver' and
'Graphics Mode' from Borland C
graphics library
• #INCLUDE<STDIO.H>   
• #INCLUDE<CONIO.H>   
• #INCLUDE<GRAPHICS.H>   
• VOID MAIN()   
• {   
• INT GDRIVER=DETECT,GMODE;   
• INITGRAPH(&GDRIVER,&GMODE," ");   
• }  

You might also like