如果给出了错误的输入,我的函数应该包含什么到 return -1.0 的值?
What should my function include to return the value of -1.0 if the wrong input is given?
这里是项目说明:
"Write a C program that includes a function of type double called divmaster accepts two double arguments (you must write the divmaster function). When called, your function will return the first argument divided by the second argument. Make sure your function includes a decision statement so that it can't divide by 0--if the caller attempts to divide by 0 the function should return a value of -1.0. Your program should call divmaster then display the value returned by the function. Test your function thoroughly then submit output using the arguments 22 and 3. Remember that a structured function should have only one return statement. "
我想我的大部分程序都是正确的,除了我在函数部分有困难。我们还没有学习 if else 语句,所以我不能使用它们。我的想法是有条件的 while 语句 return 如果输入 0,则值为 -1.0。如有任何想法,我们将不胜感激。
这是我目前的代码。
#include <stdio.h>
double divmaster(double x, double y);
int main(void)
{
double i, j, value;
printf("Please enter two numbers ");
printf("the first number will be divded by the second. The value ");
printf("will then be returned. Enter q to quit\n");
while (scanf("%d%d", &i, &j) == 2)
{
value = divmaster(i, j);
printf("%d divded by %d is %.6f\n", i, j, value);
printf("Please enter the next paid of numbers or q to quit.\n");
}
return 0;
}
double divmaster(double x, double y)
{
while (y <= 0);
printf("-1.0");
}
}
您的代码有几个主要问题:
scanf
和printf
的字符串格式错误,%d
格式取一个int,你把double作为参数,double的正确格式是%lf
- 您应该在每种格式之间使用分隔符。
- 您的函数 divmaster 缺少
return
语句。
这是修复代码后的样子。
#include <stdio.h>
double divmaster(double x, double y);
int main(void)
{
double i, j, value;
printf("Please enter two numbers ");
printf("the first number will be divded by the second. The value ");
printf("will then be returned. Enter q to quit\n");
while (scanf("%lf %lf", &i, &j) == 2)
{
value = divmaster(i, j);
printf("%lf divded by %lf is %.6lf\n", i, j, value);
printf("Please enter the next pair of numbers or q to quit.\n");
}
return 0;
}
double divmaster(double x, double y)
{
if (0.0f == y)
return -1.0f;
else
return x / y;
}
如果不能用if else语句,用while语句也是可以的
while (0.0f == y)
return -1.0f;
return x/y;
每个函数的手册中都定义了每种类型的正确格式
http://man7.org/linux/man-pages/man3/printf.3.html
http://man7.org/linux/man-pages/man3/scanf.3.html
这里是项目说明:
"Write a C program that includes a function of type double called divmaster accepts two double arguments (you must write the divmaster function). When called, your function will return the first argument divided by the second argument. Make sure your function includes a decision statement so that it can't divide by 0--if the caller attempts to divide by 0 the function should return a value of -1.0. Your program should call divmaster then display the value returned by the function. Test your function thoroughly then submit output using the arguments 22 and 3. Remember that a structured function should have only one return statement. "
我想我的大部分程序都是正确的,除了我在函数部分有困难。我们还没有学习 if else 语句,所以我不能使用它们。我的想法是有条件的 while 语句 return 如果输入 0,则值为 -1.0。如有任何想法,我们将不胜感激。
这是我目前的代码。
#include <stdio.h>
double divmaster(double x, double y);
int main(void)
{
double i, j, value;
printf("Please enter two numbers ");
printf("the first number will be divded by the second. The value ");
printf("will then be returned. Enter q to quit\n");
while (scanf("%d%d", &i, &j) == 2)
{
value = divmaster(i, j);
printf("%d divded by %d is %.6f\n", i, j, value);
printf("Please enter the next paid of numbers or q to quit.\n");
}
return 0;
}
double divmaster(double x, double y)
{
while (y <= 0);
printf("-1.0");
}
}
您的代码有几个主要问题:
scanf
和printf
的字符串格式错误,%d
格式取一个int,你把double作为参数,double的正确格式是%lf
- 您应该在每种格式之间使用分隔符。
- 您的函数 divmaster 缺少
return
语句。
这是修复代码后的样子。
#include <stdio.h>
double divmaster(double x, double y);
int main(void)
{
double i, j, value;
printf("Please enter two numbers ");
printf("the first number will be divded by the second. The value ");
printf("will then be returned. Enter q to quit\n");
while (scanf("%lf %lf", &i, &j) == 2)
{
value = divmaster(i, j);
printf("%lf divded by %lf is %.6lf\n", i, j, value);
printf("Please enter the next pair of numbers or q to quit.\n");
}
return 0;
}
double divmaster(double x, double y)
{
if (0.0f == y)
return -1.0f;
else
return x / y;
}
如果不能用if else语句,用while语句也是可以的
while (0.0f == y)
return -1.0f;
return x/y;
每个函数的手册中都定义了每种类型的正确格式
http://man7.org/linux/man-pages/man3/printf.3.html
http://man7.org/linux/man-pages/man3/scanf.3.html