calculator c++
#include <iostream> #include <string> #include <unistd.h> using namespace std; class Calculator { private: char i_numOperator; float i_a = 00.0f, i_b = 00.0f; public: Calculator(char numOperator, float a, float b) { i_numOperator = numOperator; i_a = a; i_b = b; } float the_calculator() { switch (i_numOperator) { case '+': return (i_a + i_b); break; case '-': return (i_a - i_b); break; case '*': return (i_a * i_b); break; case '/': return (i_a / i_b); break; default: cout << "invalid operator please try again\n"; break; } } }; int main() { char again = 'y'; while(again == 'y' || again == 'Y') { int a = 00.0f, b = 00.0f; char op; cout << "first num\n"; cin >> a; cout << "operator\n"; cin >> op; cout << "second num\n"; cin >> b; Calculator calt(op, a, b); cout << "result : " << calt.the_calculator() << endl; cout << "do you want to use it again?(Y/N)\n"; cin >> again; system("clear"); } system("clear"); cout << "ok.. bye!\n"; sleep(2); system("clear"); return 0; }
how to make calculaor in c++
# include <iostream> using namespace std; int main() { char op; float num1, num2; cout << "Enter operator: +, -, *, /: "; cin >> op; cout << "Enter two operands: "; cin >> num1 >> num2; switch(op) { case '+': cout << num1 << " + " << num2 << " = " << num1 + num2; break; case '-': cout << num1 << " - " << num2 << " = " << num1 - num2; break; case '*': cout << num1 << " * " << num2 << " = " << num1 * num2; break; case '/': cout << num1 << " / " << num2 << " = " << num1 / num2; break; default: // If the operator is other than +, -, * or /, error message is shown cout << "Error! operator is not correct"; break; } return 0; }
Source: www.programiz.com
calculator in cpp
#include <iostream> using namespace std; // Hi I'm Jillani Soft Tech. I'm a Developer. main() { while(true) { int sum,mul,sub,div; // variable decalration int num1,num2; int choice; cout<<"\n\n\t\t\t----------Main Menu-----------"<<endl; cout<<"Press 1 for Sum "<<endl; cout<<"Press 2 for Sub "<<endl; cout<<"Press 3 for Mul "<<endl; cout<<"Press 4 for Div "<<endl; cout<<"\nEnter Your Choice ? "<<endl; cin>>choice; cout<<"Enter frist number : "<<endl; cin>>num1; cout<<"Enter Second number : "<<endl; cin>>num2; if(choice==1) { sum=num1+num2; cout<<"There sum is this : "<<sum<<endl; } else if(choice==2) { sub=num1-num2; cout<<"There sub is this : "<<sub<<endl; } else if(choice==3) { mul=num1*num2; cout<<"There mul is this : "<<mul<<endl; } else if(choice==4) { div=num1/num2; cout<<"There div is this : "<<div<<endl; } else { cout<<"\n\n\t\t\tInvalid Selection Try Again.... "<<endl; } } }
c++ calculator
1 #include<stdio.h> 2 3 int main(){ 4 5 char operator; 6 double first, second; 7 8 printf("Enter the Operator ( +, -, *, / ) : "); 9 scanf("%c",&operator); 10 11 printf("Enter the two Numbers one by one : "); 12 scanf("%lf %lf",&first,&second); 13 14 switch (operator) 15 { 16 17 case '+': 18 printf("%.2lf + %.2lf = %.2lf",first,second,(first+second)); 19 break; 20 21 case '-': 22 printf("%.2lf - %.2lf = %.2lf",first,second,(first-second)); 23 break; 24 25 case '*': 26 printf("%.2lf * %.2lf = %.2lf",first,second,(first*second)); 27 break; 28 29 case '/': 30 if( second != 0.0 ) 31 printf("%.2lf / %.2lf = %.2lf",first,second,(first/second)); 32 else 33 printf("Divide by Zero situation"); 34 break; 35 36 default: 37 printf("%c is an invalid Operator",operator); 38 39 } 40 41 return 0; 42 }
Source: www.learninglad.com