Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 18.05.2022, 18:26   #1
mr.stranger
Пользователь
 
Регистрация: 02.05.2022
Сообщений: 52
По умолчанию Ошибка в сортировке

В сортировке по числовому признаку, смотрит только на первую цифру, а не на все число
Помогите пожалуйста исправить

Код:
procedure TForm1.Button1Click(Sender: TObject);
 var a,b,c,f1,f2,f3,q1,q2,q3,q4:integer;
begin
q1 := 0;
q2 := 0;
q3 := 0;
q4 := 0;

for a := 1 to StringGrid1.RowCount-1 do
if StringGrid1.Cells[1,a] = Trim(Edit1.Text) then
q1 := 1;
for a := 1 to StringGrid1.RowCount-1 do
if StringGrid1.Cells[2,a] = Trim(Edit2.Text) then
q2 := 1;
for a := 1 to StringGrid1.RowCount-1 do
if StringGrid1.Cells[3,a] = Trim(Edit3.Text) then
q3 := 1;
q4 := q1 + q2 + q3;

Val(Trim(Edit1.Text),b,f1);
Val(Trim(Edit2.Text),b,f2);
Val(Trim(Edit3.Text),c,f3);

if (Length(Edit1.text) <> 0) and (Length(Edit2.text) <> 0) and (Length(Edit3.text) <> 0) and (f3 = 0) and (f1 <> 0) and (f2 <> 0) and (c > 0) then
begin
if q4 <> 3 then
begin
with StringGrid1 do
InsertRowWithValues(RowCount, [IntToStr(RowCount), Edit1.Text, Edit2.Text, Edit3.Text]);
end
else
ShowMessage('Дублированная запись');
end
else
ShowMessage('Ошибки в записи');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
 ms1,ms2,ms3:array of string;
 s1,s2,s3:string;
 i,j:integer;
begin
SetLength(ms1,StringGrid1.RowCount);
SetLength(ms2,StringGrid1.RowCount);
SetLength(ms3,StringGrid1.RowCount);

if RadioButton1.Checked = True then
begin

for i := 1 to StringGrid1.RowCount-1 do
begin
ms1[i] := StringGrid1.Cells[1,i];
ms2[i] := StringGrid1.Cells[2,i];
ms3[i] := StringGrid1.Cells[3,i];
end;
for i := 1 to StringGrid1.RowCount-1 do
for j := 1 to StringGrid1.RowCount-i-1 do
begin
if ms1[j] > ms1[j+1] then
begin
s1 := ms1[j];
ms1[j] := ms1[j+1];
ms1[j+1] := s1;

s2 := ms2[j];
ms2[j] := ms2[j+1];
ms2[j+1] := s2;

s3 := ms3[j];
ms3[j] := ms3[j+1];
ms3[j+1] := s3;
end;
end;
for i := 1 to StringGrid1.RowCount-1 do
begin
StringGrid1.Cells[1,i] := ms1[i];
StringGrid1.Cells[2,i] := ms2[i];
StringGrid1.Cells[3,i] := ms3[i];
end;

end
else
if RadioButton2.Checked = True then
begin

for i := 1 to StringGrid1.RowCount-1 do
begin
ms1[i] := StringGrid1.Cells[1,i];
ms2[i] := StringGrid1.Cells[2,i];
ms3[i] := StringGrid1.Cells[3,i];
end;
for i := 1 to StringGrid1.RowCount-1 do
for j := 1 to StringGrid1.RowCount-i-1 do
begin
if ms3[j] > ms3[j+1] then
begin
s1 := ms1[j];
ms1[j] := ms1[j+1];
ms1[j+1] := s1;

s2 := ms2[j];
ms2[j] := ms2[j+1];
ms2[j+1] := s2;

s3 := ms3[j];
ms3[j] := ms3[j+1];
ms3[j+1] := s3;
end;
end;
for i := 1 to StringGrid1.RowCount-1 do
begin
StringGrid1.Cells[1,i] := ms1[i];
StringGrid1.Cells[2,i] := ms2[i];
StringGrid1.Cells[3,i] := ms3[i];
end;
end;
end;
end.
Ниже - форма ввода в стринггрид
Изображения
Тип файла: png ва.png (4.8 Кб, 23 просмотров)

Последний раз редактировалось mr.stranger; 18.05.2022 в 18:41.
mr.stranger вне форума Ответить с цитированием
Старый 18.05.2022, 18:40   #2
macomics
Участник клуба
 
Регистрация: 17.04.2022
Сообщений: 1,833
По умолчанию

Если год записан цифрами, то сравнивайте так
Код:
if strtoint(ms3[j]) > strtoint(ms3[j+1]) then
А еще лучше ms3 объявите числовым и преобразуйте при загрузке и выгрузке один раз.
macomics вне форума Ответить с цитированием
Старый 18.05.2022, 18:45   #3
mr.stranger
Пользователь
 
Регистрация: 02.05.2022
Сообщений: 52
По умолчанию

Цитата:
Сообщение от macomics Посмотреть сообщение
if strtoint(ms3[j]) > strtoint(ms3[j+1]) then
Помогло, спасибо
mr.stranger вне форума Ответить с цитированием
Старый 18.05.2022, 19:26   #4
Valick
Форумчанин
 
Регистрация: 27.04.2022
Сообщений: 484
По умолчанию

Цитата:
Сообщение от macomics Посмотреть сообщение
А еще лучше ms3 объявите числовым и преобразуйте при загрузке и выгрузке один раз.
а это видимо для Александра Сергеевича...
Valick вне форума Ответить с цитированием
Старый 18.05.2022, 19:33   #5
macomics
Участник клуба
 
Регистрация: 17.04.2022
Сообщений: 1,833
По умолчанию

Пушкин писал на Русском. Паскаля тогда еще не придумали.
macomics вне форума Ответить с цитированием
Старый 24.05.2022, 22:32   #6
mr.stranger
Пользователь
 
Регистрация: 02.05.2022
Сообщений: 52
По умолчанию

Обновлено
Код:
procedure TForm1.Button2Click(Sender: TObject);
var
 ms1,ms2:array of string;
 ms3:array of integer;
 s1,s2,s3:string;
 i,j:integer;
begin
SetLength(ms1,StringGrid1.RowCount);
SetLength(ms2,StringGrid1.RowCount);
SetLength(ms3,StringGrid1.RowCount);

if RadioButton1.Checked = True then
begin
for i := 1 to StringGrid1.RowCount-1 do
begin
ms1[i] := StringGrid1.Cells[1,i];
ms2[i] := StringGrid1.Cells[2,i];
ms3[i] := StrToInt(StringGrid1.Cells[3,i]);
end;
for i := 1 to StringGrid1.RowCount-1 do
for j := 1 to StringGrid1.RowCount-i-1 do
begin
if ms1[j] > ms1[j+1] then
begin
s1 := ms1[j];
ms1[j] := ms1[j+1];
ms1[j+1] := s1;

s2 := ms2[j];
ms2[j] := ms2[j+1];
ms2[j+1] := s2;

s3 := IntToStr(ms3[j]);
ms3[j] := ms3[j+1];
ms3[j+1] := StrToInt(s3);

end;
end;
for i := 1 to StringGrid1.RowCount-1 do
begin
StringGrid1.Cells[1,i] := ms1[i];
StringGrid1.Cells[2,i] := ms2[i];
StringGrid1.Cells[3,i] := IntToStr(ms3[i]);
end;

end
else
if RadioButton2.Checked = True then
begin

for i := 1 to StringGrid1.RowCount-1 do
begin
ms1[i] := StringGrid1.Cells[1,i];
ms2[i] := StringGrid1.Cells[2,i];
ms3[i] := StrToInt(StringGrid1.Cells[3,i]);
end;
for i := 1 to StringGrid1.RowCount-1 do
for j := 1 to StringGrid1.RowCount-i-1 do
begin
if (ms3[j]) > (ms3[j+1]) then
begin
s1 := ms1[j];
ms1[j] := ms1[j+1];
ms1[j+1] := s1;

