-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.c
196 lines (181 loc) · 2.78 KB
/
mem.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*Input states*/
#define N 0
#define I 1
#define II 2
#define III 3
#define IV 10
/*Input Types*/
#define NUM 4
#define COM 5
#define CH 6
#define CR 7
#define ERR 8
/*Memory count states*/
#define F 0
#define NF 1
int used=1;
struct object
{
int offset;
int size;
char status;
}table[100];
void makeobj(int index,int offset, int size, char status)
{
struct object a;
a.offset = offset;
a.size = size;
a.status = status;
table[index]=a;
used++;
}
void display()
{
int i=1;
for(i;i<used;i++)
printf("%d %d %c\n",table[i].offset,table[i].size,table[i].status);
}
void error()
{
printf("Invalid Input\n");
exit(0);
}
void FSM_simulate(char *str)
{
FILE *fp;
fp=fopen(str,"r");
if(fp == NULL)
exit(0);
int input_type;
int prev_val=0,val,index,offset,size;
int curr_state = N;
char ch = getc(fp),flag;
while( !feof(fp) )
{
int ascii_val = (int)ch;
if(ascii_val>=48 && ascii_val<=57)
{
val = ascii_val - 48;
input_type = NUM;
}
else if(ch == 'Y' || ch== 'N')
input_type = CH;
else if(ascii_val==44)
input_type = COM;
else if(ascii_val==10)
input_type = CR;
else
input_type = ERR;
switch(input_type)
{
case NUM :
{
if(curr_state == N)
{
curr_state = N;
prev_val = prev_val*10 + val;
}
else if(curr_state == I)
{
curr_state = I;
prev_val = prev_val*10 + val;
}
else if(curr_state == II)
{
curr_state = II;
prev_val = prev_val*10 + val;
}
break;
}
case COM :
{
if(curr_state == N)
{
curr_state = I;
index = prev_val;
prev_val =0;
}
else if(curr_state == I)
{
curr_state = II;
offset = prev_val;
prev_val =0;
}
else if(curr_state == II)
{
curr_state = III;
size = prev_val;
prev_val =0;
}
else
error();
break;
}
case CH:
{
if(curr_state == III )
{
curr_state = IV;
flag = ch;
}
else
error();
break;
}
case CR :
{
if(curr_state == IV)
{
makeobj(index,offset,size,flag);
curr_state = N;
break;
}
else
error();
break;
}
case ERR:
error();
}
ch=getc(fp);
}
fclose(fp);
}
int count(int req_mem)
{
int i,index,curr_state,free_mem=0;
for(i=1;i<used;i++)
{
if(table[i].status == 'Y')
{
if(curr_state == F)
free_mem+=table[i].size;
else if (curr_state == NF)
{
curr_state = F;
index = i;
free_mem+=table[i].size;
}
if(req_mem <= free_mem)
return index;
}
else
curr_state = NF;
}
return -1;
}
int main()
{
int num;
FSM_simulate("input.txt");
scanf("%d",&num);
int i=count(num);
if(i!=-1)
printf("%d",i);
else
printf("Can not be inserted");
return 1;
}