如何解决使用C通过函数查找最小值和最大值
我需要编写一个程序,用户可以输入定义的数字,然后该程序将尝试找出哪一个是最低值和最高值。我面临的问题是:
您可以在答案中给出提示或更正。非常感谢。
#include <stdio.h>
float max(float num1){
float a=0,b;
if(num1 > a){
a=num1;
}
return a;
}
float min(float num2){
float x=100,y;
if(num2 < x ){
x=num2;
}
return num2;
}
int main(){
int times,interval;
float mini,maxi,data_Input;
printf("How many number would you like to type in ? : ");
scanf("%d\n",×);
printf("Type in the number: ");
scanf("%f",&data_Input);
for(interval=2; interval<=times; intervaL++){
printf("\nType in the number: ");
scanf("%f",&data_Input);
while(data_Input<0){
printf("Invalid Input! Please re-enter the number:");
scanf("%f",&data_Input);
}
while(data_Input>100){
printf("Invalid Input! Please re-enter the number:");
scanf("%f",&data_Input);
}
}
maxi= max(data_Input);
mini= min(data_Input);
printf("The Lowest Number is %.2f\n",mini);
printf("The Highest Number is %.2f\n",maxi);
return 0;
}
输出:
How many number would you like to type in? : 5
70
Type in the number :
Type in the number : 90.7
Type in the number : 99
Type in the number : 30
Type in the number : 50
The Lowest Number is 50.00
The Highest Number is 50.00
解决方法
好吧,事实是,您不必在输入每个连续数字后更新data_input。您正在做的是将最后一个数字与0或100进行比较,这在逻辑上是不正确的。 如何将第一个数字作为输入,然后在每次连续输入之后,将其与最小值和最大值进行比较。这是示例代码。
def transform(element):
if something_is_wrong_with_element:
yield pvalue.TaggedOutput('rejected',element)
transformed_element = ....
yield pvalue.TaggedOutput('gcs_output',transformed_element)
,
程序将检查最后一个数字,因为您正在从for镯子中调用min和max函数,因此您可以在for镯子中调用它们,例如:
for(interval=2; interval<=times; interval++){
printf("\nType in the number: ");
scanf("%f",&data_Input);
while(data_Input<0){
printf("Invalid Input! Please re-enter the number:");
scanf("%f",&data_Input);
}
while(data_Input>100){
printf("Invalid Input! Please re-enter the number:");
scanf("%f",&data_Input);
}
maxi= max(data_Input);
mini= min(data_Input);
}
除了重写相同的代码外,您只需要在for循环内输入数字,并将间隔初始化为1,这样您的主程序就会像这样:
int main(){
int times,interval;
float mini,maxi,data_Input;
printf("How many number would you like to type in ? : ");
scanf("%d\n",×);
for(interval=1; interval<=times; interval++){
printf("\nType in the number: ");
scanf("%f",&data_Input);
}
maxi= max(data_Input);
mini= min(data_Input);
}
printf("The Lowest Number is %.2f\n",mini);
printf("The Highest Number is %.2f\n",maxi);
return 0;
}
,
解决 printf问题很容易。由于stdout
是行缓冲的(至少默认情况下-可以更改),因此只要在缓冲区中插入换行符,就将其刷新。因此,只需在每条消息打印后添加换行符,例如
printf("How many number would you like to type in ? : \n");
您会没事的。
谈到 min 和 max 的错误计算,您的尝试基本上有两个大问题:
- 您没有获得
times
输入,只有一个。实际上,每次调用scanf
时,您都会覆盖相同的变量data_Input
,而不会与之前的输入进行任何比较 - 在最后一次输入之后,您只能调用一次
min()
和max()
函数。此外,您尝试将参数与具有本地存储且生命周期仅限于函数本身的局部变量进行比较,以便在下次调用时将其再次初始化
要在仅第一次初始化函数的函数中包含变量,可以使用static
关键字。但是不是,我建议您解决该问题。
我认为您不需要比较功能:每次获得新输入时,只需更新maxi
和mini
(可同时解决上述两个问题):
int main(){
int times,data_Input;
printf("How many number would you like to type in ? : \n");
scanf("%d",×);
printf("Type in the number: ");
scanf("%f",&data_Input);
maxi = data_Input;
mini = data_Input;
for(interval=2; interval<=times; interval++){
printf("\nType in the number: \n");
scanf("%f",&data_Input);
while(data_Input<0){
printf("Invalid Input! Please re-enter the number:\n");
scanf("%f",&data_Input);
}
while(data_Input>100){
printf("Invalid Input! Please re-enter the number:\n");
scanf("%f",&data_Input);
}
/* Check input and,in case,update max and min */
if(data_Input > maxi)
maxi = data_Input;
if(data_Input < mini)
mini = data_Input;
}
printf("The Lowest Number is %.2f\n",maxi);
return 0;
}
比较是在循环内执行的,因此您不需要将输入存储到数组中。
,您的代码中有很多问题。
- 函数
min
和max
没有任何意义 - 您不检查
scanf
的结果,也不知道它是否成功
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
int main()
{
int times,data_Input;
do
{
printf("\nHow many number would you like to type in ? : ");
}while(scanf(" %d\n",×) != 1);
for(interval = 0; interval < times; interval++)
{
do
{
printf("\nType in the number: ");
}while(scanf(" %f",&data_Input) != 1 && (data_Input < 0 || data_Input > 100));
printf("%f\n",data_Input);
maxi = interval == 0 ? data_Input : MAX(maxi,data_Input);
mini = interval == 0 ? data_Input : MIN(mini,data_Input);
}
printf("The Lowest Number is %.2f\n",maxi);
return 0;
}
,
执行程序时,第二行将在打印f之前等待用户输入(数字)
从"\n"
中删除"%d\n"
。它会一直阻塞,直到在该数字之后检测到非空白并且不需要为止。
printf("How many number would you like to type in ? : ");
// scanf("%d\n",×);
scanf("%d",×);
printf("Type in the number: ");
如果在预期时仍看不到输出,请冲洗它。通常,打印'\n'
会刷新stdout
,但不一定。
printf("How many number would you like to type in ? : ");
fflush(stdout); // add
错误“系统”似乎不可靠,有时有效,有时无效
至少这个问题:顺序检查与同时检查两个条件一起输入的有效性。
而不是
while(data_Input<0){
...
}
while(data_Input>100){
...
}
一起测试。
while(data_Input<0 || data_Input>100){
...
}
该程序仅检查最后一个数字条目,因此仅以最小和最大为单位显示最后一个数字
真正的代码一次调用max()
只会比较一次值。该函数仅将数字与0进行比较,而不是先前的值。 min()
也是如此。
考虑算法更改
// Pseudo code for max
prompt How many number would you like to type in ? : "
get times
max_value = -INF // set to minimum possible float
for each value 1 to times
prompt "Type in the number: "
get data_Input
if data_Input > max_value
max_value = data_Input
print max_value
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。