Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <stdlib.h>
- using namespace std;
- vector<int> strToVector(const string& s) {
- bool reading_num = 1;
- bool reading_sign = 1;
- bool reading_degree = 0;
- int sign = 1;
- int num = 0;
- int degree;
- bool is_first = 1;
- vector<int> coef(20);
- for (const char& c: s) {
- if (reading_num == 1 && isdigit(c)) {
- num = num*10 + c - '0';
- }
- if (reading_degree && isdigit(c)) {
- degree = degree*10 + c - '0';
- }
- if (reading_sign == 1) {
- if (c == '+') {
- num = max(1, num);
- degree = max(1, degree);
- coef[degree] = sign*num;
- num = 0;
- sign = 1;
- reading_num = 1;
- degree = 0;
- reading_degree = 0;
- } else if (c == '-') {
- num = max(1, num);
- degree = max(1, degree);
- coef[degree] = sign*num;
- num = 0;
- sign = -1;
- reading_num = 1;
- degree = 0;
- reading_degree = 0;
- }
- }
- if (c == 'x') {
- reading_num = 0;
- reading_sign = 0;
- degree = 0;
- reading_degree = 1;
- reading_sign = 1;
- }
- }
- num = max(1, num);
- coef[degree] = sign*num;
- return coef;
- }
- int main() {
- string s;
- cin >> s;
- vector<int> vec = strToVector(s);
- for (int i = 0; i < vec.size(); ++i) {
- if (vec[i]) {
- cout << i << " - " << vec[i] << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement