Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  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. coef[degree] = sign*num;
  60. return coef;
  61. }
  62.  
  63. int main() {
  64. string s;
  65. cin >> s;
  66. vector<int> vec = strToVector(s);
  67. for (int i = 0; i < vec.size(); ++i) {
  68. if (vec[i]) {
  69. cout << i << " - " << vec[i] << endl;
  70. }
  71. }
  72. return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement