4

编写一个程序,请求两个浮点数并打印它们的差值除以它们的乘积。让程序循环遍历输入值对,直到用户输入非数字输入。使用函数返回计算值。

我在不使用函数的情况下成功完成了这个练习,但使用函数却无法正确完成。程序本身运行但不返回任何值实际上它崩溃了。

请任何帮助将不胜感激。

这是我的程序:

#include <stdio.h>
#include <string.h>

double result (double x, double y);

int main(void)
{
     double num1, num2, res;
     printf("This while calculate difference of two numbers by their product.\n");
     printf("Enter first number followed by second number\n");

     while (scanf("%lf %lf", &num1, &num2 ==2))
     {
         res= result(num1, num2);
         printf("the result is equal to %.3g\n", res);
         printf("Enter next set of numbers or q to quit\n");
     }
     return 0;
}
double result(double x, double y)
{
    double output;
    output = (y-x)/(x*y);
    return output;
}
4

2 回答 2

8
while (scanf("%lf %lf", &num1, &num2 ==2))

本来是:

while (scanf("%lf %lf", &num1, &num2) ==2)
于 2013-10-12T21:51:43.653 回答
2

尝试改变

  while (scanf("%lf %lf", &num1, &num2 ==2))

 while (scanf("%lf %lf", &num1, &num2) ==2)
于 2013-10-12T21:53:35.380 回答