Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "demo4_math.h"
  5.  
  6. #define BINARY_STR_LEN 16
  7. #define MAX_STR_LEN 256
  8.  
  9. long unsigned init(long unsigned total, long unsigned new)
  10. {
  11.   return new;
  12. }
  13.  
  14. long unsigned add(long unsigned total, long unsigned new)
  15. {
  16.   return total + new;
  17. }
  18.  
  19. typedef struct {
  20.   char *name;
  21.   long unsigned (*func)(long unsigned, long unsigned);
  22. } command;
  23.  
  24. command* commands[2];
  25.  
  26. command* find_func(char *line)
  27. {
  28.   command *cmd;
  29.   cmd->name = "+";
  30.   cmd->func = &add;
  31.   commands[0] = cmd;
  32.  
  33.   command *cmd2;
  34.   cmd2->name = "l";
  35.   cmd2->func = &init;
  36.   commands[1] = cmd2;
  37.  
  38.   return commands[0];
  39. }
  40.  
  41. void error(char *msg)
  42. {
  43.   printf("%s\n", msg);
  44.   exit(EXIT_FAILURE);
  45. }
  46.  
  47. void remove_newline(char *input)
  48. {
  49.   for (; *input; input++)
  50.   {
  51.     if (*input == '\n')
  52.       *input = 0;
  53.   }
  54. }
  55.  
  56. int main(void)
  57. {
  58.   char line[MAX_STR_LEN + 1];
  59.   long unsigned total = 0;
  60.  
  61.   while (1)
  62.   {
  63.     printf("? ");
  64.     fgets(line, MAX_STR_LEN, stdin);
  65.     remove_newline(line);
  66.  
  67.     if (strlen(line) == 0)
  68.       break;
  69.  
  70.     command *cmd = find_func(line);
  71.  
  72.     long unsigned current = numscan(line);
  73.     total = cmd->func(total, current);
  74.  
  75.     printbin(total, BINARY_STR_LEN);
  76.     printf(" (%lu)\n", total);
  77.   }
  78.  
  79.   return EXIT_SUCCESS;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement