0% found this document useful (0 votes)
107 views

C CALC Project

The document outlines a 15-lecture C programming project to build a basic calculator application. Each lecture covers a new programming concept and adds functionality to the calculator, such as printing values, operators, conditionals, loops, arrays, structures, pointers, functions and graphics. The final lectures integrate all the concepts to display a working calculator with number buttons, basic math operations, and memory functions.

Uploaded by

Vijay Kanuga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views

C CALC Project

The document outlines a 15-lecture C programming project to build a basic calculator application. Each lecture covers a new programming concept and adds functionality to the calculator, such as printing values, operators, conditionals, loops, arrays, structures, pointers, functions and graphics. The final lectures integrate all the concepts to display a working calculator with number buttons, basic math operations, and memory functions.

Uploaded by

Vijay Kanuga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

C Programming Project

C Programming

CALC Project

C Programming Project

Project 1
Making Calc

Description: This Assignment is divided into section each lecture will complete a portion of the CALC Lecture 1: Printf command, declaring variable We will be using printf command to display messages like #include <stdio.h> #include <conio.h> void main() { int a=5; printf(the value of a is %d, a); getch(); }

C Programming Project

Lecture 2: Operators For performing mathematical calculation in CALCULATOR will have to use Operators as shown in the example #include <stdio.h> #include <conio.h> void main() { int a,b,c; printf(Enter the value of a\n); scanf(%d,&a); printf(Enter the value of b\n); scanf(%d,&b); c=a+b; printf(Sum of a and b is %d,c); getch(); } Lecture 3: if else, switch In the program there are four basic operations to be done sum, subtraction, multiplication, division depending upon the selection #include <stdio.h> #include <conio.h> void main() { int opr; float a,b,c; printf(enter value of a); scanf(%f,&a); printf(enter value of b); scanf(%f,&b); printf(Enter your choice for operation to be done); scanf(%d,&opr); switch(opr) { case 1: c=a+b; break; case 2: c=a-b; break; case 3: c=a*b; break; case 4: 3

C Programming Project

c=a/b; break; default: c=0; } printf(ans is %f,c); getch(); } Lecture 4: Loops Using loops we can repeat a similar task in cycle like in CALC there are buttons on it with different co-ordinates on x & y axis. Value of x & y co-ordinates can be incremented using loop as shown below #include <stdio.h> #include <conio.h> void main() { int x=0,y=0; for(x=1;x<=3;x++) { for(y=0;y<=4;y++) { printf((x,y)=(%d,%d),x,y); } printf(\n); } getch(); } Lecture 5: Preprocessors Using Preprocessors we declare some constants for convenience in lecture 3 example instead of comparing integer value with choice i.e. 1 for sum, 2 for subtraction, 3- multiplication, 4 for division we can use constants declared using preprocessor #include <stdio.h> #include <conio.h> #define sum 1 #define sub 2 #define prod 3 #define div 4 void main() { int opr; float a,b,c; printf(enter value of a); 4

C Programming Project

scanf(%f,&a); printf(enter value of b); scanf(%f,&b); printf(Enter your choice for operation to be done); scanf(%d,&opr); switch(opr) { case sum: c=a+b; break; case sub: c=a-b; break; case prod: c=a*b; break; case div: c=a/b; break; default: c=0; } printf(ans is %f,c); getch(); } Lecture 6 & 7: Arrays For creating button labels on CALC we will use two dimensional Array. The structure of this is shown as follows 7 4 1 0 #include <stdio.h> #include <conio.h> void main() { char buttons[][]={7,8,9,/}, {4,5,6,x}, {1,2,3,-}, int i,j; for(i=0;i<4;i++) { for(j=0;j<4;j++) { printf(%c\t,buttons[i][j]); 5 8 5 2 . 9 6 3 = / X +

{0,.,=,+};

C Programming Project

} Printf(\n); } getch(); }

Lecture 9: structures Co-ordinates of a point can be represented with the help of structure #include <stdio.h> #include <conio.h> struct coord { int x; int y; } void main() { struct coord point1; point1.x=250; point1.y=300; printf(point1(%d,%d),point1.x,point1.y); getch(); }

C Programming Project

Lecture 10: Union In the example the use of Union REGS and also shows how to move cursor position #include <stdio.h> #include <conio.h> #include <dos.h> #define VIDEO 0x10 void movetoxy(int x, int y) { union REGS regs; /* The union REGS is used to pass information to and from these functions*/ regs.h.ah = 2; /* set cursor position */ regs.h.dh = y; regs.h.dl = x; regs.h.bh = 0; /* video page 0 */ int86(VIDEO, &regs, &regs); } int main(void) { clrscr(); movetoxy(35, 10); printf(Hello\n); getch(); return 0; } Lecture 11: string functions Using string functions we can manipulate a string like displaying a text in UPPER CASE #include <stdio.h> #include <conio.h> #include <string.h> void main() { char heading=calculator; printf(%s,strupr(heading)); getch(); }

