完整的delphi程序设计教程课后习题答案由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“教材课后习题答案”。
1.239页1题小时钟 procedure TForm1.Timer1Timer(Sender: TObject);var xt,yt:integer;h,m,s,ms:word;begin decodetime(time,h,m,s,ms);xt:=paintbox1.clientwidth div 2;yt:=paintbox1.clientheight div 2;paintbox1.refresh;paintbox1.canvas.moveto(xt,yt);paintbox1.canvas.pen.width:=3;paintbox1.canvas.lineto(xt+round(sin((60*h+m)/(12*60)*2*pi)*100), yt-round(cos((60*h+m)/(12*60)*2*pi)*100));paintbox1.canvas.moveto(xt,yt);paintbox1.canvas.pen.width:=2;paintbox1.canvas.lineto(xt+round(sin(m/60*2*pi)*140), yt-round(cos(m/60*2*pi)*140));paintbox1.canvas.moveto(xt,yt);paintbox1.canvas.pen.width:=1;paintbox1.canvas.lineto(xt+round(sin(s/60*2*pi)*180), yt-round(cos(s/60*2*pi)*180));end;end.2.239页3题求两点间的距离 procedure TForm1.Button1Click(Sender: TObject);begin paintbox1.Refresh;formpaint(sender);end;procedure TForm1.FormPaint(Sender: TObject);var xt,yt:integer;ax,ay,bx,by:integer;begin xt:=paintbox1.ClientWidth div 2;yt:=paintbox1.Clientheight div 2;paintbox1.canvas.MoveTo(xt,0);paintbox1.canvas.lineTo(xt,2*yt);paintbox1.canvas.MoveTo(0,yt);paintbox1.canvas.lineTo(2*xt,yt);ax:=strtoint(edit1.text);ay:=strtoint(edit2.text);bx:=strtoint(edit3.text);by:=strtoint(edit4.text);
edit5.Text:=floattostr(sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by)));
paintbox1.canvas.MoveTo(xt+ax*10,yt-ay*10);
paintbox1.canvas.lineTo(xt+bx*10,yt-by*10);end;end.3.226页5题输入学生成绩 var
Form1: TForm1;type
studentrecord=record xh,xm:string[6];xb:boolean;yy,sx,dz:integer;end;
implementation {$R *.dfm} procedure
TForm1.Button1Click(Sender: TObject);
var t:studentrecord;
f,f1:file of studentrecord;p1,p2:boolean;begin
aignfile(f,'c:my documentsstu.dat');reset(f);
aignfile(f1,'c:my documentsstu1.dat');rewrite(f1);
while not eof(f)do begin read(f,t);
p1:=(t.sx>=85)and(t.yy>=85)and(t.dz>=85);
p2:=t.sx+t.yy+t.dz>=270;if p1 or p2 then write(f1,t);end;
closefile(f);closefile(f1);end;
procedure
TForm1.FormCreate(Sender: TObject);
var t:studentrecord;f:file of studentrecord;xingbie:string;begin
aignfile(f,'c:my documentsstu.dat');reset(f);
while not eof(f)do begin read(f,t);if t.xb then xingbie:='男' else
xingbie:='女';
listbox1.Items.Add(t.xh+' '+t.xm+' '+xingbie+' '+inttostr(t.sx)+
' '+inttostr(t.yy)+' '+inttostr(t.dz));end;
closefile(f);end;
procedure
TForm1.Button2Click(Sender: TObject);begin close;end;end.4.226页8题保存获得奖学金者 var
Form1: TForm1;type
studentrecord=record xh,xm:string[6];xb:boolean;yy,sx,dz:integer;end;
implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject);var t:studentrecord;f,f1:file of studentrecord;p1,p2:boolean;begin aignfile(f,'c:my documentsstu.dat');reset(f);aignfile(f1,'c:my documentsstu1.dat');rewrite(f1);while not eof(f)do begin read(f,t);p1:=(t.sx>=85)and(t.yy>=85)and(t.dz>=85);p2:=t.sx+t.yy+t.dz>=270;if p1 or p2 then write(f1,t);end;closefile(f);closefile(f1);end;procedure TForm1.FormCreate(Sender: TObject);var t:studentrecord;f:file of studentrecord;xingbie:string;begin aignfile(f,'c:my documentsstu1.dat');reset(f);while not eof(f)do begin read(f,t);if t.xb then xingbie:='男' else xingbie:='女';listbox1.Items.Add(t.xh+' '+t.xm+' '+xingbie+' '+inttostr(t.sx)+ ' '+inttostr(t.yy)+' '+inttostr(t.dz));end;closefile(f);end;procedure TForm1.Button2Click(Sender: TObject);begin close;end;end.5.166页22题素数 procedure
TForm1.Button1Click(Sender: TObject);type
sushuset=set of byte;var
sushu:sushuset;i,j,n,m,k:longword;s,s0:string;begin
n:=strtoint(edit1.text);m:=strtoint(edit2.text);if n mod 2=0 then n:=n+1;k:=m-n;
if k>255 then
showmeage('范围太大,请重新输入区间端点!')else begin
sushu:=[0..k];for i:=2 to n-1 do for j:=n to m do
if j mod i =0 then sushu:=sushu-[j-n];for i:=n to m div 2 do for j:= i+1 to m do
if j mod i =0 then sushu:=sushu-[j-n];s:='';s0:='';
for i:=n to m do if i-n in sushu then begin
if length(s0+' '+inttostr(i))>60 then begin
s:=s+s0+chr(13);s0:='' end;
s0:=s0+' '+inttostr(i);end;s:=s+s0;label1.caption:=s;
groupbox1.Top:=(panel1.Top-groupbox1.height)div 2;end;end;
procedure
TForm1.FormCreate(Sender: TObject);begin
groupbox1.Top:=(panel1.Top-groupbox1.height)div 2;end;
procedure
TForm1.Edit1KeyPre(Sender: TObject;var Key: Char);var j:set of char;begin
j:=['0'..'9',#8];
if not(key in j)then key:=#0;end;end.6.148页12题整除的数 implementation {$R *.dfm}
function d7(x:integer):boolean;begin
if x mod 7=0 then d7:=true else d7:=false;end;
function d17(x:integer):boolean;begin
if x mod 17=0 then d17:=true else d17:=false;end;
procedure
TForm1.FormActivate(Sender: TObject);var
n:integer;begin
for n:=1000 to 4000 do if d7(n)and d17(n)then memo1.Lines.Add(inttostr(n));end;end.7.148页17题求斐波那契数列问题 implementation {$R *.dfm} function fib(n:integer):int64;begin case n of 1,2:fib:=1;else fib:=fib(n-1)+fib(n-2);end;end;procedure TForm1.Button1Click(Sender: TObject);var k:integer;begin memo1.lines.clear;for k:=1 to strtoint(edit1.text)do memo1.lines.add(format('%2d %3d',[k,fib(k)]));end;end.8.120页10题,判断是否是闰年 procedure TForm1.Button1Click(Sender: TObject);var y,m:integer;begin y:=strtoint(edit1.text);m:=strtoint(edit2.text);label3.Caption:='';if((y mod 4 = 0)and(y mod 100 0))or(y mod 400=0)then label3.Caption:=edit1.text+'年是闰年,' else label3.Caption:=edit1.text+'年不是闰年,';case m of 3,4,5:Label3.Caption:=Label3.Caption+edit2.Text+'月份是春天,';6,7,8:Label3.Caption:=Label3.Caption+edit2.Text+'月份是夏天,';9,10,11:Label3.Caption:=Label3.Caption+edit2.Text+'月份是秋天,';else Label3.Caption:=Label3.Caption+edit2.Text+'月份是冬天,';end;
case m of
1,3,5,7,8,10,12:Label3.Caption:=Label3.Caption+'共有31天,';
4,6,9,11:Label3.Caption:=Label3.Caption+'共有30天,';else
if((y mod 4 = 0)and(y mod 100 0)or(y mod 400=0))then Label3.Caption:=Label3.Caption+'共有29天,' else
Label3.Caption:=Label3.Caption+'共有28天,';end;end;end.9.120页11题求和的 procedure
TForm1.Button1Click(Sender: TObject);var
n,k,j:integer;s:int64;begin
n:=strtoint(edit1.text);j:=0;s:=0;
for k:=1 to n do begin j:=j+k;s:=s+j;end;
panel2.Caption:=format('s = %d',[s]);edit1.setfocus;end;
procedure
TForm1.Edit1KeyPre(Sender: TObject;var Key: Char);var b:boolean;begin
b:=(key#8)and(key#57);
if b then key:=#0;end;end.10.120页12题男人,女人,小孩各有多少人 procedure
TForm1.Button1Click(Sender: TObject);var
x,y,z:integer;p:string;begin
memo1.clear;for x:=0 to 16 do for y:=0 to 24 do begin
z:=30-x-y;
if 3*x+2*y+z/1=50 then begin
p:=format('%3d %3d %3d',[x,y,z]);
memo1.lines.add(p);end;end;end;end.11.98页2题求长方体的表面积 procedure
TForm1.Button1Click(Sender: TObject);var
s,l,k,h:double;begin
l:=strtofloat(edit1.text);k:=strtofloat(edit2.text);h:=strtofloat(edit3.text);s:=2*l*k+2*k*h+2*l*h;label1.Caption:=floattostr(s);end;end.12.98页5题计算圆的面积和周长 procedure
TForm1.Edit1KeyPre(Sender: TObject;var Key: Char);var r:real;b:boolean;begin b:=(key#8)and(key'.')and(key#13)and(key#57);if b then key:=#0;if key =#13 then begin if edit1.text='' then edit1.text:='0';r:=strtofloat(edit1.text);if radiobutton1.Checked then label1.Caption:=format('圆的面积为: %f',[pi*r*r]);if radiobutton2.Checked then label1.Caption:=format('圆的周长为: %f',[2*pi*r]);if radiobutton3.Checked then label1.Caption:=format('圆的面积为: %f'+chr(13)+ '周长为: %f',[pi*r*r,2*pi*r]);edit1.SelectAll;end;end;procedure TForm1.RadioButton1Click(Sender: TObject);var sender0:tobject;key:char;begin sender0:=edit1;key:=#13;form1.edit1keypre(sender0,key);end;procedure TForm1.RadioButton2Click(Sender: TObject);var sender0:tobject;key:char;begin sender0:=edit1;key:=#13;form1.edit1keypre(sender0,key);end;procedure TForm1.RadioButton3Click(Sender: TObject);var sender0:tobject;key:char;begin
sender0:=edit1;key:=#13;
form1.edit1keypre(sender0,key);end;end.13.98页6题统计奇数和偶数的个数
procedure
TForm1.Button1Click(Sender: TObject);var
k,j,n:word;begin k:=0;
n:=combobox1.items.count;for j:=1to n do
if strtoint(combobox1.items[j-1])mod 2 = 0 then k:=k+1;
label1.caption:=format('共有: '+#13+'%d个偶数,'+#13+'%d个奇数。',[k,n-k]);end;
procedure
TForm1.Button2Click(Sender: TObject);begin
combobox1.items.clear;end;
procedure
TForm1.ComboBox1KeyPre(Sender: TObject;var Key: Char);begin if(key =#13)and(combobox1.text'')then
//按键后接收输入的选项 begin
combobox1.items.insert(0,combobox1.text);
//按键后移去选项
combobox1.selstart:=0;combobox1.text:='';end;
if key = #27 then
if combobox1.itemindex-1 then
combobox1.items.Delete(combobox1.itemindex);end;end.14.98页8题设置倒计时的时间,并进行倒计时 procedure
TForm1.Timer1Timer(Sender: TObject);var
m,n,s,h:integer;begin
timer1.Tag:=timer1.Tag-1;m:=timer1.Tag;if m
timer1.enabled:=false;
showmeage('预定的时间到了!');edit1.Text:='0';end else begin s:=m;
n:=s div 60;s:=s mod 60;h:=n div 60;n:=n mod 60;
maskedit1.text:=format('%d:%d:%d',[h,n,s]);
if n strtoint(edit1.text)then edit1.Text:=inttostr(n);end;end;
procedure
TForm1.Button1Click(Sender: TObject);begin
timer1.enabled:=True;
timer1.Tag:=strtoint(edit1.Text)*60;end;end.
C语言程序设计教程课后习题答案第一章 C语言程序设计概述 -习题答案1 算法的描述有哪些基本方法?答1、自然语言2、专用工具2 C语言程序的基本结构是怎样的?举一个例子说明。答1......
第一章1.判断题(1)× (2) √ (3) √ (4) × (5) ×(6) √ 2.选择题(1)C (2) B (3) B (4) C (5) D(6) C 3.编程题using System; using System.Collections.Generic; using Sys......
《C语言程序设计教程》课后习题参考答案习题1 1.(1)编译、链接.exe (2)函数主函数(或main函数) (3)编辑编译链接 2.(1)-(5):DDBBC (6)-(10):ABBBC 3.(1)答:C语言简洁、紧凑,使用方便、灵活;C语言是......
第1章习题一、选择题1、A2、D3、C4、D5、B6、C7、C8、B9、B10、A二、填空题1、一,函数体2、main(主),main(主)3、编译,目标4、.c,.obj,.exe5、缩进,无影响,/* */三、程序分析题1、*......
第二章 答案题2.1 8086/8088通用寄存器的通用性表现在何处?8个通用寄存器各自有何专门用途?哪些寄存器可作为存储器寻址方式的指针寄存器? 答:8086/8088通用寄存器的通用性表现......