简体   繁体   English

寻找一个“数字”

[英]Finding a 'Number'

In my code I'm trying to find "numbers", "identifiers", and "words". 在我的代码中,我试图找到“数字”,“标识符”和“单词”。 Numbers are defined as sequences of numbers that are separated by a letter , non-letter, or a non digit character( such as a space or /n). 数字定义为由字母,非字母或非数字字符(例如空格或/ n)分隔的数字序列。

Such as: 898A111 (This has two numbers) 898 111 (This also has two numbers) :898A111(这有两个数字)898 111(这也有两个数字)

Words are defined as a letter leading a sequence of numbers, letters, or both. 单词定义为以数字,字母或两者开头的字母。

Such as: AJKALJ8923 or ALSJOIA or B93082092 (These are all considered words) 例如: AJKALJ8923或ALSJOIA或B93082092 (这些均被视为单词)

And Identifiers are the letters used to lead a word, or separate two numbers 标识符是用来引出单词的字母,或分隔两个数字

Such as: 如:

898A111 (The Identifier is A) 898A111 (标识符为A)
AJLKAKA (The Identifier is A) AJLKAKA (标识符为A)

I've been trying to scribble out possible solutions, and as far as checking words I believe I have a solution, but as far as counting and identifying both "numbers" and "identifiers" (in a string), I'm at a complete loss. 我一直在尝试找出可能的解决方案,就检查我认为可以解决的单词而言,但就计数和识别“数字”和“标识符”(以字符串形式)而言,我处于完全损失。 Anyone have any ideas? 有人有想法么? Any help at all would be appreciated. 任何帮助将不胜感激。 I'd say my knowledge of C++ is at a beginner's level. 我会说我的C ++知识是初学者的水平。

Main function: http://pastebin.com/MrXKLXYv 主要功能: http//pastebin.com/MrXKLXYv
Header file: http://pastebin.com/Xn23zn7X 头文件: http : //pastebin.com/Xn23zn7X
Assignment for reference if I was unclear: http://pastebin.com/2bgEPqbG 如果不清楚,可提供参考作业: http : //pastebin.com/2bgEPqbG

You can use regex to match the format you want. 您可以使用正则表达式来匹配所需的格式。 But as you are using C++, you must download Boost first, and include the head file "boost/regex.hpp" 但是,在使用C ++时,必须先下载Boost,并包含头文件“ boost / regex.hpp”

Maybe you can use std::bitset to handle this situation. 也许您可以使用std::bitset处理这种情况。

#include <bitset>

std::string strInput = "898A111";
std::string strDigit = "0123456789";
std::bitset<255> bsDigit;
std::vector<std::string> vctDigit;
for (int i = 0; i < strDigit.length(); i++)
{
    bsDigit[strDigit.at(i)] = true;
}

int nTemp = 0;
int nLength = strInput.length();
for (int i = 0; i < nLength; i++)
{
    if (!bsDigit[strInput.at(i)])
    {
        vctDigit.push_back(strInput.substr(nTemp, i - nTemp));
        nTemp = i + 1;
    }
    else if (i == nLength - 1)
    {
        vctDigit.push_back(strInput.substr(nTemp, (i + 1) - nTemp));
    }
}

std::vector<std::string>::iterator it = vctDigit.begin();
for (; it != vctDigit.end(); it++)
{
    std::cout << (*it).c_str() << std::endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM