Created
April 7, 2015 02:47
-
-
Save dyama/8788746bde06a5763192 to your computer and use it in GitHub Desktop.
7 lines VTL interpretor (original)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
char c[80]; | |
int v['Z'-'A'] = {0}; | |
// 数値・値を解釈して取得 | |
int getval(char* p) | |
{ | |
char* q = p; | |
int a = (int)strtol(p, &p, 0); | |
if (q == p) { | |
p++; | |
return v[*q-'A']; | |
} | |
return a; | |
} | |
// 演算 | |
int expr(char* p) | |
{ | |
int a = getval(p); | |
for (;*p;) { | |
char op = *p++; | |
int b = getval(p); | |
if (op == '+') | |
a += b; | |
if (op == '-') | |
a -= b; | |
if (op == '*') | |
a *= b; | |
if (op == '/') | |
a /= b; | |
if (op == '=') | |
a = a==b; | |
if (op == '>') | |
a = a >= b; | |
if (op == '<') | |
a = a <= b; | |
} | |
return a; | |
} | |
#define DEBUG | |
int main() | |
{ | |
#ifdef DEBUG | |
int i; | |
#endif | |
printf("OK\n* "); | |
for (;gets(c);) { | |
if (c[1] == '=') { | |
if (*c == '?') { | |
printf("%d\n", expr(c + 2)); | |
} | |
else { | |
v[*c-’A’] = expr(c + 2); | |
} | |
} | |
printf("OK\n* "); | |
} | |
#ifdef DEBUG | |
for (i=0; i < 'Z' - 'A'; i++) | |
printf("v[%d] = %d\n", i, v[i]); | |
#endif | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment