-
Notifications
You must be signed in to change notification settings - Fork 4
/
ternary.c
252 lines (244 loc) · 8.63 KB
/
ternary.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include<stdio.h>
#include<string.h>
void main(){
char str[99]; //string to input from userdata.c
char str_nextline[99]; //string to take input from other files
char str_nextline1[99]; //string to take input from other files and of next line
int str_len;
char str_nosemi[99]; //string without semicolon to be inserted in ternary
char str1[99]; //extra string
char str2[99]; //extra string
int open=0,close=0; //for counting the opening and closing brackets
int pos_condition,i,j,coma_pos,pos_neg;
char condition[10]=" "; //for condition of if
FILE *fp1,*fp2,*fp3,*fp4,*fp5;
fp1=fopen("userdata.c","r"); //main file
fp2=fopen("ternary.txt","w+"); //updated code
fp3=fopen("tern_if.txt","w+"); //altered contents of if according to needs that are to be put in ternary
fp4=fopen("if_complete.txt","w+"); //complete if for the case else not found
fp5=fopen("tern_else.txt","w+"); //altered contents of else according to needs that are to be put in ternary
while(fgets(str,99,fp1)!=NULL) //input
{
if(strstr(str,"if(")!=NULL) //checking for if in string
{
pos_condition=strstr(str,"(")-str; //looking for ( to extract condition of if
i=pos_condition+1;
j=0;
while(str[i]!=')') //extracting the condition
{
condition[j]=str[i];
i++;
j++;
}
if(strstr(str,"{")) //checking for { in same line of if
{
fputs(str,fp4);
open++;
fgets(str,99,fp1);
}
else //checking for { in next line of if
{
fputs(str,fp4);
fgets(str,99,fp1);
if(str[0]=='{')
{
fputs(str,fp4);
open++;
fgets(str,99,fp1);
}
else
goto a;
}
do //taking cases into consideration and accordingly putting data into files
{
fputs(str,fp4); //putting complete if in if_complete for the case if "else" is not found after if
if(strstr(str,";")!=NULL) //content of if
{
str_len=strlen(str);
memset(str_nosemi,0,str_len); //making the string buffer empty
j=0;
for(i=0;i<str_len-2;i++) //removing ; from the content of if
{
str_nosemi[j]=str[i];
j++;
}
}
if(strstr(str,"{")!=NULL) //checking for "{" inside if
{
if(open==0)
open++;
else
{
open++;
fputs(str_nosemi,fp3);
fputs("\n",fp3);
}
}
else if(strstr(str,"}")!=NULL) //checking for "}"
{
close++;
if((open-close)==0) //if "{"-"}"=0 then it means for has ended
{
if(strstr(str,"else")!=NULL)
goto b; //else found in same line as }else
break; //therefore goto b i.e. end of for and out of for
}
fputs(str_nosemi,fp3);
fputs("\n",fp3);
}
else
{
fputs(str_nosemi,fp3);
fputs("\n",fp3);
}
fgets(str,99,fp1);
}while((open-close)!=0); //terminating condition
fgets(str,99,fp1);
if(strstr(str,"else")!=NULL) //checking for else after if
{
goto b; // if found goto b
}
else
{
goto c; //else not found
}
b:
open=0;
close=0;
fputs("~\n",fp3); //putting ~ as an terminator
fseek(fp3,0,SEEK_SET);
fgets(str_nextline,99,fp3); //taking the next line input
j=0;
while(strstr(str_nextline1,"~")==NULL) //extract if untill ~ is not found
{
fgets(str_nextline1,99,fp3);
if(strstr(str_nextline1,"~")==NULL)
{
j=j+strlen(str_nextline); //finding the strlen of string and adding it into j
memset(str_nextline,0,99);
fseek(fp3,j-1,SEEK_SET); //seeking pointer to j-1 position
fputs(",",fp3); //putting coma in between
fseek(fp3,j+strlen(str_nextline1),SEEK_SET); //seeking pointer to end of string to extract next string
strcpy(str_nextline,str_nextline1);
}
else
break;
}
fseek(fp3,0,SEEK_END);
if(strstr(str,"{")) //checking for { in same line as if
{
open++;
fgets(str,99,fp1);
}
else //checking for { in next line
{
fgets(str,99,fp1);
if(str[0]=='{')
{
open++;
fgets(str,99,fp1);
}
else
goto a;
}
do //conditions same as condition of if
{
if(strstr(str,";")!=NULL)
{
str_len=strlen(str);
memset(str_nosemi,0,str_len);
j=0;
for(i=0;i<str_len-2;i++)
{
str_nosemi[j]=str[i];
j++;
}
//str_nosemi[j]=',';
//puts(str_nosemi);
}
if(strstr(str,"{")!=NULL) //checking for "{"
{
if(open==0)
open++;
else
{
open++;
fputs(str_nosemi,fp5);
fputs("\n",fp5);
}
}
else if(strstr(str,"}")!=NULL) //checking for "}"
{
close++;
if((open-close)==0) //if "{"-"}"=0 then it means for has ended
{
break; //therefore goto a i.e. end of for and out of for
}
fputs(str_nosemi,fp5);
fputs("\n",fp5);
}
else
{
fputs(str_nosemi,fp5);
fputs("\n",fp5);
}
fgets(str,99,fp1);
//if(strstr(str,"}")==NULL || (open-(close+1))!=0)
// fputs(",",fp3);
}while((open-close)!=0);
fputs("~\n",fp5);
fseek(fp5,0,SEEK_SET);
memset(str_nextline1,0,99);
fgets(str_nextline,99,fp5);
j=0;
while(strstr(str_nextline1,"~")==NULL) //putting , in between of statements in order to put in ternary
{
fgets(str_nextline1,99,fp5);
if(strstr(str_nextline1,"~")==NULL)
{
j=j+strlen(str_nextline);
memset(str_nextline,0,99);
fseek(fp5,j-1,SEEK_SET);
fputs(",",fp5);
fseek(fp5,j+strlen(str_nextline1),SEEK_SET);
strcpy(str_nextline,str_nextline1);
}
else
break;
}
fseek(fp5,0,SEEK_END);
fputs(condition,fp2); //putting condition xyz ie (xyz)?():()
fputs("?(",fp2); //putting ?(
fseek(fp3,0,SEEK_SET);
while(fgets(str2,99,fp3)!=NULL) //putting contents of if ie abc
{
if(strstr(str2,"~")!=NULL)
break;
fputs(str2,fp2);
}
fputs("):\n(",fp2); //(xyz)?(abc):(pqr) putting ):
fseek(fp5,0,SEEK_SET);
while(fgets(str2,99,fp5)!=NULL) //putting pqr
{
if(strstr(str2,"~")!=NULL)
break;
fputs(str2,fp2);
}
fputs(");\n",fp2); //putting );
c:
while(fgets(str1,99,fp4)!=NULL) //if else is not found
{
fputs(str1,fp2);
}
}
else //in case there is no if in the str
{
fputs(str,fp2);
}
a:;
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
}