-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_containes.cpp
More file actions
92 lines (73 loc) · 1.63 KB
/
1_containes.cpp
File metadata and controls
92 lines (73 loc) · 1.63 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
#include <iostream>
#include <string>
#include <regex>
#include <map>
#include <sstream>
std::string derivative(std::string polynomial) {
std::map<int, int> pow_coeff; //< pow to coeff map
// parse regex
// | + | [-]k | x | pow |
std::regex reg("[+]?([-]?[0-9]*)[\\*]?(x?)[\\^]?([0-9]*)");
std::smatch m;
while (std::regex_search(polynomial, m, reg) && !polynomial.empty())
{
int k, pow;
// coeff
if (!m[1].str().empty())
{
if (m[1].length() == 1 && m[1] == '-')
k = -1;
else
k = std::stoi(m[1].str());
}
else
k = 1;
// x
if (!m[2].str().empty())
{
if (!m[3].str().empty())
pow = std::stoi(m[3].str());
else
pow = 1;
}
else
pow = 0;
// insert to polynomial map
pow_coeff[pow] += k;
// get next polynominal part
polynomial = m.suffix();
}
//get derivative
std::stringstream buffer;
for (auto it = pow_coeff.rbegin(); it != pow_coeff.rend(); ++it)
{
int pow = it->first;
int k = it->second;
if (!pow) // x^0
continue;
k *= pow;
pow -= 1;
if (k >= 0)
buffer << '+';
if ((k != 1 && k != -1) || pow == 0)
buffer << k;
else if (k == -1)
buffer << '-';
if (pow)
buffer<< "*x";
if (pow > 1)
buffer << '^' << pow;
}
// return
std::string out = buffer.str();
if (out.front() == '+')
out.erase(0, 1);
return out;
}
int main(void)
{
std::string polynomial;
std::getline(std::cin, polynomial);
std::cout << derivative(polynomial) << std::endl;
return 0;
}