int main(int argc, char *argv[])
{
int i, rc;
char elements[5];
char *ptr;
int array[2];
int array2[2];
int temp, temp2;
stack_elem_t elem;
stackADT mystack = NULL;
printf ("Enter five elements, operants and operators included.\n");
for (i=0; i<5; i++)
{
scanf ("%s", &elements[i]);
if (isdigit(elements[i]))
{
// array[i] = strtol (elements, &ptr , 0);
array[i] = elements[i] - '0';
push(mystack, array[i]);
}
if (!isdigit(elements[i]))
{
switch (elements[i])
{
case '+':
if (i < 2)
printf ("Not enough operators to perform addition operation!\n");
array2[0] = pop(mystack, &array[0]); // Output: 1
array2[1] = pop(mystack, &array[1]); // Output: 1
printf ("%d\n", array[0]); // Output: 3 (as I entered)
printf ("%d\n", array[1]); // Output: 4 (as I entered)
temp = (array[0]+array[1]);
push (mystack, temp);
printf ("%d\n", temp); // Output: 1
temp2 = pop (mystack, &temp);
printf ("%d\n",temp2); // Output: 1
break;
}
And MAIN problem is:
Now the problem is that this does not add the two integers. Why is this happening? Besides, it does not take anything in array2[0] and array2[1]. When I pop the elements array[0] and array[1] into array2[0] and array2[1] and print them out, only 1 is printed out. What could be the problem?
Further, when I add array[0] and array[1] into temp and print temp out, again, 1 is printed and nothing else. However, if I print out array[0] and array[1] separately, they are displayed as I enter them, i.e 3 and 4.
What's going on?


Sign In
Create Account


Back to top









