我正在尝试编写一个小程序,它接受用户定义的有限字符串(小写字母,括号和+和*运算符)的字符串,查看每个字符,并将它们组织成单独的阵列。我认为我对这个练习的方法相当简单,但我遇到了一些我无法弄清楚的问题。
当我尝试将各个阵列打印到屏幕时,我的问题变得明显。如果所有字符都是相同类型(例如," abcd"),则阵列按预期打印。但是如果存在字符类型的组合(例如,"(a + b)"),则阵列打印不正确。我很惭愧地说我现在已经在这个(可能是显而易见的)问题上敲了几个小时,而且似乎无法弄清楚我做错了什么。任何输入都会受到赞赏 - 我不是在寻找编写程序的帮助,我只是想知道我做错了什么。我在下面提供了我的代码:
#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;
}*/
}
答案 0 :(得分:-1)
请参阅以下代码。您正在使用空字符串的长度来声明数组大小,如下所示。在这种情况下,所有数组大小仅为零大小。正确纠正: -
//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];
另外,在输入字符串后声明这些变量。