Home Forums C Programming calculator programme Reply To: calculator programme

#3262
yeswanth
Participant
#include
#include
#include
#include
#include
#include
void main()
{
int gd=0,gm,i,j,answer,flag=0,k;
char prev='1',current,*ch;
char *nums[]={" 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "," 9 "," 0 "};
char *num[]={"1","2","3","4","5","6","7","8","9","0"};
initgraph(&gd,&gm,"");
button b[10],add,sub,exit,equal,mul,div,ac;
textbox t;
initmouse();
showmouse();
t.make(150,50,160);
for(i=0,j=100;i<10;i++,j+=40)
b.make(j,100,nums);
add.make(100,150," + ");
sub.make(140,150," - ");
equal.make(260,150," = ");
mul.make(180,150," * ");
div.make(220,150," / ");
exit.make(350,400," EXIT ");
ac.make(300,150," AC ");
j=0;
while(1)
{
setcolor(WHITE);
while(!flag)//This while terminates when user is finished inputting a number
//i.e. when he presses '+', '-', '*', '/'
{
for(i=0;i<10;i++)
{
if(b.click())
{
t.append(num),j++,k=1;//display the number in textbox
break;
}
}
if(j==1&&k==1)
t.write(""),t.append(num);
if(add.click())
current='+',flag=1;
else if(sub.click())
current='-',flag=1;
else if(equal.click())
current='=',flag=1;
else if(mul.click())
current='*',flag=1;
else if(div.click())
current='/',flag=1;
else if(exit.click())
flag=2;// flag==2 indicates user wants to exit.
else if(ac.click())
{
prev='1';
t.write("");
}
k=0;
}//End of second while loop
//Now Calculate the result.
if(flag==2)
break;//flagg==2, go out of the main loop to exit.
if(prev=='1')
answer=atoi(t.text);
else if(prev=='+')
answer+=atoi(t.text);
else if(prev=='-')
answer-=atoi(t.text);
else if(prev=='*')
answer*=atoi(t.text);
else if(prev=='/'&&(atoi(t.text))==0)
msgbox("Cannot divide by zero"),prev='1',t.write("");
else if(prev=='/')
answer/=atoi(t.text);
flag=0,j=0;
prev=current;
itoa(answer,ch,10);
t.write(ch);
}
closegraph();
}