Compare commits

...

2 Commits

Author SHA1 Message Date
Tibor Bizjak 4d95f4515b k&r 04-03: rewrote calculator, added modulo and negative numbers 2023-07-08 16:37:54 +02:00
Tibor Bizjak c8cdc37824 solutions to 04-01 and 04-02 2023-07-04 10:28:05 +02:00
3 changed files with 219 additions and 0 deletions

View File

@ -0,0 +1,40 @@
#include <stdio.h>
#include <string.h>
#define STRING "test string with lost of stuff"
int strindex(char source[], char searchfor[]);
/* pass optional searchfor arg to strindex with STRING, pretty print result */
int main(int argc, char *argv[])
{
char s[] = STRING;
char *t = (argc > 1) ? argv[1] : "est";
int i = strindex(s, t);
if (i < 0) {
printf("no match\n");
return 1;
}
printf("%s\n", s);
while (i--)
putchar(' ');
printf("^\n");
return 0;
}
/* strindex: return index of the righmost occurence of t in s, -1 if none */
int strindex(char s[], char t[])
{
int i, j;
const int tlen = strlen(t);
j = tlen - 1;
for (i = strlen(s)-1; i; i--)
if (s[i] != t[j])
j = tlen - 1;
else if (!(j--)) // match found
return i;
return -1;
}

View File

@ -0,0 +1,59 @@
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#define DEC_SEP X('.') X(',')
#define EXP_SEP X('e') X('E')
double atof(char *s);
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("error: expected argument\n");
return 1;
}
printf("%f\n", atof(argv[1]));
return 0;
}
/* macros for atof */
#define SIGN(s) \
((*s == '-') ? (s++, -1) : ((*s == '+') ? (s++, 1) : 1))
#define ATOI(s, x, ...) \
for (; isdigit(*s); s++, ##__VA_ARGS__) \
x = 10 * x + (*s - '0')
/* atof: convert string s to double */
double atof(char s[])
{
double val;
while (isspace(*s))
s++;
/* integer part */
int sign = SIGN(s);
ATOI(s, val);
#define X(sep) *s != sep &&
if (DEC_SEP 1) return sign * val;
#undef X
s++;
/* fractional part */
int exp = 0;
ATOI(s, val, exp--);
val *= sign * pow(10, exp);
#define X(sep) *s != sep &&
if (EXP_SEP 1) return val;
#undef X
s++;
/* exponent */
sign = SIGN(s);
exp = 0;
ATOI(s, exp);
return val * pow(10, sign * exp);
}

View File

@ -0,0 +1,120 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define printerr(format, ...) printf("error: " format "\n", ##__VA_ARGS__)
void push(double x);
double pop(void);
int stack_len(void);
/* --------- ERROR MESSAGES */
#define ZERODIV_ERR "zero divisor"
/* --------- CALCULATOR FUNCTIONS */
/* commutative operator application */
#define COMM_OP(OP) push(pop() OP pop())
/* non-commutative operator application */
#define NONC_OP(OP, T, ...) \
do { T op2 = pop(); __VA_ARGS__ push(((T) pop()) OP op2); } while (0)
/* nonzero second operand operator application */
#define NONZ_OP(OP, T) \
NONC_OP(OP, T, if (!op2) printerr(ZERODIV_ERR); else)
#define FUNCTIONS \
X(+, COMM_OP) \
X(*, COMM_OP) \
X(-, NONC_OP, double) \
X(/, NONZ_OP, double) \
X(%, NONZ_OP, int)
#define MAXTOKEN 100
int gettoken(char *token, int max_t);
/* reverse Polish calculator */
int main()
{
char token[MAXTOKEN + 1];
double op2;
int t_len;
while (t_len = gettoken(token, MAXTOKEN)) {
if (t_len < 0)
printf("\t%g\n", pop());
else
#define X(F, APPLY, ...) \
if (!strcmp(#F, token)) \
APPLY(F, ##__VA_ARGS__); \
else
FUNCTIONS
#undef X
push(atof(token));
}
printf("%g\n", pop());
return 0;
}
/* -------- PARSING FUNCTIONS */
#include <ctype.h>
#define BLANK ' '
/* gettoken: get next space delimited token from input
* return token length, -1 on newline and 0 on EOF */
int gettoken(char *token, int t_max)
{
static int c = BLANK;
while (isspace(c) && c != '\n') /* remove leading whitespace */
c = getchar();
if (c == '\n') { /* newline is a token */
c = BLANK;
return -1;
}
/* fetch token */
int i;
for (i = 0; i < t_max && c != EOF && !isspace(c); c = getchar(), i++)
token[i] = c;
token[i] = '\0';
return i;
}
/* ------ GLOBAL STACK */
#define MAXVAL 100 /* maximum depth of val stack */
int sp = 0;
double val[MAXVAL];
int stack_len()
{
return sp;
}
/* push: push f onto value stack */
void push(double f)
{
if (sp < MAXVAL)
val[sp++] = f;
else
printf("error: stack full, can't push %g\n", f);
}
/* pop: pop and return top value from stack */
double pop(void)
{
if (sp)
return val[--sp];
printf("error: stack empty\n");
return 0.0;
}