Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "demo4_math.h"
- #define BINARY_STR_LEN 16
- #define MAX_STR_LEN 256
- long unsigned init(long unsigned total, long unsigned new)
- {
- return new;
- }
- long unsigned add(long unsigned total, long unsigned new)
- {
- return total + new;
- }
- typedef struct {
- char *name;
- long unsigned (*func)(long unsigned, long unsigned);
- } command;
- command* commands[2];
- command* find_func(char *line)
- {
- command *cmd;
- cmd->name = "+";
- cmd->func = &add;
- commands[0] = cmd;
- command *cmd2;
- cmd2->name = "l";
- cmd2->func = &init;
- commands[1] = cmd2;
- return commands[0];
- }
- void error(char *msg)
- {
- printf("%s\n", msg);
- exit(EXIT_FAILURE);
- }
- void remove_newline(char *input)
- {
- for (; *input; input++)
- {
- if (*input == '\n')
- *input = 0;
- }
- }
- int main(void)
- {
- char line[MAX_STR_LEN + 1];
- long unsigned total = 0;
- while (1)
- {
- printf("? ");
- fgets(line, MAX_STR_LEN, stdin);
- remove_newline(line);
- if (strlen(line) == 0)
- break;
- command *cmd = find_func(line);
- long unsigned current = numscan(line);
- total = cmd->func(total, current);
- printbin(total, BINARY_STR_LEN);
- printf(" (%lu)\n", total);
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement