当输入为负c++时,退出bool-void函数

Exiting a bool void function when the input is negative c++

本文关键字:退出 bool-void 函数 c++ 输入      更新时间:2023-10-16

这个程序有一个问题。在bool-void函数中,如果指数值为负数,则应该返回true其应当退出主功能循环。不过,我无法想出退出这个循环的方法因为它保持循环,因为值为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);
}

如果函数在指数值为负的情况下应该返回true,那么它应该如下所示:

bool end_poly(int exponent)
{
    return exponent < 0;
}