Home › Forums › C Programming › calculator programme › Reply To: calculator programme
March 14, 2008 at 9:08 pm
#3262
yeswanth
Participant
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #include<conio.h><br /> #include<mouse.h><br /> #include<graphics.h><br /> #include<string.h><br /> #include<stdlib.h><br /> #include<winoop.h><br /> void main()<br /> {<br /> int gd=0,gm,i,j,answer,flag=0,k;<br /> char prev='1',current,*ch;<br /> char *nums[]={" 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "," 9 "," 0 "};<br /> char *num[]={"1","2","3","4","5","6","7","8","9","0"};<br /> initgraph(&gd,&gm,"");<br /> button b[10],add,sub,exit,equal,mul,div,ac;<br /> textbox t;<br /> initmouse();<br /> showmouse();<br /> t.make(150,50,160);<br /> for(i=0,j=100;i<10;i++,j+=40)<br /> b.make(j,100,nums);<br /> add.make(100,150," + ");<br /> sub.make(140,150," - ");<br /> equal.make(260,150," = ");<br /> mul.make(180,150," * ");<br /> div.make(220,150," / ");<br /> exit.make(350,400," EXIT ");<br /> ac.make(300,150," AC ");<br /> j=0;<br /> while(1)<br /> {<br /> setcolor(WHITE);<br /> while(!flag)//This while terminates when user is finished inputting a number<br /> //i.e. when he presses '+', '-', '*', '/'<br /> {<br /> for(i=0;i<10;i++)<br /> {<br /> if(b.click())<br /> {<br /> t.append(num),j++,k=1;//display the number in textbox<br /> break;<br /> }<br /> }<br /> if(j==1&&k==1)<br /> t.write(""),t.append(num);<br /> if(add.click())<br /> current='+',flag=1;<br /> else if(sub.click())<br /> current='-',flag=1;<br /> else if(equal.click())<br /> current='=',flag=1;<br /> else if(mul.click())<br /> current='*',flag=1;<br /> else if(div.click())<br /> current='/',flag=1;<br /> else if(exit.click())<br /> flag=2;// flag==2 indicates user wants to exit.<br /> else if(ac.click())<br /> {<br /> prev='1';<br /> t.write("");<br /> }<br /> k=0;<br /> }//End of second while loop<br /> //Now Calculate the result.<br /> if(flag==2)<br /> break;//flagg==2, go out of the main loop to exit.<br /> if(prev=='1')<br /> answer=atoi(t.text);<br /> else if(prev=='+')<br /> answer+=atoi(t.text);<br /> else if(prev=='-')<br /> answer-=atoi(t.text);<br /> else if(prev=='*')<br /> answer*=atoi(t.text);<br /> else if(prev=='/'&&(atoi(t.text))==0)<br /> msgbox("Cannot divide by zero"),prev='1',t.write("");<br /> else if(prev=='/')<br /> answer/=atoi(t.text);<br /> flag=0,j=0;<br /> prev=current;<br /> itoa(answer,ch,10);<br /> t.write(ch);<br /> }<br /> closegraph();<br /> }<br /> |