Prefix function doesn't process numbers with more than one digit correctly
Feb 13, 2019 at 5:40am UTC
So my prefix takes a char array then get rid of the spaces reverses
it, then processes it. It only works for equations for numbers with single digits like 1 + 2 * 3. If I enter an equation with a double digit number like 10 + 1 it processes 10 as 0 and a 1. I'm not sure why that's happening. I had an idea of using a bool to check if theirs more than one digit but I'm not sure where to apply it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
void prefix(char * arr)
{
arr = getRideSpaces(arr);
cout << "No spaces" << arr << endl;
arr = reverse(arr);
cout << arr << endl;
arr = parenthesisCheck(arr);
cout << arr << endl;
stackADT<char > operators;
QueueADT<char > operands;
int count = 0;
int pCount =0, toQueue;
for (int i = 0; i < strlen(arr); i++)
{
switch (arr[i])
{
case '(' :
operators.push(arr[i], count);
count++;
pCount++;
break ;
case ')' :
toQueue = pCount - 1;
for (int i = 1; pCount >0; i++)
{
cout << "this is pCount\t" << pCount << endl;
operands.enqueue(arr[i]);
operators.pop(count - i);
pCount--;
}
count--;
operators.peek();
break ;
case '%' :
operators.push(arr[i], count);
count++;
if (pCount > 0)
{
pCount++;
}
break ;
case '*' :
operators.push(arr[i], count);
count++;
if (pCount > 0)
{
pCount++;
}
break ;
case '/' :
operators.push(arr[i], count);
count++;
if (pCount > 0)
{
pCount++;
}
break ;
case '+' :
operators.push(arr[i], count);
count++;
if (pCount > 0)
{
pCount++;
}
break ;
case '-' :
operators.push(arr[i], count);
count++;
if (pCount > 0)
{
pCount++;
}
break ;
}
if (isdigit(arr[i]))
{
operands.enqueue(arr[i]);
}
}
cout << "TESTING THE STACK\n" ;
operators.peek();
cout << "TESTING THE ENQUEUE\n" ;
operands.display2();
//cout<<arr<<endl;
}
char * parenthesisCheck(char *arr)
{
for (int i = 0; i < strlen(arr); i++)
{
if (arr[i] == '(' )
{
arr[i] = ')' ;
}
else if (arr[i] == ')' )
{
arr[i] = '(' ;
}
}
return arr;
}
char * reverse(char * arr)
{
char temp;
int length = strlen(arr);
for (int i = 0; i < length / 2; i++)
{
temp = *(arr + i);
*(arr + i) = *(arr + (length - (i + 1)));
*(arr + length - (i + 1)) = temp;
}
return arr;
}
char * getRideSpaces(char arr[])
{
char *ptr = new char [10];
int i = 0, j = 0;
while (arr[i])
{
if (arr[i] != ' ' )
{
arr[j++] = arr[i];
}
i++;
}
//cout << arr[0] << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n";
arr[j] = '\0' ;
return arr;
}
Last edited on Feb 13, 2019 at 5:40am UTC
Feb 13, 2019 at 2:06pm UTC
1 2 3 4
if (isdigit(arr[i]))
{
operands.enqueue(arr[i]);
}
Well, it looks like you're separately enqueuing each digit, so it sounds like expected behavior to me.
If you have a QueueADT<char>, then you can only put chars in there, not full multi-digit numbers (or at least, not numbers > 255 if you interpret the values correctly).
I would instead have a Queue of ints, and enqueue each integer.
Also, are you allowed to use std::strings?
Last edited on Feb 13, 2019 at 2:09pm UTC
Feb 13, 2019 at 2:37pm UTC
Line 117: You have a memory leak. ptr is never deleted. In fact, ptr is never used.
Topic archived. No new replies allowed.