-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkstates.pas
275 lines (251 loc) · 7.25 KB
/
kstates.pas
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
unit KStates;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
Spin, Buttons, StrUtils, LCLTranslator;
const
maxwidth: double = 60;
type
{ TKeyStatesForm }
TKeyStatesForm = class(TForm)
LabeledEditKeyStates: TLabeledEdit;
LabelFrequencyTable: TLabel;
OKButton: TButton;
CancelButton: TButton;
ComboBoxCharacters: TComboBox;
LabelCharacters: TLabel;
LabelClasses: TLabel;
MemoResults: TMemo;
Panel1: TPanel;
RadioButtonSquareRoot: TRadioButton;
RadioButtonSturgesRule: TRadioButton;
ClearButton: TSpeedButton;
SpinEditClasses: TSpinEdit;
procedure ClearButtonClick(Sender: TObject);
procedure ComboBoxCharactersChange(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormShow(Sender: TObject);
procedure OKButtonClick(Sender: TObject);
procedure RadioButtonSquareRootClick(Sender: TObject);
procedure RadioButtonSturgesRuleClick(Sender: TObject);
procedure SpinEditClassesChange(Sender: TObject);
procedure SpinEditClassesEditingDone(Sender: TObject);
private
public
KS: string;
end;
var
KeyStatesForm: TKeyStatesForm;
Data: array of array of string;
M, N, NC: integer;
KeyStatesList: TStringList;
implementation
uses
Math, Main, Delta;
{$R *.lfm}
{$I resources.inc}
procedure GetPairvalues(const Str: string; var Left, Sep, Right: string);
var
I: integer = 1;
Q: integer = 0;
D: integer = 1;
begin
Left := '';
Sep := '';
Right := '';
while I <= Length(Str) do
begin
if Str[I] = '(' then
Inc(Q)
else
if Str[I] = ')' then
Dec(Q)
else // Raise an error if neg...
if Q = 0 then
if not (Str[I] in ['-', '/']) then
case D of
1: Left += Str[I];
2: Right += Str[I];
end
else
begin
Sep := Str[I];
Inc(D);
end;
Inc(I);
end;
if Pos(',', Left) > 0 then
Left := Copy(Left, 2);
end;
{ TKeyStatesForm }
procedure TKeyStatesForm.RadioButtonSquareRootClick(Sender: TObject);
begin
NC := Round(Sqrt(N));
SpinEditClasses.Value := NC;
RadioButtonSquareRoot.Checked := True;
RadioButtonSturgesRule.Checked := False;
KeyStatesList.Clear;
ComboBoxCharacters.OnChange(Self);
end;
procedure TKeyStatesForm.FormShow(Sender: TObject);
var
I, J, M: integer;
Attribute, L, S, R: string;
begin
KeyStatesList := TStringList.Create;
KeyStatesList.Delimiter := ' ';
KeyStatesList.StrictDelimiter := True;
N := Length(Dataset.ItemList);
M := Length(Dataset.CharacterList);
SetLength(Data, N, M);
for I := 0 to Length(Dataset.ItemList) - 1 do
begin
for J := 0 to Length(Dataset.CharacterList) - 1 do
begin
if (Dataset.CharacterList[J].charType = 'IN') or
(Dataset.CharacterList[J].charType = 'RN') then
begin
Attribute := Delta.RemoveComments(Dataset.ItemList[I].itemAttributes[J]);
GetPairValues(Attribute, L, S, R);
Data[I][J] := L + S + R;
end;
end;
end;
ComboBoxCharacters.Items.Clear;
for J := 0 to Length(Dataset.CharacterList) - 1 do
begin
if (Dataset.CharacterList[J].charType = 'IN') or
(Dataset.CharacterList[J].charType = 'RN') then
ComboBoxCharacters.Items.Add(IntToStr(J + 1) + '. ' +
Dataset.CharacterList[J].charName);
end;
ComboBoxCharacters.ItemIndex := 0;
NC := Round(Sqrt(N));
SpinEditClasses.Value := NC;
RadioButtonSquareRoot.Checked := True;
RadioButtonSturgesRule.Checked := False;
end;
procedure TKeyStatesForm.ComboBoxCharactersChange(Sender: TObject);
var
Work: array of double;
CharNo, I, J, K, Freq, NoOfStars: integer;
Temp, Min, Max, Range, Interval, Cibase, Scale, Datin: double;
KeyStatesStr, Sel, x, x1, x2: string;
Outfile: TextFile;
begin
Sel := ComboBoxCharacters.Items[ComboBoxCharacters.ItemIndex];
CharNo := StrToInt(Copy(Sel, 1, Pos('.', Sel) - 1));
KeyStatesStr := IntToStr(CharNo) + ',';
MemoResults.Lines.Clear;
SetLength(Work, N);
for I := 0 to N - 1 do
begin
x := Data[I][CharNo - 1];
if Length(x) > 0 then
begin
if (Pos('-', x) > 0) or (Pos('/', x) > 0) then
begin
x1 := ExtractDelimited(1, x, ['-', '/']);
x2 := ExtractDelimited(2, x, ['-', '/']);
Datin := (StrToFloatDef(x1, 0) + StrToFloatDef(x2, 0)) / 2;
end
else
Datin := StrToFloatDef(x, 0);
Work[I] := Datin;
end;
end;
AssignFile(Outfile, 'Results.txt');
Rewrite(Outfile);
if (NC < 2) then
NC := 2;
if (NC > 30) then
NC := 30;
for I := 0 to N - 1 do
begin
for J := 1 + i to N - 1 do
if (Work[I] > Work[J]) then
begin
Temp := Work[I];
Work[I] := Work[J];
Work[J] := Temp;
end;
end;
Min := Work[0];
Max := Work[N - 1];
Range := Max - Min;
Interval := Ceil(Range / NC) + 1;
Write(Outfile, strClass);
Write(Outfile, strInterval);
Write(Outfile, strFrequency);
//Write(Outfile, strBoundary);
Write(Outfile, strMark);
WriteLn(Outfile, strHistogram);
Scale := 1.0;
Cibase := Min;
//while(cibase <= max) do
for K := 0 to NC - 1 do
begin
Write(Outfile, (K + 1): 4, Cibase: 10: 0, '-', ((Cibase - 1) + Interval): 4: 0);
KeyStatesStr := KeyStatesStr + FloatToStr(Cibase) + '-' + FloatToStr(
(Cibase + 1) + Interval - 2) + '/';
Freq := 0;
for I := 0 to N - 1 do
begin
if (Work[i] >= Cibase) and (Work[i] <= ((Cibase - 1) + Interval)) then
Inc(Freq);
end;
Write(Outfile, ' ', Freq: 4, ' ');
//Write(Outfile, (Cibase - 0.5): 6: 1, '-', (Cibase - 0.5) + Interval: 6: 1);
Write(Outfile, ((Cibase + ((Cibase - 1) + Interval)) / 2): 9: 1, ' ');
NoOfStars := Round(Freq * Scale);
for J := 1 to NoOfStars do
Write(Outfile, '*');
WriteLn(Outfile);
Cibase := Cibase + Interval;
end;
//WriteLn(Outfile, 'Minimum: ', Min: 6: 1);
//WriteLn(Outfile, 'Maximum: ', Max: 6: 1);
//WriteLn(Outfile, 'Range: ', Range: 6: 1);
//WriteLn(Outfile, 'Interval: ', Interval: 6: 1);
KeyStatesStr := Copy(KeyStatesStr, 1, RPos('/', KeyStatesStr) - 1);
if KeyStatesList.IndexOf(KeyStatesStr) < 0 then
KeyStatesList.Add(KeyStatesStr);
CloseFile(Outfile);
MemoResults.Lines.LoadFromFile('Results.txt');
LabeledEditKeyStates.Text := KeyStatesList.DelimitedText; //KeyStatesStr;
DeleteFile('Results.txt');
end;
procedure TKeyStatesForm.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
KeyStatesList.Free;
end;
procedure TKeyStatesForm.ClearButtonClick(Sender: TObject);
begin
LabeledEditKeyStates.Clear;
end;
procedure TKeyStatesForm.OKButtonClick(Sender: TObject);
begin
KS := KeyStatesList.DelimitedText;
end;
procedure TKeyStatesForm.RadioButtonSturgesRuleClick(Sender: TObject);
begin
NC := Round(1 + 3.3 * Log10(Int(N)));
SpinEditClasses.Value := NC;
RadioButtonSturgesRule.Checked := True;
RadioButtonSquareRoot.Checked := False;
KeyStatesList.Clear;
ComboBoxCharacters.OnChange(Self);
end;
procedure TKeyStatesForm.SpinEditClassesChange(Sender: TObject);
begin
NC := SpinEditClasses.Value;
KeyStatesList.Clear;
ComboBoxCharacters.OnChange(Self);
end;
procedure TKeyStatesForm.SpinEditClassesEditingDone(Sender: TObject);
begin
RadioButtonSturgesRule.Checked := False;
RadioButtonSquareRoot.Checked := False;
end;
end.