将字符串转换为 int (但前提是确实是 int)

发布于 2024-08-17 07:23:52 字数 418 浏览 7 评论 0原文

在大学里,我被问到我们的程序是否检测到从命令行参数输入的字符串是否为整数,但它没有检测到(./Program 3.7)。现在我想知道如何检测到这一点。因此,例如 a 的输入在 atoi 检测到时是无效的,但是像 3.6 这样的输入应该是无效的,但 atoi 会将其转换为整数。

#include <stdio.h>

int main(int argc, char *argv[]) {
    if (argc > 1) {
        int number = atoi(argv[1]);
        printf("okay\n");
    }
}

但当然,只有当 argv[1] 确实是一个整数时才应该打印。希望我的问题很清楚。非常感谢。

In college I was asked if our program detects if the string enter from command line arguments is a integer which it did not(./Program 3.7). Now I am wondering how I can detect this. So input as for example a is invalid which atoi detects, but input like for example 3.6 should be invalid but atoi will convert this to an integer.

#include <stdio.h>

int main(int argc, char *argv[]) {
    if (argc > 1) {
        int number = atoi(argv[1]);
        printf("okay\n");
    }
}

But offcourse okay should only be printed if argv[1] is really an integer. Hope my question is clear. Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我不是你的备胎 2024-08-24 07:23:52

看看 strtol

如果 endptr 不为 NULL,strtol() 会将第一个无效字符的地址存储在 *endptr 中。但是,如果根本没有数字,strtol() 会将 str 的原始值存储在 *endptr 中。 (因此,如果返回时 *str 不是 \0' 但 **endptr 是\0',则整个字符串有效。)

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
  if (argc > 1) {
    char* end;
    long number = strtol(argv[1], &end, 0);
    if (*end == '\0')
      printf("okay\n");
  }
}

Have a look at strtol.

If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, however, strtol() stores the original value of str in *endptr. (Thus, if *str is not \0' but **endptr is\0' on return, the entire string was valid.)

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
  if (argc > 1) {
    char* end;
    long number = strtol(argv[1], &end, 0);
    if (*end == '\0')
      printf("okay\n");
  }
}
我喜欢麦丽素 2024-08-24 07:23:52

假设您想知道如何在代码中完成它(如果它确实是家庭作业,则可能),一种方法是考虑什么构成了字符串的整数。它很可能是:

  • 可选符号,+/-。
  • 所需的数字。
  • 任意数量的可选数字(但要注意溢出)。
  • 字符串的末尾。

根据该规范,您可以编写一个函数来为您完成这项工作。

像这样的伪代码将是一个好的开始:

set sign to +1.
set gotdigit to false.
set accumulator to 0.
set index to 0.
if char[index] is '+':
    set index to index + 1.
else:
    if char[index] is '-':
        set sign to -1.
        set index to index + 1.
while char[index] not end-of-string:
    if char[index] not numeric:
        return error.
    set accumulator to accumulator * 10 + numeric value of char[index].
    catch overflow here and return error.
    set index to index + 1.
    set gotdigit to true.
if not gotdigit:
    return error.
return sign * accumulator.

Assuming you want to know how it could be done in code (possible if it is indeed homework), one way is to think about what constitutes an integer in terms of the string. It would most likely be:

  • an optional sign, +/-.
  • a required digit.
  • any number of optional digits (but watch out for overflow).
  • the end of string.

From that specification, you can write a function that will do the work for you.

Something like this pseudo-code would be a good start:

set sign to +1.
set gotdigit to false.
set accumulator to 0.
set index to 0.
if char[index] is '+':
    set index to index + 1.
else:
    if char[index] is '-':
        set sign to -1.
        set index to index + 1.
while char[index] not end-of-string:
    if char[index] not numeric:
        return error.
    set accumulator to accumulator * 10 + numeric value of char[index].
    catch overflow here and return error.
    set index to index + 1.
    set gotdigit to true.
if not gotdigit:
    return error.
return sign * accumulator.
孤独岁月 2024-08-24 07:23:52
int okay = argc>1 && *argv[1];
char* p = argv[1];
int sign = 1;
int value = 0;
if( *p=='-' ) p++, sign=-1;
else if( *p=='+' ) p++;
for( ; *p; p++ ) {
    if( *p>='0' && *p<='9' ) {
        value = 10*value + *p-'0';
    } else {
        okay = 0;
        break;
    }
}
if( okay ) {
    value *= sign;
    printf( "okay, value=%d\n", value );
}

编辑:允许 - 和 + 字符

您甚至可以将其压缩为密集的一行或两行。或者您可能会找到具有相同功能的库函数;)

编辑2:只是为了好玩 - 它现在应该解析数字

int okay = argc>1 && *argv[1];
char* p = argv[1];
int sign = 1;
int value = 0;
if( *p=='-' ) p++, sign=-1;
else if( *p=='+' ) p++;
for( ; *p; p++ ) {
    if( *p>='0' && *p<='9' ) {
        value = 10*value + *p-'0';
    } else {
        okay = 0;
        break;
    }
}
if( okay ) {
    value *= sign;
    printf( "okay, value=%d\n", value );
}

EDIT: allow - and + characters

You may even compress this into a dense one- or two-liner. Or you may find a library function with the same functionality ;)

EDIT2: just for fun - it should now parse the number

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文