-1

So i have one StringGrid called StringGrid1 with FixedRow and Column set to 1,i have 6 Columns and 2 rows. I have one edit button called EdBrUc,i have three buttons one BtUnesi, second Button1 and third Button 2 now i am supposed to do a function,with adding three numbers together then well here is the function

function tform1.sabiranje(a, b, c: tniz): real;
var
  sab: real;
  i: integer;
begin
  for i := 1 to n do
  begin
    sab := (a[i] + b[i] + c[i]) / 3;
  end;
  sabiranje := sab;
end;

so with that done, i am supposed to use that to "see" what grade did the student get? One test can have 100 points,so three tests = Max point = 300 and then if i / 3 it is 100 so that is the highest number of points a student can have.

now i am supposed to write that into the sixth column of the String Grid so

procedure TForm1.Button2Click(Sender: TObject);
var
  clan, ocenaa: real;
  i: integer;
begin
  for i := 1 to n do
  begin
    clan := StrToFloat(StringGrid1.Cells[2, i]);
    a[i] := clan;
    clan := StrToFloat(StringGrid1.Cells[3, i]);
    b[i] := clan;
    clan := StrToFloat(StringGrid1.Cells[4, i]);
    c[i] := clan;

    begin
      ocenaa := sabiranje(a, b, c);
      if StringGrid1.Cells[5, i] = 'pao' then
        StringGrid1.Cells[6, i] := '5'
      else if (ocenaa > 50) and (ocenaa < 64) then
        StringGrid1.Cells[6, i] := '6'
      else if (ocenaa > 64) and (ocenaa < 74) then
        StringGrid1.Cells[6, i] := '7'
      else if (ocenaa > 74) and (ocenaa < 84) then
        StringGrid1.Cells[6, i] := '8'
      else if (ocenaa > 84) and (ocenaa < 94) then
        StringGrid1.Cells[6, i] := '9'
      else if (ocenaa = 100) and (ocenaa > 94) then
        StringGrid1.Cells[6, i] := '10';
    end;
  end;
end;

for some reason it just wont work...here is the WHOLE CODE.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids;

type
  tniz = array [1 .. 300] of real;

  TForm1 = class(TForm)
    EdBrUc: TEdit;
    LbBrUc: TLabel;
    BtUnesi: TButton;
    Button1: TButton;
    StringGrid1: TStringGrid;
    Button2: TButton;
    function sabiranje(a, b, c: tniz): real;
    procedure BtUnesiClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;

var
  a, b, c: tniz;
  n: integer;
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.sabiranje(a, b, c: tniz): real;
var
  sab: real;
  i: integer;
begin
  for i := 1 to n do
  begin
    sab := (a[i] + b[i] + c[i]) / 3;
  end;
  sabiranje := sab;
end;

procedure TForm1.BtUnesiClick(Sender: TObject);
begin
  n := StrToInt(EdBrUc.Text);
  StringGrid1.RowCount := n + 1;
  StringGrid1.Cells[0, 0] := 'Rb';
  StringGrid1.Cells[1, 0] := 'Ime i prezime';
  StringGrid1.Cells[2, 0] := 'Kolokvijum 1';
  StringGrid1.Cells[3, 0] := 'Kolokvijum 2';
  StringGrid1.Cells[4, 0] := 'Kolokvijum 3';
  StringGrid1.Cells[5, 0] := 'Pao ili prosao';
  StringGrid1.Cells[6, 0] := 'Ocena';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  clan, ocena: real;
  i: integer;
begin
  for i := 1 to n do
  begin
    clan := StrToFloat(StringGrid1.Cells[2, i]);
    a[i] := clan;
    clan := StrToFloat(StringGrid1.Cells[3, i]);
    b[i] := clan;
    clan := StrToFloat(StringGrid1.Cells[4, i]);
    c[i] := clan;

    if (a[i] > 50) and (b[i] > 50) and (c[i] > 50) then
      StringGrid1.Cells[5, i] := 'prosao'
    else
      StringGrid1.Cells[5, i] := 'pao';
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  clan, ocenaa: real;
  i: integer;
begin
  for i := 1 to n do
  begin
    clan := StrToFloat(StringGrid1.Cells[2, i]);
    a[i] := clan;
    clan := StrToFloat(StringGrid1.Cells[3, i]);
    b[i] := clan;
    clan := StrToFloat(StringGrid1.Cells[4, i]);
    c[i] := clan;
    begin
      ocenaa := sabiranje(a, b, c);
      if StringGrid1.Cells[5, i] = 'pao' then
        StringGrid1.Cells[6, i] := '5'
      else if (ocenaa > 50) and (ocenaa < 64) then
        StringGrid1.Cells[6, i] := '6'
      else if (ocenaa > 64) and (ocenaa < 74) then
        StringGrid1.Cells[6, i] := '7'
      else if (ocenaa > 74) and (ocenaa < 84) then
        StringGrid1.Cells[6, i] := '8'
      else if (ocenaa > 84) and (ocenaa < 94) then
        StringGrid1.Cells[6, i] := '9'
      else if (ocenaa = 100) and (ocenaa > 94) then
        StringGrid1.Cells[6, i] := '10';
    end;
  end;
end;

end.
4

1 回答 1

1

这个功能没用。你总是得到sab:=(a[n]+b[n]+c[n])/3;
你从 1 覆盖到 n的值,sab并且只得到“如果 i=n”最后一个值。

function  tform1.sabiranje(a,b,c:tniz):real;
var
sab:real;
i:integer;

begin
 for i:=1 to n do
 begin
  sab:=(a[i]+b[i]+c[i])/3;
 end;
 sabiranje:=sab;
end;

我不知道你在做什么。
但我认为你只想添加 1 行 .cells。

function  tform1.sabiranje(a,b,c:tniz; at:integer):real;

begin
  result := (a[at]+b[at]+c[at])/3;
end;

得到它

 for i := 1 to n do
  begin
    clan := StrToFloat(StringGrid1.Cells[2, i]);
    a[i] := clan;
    clan := StrToFloat(StringGrid1.Cells[3, i]);
    b[i] := clan;
    clan := StrToFloat(StringGrid1.Cells[4, i]);
    c[i] := clan;

    ocenaa := sabiranje(a, b, c, i);
 [...]

如果您只想将值从 95 到 100,那是错误的。

if (ocenaa=100) and (ocenaa>94) then StringGrid1.Cells[6,i]:='10';

应该

if (ocenaa < 101) and (ocenaa>94) then StringGrid1.Cells[6,i]:='10';

记住你正在处理真实的

94.0000001 > 94

你应该圆它。

 function Round ( const Number : Extended ) : Int64;

Delphi怎么圆

于 2013-06-06T21:17:02.013 回答