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

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

Вернуться   Форум программистов > Delphi программирование > Lazarus, Free Pascal, CodeTyphon
Регистрация

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

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

Ответ
 
Опции темы Поиск в этой теме
Старый 05.04.2016, 18:40   #1
serge-first
Пользователь
 
Регистрация: 27.02.2013
Сообщений: 66
По умолчанию Табличный процессор / Доработки

Привет! Я уже наверное надоел со своими глупыми вопросами, этот должен быть последний. Спасибо) Итак, еле-еле общими усилиями троечников был создан табличный процессор:

Безымянный.jpg

Код:
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Grids;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    SG1: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure SG1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var Col,Row:integer;
begin
 SG1.ColCount:=11;
 SG1.RowCount:=11;
 for Col:= 1 to SG1.ColCount - 1 do
 SG1.Cells[Col,0]:='Столбец '+InttoStr(Col);
 for Row:=1 to SG1.RowCount-1 do
 SG1.Cells[0,Row]:='Строка '+InttoStr(Row);

 SG1.Width:=SG1.DefaultColWidth*SG1.ColCount;
 SG1.Height:=SG1.DefaultRowHeight*SG1.RowCount;

end;

procedure TForm1.Button1Click(Sender: TObject);
var Col, Row: integer;
    S :real;
begin
 for Col := SG1.Selection.Left to SG1.Selection.Right do
begin
 S:=0;
 for Row := SG1.Selection.Top to SG1.Selection.Bottom do
 if SG1.Cells[ Col , Row ]='' then S:=S + 0 else
 S:=S + StrToFloat( SG1.Cells[ Col , Row ]);

 SG1.Cells[ Col , SG1.Selection.Bottom + 1 ]:=FloatToStr( S );
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var Col,Row:integer;
    S:real;
begin
 for Col:=SG1.Selection.Left to SG1.Selection.Right do
begin
 S:=0;
 for Row:=SG1.Selection.Top to SG1.Selection.Bottom do
 if SG1.Cells[Col,Row]='' then S:=S+0 else
 S:=S+StrToFloat(SG1.Cells[Col,Row]);

 SG1.Cells[Col,SG1.Selection.Bottom+2]:=FloatToStr(S/(SG1.Selection.Bottom-SG1.Selection.Top+1));

end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var Col,Row:integer;
    max,m:real;
begin
 for Col:=SG1.Selection.Left to SG1.Selection.Right do
begin
 if SG1.Cells[Col,SG1.Selection.Top ]='' then max:=0 else
 max:=StrToFloat(SG1.Cells[Col,SG1.Selection.Top]);
 for Row:=SG1.Selection.Top+1 to SG1.Selection.Bottom do
begin
 if SG1.Cells[Col,Row]='' then m:=0 else
 m:=StrToFloat(SG1.Cells[Col,Row]);
 if max<m then max:=m;

end;

 SG1.Cells[Col,SG1.Selection.Bottom+3]:=FloatToStr(max);

end;
end;

procedure TForm1.Button4Click(Sender: TObject);
var Col,Row:integer;
    min,m:real;
begin
 for Col:=SG1.Selection.Left to SG1.Selection.Right do
begin
 if SG1.Cells[Col,SG1.Selection.Top]='' then min:=0 else
 min:=StrToFloat(SG1.Cells[Col,SG1.Selection.Top ]);
 for Row:=SG1.Selection.Top+1 to SG1.Selection.Bottom do
begin
 if SG1.Cells[Col,Row]='' then m:=0 else
 m:=StrToFloat(SG1.Cells[Col,Row]);
 if min>m then min:=m;
end;

 SG1.Cells[Col,SG1.Selection.Bottom+4]:=FloatToStr(min);

end;
end;

procedure TForm1.Button5Click(Sender: TObject);
var Col,Row:integer;
begin
 randomize;
 for Col:=SG1.Selection.Left to SG1.Selection.Right do
 for Row:=SG1.Selection.Top to SG1.Selection.Bottom do
 //SG1.Cells[Col,Row]:=IntToStr(random(Edit2.Text)-(Edit1.Text)+Edit1.Text));
 SG1.Cells[Col,Row]:=IntToStr(random(StrToInt(Edit2.Text)-StrToInt(Edit1.Text)+1)+StrToInt(Edit1.Text));
end;

procedure TForm1.Button6Click(Sender: TObject);
var Col,Row:integer;
    S:real;
begin
 for Row:=SG1.Selection.Top to SG1.Selection.Bottom do
begin
 S:=0;
 for Col:=SG1.Selection.Left to SG1.Selection.Right do
 if SG1.Cells[Col,Row]='' then S:=S+0 else  //S+0
 S:=S+StrToFloat(SG1.Cells[Col,Row]);

 SG1.Cells[SG1.Selection.Right+1,Row]:=FloatToStr(S);

end;
end;

procedure TForm1.Button7Click(Sender: TObject);
var Col,Row:integer;
    S:real;
begin
 for Row:=SG1.Selection.Top to SG1.Selection.Bottom do
begin
 S:=0;
 for Col:=SG1.Selection.Left to SG1.Selection.Right do
 if SG1.Cells[Col,Row ]='' then S:=S+0 else
 S:=S+StrToFloat(SG1.Cells[Col,Row]);
 //S/SG1+1
 SG1.Cells[SG1.Selection.Right+2,Row]:=FloatToStr(S/(SG1.Selection.Right-SG1.Selection.Left+1));
end;
end;

procedure TForm1.Button8Click(Sender: TObject);
var Col,Row:integer;
    max,m:real;
begin
 for Row:=SG1.Selection.Top to SG1.Selection.Bottom do
begin
 if SG1.Cells[SG1.Selection.Left,Row]='' then max:=0 else
 max:=StrToFloat(SG1.Cells[SG1.Selection.Left,Row]);;
 for Col:=SG1.Selection.Left+1 to SG1.Selection.Right do
begin
 if SG1.Cells[Col,Row]='' then m:=0 else
 m:=StrToFloat(SG1.Cells[Col,Row]);
 if max<m then max:=m;

end;

 SG1.Cells[SG1.Selection.Right+3,Row]:=FloatToStr(max);

end;
end;

procedure TForm1.Button9Click(Sender: TObject);
var Col,Row:integer;
    min,m:real;
begin
 for Row:=SG1.Selection.Top to SG1.Selection.Bottom do
begin
 if SG1.Cells[SG1.Selection.Left,Row]='' then min:=0 else
 min:=StrToFloat(SG1.Cells[SG1.Selection.Left,Row]);;
 for Col:=SG1.Selection.Left+1 to SG1.Selection.Right do
begin
 if SG1.Cells[Col,Row]='' then m:=0 else
 m:=StrToFloat(SG1.Cells[Col,Row]);
 if min>m then min:=m;

end;

 SG1.Cells[SG1.Selection.Right+4,Row]:=FloatToStr(min);

end;
end;

procedure TForm1.SG1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 Label3.Caption:=' Координаты левой верхней ячейки [' + IntToStr(
 SG1.Selection.Left ) + ' , ' + IntToStr(SG1.Selection.Top)+ '] ';
 Label4.Caption:=' Координаты правой нижней ячейки [' + IntToStr(
 SG1.Selection.Right ) + ' , ' + IntToStr(SG1.Selection.Bottom)+ '] ';
end;


end.
Но в нём есть ошибки и нужна доработка:
  1. неверно функционирую кнопки МАКС и МИН
  2. нужна кнопка "Среднее значение", которая будет вычислять среднее значение всей выделенной области
serge-first вне форума Ответить с цитированием
Старый 11.04.2016, 21:48   #2
serge-first
Пользователь
 
Регистрация: 27.02.2013
Сообщений: 66
По умолчанию

Исправил кнопки МАКС и МИН

Код:
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Grids;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button10: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    SG1: TStringGrid;
    procedure Button10Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure SG1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var Col,Row:integer;
begin
 SG1.ColCount:=11;
 SG1.RowCount:=11;
 for Col:= 1 to SG1.ColCount - 1 do
 SG1.Cells[Col,0]:='Столбец '+InttoStr(Col);
 for Row:=1 to SG1.RowCount-1 do
 SG1.Cells[0,Row]:='Строка '+InttoStr(Row);

 SG1.Width:=SG1.DefaultColWidth*SG1.ColCount;
 SG1.Height:=SG1.DefaultRowHeight*SG1.RowCount;

end;

procedure TForm1.Button10Click(Sender: TObject);
var Col,Row: integer;
    S :real;
begin
 for Col:= SG1.Selection.Left to SG1.Selection.Right do;
 for Row:= SG1.Selection.Top to SG1.Selection.Bottom do;
 if SG1.Cells[Col,Row]=' 'then S:=S+0 else
 S:=S+StrToFloat(SG1.Cells[Col,Row]);

 SG1.Cells[SG1.Selection.Right+1,SG1.Selection.Bottom+1]:=FloatToStr(S/SG1.Selection.Bottom);

end;

procedure TForm1.Button1Click(Sender: TObject);
var Col, Row: integer;
    S :real;
begin
 for Col := SG1.Selection.Left to SG1.Selection.Right do
begin
 S:=0;
 for Row := SG1.Selection.Top to SG1.Selection.Bottom do
 if SG1.Cells[ Col , Row ]=' 'then S:=S + 0 else
 S:=S + StrToFloat( SG1.Cells[ Col , Row ]);

 SG1.Cells[ Col , SG1.Selection.Bottom + 1 ]:=FloatToStr( S );
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var Col,Row:integer;
    S:real;
begin
 for Col:=SG1.Selection.Left to SG1.Selection.Right do
begin
 S:=0;
 for Row:=SG1.Selection.Top to SG1.Selection.Bottom do
 if SG1.Cells[Col,Row]=' 'then S:=S+0 else
 S:=S+StrToFloat(SG1.Cells[Col,Row]);

 SG1.Cells[Col,SG1.Selection.Bottom+2]:=FloatToStr(S/(SG1.Selection.Bottom-SG1.Selection.Top+1));

end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var Col,Row:integer;
    max,m:real;
begin
 for Col:=SG1.Selection.Left to SG1.Selection.Right do
begin
 if SG1.Cells[Col,SG1.Selection.Top ]=' 'then max:=0 else
 max:=StrToFloat(SG1.Cells[Col,SG1.Selection.Top]);
 for Row:=SG1.Selection.Top+1 to SG1.Selection.Bottom do
begin
 if SG1.Cells[Col,Row]=' 'then m:=0 else
 m:=StrToFloat(SG1.Cells[Col,Row]);
// if max<m then max:=m;
 if max>m then max:=m;

end;

 SG1.Cells[Col,SG1.Selection.Bottom+3]:=FloatToStr(max);

end;
end;

procedure TForm1.Button4Click(Sender: TObject);
var Col,Row:integer;
    min,m:real;
begin
 for Col:=SG1.Selection.Left to SG1.Selection.Right do
begin
 if SG1.Cells[Col,SG1.Selection.Top]=' 'then min:=0 else
 min:=StrToFloat(SG1.Cells[Col,SG1.Selection.Top ]);
 for Row:=SG1.Selection.Top+1 to SG1.Selection.Bottom do
begin
 if SG1.Cells[Col,Row]=' 'then m:=0 else
 m:=StrToFloat(SG1.Cells[Col,Row]);
// if min>m then min:=m;
 if min<m then min:=m;
end;

 SG1.Cells[Col,SG1.Selection.Bottom+4]:=FloatToStr(min);

end;
end;

procedure TForm1.Button5Click(Sender: TObject);
var Col,Row:integer;
begin
 randomize;
 for Col:=SG1.Selection.Left to SG1.Selection.Right do
 for Row:=SG1.Selection.Top to SG1.Selection.Bottom do
 //SG1.Cells[Col,Row]:=IntToStr(random(Edit2.Text)-(Edit1.Text)+Edit1.Text));
 SG1.Cells[Col,Row]:=IntToStr(random(StrToInt(Edit2.Text)-StrToInt(Edit1.Text)+1)+StrToInt(Edit1.Text));
end;

procedure TForm1.Button6Click(Sender: TObject);
var Col,Row:integer;
    S:real;
begin
 for Row:=SG1.Selection.Top to SG1.Selection.Bottom do
begin
 S:=0;
 for Col:=SG1.Selection.Left to SG1.Selection.Right do
 if SG1.Cells[Col,Row]=' 'then S:=S+0 else  //S+0
 S:=S+StrToFloat(SG1.Cells[Col,Row]);

 SG1.Cells[SG1.Selection.Right+1,Row]:=FloatToStr(S);

end;
end;

procedure TForm1.Button7Click(Sender: TObject);
var Col,Row:integer;
    S:real;
begin
 for Row:=SG1.Selection.Top to SG1.Selection.Bottom do
begin
 S:=0;
 for Col:=SG1.Selection.Left to SG1.Selection.Right do
 if SG1.Cells[Col,Row ]=' 'then S:=S+0 else
 S:=S+StrToFloat(SG1.Cells[Col,Row]);
 //S/SG1+1
 SG1.Cells[SG1.Selection.Right+2,Row]:=FloatToStr(S/(SG1.Selection.Right-SG1.Selection.Left+1));
end;
end;

procedure TForm1.Button8Click(Sender: TObject);
var Col,Row: integer;
    min:real;
begin
 for Row:=SG1.Selection.Top to SG1.Selection.Bottom do
begin
 if SG1.Cells[SG1.Selection.Left,Row]=' 'then min:=0 else
 min:=StrToFloat(SG1.Cells[SG1.Selection.Left,Row]);
 for Col:=SG1.Selection.Left+1 to SG1.Selection.Right do
 if SG1.Cells[Col,Row]=' 'then
begin
 if min>0 then min:=0;
end
 else if min>StrToFloat(SG1.Cells[Col,Row]) then min:=StrToFloat(SG1.Cells[Col,Row]);
 SG1.Cells[SG1.Selection.Right+1,Row]:=FloatToStr(min);
end;
end;

procedure TForm1.Button9Click(Sender: TObject);
var Col,Row: integer;
    max:real;
begin
 for Row:=SG1.Selection.Top to SG1.Selection.Bottom do
