#include <stdio.h>
struct precedence
{
char op;
int prec;
} precendence[] =
{ { '+', 1 },
{ '-', 1 },
{ '*', 2 },
{ '/', 2 },
{ '^', 3 },
{ '%', 4 },
{ 0, 0 }};
int compare(char *a, char *b)
{
int prec_a = 0, prec_b = 0, i;
for(i=0; precendence[i].op && (!prec_a || !prec_b); i++)
{
if (a == precendence[i].op)
prec_a = precendence[i].prec;
if (b == precendence[i].op)
prec_b = precendence[i].prec;
}
if (!prec_a || !prec_b)
{
fprintf(stderr,"Could not find operator %c and/or %c\n",a,b);
return(-2);
}
if (prec_a < prec_b)
return -1;
if (prec_a == prec_b)
return 0;
return 1;
}
main()
{
char a,b;
a='+'; b='-'; printf("Prec %c %c is %d\n", a,b,compare(a,b));
a='+'; b='*'; printf("Prec %c %c is %d\n", a,b,compare(a,b));
a='+'; b='^'; printf("Prec %c %c is %d\n", a,b,compare(a,b));
a='+'; b='%'; printf("Prec %c %c is %d\n", a,b,compare(a,b));
a='*'; b='+'; printf("Prec %c %c is %d\n", a,b,compare(a,b));
a='^'; b='+'; printf("Prec %c %c is %d\n", a,b,compare(a,b));
a='%'; b='+'; printf("Prec %c %c is %d\n", a,b,compare(a,b));
}