- 115 名前:デフォルトの名無しさん mailto:sage [2009/02/01(日) 22:40:06 ]
- >>112 括弧を対応させる方法が思い出せない
#include <stdio.h> #include <stdlib.h> #include <ctype.h> char b[256], *p = b; double t(void){ double t = 0; while(1){ if(isspace(*p)) p++; else if(*p == '*'){ p++; t *= strtol(p, &p, 10);} else if(*p == '/'){ p++; t /= strtol(p, &p, 10);} else if(isdigit(*p)) t = strtol(p, &p, 10); else return t; } } double e(double r){ while(1){ if(isspace(*p)) p++; else if(*p == '\0') return r; else if(*p == '+'){ p++; r += t();} else if(*p == '-'){ p++; r -= t();} else r = t(); } } int main(int c, char *v[]){ FILE *in, *out; if(c < 3 || (in = fopen(v[1], "r")) == NULL || (out = fopen(v[2], "w")) == NULL) return 1; fscanf(in, "%255[^\n]", b); fprintf(out, "%g\n", e(t())); return fclose(in), fclose(out), 0; }
|

|