C Programming Project

Lecture 14: functions For repetitive use of any logic we can use functions like in calc arithmetic calculation logic used again & again #include <stdio.h> #include <conio.h> #include <string.h> #include <stdlib.h> #define sum 1 #define sub 2 #define prod 3 #define div 4 void operation(int) float a,b; void main() { int choice; while(1) { printf(enter value a\n); scanf(%f%,&a) ; printf(enter value b\n); scanf(%f%,&b) ; printf(enter choice for operation 1.sum, 2.substration, 3.product, 4.division a\n); scanf(%d%,&choice) ; operation(choice); printf(continue using calc[y/n]?); if(getch()!=y) { exit(0); } } } void operation(int opr) { switch(opr) { case sum: c=a+b; break; case sub: c=a-b; break; case prod: c=a*b; break; case div: c=a/b; break; default: c=0; 8

C Programming Project

} printf(ans is %f,c); getch(); } Lecture 15 & 19: In the following example pointer are used to trace the mouse pointer button status and x, y coordinates #includegraphics.h #includedos.h #includestdio.h #includemath.h union REGS i,o; void showmouseptr(); void main() { int gd=DETECT,gm,x1,x2,y1,y2,i,j,maxx,maxy,x,y,button; char *s; initgraph(&gd,&gm,c:/tc/bgi); showmouseptr(); while(1) { getmousepos(&button,&x,&y); sprintf(s,button status=%d, x=%d, y=%d,button,x,y); setcolor(1); outtextxy(50,50,s); if(button==1) { exit(0); } } getch(); } void showmouseptr() { i.x.ax=1; int86(0x33,&i,&o); return 0; } getmousepos(int *button,int *x,int *y) { i.x.ax=3; int86(0x33,&i,&o); *button=o.x.bx; *x=o.x.cx; *y=o.x.dx; 9

C Programming Project

return 0; } Complete Code

#includegraphics.h #includedos.h #includestdio.h #includemath.h union REGS i,o; char *text[]={ 7,8,9,*, 4,5,6,/, 1,2,3,+, 0,00,.,-, M,M+,M-,+/-, MR,MC,x^2,sr, OFF,AC,CE,=}; int k=0,pass,op,prop,newnum=1,bt,memo=1,d=0,sq; long double num=0,accum,m; void normalbutton(int ,int ,int ,int,char**); void main() { int gd=DETECT,gm,x1,x2,y1,y2,i,j,maxx,maxy,x,y,button; char *text1[]={\,T,o, ,K,n,o,w, ,a,b,o, u,t, ,m,e, ,l,o,g,o,n, ,:}; char *text2[]={w,w,w,.,g,e,o,c,i,t,i,e,s, .,c,o,m,/,t,a,l,k,d,e,e,p, e,s,h}; initgraph(&gd,&gm,c:/tc/bgi); if(initmouse()==0) { closegraph(); restorecrtmode(); printf(Mouse driver not loded); exit(1); } showmouseptr(); // x=y=50; movemouseptr(&x,&y); setbkcolor(11); setcolor(1); rectangle(198,140,417,163); 10

C Programming Project

rectangle(199,141,418,164); rectangle(197,139,416,162); rectangle(185,130,430,450); rectangle(184,129,431,451); rectangle(182,127,433,454); rectangle(181,126,434,453); //setfillstyle(SOLID_FILL,3); //bar(200,142,415,161); outtextxy(200,50,A Calculator Project); outtextxy(200,100,Press OFF button to exit....); y1=140; y2=160; for(j=0;j<7;j++) { x1=200; x2=235; y1+=40; y2+=40; for(i=0;i<4;i++) { normalbutton(x1,x2,y1,y2,text); x1+=60; x2+=60; } } while(1) { getmousepos(&button,&x,&y); y1=140; y2=160; /* { if( (x>400&&x<450) && (y>400&&y<420) ) if((button & 1)==1) { sound(500); delay(5); exit(); }

} */ for(j=0;j<7;j++) { x1=200; x2=235; y1+=40; 11

C Programming Project

y2+=40; for(i=0;i<4;i++) { if((x<x2&&x>x1)&&(y<y2&&y>y1)) { if((button & 1)==1) { gotoxy(28,10); // printf(%d,ch=*text[j*4+i]); // printf(char is %c,ch); bt=j*4+i; printf(char is %d,j*4+i); setcolor(11); outtextxy(x1+12,y1+7,text[j*4+i]); if(num>pow(10.0,18)) exit(); sound(500);delay(10);nosound(); delay(250); sound(400);delay(10); nosound(); switch (bt) { case 8 : addnum(1); break; case 9 : addnum(2); break; case 10 : addnum(3); break; case 4 : addnum(4); break; case 5 : addnum(5); break; case 6 : addnum(6); break; case 0 : addnum(7); break; case 1 : addnum(8); break; case 2 : addnum(9); break; case 12 : addnum(0); break; 12

//

C Programming Project

case 11 : // plus operation(1); break; case 15 : // minus operation(2); break; case 3 : // multiplication operation(3); break; case 7 : // division operation(4); break; case 13: doublezero(); break; case 14 : decimal(); break; case 16: mem(); break; case 20: recallmem(); break; case 19: plusminus(); break; case 17: plusm(); break; case 18: minusm(); break; case 21: clearm(); break; case 22 : square(); break; case 23: sqroot(); break; case 24: // OFF hidemouseptr(); setcolor(1); for(j=0;j<20;j++) 13

C Programming Project

{ for(i=75;i<481;i+=20) line(0,0+i+j,640,j+0+i); delay(100); } setcolor(14); outtextxy(225,200,Thanks for using it !); delay(2000); setcolor(13); for(j=0;j<20;j++) { for(i=0;i<640;i+=20) line(0+i+j,0,j+0+i,640); delay(100); } setcolor(1); for(i=0;i<25;i++) { outtextxy(75+10*i,200,text1[i]); sound(3000); delay(50); nosound(); } for(i=0;i<29;i++) { outtextxy(125+10*i,225,text2[i]); sound(3000); delay(50); nosound(); } // outtextxy(200,225,www.saintangelos.com); delay(2500); sound(5000); delay(10); nosound(); exit(); break; case 25: allclear(); break; case 26: clear(); break; case 27: // equalto operation(5); 14

C Programming Project

break;

} setcolor(1); outtextxy(x1+12,y1+7,text[j*4+i]); } } x1+=60; x2+=60; } } } nosound(); } void normalbutton(int x1,int x2,int y1,int y2,char **text) { setcolor(15); rectangle(x1-2,y1-2,x2+1,y2+1); rectangle(x1-1,y1-1,x2+2,y2+2); setcolor(7); rectangle(x1,y1,x2+2,y2+2); rectangle(x1,y1,x2+1,y2+1); setfillstyle(SOLID_FILL,14); bar(x1,y1,x2,y2); setcolor(1); outtextxy(x1+12,y1+7,text[k]); k++; } /* initmouse */ initmouse() { i.x.ax=0; int86 (0x33,&i,&o); return(o.x.ax); } hidemouseptr() { i.x.ax=2; int86(0x33,&i,&o); } /* displays mouse pointer */ showmouseptr() { 15

C Programming Project

i.x.ax=1; int86(0x33,&i,&o); return 0; } /*gets mouse coordinates and button status*/ getmousepos(int *button,int *x,int *y) { i.x.ax=3; int86(0x33,&i,&o); *button=o.x.bx; *x=o.x.cx; *y=o.x.dx; return 0; } /* Move mouse ptr to x,y */ movemouseptr(int *x,int *y) { i.x.ax=4; int86(0x33,&i,&o); o.x.cx=*x; o.x.dx=*y; return 0; } addnum(int pass) { if(sq) newnum=1; if(newnum) { if(d) { num=pass/(pow(10.0,d)); d++; newnum=0; } else { num=pass; newnum=0; } } else { /* if(num==0) { if(d) { num=num+pass/(pow(10.0,d)); d++; } else num=pass; } */ 16

C Programming Project

// { {

else if(d)

if(num<0) num=num-pass/(pow(10.0,d)); else num=num+pass/(pow(10.0,d)); d++; } else { num=num*10+pass; } } } printf(%25.5Lf,num); } operation(int opr) { long double pnum; pnum=num; if(newnum && (prop != 5) && memo) { } else { newnum=1; d=0; sq=0; switch(prop) { case 1: accum=accum+pnum; break; case 2: accum=accum-pnum; break; case 3: accum=accum*pnum; break; case 4: accum=accum/pnum; break; default: accum=pnum; } } prop=opr; num=accum; printf(%25.5Lf,num); } allclear() { 17

C Programming Project

sq=0; accum=0; num=0; d=0; newnum=1; printf(%25.5Lf,num); } mem() { m=num; } recallmem() { memo=0; printf(%25.5Lf,m); num=m; } plusminus() { if(num!=0) { num*=-1; printf(%25.5Lf,num); } } plusm() { m+=num; } minusm() { m-=num; } clearm() { m=0; } decimal() { if(!d) {d=1; if(newnum==1) { num=0; } printf(%25.5Lf,num); } } square() { sq=1; num*=num; printf(%25.5Lf,num); 18

C Programming Project

// newnum=1; } sqroot() { sq=1; num=pow(num,0.5); printf(%25.5Lf,num); // newnum=1; } doublezero() { if(d) { // num=num+pass/(pow(100.0,d)); d++; d++; } else num*=100; printf(%25.5Lf,num); } clear() { num=0; printf(%25.5Lf,num); }

19

You might also like