micro-utils/src/seq.c
2024-07-09 15:43:55 +03:00

59 lines
963 B
C

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
static double parse_double(const char *str) {
char *ptr;
double res = strtod(str, &ptr);
if (*ptr) {
fprintf(stderr, "seq: %s: cant parse\n", ptr);
exit(1);
}
return res;
}
int main(int argc, char **argv) {
double start = 1;
double last = 0;
double n = 1;
argv++;
argc--;
switch (argc) {
case 1:
last = parse_double(argv[0]);
break;
case 2:
start = parse_double(argv[0]);
last = parse_double(argv[1]);
break;
case 3:
n = parse_double(argv[1]);
start = parse_double(argv[0]);
last = parse_double(argv[2]);
break;
default:
fprintf(stderr, "seq: missing operands\n");
return 1;
}
if (start <= last && n >= 0) {
for (double i = start; i <= last; i += n)
printf("%g\n", i);
}
else if (n <= 0)
for (double i = start; i >= last; i += n)
printf("%g\n", i);
return 0;
}