-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek6_p1.cpp
More file actions
44 lines (35 loc) · 854 Bytes
/
Copy pathweek6_p1.cpp
File metadata and controls
44 lines (35 loc) · 854 Bytes
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
//recursive x^y
#include <stdio.h>
typedef enum {
SUCCESS = 0,
ERR_DEVIDE_BY_ZERO = -1,
ERR_UNDEFINED = -2
} powStat;
float power_helper(float x, int y) {
if (y == 0) {
return 1.0f;
}
if (y > 0) {
return x * power_helper(x, y - 1);
}
return 1.0f / power_helper(x, -y);
}
powStat my_pow(const float x, const float y, float *result) {
if (x == 0.0f && y < 0.0f) return ERR_DEVIDE_BY_ZERO;
if (x == 0.0f && y == 0.0f) return ERR_UNDEFINED;
*result = power_helper(x, (int)y);
return SUCCESS;
}
int main() {
float x,y,result, ans = 0.0f;
powStat status;
while(1){
scanf("%f %f",&x,&y);
status = my_pow(x, y, &result);
if (status == SUCCESS) {
ans = result;
}
printf("%g ^ %g = %g\n",x,y,ans);
}
return 0;
}