我对这个程序有一个问题。 在bool void函数中,如果指数值为负,则应该返回true 哪个应该退出主函数循环。我虽然无法想出退出此循环的方法 因为它保持循环,因为值为真。
//Input: The input will consist of a data file that contains a series
//of x values and polynomials. Each term will consist of a coefficient
//and an exponent. The polynomial will be terminated by a negative exponent.
//Output: For each polynomial display the x value as well as a nicely
//formatted polynomial along with the value of the polynomial at x.
//Example: When x = 2
// 2x^2 - x + 3 = 9
#include iostream
#include cmath
#include iomanip
#include cstdlib
using namespace std;
void get_term(int &, int &);
bool end_poly(int);
void print_term(int, int, int &);
double evaluate_term(int, int, int);
int main()
{
int xvalue; //value for x used in evaluation of polynomial
int coef, exponent; //coefficient and exponent of current polynomial term
double result; //value of polynomial
int term_count; //counts # of terms displayed
cout << fixed << setprecision(0); //suppress decimal when doubles displayed
cin >> xvalue; //get first x value
while (cin)
{
term_count = 0; //no terms have been displayed yet
result = 0; //initialize accumulator for polynomial
cout << "When x = " << xvalue << endl;
get_term(coef, exponent); //get coefficient and exponent of 1st term
while (!end_poly(exponent))
{
result = result + evaluate_term(coef, exponent, xvalue);
print_term(coef, exponent, term_count);
get_term(coef, exponent);
}
cout << " = " << result << endl << endl;
cin >> xvalue;
}
return 0;
}
void get_term(int &coefficient, int &exponent)
//get_term is passed 2 int parameters that represent the coefficient and exponent
//of a term. The function reads to 2 values and passes them back to the calling
//function.
{
cin >> coefficient >> exponent;
return;
}
bool end_poly(int exponent)
{
while (exponent < 0)
true;
}
void print_term(int coef, int exp, int &term_count)
{
int tempcoef = 0; // holds the absolute value of coef
if (term_count != 0 && coef < -1 && exp > 1)
{
tempcoef = abs(coef);
cout << " - " << tempcoef << "x^" << exp;
}
if (term_count != 0 && coef == -1 && exp > 1)
cout << " - x^" << exp;
if (term_count == 0 && coef > 1 && exp > 1)
cout << coef << "x^" << exp;
if (term_count != 0 && coef > 1 && exp > 1)
cout << " + " << coef << "x^" << exp;
if (term_count > 0 && coef == 1 && exp > 1)
cout << " + x^" << exp;
if (term_count == 0 && coef == 1 && exp > 1)
cout << "x^" << exp;
if (term_count != 0 && coef < -1 && exp == 1)
{
tempcoef = abs(coef);
cout << " - " << tempcoef << "x";
}
if (term_count != 0 && coef == -1 && exp == 1)
cout << " - x";
if (term_count == 0 && coef > 1 && exp == 1)
cout << coef << "x";
if (term_count != 0 && coef > 1 && exp == 1)
cout << " + " << coef << "x";
if (term_count > 0 && coef == 1 && exp == 1)
cout << " + x";
if (term_count == 0 && coef == 1 && exp == 1)
cout << "x";
if (term_count == 0 && coef < -1 && exp == 0)
{
tempcoef = abs(coef);
cout << "-" << tempcoef;
}
if (term_count == 0 && coef == -1 && exp == 0)
cout << "-1";
if (term_count != 0 && coef < -1 && exp == 0)
{
tempcoef = abs(coef);
cout << " - " << tempcoef;
}
if (term_count != 0 && coef == -1 && exp == 0)
cout << " - 1";
if (term_count == 0 && coef > 1 && exp == 0)
cout << coef;
if (term_count != 0 && coef > 1 && exp == 0)
cout << " + " << coef;
if (term_count > 0 && coef == 1 && exp == 0)
cout << " + 1";
if (term_count == 0 && coef == 1 && exp == 0)
cout << "1";
term_count = term_count + 1;
}
double evaluate_term(int coef, int exponent, int x)
{
coef *pow(x , exponent);
}
答案 0 :(得分:0)
如果函数如果指数值为负则返回true,则它应如下所示:
bool end_poly(int exponent)
{
return exponent < 0;
}