当输入为负c++时,退出bool-void函数
Exiting a bool void function when the input is negative c++
这个程序有一个问题。在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;
}
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 写入向量<向量<bool>>
- 退出c++中的递归void方法
- 'void QAbstractButton::clicked(bool)'受到保护
- 从标准::未来<void>到非标量标准::未来<bool>引发的错误转换
- 'void *' 'void (*) (bool)' 指针使用 c ++ 编译器在 C 中编译文件的错误
- 退出代码 -1073741510 在 bool if 语句处
- isSet() 或运算符 void*() 或显式操作程序 bool() 或其他东西
- 为什么 std::fstream 返回 void 而不是 bool
- C++11 无法将 std::condition_variable::wait 从 'void' 转换为 'bool'
- 当输入为负c++时,退出bool-void函数
- 函数void QQuickWindow::setDefaultAlphaBuffer(bool useAlpha)的用途
- GetProcAddress取消闪烁以获取哪种形式的Void pascal Myfunction(BOOL)的地址
- c++程序不会在void()函数中退出循环
- 控制到达bool函数中非void函数的结束
- 这声明了什么“void func(bool b..)”
- 将 bool[] 转换为 std::string 的 void*
- GCC 问题 - 从 'void (*)(MyObject*, bool)' 到 'const void*' 的转换无效
- 为什么我可以将int和BOOL强制转换为void*,而不是float ?