Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » ProjectsRSS Feeds

Program that provides facilities of calculator in c++

Posted By: Easy Tutor     Category: C++ Programming     Views: 4003

Write a c++ program that provides all facilities of calculator as listed below.

1) Add
2) Subtract
3) Divide
4) Multiply
5) Clear

Code for Program that provides facilities of calculator in c++ in C++ Programming

/* This is a calculator program. It includes header files mouse.h, graphics.h,  winoop.h. You can get these files from attached source zip.*/

#include<conio.h>
#include<mouse.h>
#include<graphics.h>
#include<string.h>
#include<stdlib.h>
#include<winoop.h>

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[i].make(j,100,nums[i]);
   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[i].click())
                {
                    t.append(num[i]),j++,k=1;//display the number in textboxbreak;
                }
            }
            if(j==1&&k==1)
                t.write(""),t.append(num[i]);
            if(add.click())
                current='+',flag=1;
            elseif(sub.click())
                current='-',flag=1;
            elseif(equal.click())
                current='=',flag=1;
            elseif(mul.click())
                current='*',flag=1;
            elseif(div.click())
                current='/',flag=1;
            elseif(exit.click())
                flag=2;// flag==2 indicates user wants to exit.elseif(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);
        elseif(prev=='+')
            answer+=atoi(t.text);
        elseif(prev=='-')
            answer-=atoi(t.text);
        elseif(prev=='*')
            answer*=atoi(t.text);
        elseif(prev=='/'&&(atoi(t.text))==0)
            msgbox("Cannot divide by zero"),prev='1',t.write("");
        elseif(prev=='/')
            answer/=atoi(t.text);
        flag=0,j=0;
        prev=current;
        itoa(answer,ch,10);
        t.write(ch);
    }
    closegraph();
}
  
Share: 


Didn't find what you were looking for? Find more on Program that provides facilities of calculator in c++ Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program that provides facilities of calculator in c++ is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!