-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.cpp
65 lines (56 loc) · 2.8 KB
/
string.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
#include <iostream>
// This function should not be made constexpr because output and input can only be done at runtime.
// The versions of `operator<<` and `operator>>` that do output and input don't support constexpr.
double getTowerHeight()
{
std::cout << "Enter the height of the tower in meters: ";
double towerHeight{};
std::cin >> towerHeight;
return towerHeight;
}
// Returns height from ground after "seconds" seconds
// This function is made constepxr because it just calculates a value from its inputs and return it.
// Arithmetic can be done at compile-time, and no non-constexpr functions are called.
// Reminder: A constexpr function can be evaluated at compile-time or runtime.
// If its arguments are constexpr, it can be called at compile-time.
// In this case, it's called at runtime because the argument for towerHeight isn't constexpr.
// If a function can be made constexpr, it should be.
// Remember: function parameters are not constexpr, even in a constexpr function
constexpr double calculateBallHeight(double towerHeight, int seconds)
{
constexpr double gravity{ 9.8 }; // constexpr because it's a compile-time constant
// Using formula: [ s = u * t + (a * t^2) / 2 ], here u(initial velocity) = 0
// These variables can't be constexpr since their initializers aren't constant expressions
const double distanceFallen{ (gravity * (seconds * seconds)) / 2.0 };
const double currentHeight{ towerHeight - distanceFallen };
return currentHeight;
}
// This function should not be made constexpr because output and input can only be done at runtime.
// The versions of `operator<<` and `operator>>` that do output and input don't support constexpr.
void printBallHeight(double ballHeight, int seconds)
{
if (ballHeight > 0.0)
std::cout << "At " << seconds << " seconds, the ball is at height: " << ballHeight << " meters\n";
else
std::cout << "At " << seconds << " seconds, the ball is on the ground.\n";
}
// This function should not be made constexpr because output and input can only be done at runtime.
// The versions of `operator<<` and `operator>>` that do output and input don't support constexpr.
void printCalculatedBallHeight(double towerHeight, int seconds)
{
// height can only be const (not constexpr) because its initializer is not a constant expression
const double ballHeight{ calculateBallHeight(towerHeight, seconds) };
printBallHeight(ballHeight, seconds);
}
int main()
{
// towerHeight can only be const (not constexpr) because its initializer is not a constant expression
const double towerHeight{ getTowerHeight() };
printCalculatedBallHeight(towerHeight, 0);
printCalculatedBallHeight(towerHeight, 1);
printCalculatedBallHeight(towerHeight, 2);
printCalculatedBallHeight(towerHeight, 3);
printCalculatedBallHeight(towerHeight, 4);
printCalculatedBallHeight(towerHeight, 5);
return 0;
}