-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculatorusingtemplate.cpp
90 lines (68 loc) · 1.33 KB
/
calculatorusingtemplate.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
//simple calculator using template
#include<iostream>
#include<stdlib.h>
using namespace std;
template<class T>
class cal{
private:
T n1;
T n2;
public:
void addition(T, T);
void subtraction(T, T);
void multiplication(T, T);
void division(T, T);
};
template<class T>
void cal<T>::addition(T n1, T n2){
T add_result=n1+n2;
cout<<n1<<"+"<<n2<<"="<<add_result<<endl;
}
template<class T>
void cal<T>::subtraction(T n1, T n2){
T sub_result=n1-n2;
cout<<n1<<"-"<<n2<<"="<<sub_result<<endl;
}
template<class T>
void cal<T>::multiplication(T n1, T n2){
T mul_result=n1*n2;
cout<<n1<<"*"<<n2<<"="<<mul_result<<endl;
}
template<class T>
void cal<T>::division(T n1, T n2){
T div_result=n1/n2;
cout<<n1<<"/"<<n2<<"="<<div_result<<endl;
}
int main(){
cout<<"simple calculator\n\n";
// cout<<""
cal<int>c;
cal<float>f;
int n1, n2;
cout<<"enter two integers: ";
cin>>n1>>n2;
int choice, result;
while(choice!=6){
cout<<"1.add\n2.subtract\n3.multiply\n4.divide\n5.exit\n";
cout<<"enter your choice: ";
cin>>choice;
switch(choice){
case 1:
c.addition(n1, n2);
break;
case 2:
c.subtraction(n1, n2);
break;
case 3:
c.multiplication(n1, n2);
break;
case 4:
c.division(n1, n2);
break;
case 5:
exit(0);
break;
}
}
return 0;
}