【发布时间】:2017-05-29 11:28:03
【问题描述】:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
float a;
float b;
float gr;
gr = (1 + sqrt(5)) / 2;
gr = floorf(gr*1000 + 0.5) / 1000;
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);
if(isalpha(a) || isalpha(b))
{
printf("\nInvalid input.\n");
exit(0);
}
float result = a/b;
if(floorf(result*1000+0.5)/1000 != gr)
{
float temp;
temp = a;
a= b;
b = temp;
result = a/b;
}
result = floorf(result*1000+0.5)/1000;
if(result == gr)
{
printf("\nGolden ratio!\n");
} else if(result != gr) {
printf("\nMaybe next time.\n");
}
return 0;
}
除了“if(isalpha(a) || isalpha(b))”部分外,一切都很好。 我想制作程序来检查用户输入是否是数字 但是当我运行它并输入a和b时,它会打印出“也许下次”, 不是“无效输入”... 任何帮助将不胜感激!
【问题讨论】:
标签: c validation input