-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambdas.cpp
More file actions
93 lines (74 loc) · 1.69 KB
/
lambdas.cpp
File metadata and controls
93 lines (74 loc) · 1.69 KB
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
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <functional>
using std::cout;
using std::endl;
using std::vector;
using std::setw;
using std::function;
vector<double> createNumbers(double from, double to, double step);
function<double (double)> getPowerer(int power);
void printNumbersWithPowers(vector<double> numbers, int power);
int main(int argc, char** argv)
{
int power = 2;
double from = 0.0;
double to = 10.0;
double step = 1.0;
if(argc > 1)
{
power = atoi(argv[1]);
}
if(argc > 2)
{
from = strtod(argv[2],NULL);
}
if(argc > 3)
{
to = strtod(argv[3],NULL);
}
if(argc > 4)
{
step = strtod(argv[4],NULL);
}
auto numbers = createNumbers(from,to,step);
cout << "For ["<< from << ", " << to << "). (Step is " << step << ")." << endl;
printNumbersWithPowers(numbers,power);
return 0;
}
vector<double> createNumbers(double from, double to, double step)
{
vector<double> numbers;
if((to-from)*step <= 0.0)
return numbers;
for(double i = from; abs(i) < abs(to); i += step)
{
numbers.push_back(i);
}
return numbers;
}
function<double (double)> getPowerer(int power)
{
return [power](double a){return pow(a,power);};
}
void printNumbersWithPowers(vector<double> numbers, int power)
{
const int outFieldW = 8;
auto powered = getPowerer(power);
double powerSum = 0.0;
cout << std::setiosflags(std::ios::left) << setw(outFieldW) << "x" << "x^" << power << endl;
for_each(
numbers.begin(),
numbers.end(),
[&](double n)
{
double pwd = powered(n);
cout << setw(outFieldW) << n << setw(outFieldW) << pwd << endl;
powerSum += pwd;
});
cout << endl << "Sum is " << powerSum << endl;
}