- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我写了这个函数:
float checkInput1(void){
float option1,check1;
char c;
do{
printf("Enter the first side of the triangle: ");
if(scanf("%f%c",&option1,&c) == 0 || c != '\n'){
while((check1 = getchar()) != 0 && check1 != '\n' && check1 != EOF);
printf("\t[ERR] Invalid number for the triplet.\n");
}else{
break;
}
}while(1);
return option1;
}
但是,为了阻止用户输入字母等,我想扩展此验证功能,使用户无法输入 0 或负数。我怎么做?提前致谢
最佳答案
考虑使用fgets
进行输入并使用strtod
进行解析。除非有特殊原因,否则请使用 double
而不是 float
。使用最小值 -HUGE_VALF
和最大值 HUGE_VALF
会将 double 值限制为 float 范围。这使用 1.0e-10
的最小值来拒绝零和更小的值。有效的分隔符是"\n"
,但可以根据需要更改以允许逗号或其他分隔符。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
int parse_double ( char *line, char **next, char *delim, double *value, double min, double max) {
char *end = NULL;
double input = 0.0;
errno = 0;
input = strtod ( line, &end);
if ( ( errno == ERANGE && ( input == HUGE_VAL || input == -HUGE_VAL))
|| ( errno != 0 && input == 0.0)){
perror ( "input: ");
return 0;// failure
}
if ( end == line) {
line[strcspn ( line, "\n")] = '\0';
printf ( "input [%s] MUST be a number\n", line);
return 0;// failure
}
if ( *end != '\0' && !( delim && ( strchr ( delim, *end)))) {// is *end '\0' or is *end in delim
line[strcspn ( line, "\n")] = '\0';//remove trailing newline
printf ( "problem with input: [%s] \n", line);
return 0;// failure
}
if ( next != NULL) {// if next is NULL, caller did not want pointer to end of parsed value
*next = end;// *next allows modification to caller's pointer
}
if ( input < min || input > max) {
printf ( "out of range\n");
return 0;// failure
}
if ( value != NULL) {// make sure value is not NULL
*value = input;// *value allows modification to callers double
}
return 1;// success
}
int read_double ( double *dValue) {
char line[900] = {'\0'};
int valid = 0;
*dValue = 0.0;
do {
printf ( "Enter a number or quit\n");
if ( fgets ( line, sizeof ( line), stdin)) {//read a line
if ( strcmp ( line, "quit\n") == 0) {
return 0;//exit when quit is entered
}
valid = parse_double ( line, NULL, "\n", dValue, 1.0e-10, HUGE_VALF);
}
else {
return 0;//fgets failed
}
} while ( !valid);
return 1;
}
int main()
{
double dResult = 0.0;
if ( read_double ( &dResult)) {
printf ( "You entered %f\n", dResult);
}
return 0;
}
关于c - 如何防止用户在c中输入0或负数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47053160/
赏金:对于提供代码以使此子程序与负数一起工作的任何人,+50 信誉点。 我编写了一个 MIPS 程序来将华氏温度转换为摄氏温度。它打开自己的输出窗口(即 UART)并正确显示摄氏值。它在从 C 调用到
我得到了以下代码 # On va convertir en somme monétaire # We convert our integer into a way to read money
我得到了以下代码 # On va convertir en somme monétaire # We convert our integer into a way to read money
我使用以下 RegEx 基本上过滤掉任何文本,并接受数字 + 运算符。 ([-+]?[0-9]*\.?[0-9]+[\/\+\-\*])+([-+]?[0-9]*\.?[0-9]+) 所以它抓取 1+
我有一个查询,它计算我在查询中使用 union all 的平均值,以便获取我最终使用 max 函数的数据。 当联合返回结果时,如下所示:- col 1 col2 1 0
我有这样一个类: public class SpiralGenerator implements Iterator> { private void generate(int pos, E...
A = numpy.matrix([[36, 34, 26], [18, 44, 1], [11, 31, 41]]) X1 = numpy.matrix([[462
我有一个应用程序,其中有一个显示硬币 00 的 TextView ,一个按钮显示奖励视频广告,为用户提供 10 个硬币,还有一个购买按钮,将硬币减少 30 个。现在,当用户有 30 个硬币时,单击购买
话不多少,直接附上代码实例,仅供参考 ? 1
我有一系列正数和负数,我想将每个数字的绝对值增加一个,同时仍保持正数/负数。0.2 -> 1.2-0.3 -> -1.3我怎样才能做到这一点? 最佳答案 让我们尝试使用numpysign s=pd.S
我有这段代码,只允许在 keypress() 的输入字段中输入数字 if (e.which != 8 && e.which != 0 && (e.which 57)) { return fa
我试图用“-1”作为所有值填充二维数组。我正在使用的代码是: int c [] []=new int[4][4]; Arrays.fill(c,-1) 这会引发以下错误: Exception in t
在学校作业中,我们应该编写一个程序,该程序接受一个数字并将其分为三个部分:1. 检查数字是正数还是负数2. 整数(大小)3.小数部分 要求是应该有一个自己的函数,名为separate,具有输入和输出参
有没有什么方法可以在 C# 中执行整数除法(没有 float 或小数,我需要保持这个非常快)来向下舍入数字? 默认除法只是丢弃分数参数。考虑: 1 / 2 = 0 // That is correc
我正在使用 matplotlib 为报告生成图表,并指定我自己的样式表来指定文本格式以符合报告的指定文档样式。在我的 .mplstyle 样式表中,我按如下方式指定字体系列: font.family
在 C++11 中,如果我们尝试使用全局运算符 new 分配负大小的数组,它会抛出 std::bad_array_new_length,但是 C++98/C++03 呢?是 UB 还是会抛出 std:
我试过 scanf("%u",&number) 并且我输入了负数问题是当我 printf("%d",number) 我得到负数。我认为这会阻止我读取负数。scanf("%d",&number) 和 s
我的任务是解释一些看似奇怪的C代码行为(在x86上运行)。我可以轻松完成所有其他工作,但是这确实让我感到困惑。 代码段1输出-2147483648 int a = 0x80000000; int
js有问题吗? if("hello".indexOf("world")) { // I forgot to add > -1 here console.log("hello world");
我正在尝试使用 Antlr 4 设置一个简单的计算器。 语法: grammar calcGrammar; input : expression EOF; expression : MINUS
我是一名优秀的程序员,十分优秀!