-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.cpp
142 lines (142 loc) · 3.72 KB
/
Calculator.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <cmath>
using namespace std;
class calculator
{
public:
double a;
double b;
//user manual
void manual()
{
cout << "Welcome to manual !" << endl;
cout << "0. Enter 'Q' to exit from calculator "<<endl;
cout << "1. Enter '%' at operation for finding remainder " << endl;
cout << "2. Enter 'S' at operation for finding square root of number " << endl;
cout << "3. Enter 'A' at operation for avg " << endl;
cout << "4. Enter 'M' at operation for finding max and min of two numbers"<< endl;
cout << "5. Enter '!' at operation for finding factorial of a number " << endl;
cout << "6. Enter '|' at operation for finding absolute of two numbers " << endl;
cout << "7. Enter 'P' at operation for Exponent of anumber " << endl;
cout << "8. Enter 'T' for trignometric calculations " << endl;
}
//For taking input from user
void input(){
cout<<"Enter num 1 : ";
cin>>a;
cout<<"Enter num 2 : ";
cin>>b;
}
void calculation(char op)
{
switch (op){
case '+':
input();
cout <<"Result : "<< a + b;
break;
case '-':
input();
cout <<"Result : "<< a - b;
break;
case '*':
input();
cout <<"Result : "<< a * b;
break;
case '/':
input();
if (b!=0)
cout <<"Result : "<< a / b;
else
cout<<"Divison by zero not allowed";
break;
//remainder of two numbers
case '%':
input();
cout <<"Result : "<< int(a) % int(b);
break;
//average of two numbers
case 'A':
input();
cout <<"Result : "<< (a + b) / 2.0;
break;
//absolute of two numbers
case '|':
input();
cout<<"Result : "<<abs(a-b);
break;
//Max of 2 numbers
case 'M':
input();
cout <<"Max : "<<max(a,b)<<endl;
cout <<"Min : "<<min(a,b)<<endl;
break;
//Square root
case 'S':
do{
cout<<"Enter a +ve num : ";
cin>>a;
}while(a<0);
cout <<"Result : "<< sqrt(a) << endl;
break;
//sum
case 'F':
cout<<"ENter num ";
cin>>a;
int sum=0;
for (int i = 0; i < a; i++)
{
sum+=i;
}
cout<<sum;
break;
//Fcatorial
case '!':
do{
cout<<"Enter a +ve num : ";
cin>>a;
}while(a<0);
cout<<"Result : "<<fact(a);
break;
case 'P':
input();
cout<<"Result : "<<pow(a,b);
break;
case 'q':
cout<<"Exiting";
break;
default:
cout<<" INVALID SYNTAX ";
break;
}
}
//factorial function
int fact(int n){
int res=1;
for (int i = 1; i <= n; i++)
{
res *=i;
}
return res;
}
};
int main()
{
system("cls");
calculator c;
char op;
do
{
cout<<"\n---------------------------------------------------------------"<<endl;
cout << "Enter 'h' for a manual to use operations !" << endl;
cout << "Enter your operation(+,-,!,/....etc):- " << endl;
cin >> op;
cout<<"---------------------------------------------------------------"<<endl;
if (op == 'H' || op == 'h')
c.manual();
else if(op=='T'|| op =='t')
cout<<"Building....";
else
c.calculation(op);
} while (op!='q');
return 0;
}