-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainCode.cpp
More file actions
275 lines (234 loc) · 6.27 KB
/
Copy pathMainCode.cpp
File metadata and controls
275 lines (234 loc) · 6.27 KB
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
#include <iostream>
#include <string>
#include <fstream>
#include<conio.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
FILE *fp1;
char str [80];
float buffer[][5]={0};
float data[255][5],buffer_y[255][5];
float CX[2],CY[2];
char str2[255][20];
int maxrow = 0;
//======================[[-- VARIABLES ANNOTATION --[[===================//
// str2 is used to store the label in 1st column
// data is used to store the column 2 , 3 flot type height , width
// CX is an array for calculated X ie. CX[0] is u and CX[1] is v
// Similarly CY is the calculated value for Y
// Buffer and Buffer_y are the copy of the data array for calc X & Y
//=======================================================================//
void read_file();
void display_array();
void swap(float &, float &);
void calc_x(int);
void compute(int);
void write_file();
void count_lines();
//======================[[-- MAIN FUNC --[[===================//
// read_file() - reads the text file and init data and buffers
// compute(row) - Compuetes row number
// display_array() - displays buffer array fir debug purpose
// write_file() - Write file called Output.txt
//=============================================================//
void main()
{
printf("Loading file.");
count_lines();
read_file();
for(int k=0;k<(maxrow-1);k++)
{
compute(k);
printf("Display Array.\n");
display_array();
}
write_file();
getch();
}
//======================[[-- CALC X FUNC --[[===================//
// Takes input row no as index
// X=(x1+x2,min(y1,y2))
// X rep as (u,v) as (CX[0],CX[1])
// Used bufferX for computation leaving data unchanged
//===============================================================//
void calc_x(int index)
{
cout<<"buffer[index][1]="<<buffer[index][1]<<" buffer[index][2]="<<buffer[index][2]<<endl;
cout<<"buffer[index+1][1]="<<buffer[index+1][1]<<" buffer[index+1][2]="<<buffer[index+1][2]<<endl;
CX[0] = buffer[index][1]+buffer[index+1][1];
CX[1] = min(buffer[index][2],buffer[index+1][2]);
cout<<"CX[0]="<<CX[0]<<"CX[1]="<<CX[1]<<endl;
}
//======================[[-- CALC Y FUNC --[[===================//
// Takes input row no as index
// X=(min(x1,x2),y1+y2)
// Y rep as (u,v) as (CY[0],CY[1])
// Used buffer_y for computation leaving data unchanged
//===============================================================//
void calc_y(int index)
{
cout<<"buffer_y[index][1]="<<buffer_y[index][1]<<" buffer_y[index][2]="<<buffer_y[index][2]<<endl;
cout<<"buffer_y[index+1][1]="<<buffer_y[index+1][1]<<" buffer_y[index+1][2]="<<buffer_y[index+1][2]<<endl;
CY[0] = min(buffer_y[index][1],buffer_y[index+1][1]);
CY[1] = buffer_y[index][2]+buffer_y[index+1][2];
cout<<"CY[0]="<<CY[0]<<"CY[1]="<<CY[1]<<endl;
}
void compute(int i)
{
float CCX[2];
float CCY[2];
calc_x(i);
CCX[0] = CX[0];
CCX[1] = CX[1];
if(CX[0]>CX[1])
{
cout<<"B[0][1]="<<buffer[i][1]<<"B[0][2]="<<buffer[i][2];
swap(buffer[i][1],buffer[i][2]);
cout<<"B[0][1]="<<buffer[i][1]<<"B[0][2]="<<buffer[i][2]<<endl;
calc_x(i);
}
else
{
cout<<"B[i+1][1]="<<buffer[i+1][1]<<"B[i+1][2]="<<buffer[i+1][2];
swap(buffer[i+1][1],buffer[i+1][2]);
cout<<"B[i+1][1]="<<buffer[i+1][1]<<"B[i+1][2]="<<buffer[i+1][2]<<endl;
calc_x(i);
}
if((CCX[0]*CCX[1])<=(CX[0]*CX[1]))
{
if(CCX[0]>CCX[1])
{
cout<<"Swapped [i] rows"<<endl;
swap(buffer[i][1],buffer[i][2]);
calc_x(i);
}
else
{
cout<<"Swapped [i+1] rows"<<endl;
swap(buffer[i+1][1],buffer[i+1][2]);
calc_x(i);
}
}
calc_y(i);
CCY[0] = CY[0];
CCY[1] = CY[1];
if(CY[0]>CY[1])
{
cout<<"BY[0][1]="<<buffer_y[i][1]<<"BY[0][2]="<<buffer_y[i][2];
swap(buffer_y[i][1],buffer_y[i][2]);
cout<<"BY[0][1]="<<buffer_y[i][1]<<"BY[0][2]="<<buffer_y[i][2]<<endl;
calc_y(i);
}
else
{
cout<<"BY[i+1][1]="<<buffer_y[i+1][1]<<"BY[i+1][2]="<<buffer_y[i+1][2];
swap(buffer_y[i+1][1],buffer_y[i+1][2]);
cout<<"BY[i+1][1]="<<buffer_y[i+1][1]<<"BY[i+1][2]="<<buffer_y[i+1][2]<<endl;
calc_y(i);
}
if((CCY[0]*CCY[1])<=(CY[0]*CY[1]))
{
if(CCY[0]>CCY[1])
{
cout<<"Swapped [i] rows"<<endl;
swap(buffer_y[i][1],buffer_y[i][2]);
calc_y(i);
}
else
{
cout<<"Swapped [i+1] rows"<<endl;
swap(buffer_y[i+1][1],buffer_y[i+1][2]);
calc_y(i);
}
}
if((CX[0]*CX[1])<(CY[0]*CY[0]))
{
data[i][1] = buffer[i][1];
data[i][2] = buffer[i][2];
data[i+1][1] = buffer[i+1][1];
data[i+1][2] = buffer[i+1][2];
// IF Min product is X then copy value of buffer_x to buffer_y and update same to data
buffer_y[i][1] = buffer[i][1];
buffer_y[i][2] = buffer[i][2];
buffer_y[i+1][1] = buffer[i+1][1];
buffer_y[i+1][2] = buffer[i+1][2];
}
else
{
data[i][1] = buffer_y[i][1];
data[i][2] = buffer_y[i][2];
data[i+1][1] = buffer_y[i+1][1];
data[i+1][2] = buffer_y[i+1][2];
// IF Min product is Y then copy value of buffer_Y to buffer_x and update same to data
buffer[i][1] = buffer_y[i][1];
buffer[i][2] = buffer_y[i][2];
buffer[i+1][1] = buffer_y[i+1][1];
buffer[i+1][2] = buffer_y[i+1][2];
}
}
void count_lines()
{
string line;
ifstream file("Test.txt");
while (getline(file, line))
maxrow++;
file.close();
}
void read_file()
{
int i,j;
fp1 = fopen("Test.txt","r");
for(i=0;i<maxrow;i++)
{
fscanf (fp1, "%s %f %f", str2[i] ,&buffer[i][1],&buffer[i][2]);
buffer[i][3] = 0;
data[i][1] = buffer[i][1];
data[i][2] = buffer[i][2];
data[i][3] = buffer[i][3];
buffer_y[i][1] = buffer[i][1];
buffer_y[i][2] = buffer[i][2];
buffer_y[i][3] = buffer[i][3];
printf(".......");
}
printf("\n");
fclose(fp1);
}
void write_file()
{
int i,j;
fp1 = fopen("Output.txt","w");
printf("\nWriting to output.txt");
for(i=0;i<maxrow;i++)
{
fprintf(fp1,"%s", str2[i]);
fprintf(fp1," ");
for(j=1;j<4;j++)
{
fprintf(fp1,"%f", data[i][j]);
fprintf(fp1," ");
}
fprintf(fp1,"\n");
}
printf("\n");
fclose(fp1);
}
void display_array()
{
int i,j;
for(i=0;i<maxrow;i++)
{
for(j=1;j<4;j++)
{
printf("%f ",data[i][j]);
}
printf("\n");
}
}
void swap(float &x, float &y) {
float temp;
temp = x;
x = y;
y = temp;
return;
}