expseq.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define HELPMSG "USAGE: expseq LAST\n or: expseq FIRST LAST\n or: expseq --help\n"\
  5. "Print numbers 2^FIRST to 2^LAST in exponential steps.\n\n"\
  6. "If FIRST is omitted, it defaults to 0.\n"
  7. int main(int argc, char* argv[])
  8. {
  9. int fstnum = 0, lstnum, i;
  10. if (argc == 2) {
  11. if (strcmp(argv[1], "--help")==0
  12. || strcmp(argv[1], "-h")==0) {
  13. fprintf(stderr, HELPMSG);
  14. return 1;
  15. } else {
  16. char* endptr = argv[1];
  17. lstnum = strtol(argv[1], &endptr, 10);
  18. if (!endptr) {
  19. fprintf(stderr, "%s: non-numeric arg: %s\n", argv[0], argv[1]);
  20. return 1;
  21. }
  22. }
  23. } else if (argc == 3) {
  24. char* endptr = argv[1];
  25. fstnum = strtol(argv[1], &endptr, 10);
  26. if (!endptr) {
  27. fprintf(stderr, "%s: non-numeric arg: %s\n", argv[0], argv[1]);
  28. return 1;
  29. }
  30. endptr = argv[2];
  31. lstnum = strtol(argv[2], &endptr, 10);
  32. if (!endptr) {
  33. fprintf(stderr, "%s: non-numeric arg: %s\n", argv[0], argv[2]);
  34. return 1;
  35. }
  36. } else {
  37. fprintf(stderr, "%s: missing operand\n", argv[0]);
  38. return 1;
  39. }
  40. i = fstnum;
  41. while (i <= lstnum) {
  42. printf("%d\n", (int) pow(2, (float) i));
  43. i++;
  44. }
  45. return 0;
  46. }