我必须在C中编写一个代码,它有一个额外的函数,指针作为函数参数。此函数必须使用这些指针执行基本的数学计算(例如+ - * /),然后将结果发送回调用函数main()。
但我不知道,被调用的函数如何表示除以零是未定义的并将其发送回main()?
#include "stdafx.h"
char count(float *, float *, float *, float *, float *, float *);
//prototype the function
int _tmain(int argc, _TCHAR* argv[])
{
float a, b, sum, diff, prod, div;
printf("\n a und b eingeben: ");
scanf("%f %f", &a, &b);
printf("\n a und b lauten: %f %f\n", a, b);
count(&a, &b, &sum, &diff, &prod, &div); //send addresses to function
printf("\n a+b = %f \n", sum);
printf("\n a-b = %f \n", diff);
printf("\n a*b = %f \n", prod);
if (b == 0)
printf("\n a/b ist nicht definiert\n\n"); //I wrote this just in
//the main (), but it's
//not the same exercise :))
else
printf("\n a/b = %f \n", div);
return 0;
}
char count(float *a, float *b, float *sum, float *diff, float *prod, float
*div) //define the function
{
*sum = *a + *b;
*diff = *a - *b;
*prod = *a * *b;
*div = *a / *b;
if (*div == 0) //?????????????
return NULL;
}
答案 0 :(得分:1)
如果你包含math.h并使用std = c99,你可以使用
isinf(div)
检查你是否除以0。
即。
if(isinf(div)) {
printf("\n a/b is undefined\n\n");
}