浙江大学远程教育学院
《数据库应用程序设计》课程作业
7 姓名: 陈 元 学 号:
年级: 2013年秋 学习中心: 台州电大
—————————————————————————————
作业 第一章
1.5 如何保存Delphi的项目?尝试自己动手创建一个项目,并保存。
答:执行 All菜单命令或单击工具栏中的Save All按钮便可保存,在保存时可以对工程文件和单元文件进行改名,但后缀名不能改。保存文件之后,单击工具中的Run按钮或按F9键,系统将开始编译、连接、运行该工程。
1.7 尝试设计如图1-10所示的窗体。(图见教材P15页图1-10)
图1-10
答:设计界面:
运行界面:
第二章
2.8设计如图2-5所示的界面。单击“按钮1”或“按钮2”时在标签上显示用户所执行的操作。单击“开启/停用按钮”可控制“按钮1”和“按钮2”是否可用,单击“退出系统”按钮时,结束程序的运行。 答:设计界面:
运行界面:
代码: unit Unit1;
interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type
TForm1 = class(TForm) Button1: TButton; Button2: TButton; ctlButton: TButton; exitButton: TButton; showLabel: TLabel;
procedure Button1Click(Sender: TObject); //按钮1 procedure Button2Click(Sender: TObject); //按钮2
procedure ctlButtonClick(Sender: TObject); //开启停用按钮 procedure exitButtonClick(Sender: TObject); //退出按钮 private
{ Private declarations } public
{ Public declarations } end; var
Form1: TForm1;
ctlStatus:integer = 0; //控制按钮1和2的开启和关闭,0表示当前为开启,1表示关闭 implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject); begin
form1.showLabel.Caption := '您点击了按钮1'; end;
procedure TForm1.Button2Click(Sender: TObject); begin
form1.showLabel.Caption := '您点击了按钮2'; end;
procedure TForm1.exitButtonClick(Sender: TObject); begin
form1.Close; end;
procedure TForm1.ctlButtonClick(Sender: TObject); begin
if ctlStatus = 0 then begin
form1.Button1.Enabled := false; form1.Button2.Enabled := false; ctlStatus := 1; end else begin
form1.Button1.Enabled := true; form1.Button2.Enabled := true; ctlStatus := 0; end; end; end.
2.9 设计如图2-6所示的界面。当单击按钮时,可控制文本框中字体的颜色。
图2-5 答:设计界面
图2-6
运行界面:
代码:(按钮快捷键的设置方法为在字母前加&,如&R,&G,&B) unit test;
interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type
TForm1 = class(TForm) Label1: TLabel; Edit1: TEdit;
btnRed: TButton; btnGreen: TButton; btnBlue: TButton;
procedure btnRedClick(Sender: TObject); procedure btnGreenClick(Sender: TObject); procedure btnBlueClick(Sender: TObject);
private
{ Private declarations } public
{ Public declarations }
end; var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnRedClick(Sender: TObject); begin
form1.Edit1.Font.Color := clred; end;
procedure TForm1.btnGreenClick(Sender: TObject); begin
form1.Edit1.Font.Color := clgreen; end;
procedure TForm1.btnBlueClick(Sender: TObject); begin
form1.Edit1.Font.Color := clblue; end; End.
第三章
3.8 下列实数中哪些是合法的,哪些是不合法的?不合法的请说明理由。 (A)0.25E+02 (B).25+2 (C)25E+2 (D)34.5 (E).123 (F)-3E-4 答:(A)合法,即为25
(B)不合法,小数点前必须有数字,如表示为0.25+2 (C)合法,即为2500 (D)合法,即为34.5 (E)不合法,同(2) (F)合法,即为-0.0003
。
3.12 数学式子sin30写成Delphi表达式是下列哪个?
。
(A)Sin30 (B)Sin(30) (C)SIN(30) (D)Sin(30*Pi/180) 答:D,使用正切函数时需要把角度转化为弧度表示。
第四章
4.7 利用3个数字编辑框分别输入小时、分、秒,换算共有多少秒,然后使用标签输出。 答:设计界面:
运行界面:
代码: unit Unit1;
interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Spin; type
TForm1 = class(TForm) Label1: TLabel; Label2: TLabel; Label3: TLabel;
SpinEdit1: TSpinEdit; SpinEdit2: TSpinEdit; SpinEdit3: TSpinEdit; Label4: TLabel;
procedure SpinEdit1Change(Sender: TObject); procedure SpinEdit2Change(Sender: TObject); procedure SpinEdit3Change(Sender: TObject);
private
{ Private declarations }
procedure CalculateTimeToSencond(timeKind:String; time:integer); public
{ Public declarations } end; var
Form1: TForm1; hour: integer = 0; minute: integer = 0; second: integer = 0; tot:integer = 0; implementation
{$R *.dfm}
procedure TForm1.CalculateTimeToSencond(timeKind:String; time:integer); begin
if timeKind = 'hh' then hour := time
else if timeKind = 'mi' then minute := time
else if timeKind = 'ss' then second := time;
tot := hour * 60 * 60 + minute * 60 + second;
form1.Label4.Caption := '总共为' + IntToStr(tot) + '秒'; end;
procedure TForm1.SpinEdit1Change(Sender: TObject); begin
Form1.CalculateTimeToSencond('hh', StrToInt(Form1.SpinEdit1.Text)); end;
procedure TForm1.SpinEdit2Change(Sender: TObject); begin
Form1.CalculateTimeToSencond('mi', StrToInt(Form1.SpinEdit2.Text)); end;
procedure TForm1.SpinEdit3Change(Sender: TObject); begin
Form1.CalculateTimeToSencond('ss', StrToInt(Form1.SpinEdit3.Text)); end; end.
4.8 在编辑框中输入一个实数,利用备注框输出该实数及其平方和平方根。 答:设计界面:
运行界面:
代码: unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type
TForm1 = class(TForm) Edit1: TEdit; Label1: TLabel; Memo1: TMemo; Label2: TLabel;
procedure Edit1Change(Sender: TObject); procedure FormCreate(Sender: TObject); private
{ Private declarations } public
{ Public declarations } end; var
Form1: TForm1;
num1 : Real; //原实数 num2 : Real; //实数平方 num3 : Real; //实数平方根 implementation
{$R *.dfm}
procedure TForm1.Edit1Change(Sender: TObject); begin
if form1.Edit1.Text <> '' then
begin
num1 := StrToFloat(form1.Edit1.Text); num2 := Sqr(num1); num3 := Sqrt(num1);
form1.Memo1.Lines.Clear;
form1.Memo1.Lines.Add('实数为 : ' + FloatToStr(num1)); form1.Memo1.Lines.Add('平方为 : ' + FloatToStr(num2)); form1.Memo1.Lines.Add('平方根为 : ' + FloatToStr(num3)); end; end; End.
第五章
5.11 任意给定3个实数,按照从大到小的顺序依次输出这3个数。 答:设计界面:
运行界面:
代码:unit Unit1;
interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type
TForm1 = class(TForm) num1Edit: TEdit; Label1: TLabel; Label2: TLabel; resultLabel: TLabel; num2Edit: TEdit; num3Edit: TEdit;
procedure num1EditChange(Sender: TObject); procedure num2EditChange(Sender: TObject); procedure num3EditChange(Sender: TObject); private
{ Private declarations }
procedure CompareNumber(); public
{ Public declarations } end; var
Form1: TForm1;
num1 : Real; //数字1 num2 : Real; //数字2 num3 : Real; //数字3
compnum : Real; //比较时转换2数 outStr : String; //输出结果; implementation
{$R *.dfm}
procedure TForm1.CompareNumber(); begin
if (trim(form1.num1Edit.Text) <> '') And (trim(form1.num2Edit.Text) <> '') And (trim(form1.num3Edit.Text) <> '') then begin
num1 := StrToFloat(form1.num1Edit.Text); num2 := StrToFloat(form1.num2Edit.Text); num3 := StrToFloat(form1.num3Edit.Text);
if num2 > num1 then begin
compnum := num1; num1 := num2; num2 := compnum; end;
if num3 > num1 then begin
compnum := num1; num1 := num3; num3 := compnum; end;
if num3 > num2 then begin
compnum := num2; num2 := num3; num3 := compnum; end;
outStr := FloatToStr(num1) + ',' + FloatToStr(num2) + ',' + FloatToStr(num3);
form1.resultLabel.Caption := outStr; end; end;
procedure TForm1.num1EditChange(Sender: TObject); begin
form1.CompareNumber; end;
procedure TForm1.num2EditChange(Sender: TObject); begin
form1.CompareNumber; end;
procedure TForm1.num3EditChange(Sender: TObject); begin
form1.CompareNumber; end; end.
5.13 假设工资的增幅标准为:若基本工资大于等于1000元,增加工资20%;若小于1000元大于等于800元,则增加工资15%;若小于800元,则增加工资10%。请根据用户在文本框中输入的基本工资,计算出增加后的工资。
答:设计界面:
运行界面:
代码: unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type
TForm1 = class(TForm) Label1: TLabel; Edit1: TEdit; Label2: TLabel; resultLabel: TLabel;
procedure Edit1Change(Sender: TObject); private
{ Private declarations } public
{ Public declarations } end; var
Form1: TForm1; salary: Real; outSalary: Real; implementation
{$R *.dfm}
procedure TForm1.Edit1Change(Sender: TObject); begin
salary := StrToFloat(form1.Edit1.Text);
if (salary >= 1000) then
outSalary := salary * (1 + 0.2)
else if (salary < 1000) And (salary >= 800) then outSalary := salary * (1 + 0.15) else if (salary < 800) then
outSalary := salary * (1 + 0.1);
form1.resultLabel.Caption := FloatToStr(outSalary); end; End.
第六章
6.5 设s=1X2X3X…Xn,求s不大于20000时最大的n。 答:设计界面:
运行界面:
代码: unit Unit1;
interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type
TForm1 = class(TForm) Label1: TLabel; Edit1: TEdit; Label2: TLabel; resultLabel: TLabel;
procedure FormCreate(Sender: TObject); private
{ Private declarations } public
{ Public declarations } end; var
Form1: TForm1;
number, n, tot: Integer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject); begin
tot := 1; n := 0;
number := StrToInt(form1.Edit1.Text); While tot <= number do begin
台州电大陈元的数据库应用程序设计离线作业



