2014-10-26 90 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

相關問題