-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
67 lines (50 loc) · 1.62 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <stdlib.h>
#include "card.h"
#include "cell.h"
// maximum number of shifts done by the program
const uint MAX_STEPS = 10000;
void main(int argc, char *argv[]) {
struct Card *cards_head = get_cards(argv[1]);
if(cards_head == NULL) {
printf("System out of memory");
exit(0);
}
if(validate_cards(cards_head) == 0)
return;
print_cards(cards_head);
struct Cell *tape_head = (struct Cell*)malloc(sizeof(struct Cell));
if(tape_head == NULL) {
printf("System out of memory");
exit(0);
}
tape_head->prev = NULL, tape_head->next = NULL;
get_initial_tape(tape_head, argv[2]);
if(validate_tape(tape_head) == 0)
return;
print_tape(tape_head);
printf("Starting Execution...\n---------------------\n\n");
struct Card *current_card = cards_head->next;
int idx = 0;
while(1) {
printf("STEP: %d\n", idx);
print_tape(tape_head);
if(current_card->id == 0)
break;
uint temp_val = tape_head->val;
tape_head->val = current_card->state[temp_val].write;
tape_head = move(tape_head, current_card->state[temp_val].move);
current_card = search_card(cards_head, current_card->state[temp_val].next_card);
idx++;
if(idx == MAX_STEPS+1) {
printf("Maximum steps exeeced\n");
return;
}
}
printf("---------------------\nEnd of Execution\n\n");
printf("Result\n");
print_tape(tape_head);
printf("Number of ones: %d\n", tape_count_ones(tape_head));
free_cards(cards_head);
free_tape(tape_head);
}