daily pastebin goal
26%
SHARE
TWEET

Test

a guest Jan 20th, 2019 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdlib.h>
  4. using namespace std;
  5.  
  6.  
  7.  
  8. vector<int> strToVector(const string& s) {
  9.     bool reading_num = 1;
  10.     bool reading_sign = 1;
  11.     bool reading_degree = 0;
  12.     int sign = 1;
  13.     int num = 0;
  14.     int degree;
  15.     bool is_first = 1;
  16.     vector<int> coef(20);
  17.     for (const char& c: s) {
  18.         if (reading_num == 1 && isdigit(c)) {
  19.             num = num*10 + c - '0';
  20.         }
  21.  
  22.         if (reading_degree && isdigit(c)) {
  23.             degree = degree*10 + c - '0';
  24.         }
  25.  
  26.         if (reading_sign == 1) {
  27.             if (c == '+') {
  28.                 num = max(1, num);
  29.                 degree = max(1, degree);
  30.                 coef[degree] = sign*num;
  31.                 num = 0;
  32.                 sign = 1;
  33.                 reading_num = 1;
  34.                 degree = 0;
  35.                 reading_degree = 0;
  36.             } else if (c == '-') {
  37.                 num = max(1, num);
  38.                 degree = max(1, degree);
  39.                 coef[degree] = sign*num;
  40.                 num = 0;
  41.                 sign = -1;
  42.                 reading_num = 1;
  43.                 degree = 0;
  44.                 reading_degree = 0;
  45.             }
  46.         }
  47.  
  48.         if (c == 'x') {
  49.  
  50.             reading_num = 0;
  51.             reading_sign = 0;
  52.             degree = 0;
  53.             reading_degree = 1;
  54.             reading_sign = 1;
  55.         }
  56.  
  57.  
  58.     }
  59.     return coef;
  60. }
  61.  
  62. int main() {
  63.     string s;
  64.     cin >> s;
  65.     vector<int> vec = strToVector(s);
  66.     for (int i = 0; i < vec.size(); ++i) {
  67.         if (vec[i]) {
  68.             cout << i << " - " << vec[i] << endl;
  69.         }
  70.     }
  71.     return 0;
  72. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top