Skip to content

Commit 614ae8d

Browse files
committed
4.3
1 parent 21ea665 commit 614ae8d

File tree

6 files changed

+353
-0
lines changed

6 files changed

+353
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h> // for atof()
4+
#include <math.h> // for fmod()
5+
#include "calc.h"
6+
7+
#define MAXOP 100 // max size of operand or operator
8+
#define MAX_VAR_LEN 26 // max size of array for variables
9+
10+
void mathfunc(char[]);
11+
void setVar(double, double);
12+
void getVar(char[]);
13+
14+
double variables[MAX_VAR_LEN];
15+
int variableFlags[MAX_VAR_LEN]; // if [0] is 0, means 'a' not set, if [0] is 1, means 'a' set
16+
17+
// compile with stack.c, getop.c, getch.c
18+
main()
19+
{
20+
int type;
21+
double op2;
22+
char s[MAXOP];
23+
24+
int i;
25+
26+
for (i = 0; i < MAX_VAR_LEN; i++)
27+
variableFlags[i] = 0;
28+
29+
while ((type = getop(s)) != EOF) {
30+
switch(type) {
31+
case NUMBER:
32+
push(atof(s));
33+
break;
34+
case VAR:
35+
getVar(s);
36+
break;
37+
case FUNC:
38+
mathfunc(s);
39+
break;
40+
case '+':
41+
push(pop() + pop());
42+
break;
43+
case '*':
44+
push(pop() * pop());
45+
break;
46+
case '-':
47+
op2 = pop();
48+
push(pop() - op2);
49+
break;
50+
case '/':
51+
op2 = pop();
52+
if (op2 != 0.0)
53+
push(pop() / op2);
54+
else
55+
printf("error: zero divisor\n");
56+
break;
57+
case '%':
58+
op2 = pop();
59+
if (op2 == 0.0)
60+
printf("error: can not mod 0\n");
61+
else
62+
push(fmod(pop(), op2));
63+
break;
64+
case '=':
65+
op2 = pop();
66+
setVar(pop(), op2);
67+
break;
68+
case '\n':
69+
printf("\t%.8g\n", pop());
70+
break;
71+
default:
72+
printf("error: unknown command %s\n", s);
73+
break;
74+
}
75+
}
76+
return 0;
77+
}
78+
79+
void mathfunc(char s[])
80+
{
81+
int index;
82+
if (0 == strrindex(s, "sin")) {
83+
push(sin(pop()));
84+
return;
85+
}
86+
87+
if (0 == strrindex(s, "exp")) {
88+
push(exp(pop()));
89+
return;
90+
}
91+
92+
if (0 == strrindex(s, "pow")) {
93+
double ey = pop();
94+
push(pow(pop(), ey));
95+
return;
96+
}
97+
98+
printf("error: unknown func %s\n", s);
99+
return;
100+
}
101+
102+
void setVar(double varIndex, double value)
103+
{
104+
int index = (int)varIndex;
105+
if (index < 0 || index > MAX_VAR_LEN) {
106+
printf("error: cannot set variable, invalid variable: %c\n", index + 'a');
107+
return;
108+
}
109+
110+
variables[index] = value;
111+
variableFlags[index] = 1;
112+
113+
push(value); // to show result
114+
115+
return;
116+
}
117+
118+
void getVar(char s[])
119+
{
120+
if (strlen(s) != 1 && s[0] != 0) {
121+
// if variable is 'a', s[0] == 0, strlen(s) == 0
122+
printf("error: cannot get variable, invalid variable: %s\n", s);
123+
return;
124+
}
125+
126+
double value = atof(s);
127+
int index = (int)value;
128+
if (variableFlags[index] == 0)
129+
push(value);
130+
else
131+
push(variables[index]);
132+
133+
return;
134+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#define NUMBER '0' // signal that a number was found
2+
#define FUNC '1' // signal that math function was found
3+
#define VAR '2' // signal that variable was found
4+
5+
void push(double);
6+
double pop(void);
7+
8+
int getop(char []);
9+
10+
int getch(void);
11+
void ungetch(int);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
#define BUFSIZE 100
5+
6+
int buf[BUFSIZE];
7+
int bufp = 0;
8+
9+
int value = EOF;
10+
11+
int getch(void)
12+
{
13+
return (bufp > 0) ? buf[--bufp] : getchar();
14+
}
15+
16+
void ungetch(int c)
17+
{
18+
if (bufp >= BUFSIZE)
19+
printf("ungetch: too many characters\n");
20+
else
21+
buf[bufp++] = c;
22+
}
23+
24+
void ungetchs(char s[])
25+
{
26+
int len = strlen(s);
27+
while (len > 0) {
28+
ungetch(s[--len]);
29+
}
30+
}
31+
32+
int getchOne(void)
33+
{
34+
if (value != EOF) {
35+
int temp = value;
36+
value = EOF;
37+
return temp;
38+
}
39+
else
40+
return getchar();
41+
}
42+
43+
void ungetchOne(int c)
44+
{
45+
if (value != EOF)
46+
printf("ungetchOne: cannot ungetch more char\n");
47+
else
48+
value = c;
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <ctype.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include "calc.h"
5+
6+
int strrindex(char source[], char searchfor[]);
7+
8+
int getop(char s[])
9+
{
10+
int i, c;
11+
12+
while ((s[0] = c = getch()) == ' ' || c == '\t')
13+
;
14+
15+
s[1] = '\0';
16+
17+
i = 0;
18+
19+
if (c == '+' || c == '-') {
20+
int op = c;
21+
c = getch();
22+
if (!isdigit(c) && c != '.') {
23+
ungetch(c);
24+
return op;
25+
} else {
26+
ungetch(c);
27+
}
28+
} else if (isalpha(c)) {
29+
while (isalpha(s[++i] = c = getch()))
30+
;
31+
ungetch(c);
32+
33+
if (i == 1) {
34+
s[0] = tolower(s[0]) - 'a';
35+
s[1] = '\0';
36+
return VAR;
37+
}
38+
39+
s[++i] = '\0';
40+
41+
int index;
42+
index = strrindex(s, "sin");
43+
if (index == 0)
44+
return FUNC;
45+
46+
index = strrindex(s, "exp");
47+
if (index == 0)
48+
return FUNC;
49+
50+
index = strrindex(s, "pow");
51+
if (index == 0)
52+
return FUNC;
53+
54+
return 0;
55+
} else if (!isdigit(c) && c != '.') {
56+
return c;
57+
}
58+
59+
i = 0;
60+
61+
if (isdigit(c))
62+
while (isdigit(s[++i] = c = getch()))
63+
;
64+
65+
if (c == '.')
66+
while (isdigit(s[++i] = c = getch()))
67+
;
68+
69+
s[i] = '\0';
70+
if (c != EOF)
71+
ungetch(c);
72+
return NUMBER;
73+
}
74+
75+
int strrindex(char s[], char t[])
76+
{
77+
int i, j, k;
78+
79+
for (i = strlen(s) - 2; i > 0; i--) {
80+
for (j = i, k = strlen(t) - 1; j > 0 && k > 0 && s[j] == t[k]; j--, k--)
81+
;
82+
83+
if (k == 0)
84+
return j;
85+
}
86+
return -1;
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <stdio.h>
2+
3+
#define MAXVAL 100
4+
5+
int sp = 0;
6+
double val[MAXVAL];
7+
8+
void push(double f)
9+
{
10+
if (sp < MAXVAL)
11+
val[sp++] = f;
12+
else
13+
printf("error: stack full, can't push %g\n", f);
14+
}
15+
16+
double pop(void)
17+
{
18+
if (sp > 0)
19+
return val[--sp];
20+
else {
21+
printf("error: stack empty\n");
22+
return 0.0;
23+
}
24+
}
25+
26+
void top(void)
27+
{
28+
if (sp > 0)
29+
printf("stack top: %g\n", val[sp-1]);
30+
else
31+
printf("stack empty\n");
32+
}
33+
34+
void duplicate(void)
35+
{
36+
if (sp > MAXVAL -1)
37+
printf("error: stack full, can't duplicate\n");
38+
else {
39+
double element = pop();
40+
push(element);
41+
push(element);
42+
}
43+
}
44+
45+
void swap(void)
46+
{
47+
if (sp < 2)
48+
printf("error: stack size < 2, can't swap\n");
49+
else {
50+
double e1 = pop();
51+
double e2 = pop();
52+
push(e1);
53+
push(e2);
54+
}
55+
}
56+
57+
void empty(void)
58+
{
59+
sp = 0;
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
main()
5+
{
6+
char s[100];
7+
s[0] = 'a' - 'a';
8+
s[1] = 'b';
9+
s[2] = '\0';
10+
printf("strlen(s): %d\n", strlen(s));
11+
return 0;
12+
}

0 commit comments

Comments
 (0)