begin
 if SG1.Cells[SG1.Selection.Left,Row]=' 'then max:=0 else
 max:=StrToFloat(SG1.Cells[SG1.Selection.Left,Row]);
 for Col:=SG1.Selection.Left+1 to SG1.Selection.Right do
 if SG1.Cells[Col,Row]=' 'then
begin
 if max<0 then max:=0;
end
 else if max<StrToFloat(SG1.Cells[Col,Row]) then max:=StrToFloat(SG1.Cells[Col,Row]);
 SG1.Cells[SG1.Selection.Right+1,Row]:=FloatToStr(max);
end;

end;

procedure TForm1.SG1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 Label3.Caption:=' Координаты левой верхней ячейки [' + IntToStr(
 SG1.Selection.Left ) + ' , ' + IntToStr(SG1.Selection.Top)+ '] ';
 Label4.Caption:=' Координаты правой нижней ячейки [' + IntToStr(
 SG1.Selection.Right ) + ' , ' + IntToStr(SG1.Selection.Bottom)+ '] ';
end;


end.
serge-first вне форума Ответить с цитированием
Старый 11.04.2016, 21:49   #3
serge-first
Пользователь
 
Регистрация: 27.02.2013
Сообщений: 66
По умолчанию

Добавил кнопку среднего значения выделенной всей области, но где-то косяк, не правильно считывает среднее значение:

Код:
procedure TForm1.Button10Click(Sender: TObject);
var Col,Row: integer;
    S :real;
begin
 for Col:= SG1.Selection.Left to SG1.Selection.Right do;
 for Row:= SG1.Selection.Top to SG1.Selection.Bottom do;
 if SG1.Cells[Col,Row]=' 'then S:=S+0 else
 S:=S+StrToFloat(SG1.Cells[Col,Row]);

 SG1.Cells[SG1.Selection.Right+1,SG1.Selection.Bottom+1]:=FloatToStr(S/SG1.Selection.Bottom);

end;
serge-first вне форума Ответить с цитированием
Старый 12.04.2016, 07:57   #4
serge-first
Пользователь
 
Регистрация: 27.02.2013
Сообщений: 66
По умолчанию

Вроде так:

Код:
procedure TForm1.Button10Click(Sender: TObject);
var Col,Row: integer;

begin
 for Col:= SG1.Selection.Left to SG1.Selection.Right do
 for Row:= SG1.Selection.Top to SG1.Selection.Bottom do
 if SG1.Cells[Col,Row]=' 'then S:=S+0 else
 S:=S+StrToFloat(SG1.Cells[Col,Row]);

 SG1.Cells[SG1.Selection.Right+1,SG1.Selection.Bottom+1]:=FloatToStr(S/(Col*Row));

S:=0;

end;

Последний раз редактировалось serge-first; 12.04.2016 в 08:09.
serge-first вне форума Ответить с цитированием
Старый 12.04.2016, 08:10   #5
serge-first
Пользователь
 
Регистрация: 27.02.2013
Сообщений: 66
По умолчанию

Хотя опять не так, кто подскажет, где ошибка?
serge-first вне форума Ответить с цитированием
Старый 12.04.2016, 08:23   #6
Аватар
Старожил
 
Аватар для Аватар
 
Регистрация: 17.11.2010
Сообщений: 19,042
По умолчанию

Код:
SG1.Cells[SG1.Selection.Right+1,SG1.Selection.Bottom+1]:=FloatToStr(S/((SG1.Selection.Right-SG1.Selection.Left+1)*(SG1.Selection.Bottom-SG1.Selection.Top+1)));
Если бы архитекторы строили здания так, как программисты пишут программы, то первый залетевший дятел разрушил бы цивилизацию
Аватар вне форума Ответить с цитированием
Старый 12.04.2016, 08:28   #7
serge-first
Пользователь
 
Регистрация: 27.02.2013
Сообщений: 66
По умолчанию

Цитата:
Сообщение от Аватар Посмотреть сообщение
Код:
SG1.Cells[SG1.Selection.Right+1,SG1.Selection.Bottom+1]:=FloatToStr(S/((SG1.Selection.Right-SG1.Selection.Left+1)*(SG1.Selection.Bottom-SG1.Selection.Top+1)));
Блин)

Спасибо)))

serge-first вне форума Ответить с цитированием
Ответ


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

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

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


Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
QT, C++, Табличный компонент Алексей_2012 Общие вопросы C/C++ 5 25.08.2015 16:39
Табличный CheckListBox bilibian Общие вопросы Delphi 4 18.10.2014 10:11
Табличный вывод.Язык Си. East Undia Trading Помощь студентам 4 04.05.2014 22:46
Табличный компонент для работы с СУБД. Небесный Компоненты Delphi 11 02.01.2013 19:38
табличный вывод данных zatoichi Общие вопросы Delphi 1 28.03.2008 08:23