Solved

# insert parentheses to an arithmetic expression

Posted on 2004-10-31

I wrote a program that would input a arithmetic expression that would be inputed into an array and a give an output. This how it is suppost to do:

Input:

5+3/-2

Output:

5+3/(-2)

5+(3/(-2))

(5+(3/(-2)))

The problem is the program I wrote below doesn't do that?:

#include <stdio.h>

#define SIZE 20

int main()

{

int i = 0 ;

int a[SIZE];

int b[40];

int inext =i + 1;

char operator1= '-', operator2 = '+',operator3 = '*',operator4 = '/';

printf("-------------------------------------------------------------------\n");

printf(" Welcome to the Arithmetic Expression Evaluator! \n ");

printf("-------------------------------------------------------------------\n");

printf(" Give me an arithmetic expression and I will insert the \n");

printf(" appropriate parentheses per the C language's arithmetic \n");

printf(" operator precedence and associativity rules. \n ");

printf("Expression:\n");

while((a[SIZE]=getchar()): = '\n');

printf("%d : the size of input.\n", SIZE);

for(i=0; i<SIZE; i++)

{

b[i] = a[i];

}

//operator1

for(i =0; i<SIZE; i++)

{ if(a[i] == operator1)

{

b[0] = '(';

b[1] = a[i];

b[2] = a[inext];

b[3] = ')';

} // end of if

printf("%s", a[SIZE]);} // end of for

//operator2

for(i =0; i<SIZE; i++)

{ if(a[i] == operator2)

{

b[0] = '(';

b[1] = a[i];

b[2] = a[inext];

b[3] = ')';

} // end of if

printf("%s", a[SIZE]);} // end of for

//operator3

for(i =0; i<SIZE; i++)

{ if(a[i] == operator2)

{

b[0] = '(';

b[1] = a[i];

b[2] = a[inext];

b[3] = ')';

} // end of if

printf("%s", a[SIZE]);} // end of for

//operator4

for(i =0; i<SIZE; i++)

{ if(a[i] == operator2)

{

b[0] = '(';

b[1] = a[i];

b[2] = a[inext];

b[3] = ')';

} // end of if

printf("%s", a[SIZE]);} // end of for

printf("\n%c", a[i]);

if(a[i]!=operator1 && a[i] !=operator2 && a[i] !=operator3 && a[i] !=operator4)

{ ;

} // end of if

return 0;

}