C++ 继续获取1.#INF作为我的输出 #包括 使用名称空间std; 双重计算(整数a、整数b); int main() { int n1,n2; cout>n1; cout>n2; cout

C++ 继续获取1.#INF作为我的输出 #包括 使用名称空间std; 双重计算(整数a、整数b); int main() { int n1,n2; cout>n1; cout>n2; cout,c++,C++,在这里,您使用的是int数字: #include <iostream> using namespace std; double calc(int a, int b); int main() { int n1, n2; cout << "Enter a number for a: "; cin >> n1; cout << "Enter a number for b: "; cin >> n2

在这里,您使用的是
int
数字:

#include <iostream>
using namespace std;

double calc(int a, int b);

int main()
{
    int n1, n2;

    cout << "Enter a number for a: ";
    cin >> n1;
    cout << "Enter a number for b: ";
    cin >> n2;

    cout << calc(n1, n2) << endl;

    system("PAUSE");
    return 0;

}

double calc(int a, int b)
{
    double s;
    s = (a) / ((sqrt(a / b)));
    return s;
}
如果
a
小于
b
,则
a/b
(请记住,两者都是整数,因此结果的小数部分将丢失)将等于0,这将导致除以0。您需要将其中一个数字转换为两倍:

s = (a) / ((sqrt(a / b)));
s=(a)/(sqrt(static_cast(a)/b));

在这里,您使用的是
int
编号:

#include <iostream>
using namespace std;

double calc(int a, int b);

int main()
{
    int n1, n2;

    cout << "Enter a number for a: ";
    cin >> n1;
    cout << "Enter a number for b: ";
    cin >> n2;

    cout << calc(n1, n2) << endl;

    system("PAUSE");
    return 0;

}

double calc(int a, int b)
{
    double s;
    s = (a) / ((sqrt(a / b)));
    return s;
}
如果
a
小于
b
,则
a/b
(请记住,两者都是整数,因此结果的小数部分将丢失)将等于0,这将导致除以0。您需要将其中一个数字转换为两倍:

s = (a) / ((sqrt(a / b)));
s=(a)/(sqrt(static_cast(a)/b));

您说当其中一个整数等于或小于0时,函数将退出程序,但在哪里

试着像这样检查它们:

此外,您应该检查a是否大于b

s = (a) / ((sqrt(static_cast<double>(a) / b)));
双重计算(整数a、整数b)
{
双s;

如果(a您说当其中一个整数为0或更小时,函数将退出程序,但在哪里

试着像这样检查它们:

此外,您应该检查a是否大于b

s = (a) / ((sqrt(static_cast<double>(a) / b)));
双重计算(整数a、整数b)
{
双s;

如果(a您在无穷大方面遇到问题,请使用
isinf
。以下是一些示例用法:

double calc(int a, int b)
{
    double s;
    if(a <= 0) exit(-1);
    if(b <= 0) exit(-1);
    if(a < b)  exit(-1);
    s = (a) / ((sqrt(a / b)));
    return s;
}
#包括/*printf*/
#包括/*isinf、sqrt*/
int main()
{
printf(“isinf(-1.0/0.0):%d\n”,isinf(-1.0/0.0));
printf(“isinf(sqrt(-1.0)):%d\n”,isinf(sqrt(-1.0));
返回0;
}
输出:

isinf(-1.0/0.0):1 isinf(sqrt(-1.0):0


您在无穷大方面遇到了问题。对于无穷大,请使用
isinf
。以下是一些示例用法:

double calc(int a, int b)
{
    double s;
    if(a <= 0) exit(-1);
    if(b <= 0) exit(-1);
    if(a < b)  exit(-1);
    s = (a) / ((sqrt(a / b)));
    return s;
}
#包括/*printf*/
#包括/*isinf、sqrt*/
int main()
{
printf(“isinf(-1.0/0.0):%d\n”,isinf(-1.0/0.0));
printf(“isinf(sqrt(-1.0)):%d\n”,isinf(sqrt(-1.0));
返回0;
}
输出:

isinf(-1.0/0.0):1 isinf(sqrt(-1.0):0


sqrt
获取并返回一个double。当您使用整数参数调用它时,它将转换为double,从而得到无穷大的值

将函数签名更改为:

#include <stdio.h>      /* printf */
#include <math.h>       /* isinf, sqrt */

    int main()
    {
      printf ("isinf(-1.0/0.0)  : %d\n",isinf(-1.0/0.0));
      printf ("isinf(sqrt(-1.0)): %d\n",isinf(sqrt(-1.0)));
      return 0;
    }

并将n1和n2声明为double。

sqrt
获取并返回一个double。当您使用整数参数调用它时,它将转换为double,从而获得无穷大的值

将函数签名更改为:

#include <stdio.h>      /* printf */
#include <math.h>       /* isinf, sqrt */

    int main()
    {
      printf ("isinf(-1.0/0.0)  : %d\n",isinf(-1.0/0.0));
      printf ("isinf(sqrt(-1.0)): %d\n",isinf(sqrt(-1.0)));
      return 0;
    }

并将n1和n2声明为双精度。

您输入的是哪些数字?a=4,b=1会发生什么?我猜您输入的是a