tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
|
|
@ -0,0 +1,62 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
void die(const char *msg)
|
||||
{
|
||||
fprintf(stderr, "%s", msg);
|
||||
abort();
|
||||
}
|
||||
|
||||
#define MAX_D 256
|
||||
double stack[MAX_D];
|
||||
int depth;
|
||||
|
||||
void push(double v)
|
||||
{
|
||||
if (depth >= MAX_D) die("stack overflow\n");
|
||||
stack[depth++] = v;
|
||||
}
|
||||
|
||||
double pop()
|
||||
{
|
||||
if (!depth) die("stack underflow\n");
|
||||
return stack[--depth];
|
||||
}
|
||||
|
||||
double rpn(char *s)
|
||||
{
|
||||
double a, b;
|
||||
int i;
|
||||
char *e, *w = " \t\n\r\f";
|
||||
|
||||
for (s = strtok(s, w); s; s = strtok(0, w)) {
|
||||
a = strtod(s, &e);
|
||||
if (e > s) printf(" :"), push(a);
|
||||
#define binop(x) printf("%c:", *s), b = pop(), a = pop(), push(x)
|
||||
else if (*s == '+') binop(a + b);
|
||||
else if (*s == '-') binop(a - b);
|
||||
else if (*s == '*') binop(a * b);
|
||||
else if (*s == '/') binop(a / b);
|
||||
else if (*s == '^') binop(pow(a, b));
|
||||
#undef binop
|
||||
else {
|
||||
fprintf(stderr, "'%c': ", *s);
|
||||
die("unknown oeprator\n");
|
||||
}
|
||||
for (i = depth; i-- || 0 * putchar('\n'); )
|
||||
printf(" %g", stack[i]);
|
||||
}
|
||||
|
||||
if (depth != 1) die("stack leftover\n");
|
||||
|
||||
return pop();
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char s[] = " 3 4 2 * 1 5 - 2 3 ^ ^ / + ";
|
||||
printf("%g\n", rpn(s));
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
#define die(msg) fprintf(stderr, msg"\n"), abort();
|
||||
double get(const char *s, const char *e, char **new_e)
|
||||
{
|
||||
const char *t;
|
||||
double a, b;
|
||||
|
||||
for (e--; e >= s && isspace(*e); e--);
|
||||
for (t = e; t > s && !isspace(t[-1]); t--);
|
||||
|
||||
if (t < s) die("underflow");
|
||||
|
||||
#define get2(expr) b = get(s, t, (char **)&t), a = get(s, t, (char **)&t), a = expr
|
||||
a = strtod(t, (char **)&e);
|
||||
if (e <= t) {
|
||||
if (t[0] == '+') get2(a + b);
|
||||
else if (t[0] == '-') get2(a - b);
|
||||
else if (t[0] == '*') get2(a * b);
|
||||
else if (t[0] == '/') get2(a / b);
|
||||
else if (t[0] == '^') get2(pow(a, b));
|
||||
else {
|
||||
fprintf(stderr, "'%c': ", t[0]);
|
||||
die("unknown token");
|
||||
}
|
||||
}
|
||||
#undef get2
|
||||
|
||||
*(const char **)new_e = t;
|
||||
return a;
|
||||
}
|
||||
|
||||
double rpn(const char *s)
|
||||
{
|
||||
const char *e = s + strlen(s);
|
||||
double v = get(s, e, (char**)&e);
|
||||
|
||||
while (e > s && isspace(e[-1])) e--;
|
||||
if (e == s) return v;
|
||||
|
||||
fprintf(stderr, "\"%.*s\": ", e - s, s);
|
||||
die("front garbage");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("%g\n", rpn("3 4 2 * 1 5 - 2 3 ^ ^ / +"));
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue