C++
C++ passing reference to pointer
GOD03219
2018. 9. 12. 22:38
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 |
#include<iostream>
#include<cstdlib>
#include"input.h"
#include"operators.h"
#include"disp.h"
using namespace std;
class Calculation{
int *a;
int*b;
char *c;
float *res;
public : Calculation(){
a=new int;
b=new int;
c=new char;
res=new float;
}
void cac(){
input(*a,b,&c);
*res=operators(a,b,c);
disp(res);
}
};
void main(){
Calculation *ca = new Calculation;
ca->cac();
delete ca;
ca=NULL; }
|
cs |
// input.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
#include<iostream>
using namespace std;
void input(int &a, int *&b, char **c){
cout << "숫자 1 입력 :";
cin >> a;
cout << "연산자 입력 :";
cin >> **c;
cout << "숫자 2 입력 :" ;
cin >> *b;
} |
cs |
// operators.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
#include<iostream>
using namespace std;
float operators(int *&a, int *&b ,char *&c){
float res;
switch(*c){
case '+': res= (float)*a+*b; break;
case '-': res=(float)*a-*b; break;
case '*': res=(float)*a * *b; break;
case '/':res=(float)*a / *b ; break;
default: cout << "wrong input"; break;
}
return res;
} |
cs |
// disp.h
1
2
3
4
5
6
7 |
#include<iostream>
using namespace std;
void disp(float *&res){
cout << *res<<endl;
} |
cs |