forked from sdsubhajitdas/Compiler-Design-Assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.c
80 lines (62 loc) · 1.51 KB
/
7.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
#include <stdio.h>
#include <string.h>
int parseLine(char []);
int countVariables(char []);
void parseType(char [], char []);
/*
Input: int a,b,c;float x,y;
Output: Total memory required 20 bytes
*/
int main(){
char string[1000];
char *line;
int totalSize = 0;
scanf("%[^\n]%*c", string);
line = strtok(string, ";");
do{
totalSize += parseLine(line);
line = strtok(NULL,";");
}while(line != NULL);
printf("Total memory required %d bytes",totalSize);
return 0;
}
int parseLine(char line[]){
/* Parses a single line for data type used and no. of variables.
*/
char type[50];
int varCount,size = 0;
parseType(line,type);
varCount = countVariables(line);
// Calculating the space required according to the datatype.
if(strcmp(type,"int") == 0)
size = sizeof(int);
else if(strcmp(type,"char") == 0)
size = sizeof(char);
else if(strcmp(type,"double") == 0)
size = sizeof(double);
else if(strcmp(type,"float") == 0)
size = sizeof(float);
return size * varCount;
}
int countVariables(char line[]){
// Counts number of variable used
char *ch = line;
int count = 1;
while(*ch != '\0'){
if(*ch == ',')
count++;
ch+=1;
}
return count;
}
void parseType(char line[], char type[]){
// Parser to identify the datatype used.
char *ch = line;
int i=0;
while(*ch != ' '){
type[i] = *ch;
ch +=1;
i++;
}
type[i] = '\0';
}