Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <string.h>
- #define usage() {printf("%s HH:MM\n", argv0); exit(1);}
- const static char *argv0;
- char *
- itoa_base(u_int64_t n, int base)
- {
- char *res, *tab;
- int i;
- if (n == 0)
- return strdup("0");
- tab = "0123456789abcdef";
- res = calloc(65, 1);
- i = 0;
- if (res)
- {
- while (n > 0)
- {
- res[i++] = tab[n % base];
- n /= base;
- }
- res[i] = 0;
- int len = strlen(res);
- for (int i = 0; i < len / 2; i++) {
- char tmp = res[i];
- res[i] = res[len-i-1];
- res[len-i-1] = tmp;
- }
- }
- return (res);
- }
- void
- checkstr(const char *s)
- {
- if (!isdigit(s[0])
- || !isdigit(s[1])
- || s[2] != ':'
- || !isdigit(s[3])
- || !isdigit(s[4])
- || s[5] != 0)
- usage();
- }
- int
- maketime(const char *s)
- {
- char num[3] = {0};
- int minutes, hours;
- memmove(num, s, 2);
- hours = atoi(num);
- memmove(num, s+3, 2);
- minutes = atoi(num);
- return minutes + (hours * 60);
- }
- int
- putemtime(int time)
- {
- static int em_min, em_hr;
- if (time >= 60) {
- em_hr++;
- return putemtime(time - 60);
- }
- if (time > 0) {
- em_min++;
- return putemtime(time - 1);
- }
- char *m = itoa_base(em_min, 6);
- char *h = itoa_base(em_hr, 6);
- printf("%s:%s\n", h, m);
- return 0;
- }
- int
- main(int argc, char **argv)
- {
- static int time;
- argv0 = argv[0]; // so we can have argv0 in usage
- if (argc != 2)
- usage();
- checkstr(argv[1]);
- time = maketime(argv[1]);
- time *= 1.5;
- putemtime(time);
- }
Add Comment
Please, Sign In to add comment