include stdio include conio include string Constants define STR_INPUT_

  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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <stdio.h>
#include <conio.h>
#include <string.h>
//* *********
//* Constants
//* *********
#define STR_INPUT_FILE "Input.txt"
#define STR_OUTPUT_FILE "Output.txt"
#define STR_BUFFER_SIZE 128
//* ****************
//* Type definitions
//* ****************
typedef struct list
{
float koeff;
char * variables;
float * powers;
list * next;
} element;
//* ********************
//* Function definitions
//* ********************
//* List methods
inline element * NewList(void)
{ return NULL; }
void DeleteList(element * source)
{
element * p;
while(source)
{
p=source;
source=source->next;
delete [] p->variables;
delete [] p->powers;
delete p;
}
}
void PrintList(element * source,FILE * fpout)
{
int i, len;
fprintf(fpout,"// --- Echo ---\n");
if(!source) fprintf(fpout,"! List is empty\n");
else
while(source)
{
fprintf(fpout,"* Koeff: %+3.2f, Variables: %s, Powers: (",source->koeff,source->variables);
len=strlen(source->variables);
for(i=0; i<len; i++)fprintf(fpout," %3.2f",source->powers[i]);
fputs(" )\n",fpout);
source=source->next;
}
fprintf(fpout,"// --- ---- ---\n");
}
element * NewElement(element * source, float koeff, char * variables, float * powers)
{
element * ptr=new element;
ptr->koeff=koeff;
ptr->variables=variables;
ptr->powers=powers;
if(source)
{
ptr->next=source->next;
source->next=ptr;
} else
ptr->next=NULL;
return ptr;
}
element * NewElementEx(element ** source, float koeff, char * variables, float * powers)
{
element * ptr=new element;
ptr->koeff=koeff;
ptr->variables=variables;
ptr->powers=powers;
ptr->next=*source;
*source=ptr;
return ptr;
}
void DeleteElement(element * source)
{
element * h=source->next;
source->next=h->next;
delete [] h->variables;
delete [] h->powers;
delete h;
}
void DeleteElementEx(element ** source)
{
element * h=*source;
*source=h->next;
delete [] h->variables;
delete [] h->powers;
delete h;
}
//* Parse & process methods
bool FloatArrayCompare(float * ptr1, float * ptr2, int size)
{
bool result=true;
if(ptr1!=ptr2) // Same address in pointers - return true
while(--size>=0 && result)
if(ptr1[size]!=ptr2[size])
result=false;
return result;
}
int FloatArrayFindValue(float * ptr, float value, int size)
{
while(--size>=0 && ptr[size]!=value);
return size; // Returns -1 if there is no same element in array
}
void Process(element ** source, float koeff, char * variables, float * powers)
{
int len = strlen(variables),i,j;
char ch;
float f;
if(koeff==0) return;
// Cut same symbols and sum their powers
for(i=0; i<len; i++)
for(j=i+1; j<len; )
if(variables[i]==variables[j])
{
powers[i]+=powers[j];
variables[j]=variables[len-1];
powers[j]=powers[len-1];
variables[len-1]='\0';
len--;
} else j++;
// If any zero powers were found - erase them
for(i=0; i<len;)
if(powers[i]==0.)
{
powers[i]=powers[len-1];
variables[i]=variables[len-1];
variables[len-1]='\0';
len--;
} else i++;
// Sort arrays. The key is a variable letter
for(j=len-1; j>0; j--)
for(i=0; i<len-1; i++)
if(variables[i]>variables[i+1])
{
ch=variables[i];
variables[i]=variables[i+1];
variables[i+1]=ch;
f=powers[i];
powers[i]=powers[i+1];
powers[i+1]=f;
}
// Search for existing elements
bool found = false;
element * current = *source;
while(current && !found)
{
if(!strcmp(current->variables,variables) && FloatArrayCompare(current->powers,powers,len))
{
current->koeff+=koeff;
found=true;
}
current=current->next;
}
if(!found)
{
char * str = new char [len+1];
float * ptr = new float [len];
strcpy(str,variables);
while(--len>=0)
ptr[len]=powers[len];
NewElementEx(source,koeff,str,ptr);
}
}
element * Parse(void)
{
element * ptr=NewList();
FILE * fpin=fopen(STR_INPUT_FILE,"r");
if(fpin)
{
char str[STR_BUFFER_SIZE]="";
float pow[STR_BUFFER_SIZE];
float koeff=1.0,
f;
char buff=fgetc(fpin),
sign='+';
int i=0;
// Check for any +/- before first member or for empty file
while(buff==' ') buff=fgetc(fpin);
if(buff==EOF) koeff=0.;
else if(buff=='-' || buff=='+') sign=buff;
else fseek(fpin,-1,1);
// Main parse loop
buff=fgetc(fpin);
while(buff!=EOF)
{
if(buff!=' ' && buff!=EOF && buff!='*')
if(buff=='-' || buff=='+')
{
if(sign=='-') koeff *= -1;
Process(&ptr,koeff,str,pow);
// Set defaults
koeff=1.0;
str[0]='\0';
i=0;
sign=buff;
}
else if(buff=='.' || buff>='0' && buff<='9')
{
fseek(fpin,-1,1);
fscanf(fpin,"%f",&f);
koeff*=f;
}
else if(buff=='^') fscanf(fpin,"%f",&pow[i-1]);
else {
str[i]=buff;
str[i+1]='\0';
pow[i++]=1.0;
}
buff=fgetc(fpin);
}
if(sign=='-') koeff *= -1;
Process(&ptr,koeff,str,pow);
fclose(fpin);
} else perror("! Can not open source file");
return ptr;
}
//* Output method
int Output(element * source)
{
FILE * fpout=fopen(STR_OUTPUT_FILE,"w");
if(fpout)
{
int i;
char * str;
float * ptr;
while(source)
{
if(source->koeff)
{
str=source->variables;
ptr=source->powers;
fprintf(fpout," % +.2f",source->koeff);
for(i=0; str[i]!='\0'; i++)
{
fprintf(fpout,"*%c",str[i]);
if(ptr[i]!=1.)
fprintf(fpout,"^%.2f",ptr[i]);
}
}
source=source->next;
}
if(!ftell(fpout)) fputc('0',fpout);
fclose(fpout);
} else perror("! Can not write output file");
return fpout!=NULL;
}
//* *************
//* Main function
//* *************
int main()
{
element * h=Parse();
Output(h);
DeleteList(h);
return 0x0;
}