V7: A much more sophistimacated doohickey
v7: A much more sophistimacated doohickey
pointers and dynamic allocation replacing the limited clumsy array 20060214
#include
#include
#include
#include
struct element
{ char op;
int number;
element * left, * right;
element(char p, element * a, element * b): op(p), left(a), right(b) { }
element(char p, int a): op(p), number(a) { } };
struct variable
{ char name[10];
int value; };
variable environment[100];
int nvars=1, maxvars=100;
int findvar(char s[])
{ for (int i=1; i=maxvars)
{ printf("Error: too many variables\n");
return 0; }
strcpy(environment[nvars].name, s);
nvars+=1;
return nvars-1; }
void getlexeme(char s[])
{ int c=getchar(), length=0;
while (c==' ') c=getchar();
if (c>='a' && c='A' && c='a' && c='A' && cnumber);
else if (e->op=='V')
printf("%s", environment[e->number].name);
else
{ printf("(");
print(e->left);
printf("%c", e->op);
print(e->right);
printf(")"); } }
int evaluate(element * e)
{ if (e->op=='N')
return e->number;
else if (e->op=='V')
return environment[e->number].value;
else if (e->op=='=')
{ element * var=e->left;
if (var->op!='V')
{ printf("\nError: assigning to non-variable\n");
return 0; }
int b=evaluate(e->right);
environment[var->number].value=b;
return b; }
else if (e->op==';')
{ int a=evaluate(e->left);
int b=evaluate(e->right);
return b; }
else
{ int a=evaluate(e->left);
int b=evaluate(e->right);
switch (e->op)
{ case '+': return a+b;
case '-': return a-b;
case '*': return a*b;
case '/':
{ if (b==0)
{ printf("\nError: Division by zero\n");
return 0; }
return a/b; }
case '^': return (int)(pow(a, b));
default: { printf ("\nError: bad operator %c\n", e->op);
return 0; } } } }
void main(void)
{ strcpy(environment[0].name, "error");
element * formula=parse();
if (formula!=0)
print(formula);
printf("\n= %d\n", evaluate(formula)); }
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- a more perfect union
- much more thesaurus
- more importantly in a sentence
- more words for a thing
- a word for more and more
- so much more synonym
- how much more meaning in math
- 3 more than a number in math
- a more professional word for amazing
- and so much more synonym
- bob had been with a meat packing plant for more than 26 years when suddenly tha
- a more conservative home page