2014-10-26 80 views
-6

我有这个程序的这个问题。 在bool void函数中,如果指数值为负 ,它应该返回true,它应该退出主函数循环。我无法想出一个办法来退出这个循环,虽然 ,因为它保持循环,因为值是真实的。当输入为负时退出一个bool void函数C++

//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); 
} 
+5

什么是“布尔void函数”,并且它在哪里? – Deduplicator 2014-10-26 22:35:48

+0

什么是while(指数<0)true;'应该是做什么的? C++不是Pascal。你需要一个return语句。 – 2014-10-26 22:38:26

回答

0

如果函数应该如果指数值为负返回true,它应该是这样的:

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

所以我做到了这一点,print_term的输出是正确的,但现在数学已经搞砸了,并返回了错误的答案,为什么会这样呢? – Trevor 2014-10-26 23:22:08

+0

@Trevor这是一个不同的问题 – 2014-10-26 23:25:24