s2 := ms2[j];
ms2[j] := ms2[j+1];
ms2[j+1] := s2;

s3 := IntToStr(ms3[j]);
ms3[j] := ms3[j+1];
ms3[j+1] := StrToInt(s3);
end;
end;
for i := 1 to StringGrid1.RowCount-1 do
begin
StringGrid1.Cells[1,i] := ms1[i];
StringGrid1.Cells[2,i] := ms2[i];
StringGrid1.Cells[3,i] := IntToStr(ms3[i]);
end;
end
else
if RadioButton3.Checked = True then
begin
for i := 1 to StringGrid1.RowCount-1 do
begin
ms1[i] := StringGrid1.Cells[1,i];
ms2[i] := StringGrid1.Cells[2,i];
ms3[i] := StrToInt(StringGrid1.Cells[3,i]);
end;
for i := 1 to StringGrid1.RowCount-1 do
for j := 1 to StringGrid1.RowCount-i-1 do
begin
if (ms1[j] > ms1[j + 1]) or ((ms1[j] = ms1[j + 1]) and (ms3[j] > ms3[j + 1])) then
begin
s1 := ms1[j];
ms1[j] := ms1[j+1];
ms1[j+1] := s1;

s2 := ms2[j];
ms2[j] := ms2[j+1];
ms2[j+1] := s2;

s3 := IntToStr(ms3[j]);
ms3[j] := ms3[j+1];
ms3[j+1] := StrToInt(s3);

end;
end;
for i := 1 to StringGrid1.RowCount-1 do
begin
StringGrid1.Cells[1,i] := ms1[i];
StringGrid1.Cells[2,i] := ms2[i];
StringGrid1.Cells[3,i] := IntToStr(ms3[i]);
end;
end;
end;
mr.stranger вне форума Ответить с цитированием
Старый 24.05.2022, 23:08   #7
macomics
Участник клуба
 
Регистрация: 17.04.2022
Сообщений: 1,833
По умолчанию

Код:
for i := 1 to StringGrid1.RowCount-1 do
begin
ms1[i] := StringGrid1.Cells[1,i];
ms2[i] := StringGrid1.Cells[2,i];
ms3[i] := StrToInt(StringGrid1.Cells[3,i]);
end;
for i := 1 to StringGrid1.RowCount-1 do
for j := 1 to StringGrid1.RowCount-i-1 do
begin if ... then
begin
s1 := ms1[j];
ms1[j] := ms1[j+1];
ms1[j+1] := s1;

s2 := ms2[j];
ms2[j] := ms2[j+1];
ms2[j+1] := s2;

s3 := IntToStr(ms3[j]);
ms3[j] := ms3[j+1];
ms3[j+1] := StrToInt(s3);

end;
end;
for i := 1 to StringGrid1.RowCount-1 do
begin
StringGrid1.Cells[1,i] := ms1[i];
StringGrid1.Cells[2,i] := ms2[i];
StringGrid1.Cells[3,i] := IntToStr(ms3[i]);
end;
Зачем вы повторяете один и тот же код 3 раза?
Код:
SetLength(ms1,StringGrid1.RowCount);
SetLength(ms2,StringGrid1.RowCount);
SetLength(ms3,StringGrid1.RowCount);
А вот выделить память под массивы догадались сделать однократно

Код:
if ((RadioButton1.Checked) and (ms1[j] > ms1[j+1]))
or ((RadioButton2.Checked) and (ms3[j]) > (ms3[j+1]))
or ((RadioButton3.Checked) and ((ms1[j] > ms1[j + 1]) or ((ms1[j] = ms1[j + 1]) and (ms3[j] > ms3[j + 1]))))
then
...
Все три проверки одним условием

Последний раз редактировалось macomics; 24.05.2022 в 23:16.
macomics вне форума Ответить с цитированием
Старый 25.05.2022, 15:18   #8
mr.stranger
Пользователь
 
