-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarmup.cpp
105 lines (93 loc) · 2.92 KB
/
warmup.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
/**
* File: warmup.cpp
* ----------------
* This code is provided as part of an exercise to practice using a
* debugger. This program has buggy use of recursion.
*/
#include <iostream> // for cout, endl
#include "error.h"
#include "random.h"
#include "testing/SimpleTest.h"
using namespace std;
/* This is a recursive implementation of a function to calculate
* factorials. This function works correctly for positive values
* of n whose computed factorial value does not overflow the int
* data type (n <= 12).
*
* As currently written, this function does not correctly handle
* negative inputs.
*/
int factorial(int n) {
if (n < 0) {
error("负数无法计算阶乘!");
}
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
/* The myPower function computes exponentiation.
* The call myPower(base, exp) returns base raised to the power exp.
* Note that either/both of base and exp can be negative.
* The calculation is done using type double to allow for the result
* to have a fractional component, such as would be needed for
* a negative exponent.
*
* WARNING: The given implementation is BUGGY! It works mostly correctly
* but it has some bugs that you should find and fix. Refer to the
* writeup for further instructions on how to do so.
*/
double myPower(int base, int exp) {
if (exp == 0) { // handle zero exp
return 1;
}
// else if (base == 0) { // handle zero base
// return 0;
// }
else if (exp < 0) { // handle negative exp
return 1.0 / myPower(base, -exp);
}
// else if (base < 0) { // handle negative base
// return -myPower(-base, exp);
// }
else { // both base and exp are positive
return base * myPower(base, exp - 1);
}
}
/* * * * * * Test Cases * * * * * */
PROVIDED_TEST("Confirm result of factorial(7)") {
EXPECT_EQUAL(factorial(7), 7*6*5*4*3*2);
}
PROVIDED_TEST("myPower(), compare to library pow(), fixed inputs") {
EXPECT_EQUAL(myPower(7, 0), pow(7, 0));
EXPECT_EQUAL(myPower(10, 2), pow(10, 2));
EXPECT_EQUAL(myPower(5, -1), pow(5, -1));
EXPECT_EQUAL(myPower(-3, 3), pow(-3, 3));
}
PROVIDED_TEST("myPower(), generated inputs") {
for (int base = 1; base < 25; base++) {
for (int exp = 1; exp < 10; exp++) {
EXPECT_EQUAL(myPower(base, exp), pow(base, exp));
}
}
}
STUDENT_TEST("Stack Overflow of factorial(-3)") {
EXPECT_ERROR(factorial(-3));
}
STUDENT_TEST("Error Test of myPower") {
#define singleTest
#ifdef loopTest
for (int i = -100; i < 100; i++) {
for (int j = -5; j < 5; j++) {
cout << "base = " << i << " exp = " << j << endl;
EXPECT_EQUAL(myPower(i,j), pow(i,j));
}
}
#endif
#ifdef singleTest
EXPECT_EQUAL(myPower(-100, 4), pow(-100, 4));
EXPECT_EQUAL(myPower(100, -4), pow(100, -4));
EXPECT_EQUAL(myPower(-100, -4), pow(-100, -4));
#endif
}