如何检查字符串包含有效的实数(How to check a string contains a valid real number)

编程入门 行业动态 更新时间:2024-10-18 22:29:29
如何检查字符串包含有效的实数(How to check a string contains a valid real number)

我需要检查第二个命令行参数,看看它是否是一个有效的浮点数。

#include <stdio.h> #include <math.h> long int fact(int n) { if (n >= 1) return n*fact(n - 1); else return 1; } double func(int x0, int n) { int p = 2 * n + 1; return (double)pow(x0, p) / (double)fact(p); } double sinh_(int x0, int N) { double sum = 0; int i = 0; for (i = 0; i <= N; i++) { sum = sum + func(x0, i); } return sum; } double err(int x0, int N) { return abs(sinh(x0) - sinh_(x0, N)); } int main(int argc, char**argv) { double x0 = 0; int N = 0; printf("No of args %d\n", argc); //if CLA doesn't have exactly 4 arguments passed return -1 //1st argument is app-name //2nd argument is x0 //4th argument is N if (argc >= 3) { printf("Err: no of arguments should be 2"); return -1; } //if the 2nd CLA is not a number, return -1 if (!isdigit(atof(argv[1]))) { printf("1st argument must be a real value"); return -1; } //if the 3rd CLA is not a number, return -1 if (!isdigit(atoi(argv[2]))) { printf("2nd argument must be an int value"); return -1; } x0 = atof(argv[1]); N = atoi(argv[2]); printf("Builtin sinh = %f", sinh(x0)); printf("Approx sinh = %f", sinh_(x0, N)); printf("Err value = %f", err(x0, N)); }

I need to check the 2nd command line argument to see if it's a valid floating point number.

#include <stdio.h> #include <math.h> long int fact(int n) { if (n >= 1) return n*fact(n - 1); else return 1; } double func(int x0, int n) { int p = 2 * n + 1; return (double)pow(x0, p) / (double)fact(p); } double sinh_(int x0, int N) { double sum = 0; int i = 0; for (i = 0; i <= N; i++) { sum = sum + func(x0, i); } return sum; } double err(int x0, int N) { return abs(sinh(x0) - sinh_(x0, N)); } int main(int argc, char**argv) { double x0 = 0; int N = 0; printf("No of args %d\n", argc); //if CLA doesn't have exactly 4 arguments passed return -1 //1st argument is app-name //2nd argument is x0 //4th argument is N if (argc >= 3) { printf("Err: no of arguments should be 2"); return -1; } //if the 2nd CLA is not a number, return -1 if (!isdigit(atof(argv[1]))) { printf("1st argument must be a real value"); return -1; } //if the 3rd CLA is not a number, return -1 if (!isdigit(atoi(argv[2]))) { printf("2nd argument must be an int value"); return -1; } x0 = atof(argv[1]); N = atoi(argv[2]); printf("Builtin sinh = %f", sinh(x0)); printf("Approx sinh = %f", sinh_(x0, N)); printf("Err value = %f", err(x0, N)); }

最满意答案

strto...函数系列,如将字符串转换为double strtod和将字符串转换为long int strtol ,可以为您提供有关转换结束的字符串中的行踪的信息。

如果字符串不以可选空格开头,后跟有效数字,则它们将报告在字符串开头结束的转换。 否则,他们将报告导致转换结束的角色的位置。 因此,要检查错误,请检查位置是否已从字符串的开头“移动”,并检查报告的结束位置处的字符。

这些函数还会检查超出范围的值,将errno为ERANGE并返回特定值。

下面的代码检查字符串在数字后面是否以空终止符结尾,但在某些情况下,对于包含多个分隔值的字符串,您可能需要检查有效的分隔字符。

#include <stdio.h> #include <stdlib.h> #include <errno.h> int main(int argc, char **argv) { double x0; long int N; char *endptr; if (argc != 3) { fprintf(stderr, "usage: %s x0 N\n", argv[0]); return 2; } errno = 0; x0 = strtod(argv[1], &endptr); if (endptr == argv[1] || *endptr != '\0') { fprintf(stderr, "1st argument must be a real value\n"); return 2; } if (errno == ERANGE) { fprintf(stderr, "1st argument out of range\n"); return 2; } errno = 0; N = strtol(argv[2], &endptr, 10); if (endptr == argv[2] || *endptr != '\0') { fprintf(stderr, "2nd argument must be an integer value\n"); return 2; } if (errno == ERANGE) { fprintf(stderr, "2nd argument out of range\n"); return 2; } printf("x0:%g; N:%ld\n", x0, N); return 0; }

为方便起见,我将N的类型更改为long int ,因为没有返回int的strtoi函数。

The strto... family of functions such as strtod to convert a string to a double and strtol to convert a string to a long int can give you information about whereabouts in the string the conversion ended.

If the string does not start with optional whitespace followed by a valid number, they will report the conversion ended at the beginning of the string. Otherwise they will report the position of the character that caused the conversion to end. So to check for error, check the position has "moved on" from the beginning of the string, and check the characters at the reported end position.

The functions also check for out-of-range values, setting errno to ERANGE and returning specific values.

The code below checks that the string ends with a null terminator after the number, but in some circumstances, for a string containing multiple separated values, you might want to check for a valid separation character.

#include <stdio.h> #include <stdlib.h> #include <errno.h> int main(int argc, char **argv) { double x0; long int N; char *endptr; if (argc != 3) { fprintf(stderr, "usage: %s x0 N\n", argv[0]); return 2; } errno = 0; x0 = strtod(argv[1], &endptr); if (endptr == argv[1] || *endptr != '\0') { fprintf(stderr, "1st argument must be a real value\n"); return 2; } if (errno == ERANGE) { fprintf(stderr, "1st argument out of range\n"); return 2; } errno = 0; N = strtol(argv[2], &endptr, 10); if (endptr == argv[2] || *endptr != '\0') { fprintf(stderr, "2nd argument must be an integer value\n"); return 2; } if (errno == ERANGE) { fprintf(stderr, "2nd argument out of range\n"); return 2; } printf("x0:%g; N:%ld\n", x0, N); return 0; }

I changed the type of N to long int for convenience because there isn't a strtoi function that returns an int.

更多推荐

本文发布于:2023-08-06 19:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455740.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实数   字符串   check   string   real

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!