为什么这个函数不分解这个输入字符串? [英] Why is this function not breaking up this input string?

查看:34
本文介绍了为什么这个函数不分解这个输入字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 C++ 将字符串分解为符号"以进行进一步的工作.我已经很久没有用 C++ 写过任何东西了,所以如果这段代码有本质上的错误,请原谅我.

I'm trying to break up a string into "symbols" with C++ for further work. I haven't written anything in C++ for a long while, so forgive me if there is something inherently wrong with this code.

下面的 symbolize() 函数的目的是将一个字符串,例如5+5",分解成一个字符串的 vector,例如 {"5","+","5"}.它不起作用.如果你觉得代码太乱,请提出一个简化的方法.

The purpose of the symbolize() function below is to break up a string, such as "5+5", into a vector of strings, eg {"5","+","5"}. It's not working. If you think the code is too messy, please suggest a way to simplify it.

这是我目前的代码:

#include <iostream>
#include <string>
#include <vector>
#include <ctype.h>
#include <sstream>

using namespace std;

vector<string> symbolize(string);

int main(int argc, const char * argv[])
{

    string input;
    cin >> input;

    vector<string> symbols;

    symbols = symbolize(input);

    for(int i=0;i<symbols.size();i++){
        cout<<symbols.at(i) << endl;
    }

    return 0;
}


vector<string> symbolize(string input){
    int position = 0;
    char c;
    stringstream s;
    vector<string> symbols;
    enum symbolType {TEXT,OPERATOR}symbolType,charType;

    while(position < input.size()){
        c = input.at(position);
        if(isalnum(c))symbolType = TEXT;
        else symbolType = OPERATOR;
        charType = symbolType;

        while(symbolType == charType){
            s << c;
            position++;
            if(position>=input.length())break;
            c = input.at(position);
            if(isalnum(c)) charType = TEXT;
            else charType = OPERATOR;
        }

        symbols.push_back(s.str());
        s.clear();
    }

    return symbols;
}

感谢您的浏览.

顺便说一句,我应该提到该函数返回第一个令牌",例如5+5"->5"

BTW, I should mention that the function returns the fist "token", eg "5+5" -> "5"

Edit2:我错了.我刚刚尝试了5+5",它返回了 {"5","5+","5+5"}.但是,它只返回空格前的第一个.抱歉造成混乱!

I was mistaken. I just tried "5+5", and it returned {"5","5+","5+5"}. However, it only returns the first before a space. Sorry for the confusion!

Edit3:谢谢大家!对于那些将来可能会遇到此页面的人,这里是所有事情都已完成的代码:

Thank you all! For those who may come across this page in the future, here's the code when everything's said and done:

#include <iostream>
#include <string>
#include <vector>
#include <ctype.h>
#include <sstream>

using namespace std;

vector<string> symbolize(string);

int main(int argc, const char * argv[])
{

    string input;
    getline(cin,input);

    vector<string> symbols;

    symbols = symbolize(input);

    for(int i=0;i<symbols.size();i++){
        cout<<symbols.at(i) << endl;
    }

    return 0;
}


vector<string> symbolize(string input){
    int position = 0;
    char c;
    //stringstream s;
    vector<string> symbols;
    enum symbolType {TEXT,OPERATOR}symbolType,charType;

    while(position < input.size()){
        stringstream s;
        c = input.at(position);
        if(isalnum(c))symbolType = TEXT;
        else symbolType = OPERATOR;
        charType = symbolType;

        while(symbolType == charType){
            s << c;
            position++;
            if(position>=input.length())break;
            c = input.at(position);
            if (isspace(c)||c=='\n'){position++; break;}
            if(isalnum(c)) charType = TEXT;
            else charType = OPERATOR;
        }

        symbols.push_back(s.str());
    }

    return symbols;
}

推荐答案

如果您想阅读整行而不是一个单词,请使用 getline 而不是 operator>>.请参阅 http://www.cplusplus.com/reference/string/getline/详细信息,或者只是将第 14 行更改为getline(cin, input);".

If you want to read an entire line instead of just one word, use getline instead of operator>>. See http://www.cplusplus.com/reference/string/getline/ for details, or just change line 14 to "getline(cin, input);".

另外,如果你想输出5"、+"、5"而不是5"、5+"、5+5",你需要在每次循环时重置stringstream,而 clear 不会那样做.解决这个问题的最简单方法是在外循环中声明 stringstream 并去掉 clear 调用.

Also, if you want to output "5", "+", "5" instead of "5", "5+", "5+5", you need to reset the stringstream each time through the loop, and clear doesn't do that. The simplest way around this is to just declare the stringstream in the outer loop and get rid of the clear call.

这篇关于为什么这个函数不分解这个输入字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