Это частичная подсказка для вас.
Что это может сделать: a + b;а - б;а * б;a / b;
Примечание
a / b не даст правильных ответов, потому что делает целочисленное деление.
Что не может сделать
Правильная математика, сделай кофе и все остальное
silly_calc.c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
enum operation {
INVALID,
CONTINUE, /* got just a number */
QUIT,
PLUS,
MINUS,
MULTIPLY,
DIVIDE
};
enum operation parse(char *expr, int *left, int *right);
int eval(const char *expr);
char *remove_spaces(char *expr);
int main() {
char expr[256];
FILE *fp = stdin;
char *rv;
int left, right;
enum operation oper = CONTINUE;
while (oper != QUIT) {
fputc(':', stdout);
rv = fgets(expr, sizeof(expr), fp);
if( rv == NULL ) {
if(feof(fp)) {
fprintf(stderr, "someone closed the input\n");
break;
}
if(ferror(fp)) {
fprintf(stderr, "something bad happened\n");
break;
}
}
oper = parse(expr, &left, & right);
/* this switch should be moved into an eval function
* which would deal with precedence of operations etc.
*/
switch(oper) {
case CONTINUE:
fprintf (stderr, "%d\n", left);
continue;
case INVALID:
fprintf(stderr, "> Don't know what to do with %-32s...\n", expr);
continue;
case QUIT:
fprintf(stderr, "> quitting\n");
break;
case PLUS:
fprintf(stdout, "%d\n", left + right);
break;
case MINUS:
fprintf(stdout, "%d\n", left - right);
break;
case MULTIPLY:
fprintf(stdout, "%d\n", left * right);
break;
case DIVIDE:
fprintf(stdout, "%d\n", left / right);
break;
}
}
return 0;
}
enum operation next_expr(char *expr, int *left, int *right)
{
enum operation oper;
char *nextbit = NULL;
*left = strtol(expr, &nextbit, 10);
if ((*left == 0 && errno == EINVAL) || nextbit == expr ) {
if(strcasecmp(expr, "quit") == 0)
return QUIT;
return INVALID;
}
if (*nextbit == 0 )
return CONTINUE;
switch(*nextbit) {
case '+': oper = PLUS; break;
case '-': oper = MINUS; break;
case '*': oper = MULTIPLY; break;
case '/': oper = DIVIDE; break;
default:
return INVALID;
}
expr = nextbit+1; /* move over to the next number */
*right = strtol(expr, &nextbit, 10);
if((*right == 0 && errno == EINVAL ) || nextbit == expr) {
return INVALID;
}
/* what if there are still more bits in next bit? */
return oper;
}
enum operation parse(char *expr, int *left, int *right)
{
enum operation suboperation;
remove_spaces(expr);
suboperation = next_expr(expr, left, right);
/* TODO: take care of the rest of the bits that are in the expr */
return suboperation;
}
char * remove_spaces(char *expr)
{
char *p = expr, *q = expr;
while(*q != 0) {
*p = *q;
q++;
if(!isspace(*p))
p++;
}
*p = 0;
return expr;
}