fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. #include <cstdlib>
  5. using namespace std;
  6.  
  7. void get_term(int&,int&);
  8. bool end_poly(int);
  9. void print_term(int,int,int&);
  10. double evaluate_term(int,int,int);
  11.  
  12. int main()
  13. {
  14. int xvalue;
  15. int coef, exponent;
  16. double result;
  17. int term_count;
  18.  
  19. cout << fixed << setprecision(0);
  20. cin >> xvalue;
  21.  
  22. while (cin)
  23. {
  24. term_count = 0;
  25. result = 0;
  26. cout << "When x = " << xvalue << endl;
  27. get_term(coef,exponent);
  28. while (!end_poly(exponent))
  29. {
  30. result = result + evaluate_term(coef,exponent,xvalue);
  31. print_term(coef,exponent,term_count);
  32. get_term(coef,exponent);
  33. }
  34. cout << " = " << result << endl << endl;
  35. cin >> xvalue;
  36. }
  37. return 0;
  38. }
  39.  
  40. void get_term(int& coefficient, int& exponent)
  41. {
  42. cin >> coefficient >> exponent;
  43. return;
  44. }
  45.  
  46. bool end_poly(int exponent)
  47. {
  48. if(exponent >= 0)
  49. {
  50. return false;
  51. }
  52. else
  53. {
  54. return true;
  55. }
  56. }
  57.  
  58. void print_term(int coef, int exp, int& term_count)
  59. {
  60. if(coef < 0)
  61. {
  62. cout << "- ";
  63. coef = abs(coef);
  64. }
  65. if(term_count > 0)
  66. {
  67. if(coef != 0)
  68. {
  69. cout << "+ ";
  70. }
  71. }
  72. if(coef > 1)
  73. {
  74. cout << coef;
  75. }
  76. if((coef > 1 || coef == 1) && exp > 1)
  77. {
  78. cout << "x^" << exp << " ";
  79. }
  80. if((coef > 1 || coef ==1) && exp ==1)
  81. {
  82. cout << "x ";
  83. }
  84. if(coef == 1 && exp == 0)
  85. {
  86. cout << coef << " ";
  87. }
  88. term_count++;
  89. }
  90.  
  91. double evaluate_term(int coef, int exponent, int x)
  92. {
  93. return (pow(x,exponent) * coef);
  94. }
  95.  
Success #stdin #stdout 0.01s 5308KB
stdin
1
2
stdout
When x = 1
 = 0