Регистрация: 02.05.2022
Сообщений: 52
По умолчанию

Спасибо вам огромное за помощь

Обновлено 2.0
Код:
procedure TForm1.Button1Click(Sender: TObject);
 var a,b,c,f1,f2,f3,q1,q2,q3,q4:integer;
begin
q1 := 0;
q2 := 0;
q3 := 0;
q4 := 0;

for a := 1 to StringGrid1.RowCount-1 do
if StringGrid1.Cells[1,a] = Trim(Edit1.Text) then
q1 := 1;
for a := 1 to StringGrid1.RowCount-1 do
if StringGrid1.Cells[2,a] = Trim(Edit2.Text) then
q2 := 1;
for a := 1 to StringGrid1.RowCount-1 do
if StringGrid1.Cells[3,a] = Trim(Edit3.Text) then
q3 := 1;
q4 := q1 + q2 + q3;

Val(Trim(Edit1.Text),b,f1);
Val(Trim(Edit2.Text),b,f2);
Val(Trim(Edit3.Text),c,f3);

if (Length(Edit1.text) <> 0) and (Length(Edit2.text) <> 0) and (Length(Edit3.text) <> 0) and (f3 = 0) and (f1 <> 0) and (f2 <> 0) and (c > 0) then
begin
if q4 <> 3 then
begin
with StringGrid1 do
InsertRowWithValues(RowCount, [IntToStr(RowCount), Edit1.Text, Edit2.Text, Edit3.Text]);
end
else
ShowMessage('Дубль');
end
else
ShowMessage('Ошибки в записи');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
 ms1,ms2:array of string;
 ms3:array of integer;
 s1,s2,s3:string;
 i,j:integer;
begin
SetLength(ms1,StringGrid1.RowCount);
SetLength(ms2,StringGrid1.RowCount);
SetLength(ms3,StringGrid1.RowCount);

for i := 1 to StringGrid1.RowCount-1 do
begin
ms1[i] := StringGrid1.Cells[1,i];
ms2[i] := StringGrid1.Cells[2,i];
ms3[i] := StrToInt(StringGrid1.Cells[3,i]);
end;
for i := 1 to StringGrid1.RowCount-1 do
for j := 1 to StringGrid1.RowCount-i-1 do
begin
if ((RadioButton1.Checked) and (ms1[j] > ms1[j+1])) or ((RadioButton2.Checked) and (ms3[j] > ms3[j+1])) or ((RadioButton3.Checked) and ((ms1[j] > ms1[j + 1]) or ((ms1[j] = ms1[j + 1]) and (ms3[j] > ms3[j + 1])))) then
begin
s1 := ms1[j];
ms1[j] := ms1[j+1];
ms1[j+1] := s1;

s2 := ms2[j];
ms2[j] := ms2[j+1];
ms2[j+1] := s2;

s3 := IntToStr(ms3[j]);
ms3[j] := ms3[j+1];
ms3[j+1] := StrToInt(s3);

end;
end;
for i := 1 to StringGrid1.RowCount-1 do
begin
StringGrid1.Cells[1,i] := ms1[i];
StringGrid1.Cells[2,i] := ms2[i];
StringGrid1.Cells[3,i] := IntToStr(ms3[i]);
end;
for i := 1 to StringGrid1.RowCount-1 do
begin
StringGrid1.Cells[1,i] := ms1[i];
StringGrid1.Cells[2,i] := ms2[i];
StringGrid1.Cells[3,i] := IntToStr(ms3[i]);
end;
end;
mr.stranger вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц

Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск


Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Ошибка при сортировке Fenix1987 БД в Delphi 2 22.06.2012 10:20
ошибка в сортировке массива gylayko Помощь студентам 0 15.11.2011 20:56
Ошибка при сортировке Chikanog Microsoft Office Access 2 05.01.2011 13:08
Где ошибка в сортировке? vaan.sk Общие вопросы C/C++ 1 16.03.2010 23:33
Ошибка в сортировке Veiron Общие вопросы Delphi 14 21.01.2008 21:35