C++ 如何解析用户';s输入每个字符并将其存储为字符串

C++ 如何解析用户';s输入每个字符并将其存储为字符串,c++,string,parsing,vector,char,C++,String,Parsing,Vector,Char,好的,我正在使用一个计算器程序,它接收用户输入(例如“(3+(4+12))),我需要解析用户的输入并将其存储在字符串数组中,但我在这样做时遇到了麻烦。我目前的代码是 void parseInput(string input) { vector <string> input_vector; for(int i = 0; i < input.size(); ++i) { if(isdigit(input.at(i)) == 0 && isdigit(i

好的,我正在使用一个计算器程序,它接收用户输入(例如“(3+(4+12))),我需要解析用户的输入并将其存储在字符串数组中,但我在这样做时遇到了麻烦。我目前的代码是

void parseInput(string input) {
 vector <string> input_vector;

 for(int i = 0; i < input.size(); ++i) {
    if(isdigit(input.at(i)) == 0 && isdigit(input.at(i + 1)) == 0) {
        ++i;
        input_vector.push_back((input.at(i) + input.at(i+1)));
    }
    else {
        input_vector.push_back(input.at(i));
    }
 }

 for(int i = 0; i < input_vector.size(); ++i) {
    cout << endl << input_vector[i];
 } 
}
void parseInput(字符串输入){
矢量输入;
对于(int i=0;i这里有一个解决方案(使用c++11):
#包括
#包括
#包括
#包括
int main(){
std::string const input=“(3+(4+12))”;
std::vector字符(input.length());
//将'input'的每个字符映射到'std::string'`
//使用该字符并将结果保存在
//“chars”向量中的对应位置:
std::transform(input.cbegin()、input.cend()、chars.begin(),
[](字符c){
//将'char'强制转换为'std::string'的方法之一:
返回std::string(1,c);
});
//为了确保它正常工作,它会打印
//生成的字符串:
对于(大小i=0;istd::cout答案是您需要将字符串拆分为令牌,我给出了一个示例,将4添加到12,使其成为16,但认为字符串没有任何括号,假设用户输入4+12,您需要添加它,您可以执行以下操作:

char string[10], nstr[10];
int p=0, a=0, b=0, op=0;
cin>>string; // input string
While (string[i]!='+' || string[i]!='-')
{
nstr[p]=string[i]; // copy the contents of string to nstr.
p++;
i++;
}// loop exits if the string[i] reaches to the operator (+/-*).
nstr[p]='\0';
a=atoi(nstr);// convert the string to integer.
op=i;// this will hold the position of array of the operator in the string.
i++;
p=0;
while (string[i]!='\0')
{
nstr[p]=string[i];// this will copy the contents of the string after the operator.
i++;
p++;
}
nstr[p]='\0';
b=atoi(nstr);
if (string[op]=='+')// check what the user want to do. Add/subtract/divide etc.
c=a+b;
else if (string[op]=='-')
c=a-b;

这个程序没有经过测试,但是可以工作,如果没有,那么在程序中使用逻辑,就像我在我的程序中所做的那样,这不会单独使用1和2,而是使用4和12,您可以键入更多字符,但限于long,我在这里使用int来获取atoi()的返回值,希望这对你有帮助。

如果你使用的是C++11,你只需在
push_back
表达式中添加一对额外的大括号,即
input_vector.push_back({input.at(i)})
std::string
有一个
初始化器列表
构造函数。问题是它仍然将12分为两个单独的字符。@looseyjoosey,因为您写的是需要将每个字符存储为字符串。
“12”
不是一个字符,它是一个
'1'
字符,后跟
'2'
字符。它们是独立的。这就是我的解决方案将它们分开的原因。为了避免这种情况,需要将表达式解析为标记。缩进非常可怕。
char string[10], nstr[10];
int p=0, a=0, b=0, op=0;
cin>>string; // input string
While (string[i]!='+' || string[i]!='-')
{
nstr[p]=string[i]; // copy the contents of string to nstr.
p++;
i++;
}// loop exits if the string[i] reaches to the operator (+/-*).
nstr[p]='\0';
a=atoi(nstr);// convert the string to integer.
op=i;// this will hold the position of array of the operator in the string.
i++;
p=0;
while (string[i]!='\0')
{
nstr[p]=string[i];// this will copy the contents of the string after the operator.
i++;
p++;
}
nstr[p]='\0';
b=atoi(nstr);
if (string[op]=='+')// check what the user want to do. Add/subtract/divide etc.
c=a+b;
else if (string[op]=='-')
c=a-b;