【问题标题】:using isDigit to find charecters c++使用 isDigit 查找字符 c++
【发布时间】:2025-11-29 05:45:01
【问题描述】:

试图弄清楚如何使用 isDigit 忽略字符串中除 x、X、e、E 之外的每个字符。正如您在下面看到的,我正在使用 x 等于 10 和 e 等于 11(不区分大小写)对十进制进行十进制。 cin.ignore() 遇到问题。输出应该是 36。字符串 duo 应该读入 3 然后 0 并否定其余部分。

#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>

using namespace std;

main() {
  using str_t = std::string::size_type;

  str_t idx = 0;
  int decValue = 0;

  string duo = "30-something";

  while (isspace(duo.at(idx)) && idx < duo.length()) {
    idx++;
  }

  for (std::string::size_type i = 0; i < duo.length(); ++i) {
    decValue *= 12;

    if (isdigit(duo.at(i))) {
      decValue += duo.at(i) - '0';
    }

    else if (duo.at(i) == 'x' || duo.at(i) == 'X') {
      decValue += 10;
    }

    else if (duo.at(i) == 'e' || duo.at(i) == 'E') {
      decValue += 11;
    }
    /// Program works if this executable line is taken out
    else if (!char.isDigit(duo.at(i))) {
      cin.ignore();
    }
  }
  cout << decValue << endl;
}

【问题讨论】:

  • 为什么要std::isdigit()?如果您只关心 X 和 E,那么检查它是 X 还是 E 不是更简单吗?您甚至可以使用std::tolower()std::toupper()(经销商的选择)来简化检查。
  • 为什么要ignorecin 没有其他用途。总而言之,这是一个令人困惑和困惑的问题。
  • 如果我理解正确,您应该将std::string 从base-12 转换为base-10?这是如何运作的? Base-12 应该是 0-9 + AB,对吧?
  • 终于找到了抛出错误的注释行,这是非常糟糕的语法。那条线的目标是什么,为什么它与您的其他检查如此不同?也许是复制/粘贴?
  • 回到问题的文本,您声称应该读入 3 并且所有其他内容都被否定,但是您的字符串中有 e,您声称您不想这样做忽视。那么它是什么?

标签: c++ loops char decimal


【解决方案1】:

下面我修改了您的代码,以便它给出正确的答案。对于输入30-something,它给出36 作为输出。

据我了解,您希望在 30 之后立即完成转换,即忽略 -something。对于这种行为,我在我的代码中放置了一个标志 stop_on_first_non_digit,如果它是 true,那么它会在第一个非数字字符上结束。但是您可以将其设置为 false 然后我只是跳过非数字字符但使用所有数字字符,例如 -somethinge 在它,所以包含一个数字,如果 stop_on_first_non_digitfalse那么将使用这个单一的e 数字。现在默认情况下是true,因此它会按照您喜欢的方式运行。

我还保留了您跳过字符串中第一个空格的逻辑,以便您可以输入 30-something(前导空格),它也给出36

Try it online!

#include <string>
#include <cctype>
#include <iostream>

int main() {
    std::string duo = "30-something";
    bool stop_on_first_non_digit = true;
    size_t i = 0;
    int decValue = 0;

    while (i < duo.length() && std::isspace(duo.at(i)))
        ++i;

    for (; i < duo.length(); ++i) {
        char c = duo.at(i);
        int digit = 0;
        if (std::isdigit(c))
            digit = c - '0';
        else if (c == 'x' || c == 'X')
            digit = 10;
        else if (c == 'e' || c == 'E')
            digit = 11;
        else {
            if (stop_on_first_non_digit)
                break;
            else
                continue;
        }
        decValue *= 12;
        decValue += digit;
    }
    std::cout << decValue << std::endl;
}

输出:

36

【讨论】:

  • 谢谢你,谢谢你的帮助。
  • @codingStudent702 如果我的回答是正确的和/或根据您的想法有用,那么不要忘记接受和/或支持它。它是在我左侧答案的最顶部完成的,有一个复选标记表示接受答案为正确,而上箭头表示 UpVoting 答案很有用。
  • 你能告诉我,如果数字前有一个字母,我怎么能选择忽略它。每当我用谷歌搜索如何忽略一个字符时,它都会返回 cin.ignore() 但我不确定如何正确地使用它。我不是在找人来给我答案我只是诚实地需要帮助,即使它“检查这个网站””,你知道我可以使用的任何东西都会受到赞赏,是的。
  • @codingStudent702 如果您在谈论相同的任务,那么在我的代码开头尝试设置变量stop_on_first_non_digit = false(设置为false)然后任何非数字字符都将被忽略。在这个任务中你根本没有任何std::cin,所以你不需要忽略它的字符。在循环中忽略字符的方法是将它们与可能的数字值进行比较,如果它是非数字的,那么只需在循环中执行 continue; 即可。循环上面的代码,我做了你想要的,我已经通过continue;跳过了非数字字符。
  • 这忽略了 OP 使用字母 E 和 X(包括小写)作为它们的额外数字这一事实。