Advertisement
a3f

Simple addition/substraction calculator

a3f
Sep 24th, 2014
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #define ended(X) (X == '\n' || X == '\r' || X == '\0')
  5. #define SIZE 100
  6. int main (void)
  7. {
  8.     double ans = 0, tmp;
  9.     char line[SIZE];
  10.     fgets(line, SIZE, stdin);
  11.     char *endptr, *currptr = line;
  12.     do
  13.     {
  14.         ans += tmp = strtod(currptr, &endptr);
  15.         if (!tmp && currptr == endptr)
  16.             return fputs("Invalid input", stderr);
  17.         if (errno == ERANGE)
  18.             return fputs("Out of range", stderr);
  19.         currptr = endptr;
  20.     }
  21.     while (!ended(*endptr) && endptr - line + SIZE > 0);
  22.     printf("= %g", ans);
  23.     return 0;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement