fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9. string s;
  10. cin >> s; // it would work only if there are no spaces between the terms
  11. istringstream iss(s);
  12. double coeff;
  13. char x, sym;
  14. int degree;
  15. vector<double> coefficients;
  16. while (iss >> coeff >> x >> sym >> degree) {
  17. // you should also check if degree < 0...
  18. if (degree >= coefficients.size()) {
  19. coefficients.insert(coefficients.end(), degree - coefficients.size(), 0.0);
  20. coefficients.push_back(coeff);
  21. }
  22. else
  23. coefficients[degree] = coeff;
  24. }
  25.  
  26. for (size_t i = 0; i < coefficients.size(); ++i)
  27. std::cout << ' ' << coefficients[i];
  28. }
Success #stdin #stdout 0s 4356KB
stdin
3x^4-2x^1
stdout
 0 -2 0 0 3