English 中文(简体)
如果扼杀是一种正面的、非零的,我怎么能检查?
原标题:How Can I Check if a string represents an positive, non-zero int?

我无需知道WHAT的隐蔽性,我只需要知道它是否在Decimal代表中是一个积极的、非零的无动于衷的代表,没有领导零。

它将要求大量记录,因此,我喜欢尽可能少地进行检查。

期望的行为是,该制度绝不应采用某种确实有效的手段(因为该系统通常会通过它转换为储存地的帐篷),因此这只是最后的安全检查,以确保不会发生任何意外事件。

最佳回答

虽然你实际上可以走到把它变为暗中的过程,但我的假设是,你真的想要知道的是,它的所有特性是否都是数字?

不幸的是,即便如此,也别无选择,但确实有条不紊地穿透,尽管这应比首先转换为愤怒者更快。

• 利用STL,你可以使用以下 st子:

template<typename FwdIter>
bool all_digits( FwdIter start, FwdIter end )
{
   return std::find( start, end, std::not1(::isdigit) ) == end;
}

当然,你只能写一 lo。

template<typename FwdIter>
bool all_digits( FwdIter start, FwdIter end )
{
   for( ; start != end; ++start )
   {
     if( !::isdigit( *start ) )
        return false;
   }
   return true;
}

如果显示投入数量是肯定的,那么这不会完全告诉你,因为它们可能都是零,而扼杀可能是空的。 我们可以很容易地将这种内容列入 lo版。

template<typename FwdIter>
bool is_positive_int( FwdIter start, FwdIter end )
{
   bool foundNonZero;
   for( ; start != end; ++start )
   {
     if( !::isdigit( *start ) )
        return false;
     if( *start >  0  ) // it must be a digit
        foundNonZero = true;
   }
   return foundNonZero;
}

假设:

  • You may have leading zeros (but there must be at least one non-zero in there) so 0234 is a valid positive number
  • No whitespace allowed
问题回答
  • first character must be one of 1 through 9
  • all other characters must be one of 0 through 9

Check the first, loop over the rest, easy-peasy.

这确实非常简单,但它取决于要求/允许的投入:

  • what representation/basis are allowed: binary, octal, hexadecimal? (the main difference is made by hex, your digits set is different)
  • are leading and trailing whitespace allowed (your input is a string, you should not disregard this unless you are promised it will not happen)
  • are leading zeros allowed?
  • is leading + allowed?

根据上述情况,您可能需要:

  • strip leading and trailing whitespace
  • scan the string checking that it contains only the allowed digits and a +
  • verify that it contains at least one non-zero digit

问题也是你需要确认数字,例如: 输入<代码>123+456

如果需要,你可以撰写一份简单的国家机器。

http://www.ohchr.org。

如果我理解你的要求,一个子系统将一些积极因素转化为扼杀,而最终又进入另一个子系统,希望证实这些投入没有发生。 如果你知道你直系的转换,那么任务就容易得多:假定不会有白天空间,不会有头零,也不会有领先的<条码>+<>>/代码”。





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签