-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathKhozheev.c
146 lines (134 loc) · 3.32 KB
/
Khozheev.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
int *lenta;
int size = 1;
int loc = 0;
int movl(void){
if (loc == 0){
int i;
int* tmp;
size++;
tmp = (int*) realloc (lenta, sizeof(int) * size);
if(tmp != NULL){
for (i = size - 1; i != 0; i--)
tmp[i]=tmp[i-1];
tmp[0] = 0;
lenta = tmp;
}
else
return 1;
}
else loc--;
return 0;
}
int movr(void){
if (loc == (size - 1)){
int* tmp;
size++;
loc++;
tmp = (int*) realloc (lenta, sizeof(int) * size);
if(tmp != NULL){
tmp[size-1] = 0;
lenta = tmp;
}
else
return 1;
}
else loc++;
return 0;
}
int inc (void) {
if(lenta[loc] < 255)
lenta[loc]++;
else
lenta[loc] = 0;
return 0;
}
int dec (void) {
if(lenta[loc] > 0)
lenta[loc]--;
else
lenta[loc] = 255;
return 0;
}
int commands (char** a, int first, int last){
int i, tmp = 0;
for (i = first; i < last; i++){
if (strcmp(a[i], "movl") == 0)
movl();
else if (strcmp(a[i], "movr") == 0)
movr();
else if (strcmp(a[i], "inc") == 0)
inc();
else if (strcmp(a[i], "dec") == 0)
dec();
else if (strcmp(a[i], "print") == 0)
printf("\n%d", lenta[loc]);
else if (strcmp(a[i], "get") == 0){
printf("\nEnter symbol:");
lenta[loc] = getchar();
}
else if (strcmp(a[i], "printc") == 0)
printf("\n%c", lenta[loc]);
else if (strcmp(a[i], "begin") == 0)
tmp=i+1;
else if (strcmp(a[i], "end") == 0)
{
while (lenta[loc] != 0)
commands(a, tmp, i);
}
else
printf("\nError");
}
return 0;
}
char* delprobel (char* s, char* a){
int i, j;
j = 0;
a = (char*) malloc (sizeof(char)*6);
for (i = 0; s[i]; i++){
if (s[i] != ' ' && s[i] != '\n'){
a[j]=s[i];
j++;
a = (char*) realloc (a,(j+6)*sizeof(char));
}
}
a = (char*) realloc (a, j*sizeof(char));
return a;
}
int main(){
setlocale(LC_ALL, "RUS");
char s[255];
char **a;
int num = 0, i = 0;;
char *fname = NULL;
FILE * file;
file = fopen("laba.txt", "r");
while (file == NULL){
printf(" Error opening the file\n Enter new file name (Example: laba.dat or C:\\\\Users\\\\Desktop\\\\laba.dat):");
scanf("%s", fname);
file=fopen(fname, "rb");
}
a=(char **) malloc ((num+1)*sizeof(char *));
while(fgets(s, 255, file)){
if(s[0] != '*'){
a[num]=(char *) malloc (200*sizeof(char));
a[num] = delprobel(s, a[num]);
num++;
a = (char**) realloc (a,(num+1)*sizeof(char*));
}
}
fclose(file);
lenta = (int*) malloc (sizeof(int));
lenta[loc]=0;
commands(a, 0, num);
for(i = 0; i < num; i++)
free(a[num]);
free(a);
printf("\n Tape : ");
for(i = 0; i < size; i++)
printf(" %d", lenta[i]);
return 0;
}