简体   繁体   English

C++中将数组的字符类型组织成单独的数组

[英]Organize character types of array into separate arrays in C++

I am trying to write a small program that takes a user-defined string of a limited selection of characters (lowercase letters, parentheses, and the + and * operators), looks at each of the characters, and organizes them into separate arrays.我正在尝试编写一个小程序,它采用用户定义的有限字符选择字符串(小写字母、括号以及 + 和 * 运算符),查看每个字符,并将它们组织成单独的数组。 I thought my approach to this exercise would be fairly straightforward, but I have run into some issues that I cannot figure out.我认为我对这个练习的方法会相当简单,但我遇到了一些我无法弄清楚的问题。

My problem becomes evident when I attempt to print the individual arrays to the screen.当我尝试将单个数组打印到屏幕时,我的问题变得明显。 If all of the characters are of the same type (for example, "abcd"), the arrays print as intended.如果所有字符都属于相同类型(例如,“abcd”),则数组按预期打印。 But if there is a combination of character types (for example, "(a+b)"), the arrays print incorrectly.但是,如果存在字符类型的组合(例如,“(a+b)”),则数组打印不正确。 I'm ashamed to say I have been banging my head against this (probably obvious) problem for many hours now and cannot seem to figure out what I have done wrong.我很惭愧地说,我已经在这个(可能很明显的)问题上挣扎了好几个小时,似乎无法弄清楚我做错了什么。 Any input would be appreciated - I'm not looking for help writing the program, I just want to know what I have done wrong.任何输入将不胜感激 - 我不是在寻求帮助编写程序,我只是想知道我做错了什么。 I have included my code below:我在下面包含了我的代码:

#include <iostream>
#include <string>

using namespace std;

void charOrganizer(int[], char[], char[], char[], int, int&, int&, int&);

int main()
{
//Variable declaration
string userExpression;
int expressionArray[userExpression.length()];
char letterToken[userExpression.length()];
char parenthesesToken[userExpression.length()];
char plusTimesToken[userExpression.length()];
int letterTokenPos = 0;
int parenthesesTokenPos = 0;
int plusTimesTokenPos = 0;

//Prompt user for string input
cout << "Please enter a mathematical expression only using lowercase letters of the \nalphabet, parentheses, and/or the addition/multiplication operators."<< endl;
cin >> userExpression;

int arraySize = userExpression.length();

for (int i = 0; i < arraySize; ++i)
{
    expressionArray[i] = userExpression[i];
}

charOrganizer(expressionArray, letterToken, parenthesesToken, plusTimesToken, arraySize, letterTokenPos, parenthesesTokenPos, plusTimesTokenPos);

//Print tokens to screen
cout << "LowerCase Letter Token values in your string:" << endl;
for (int i = 0; i < letterTokenPos; i++)
{
    cout << letterToken[i] << endl;
}

cout << "Parentheses Token values in your string:" << endl;
for (int i = 0; i < parenthesesTokenPos; i++)
{
    cout << parenthesesToken[i] << endl;
}

cout << "Operator Token values in your string:" << endl;
for (int i = 0; i < plusTimesTokenPos; i++)
{
    cout << plusTimesToken[i] << endl;
}

return 0;
}

void charOrganizer (int charValue[], char letArr[], char parArr[], char pluTimArr[], int size, int& letPosition, int&parPosition, int& operPosition)
{   
for (int i = 0; i < size; i++)
{
    if (charValue[i] > 96 && charValue[i] < 123)
    {
        letArr[letPosition] = charValue[i];
        cout << "Letter Copy Test: " << letArr[letPosition] << endl;
        letPosition++;
    }

    else if (charValue[i] == 40 || charValue[i] == 41)
    {
        parArr[parPosition] = charValue[i];
        cout << "Parentheses Copy Test: " << parArr[parPosition] << endl;
        parPosition++;
    }

    else if (charValue[i] == 42 || charValue[i] == 43)
    {
        pluTimArr[operPosition] = charValue[i];
        cout << "Operator Copy Test: " << pluTimArr[operPosition] << endl;
        operPosition++;
    }

    else
    {
        cout << "Invalid input" << endl;
    }
}

/*cout << "Print Array Test: " << endl;
for (int i = 0; i < letPosition; i++)
{
    cout << letArr[i] << endl;
    //cout << parArr[i] << endl;
    //cout << pluTimArr[i] << endl;
}*/
}

see your below code.看到你下面的代码。 You are using the length of a empty string to declare arrays size like below.您正在使用空字符串的长度来声明数组大小,如下所示。 In this case all the array size will zero size only.在这种情况下,所有数组大小将仅为零大小。 Correct it properly:-正确更正:-

//Variable declaration
string userExpression;
int expressionArray[userExpression.length()];// it would be just like int expressionArray[0];
char letterToken[userExpression.length()];
char parenthesesToken[userExpression.length()];
char plusTimesToken[userExpression.length()];

int expressionArray[30];
char letterToken[30];
char parenthesesToken[30];
char plusTimesToken[30];

Else you declare these variables after you enter your string.否则,您在输入字符串后声明这些变量。

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

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