delphi的一些程序代码.docx
《delphi的一些程序代码.docx》由会员分享,可在线阅读,更多相关《delphi的一些程序代码.docx(28页珍藏版)》请在冰豆网上搜索。
delphi的一些程序代码
1.程序实现求最大公约数。
procedureTForm1.Button1Click(Sender:
TObject);
var
m,n,t,r:
longword;
begin
m:
=strtoint(edit1.Text);
n:
=strtoint(edit2.Text);
ifm<=nthenbegin
t:
=m;
m:
=n;
n:
=t;
end;
repeat
r:
=mmodn;
m:
=n;
n:
=r;
untilr=0;
Label3.Caption:
=inttostr(m);
end;
2.程序实现求最小公倍数。
procedureTForm1.Button1Click(Sender:
TObject);
varm,n,i,j:
integer;
begin
m:
=strtoint(edit1.Text);
n:
=strtoint(edit2.Text);
j:
=compare(m,n);
ifm=jthenm:
=n;
fori:
=1tojdobegin
if(i*m)modj=0thenbreak;
end;
edit3.Text:
=inttostr(i*m);
end;
functionTpare(a:
integer;b:
integer):
integer;
begin
ifa<=bthencompare:
=a
elsecompare:
=b;
end;
procedureTForm1.Button2Click(Sender:
TObject);
begin
edit1.text:
='';
edit2.text:
='';
edit3.text:
='';
end;
3程序计算Sin(X)函数的值(不用delphiobjectpascal内部函数)
procedureTForm1.Button1Click(Sender:
TObject);
vardone,n,negative:
integer;
x,sinx,pow,x1,factor:
real;
begin
x:
=strtofloat(edit1.Text);
done:
=1;n:
=1;factor:
=1;negative:
=1;
pow:
=x;
sinx:
=pow/(factor*negative);
whiledone=1dobegin
n:
=n+2;
pow:
=pow*x*x;
negative:
=negative*(-1);
factor:
=factor*n*(n-1);
x1:
=pow/(factor*negative);
if(abs(x1)<1e-3)thendone:
=0
elsesinx:
=sinx+x1;
end;
edit2.Text:
=floattostr(sinx);
end;
4.程序实现打印九九乘法表。
procedureTForm1.Button1Click(Sender:
TObject);
vari,j:
integer;
s:
string;
begin
fori:
=1to9do
begin
s:
='';
forj:
=1toido
begins:
=s+''+inttostr(i)+'*'+inttostr(j)+'='+inttostr(i*J);
end;
listbox1.items.add(s);
end;
end;
5.程序实现删除字符串中空格。
procedureTForm1.Button1Click(Sender:
TObject);
var
s1,s:
string;
k,i:
integer;
s2:
string;
begin
s:
=edit1.Text;
i:
=length(s);
fork:
=1toidobegin
s1:
=copy(s,k,1);
if(s1='')then
begin
s2:
=copy(s,k+1,(i-k));
s2:
=trim(s2);
s:
=copy(s,1,k-1)+s2;
end;
end;
s:
=trim(s);
edit2.Text:
=s;
end;
6.程序实现统计字符串中各种字符个数。
procedureTForm1.Button1Click(Sender:
TObject);
vari,j,k,m,idx:
integer;
s,st:
string;
s0:
string[1];
begin
s:
=uppercase(edit1.text);
fori:
=1tolength(s)do
begin
s0:
=copy(s,i,1);
if(s0>='A')and(s0<='Z')then
begin
form:
=0to25do
begin
if(chr(m+65)=s0)then
begin
A[m]:
=A[m]+1;
break;
end;
end;
end;
if(s0>='0')and(s0<='9')then
begin
idx:
=strtoint(s0);
b[idx]:
=b[idx]+1;
end;
end;
forj:
=0to25do
begin
if(A[j]<>0)then
begin
st:
=chr(j+65)+':
'+inttostr(A[j]);
listbox1.items.add(st);
end;
end;
fork:
=0to9do
begin
if(B[k]<>0)then
begin
st:
=inttostr(k)+':
'+inttostr(B[k]);
listbox1.items.add(st);
end;
end;
end;
procedureTForm1.FormCreate(Sender:
TObject);
varm,n:
integer;
begin
form:
=0to25doA[m]:
=0;
forn:
=0to9doB[n]:
=0;
end;
7.程序实现数的阶乘。
procedureTForm1.Button1Click(Sender:
TObject);
var
s:
real;
n,i:
integer;
begin
n:
=strtoint(edit1.text);
ifn<0then
showmessage('数据有误');
s:
=1;
fori:
=1tondo
begin
S:
=i*S;
end;
edit2.Text:
=floattostr(s);
end;
8.从键盘任意输入一个正整数,然后把该数的每位数字逆序输出。
procedureTForm1.Button1Click(Sender:
TObject);
Vars1,s2:
String;i,p:
Integer;
sz:
Array[1..100]ofString;
begin
s1:
=Edit1.Text;
p:
=0;
ifCopy(s1,1,1)='+'thenbegin
Fori:
=1tolength(s1)-1dobegin
sz[i]:
=Copy(s1,i+1,1);
if(sz[i]<'0')or(sz[i]>'9')thenp:
=p+1;
end;
if(p>0)thenShowmessage('输入错误,请输入正整数!
')
elsebegin
Fori:
=1toLength(s1)-1dobegin
s2:
=sz[i]+s2;
end;
label2.Caption:
=s2;
end;
endelsebegin
Fori:
=0tolength(s1)-1dobegin
sz[i]:
=Copy(s1,i+1,1);
if(sz[i]<'0')or(sz[i]>'9')thenp:
=p+1;
end;
if(p>0)thenShowmessage('输入错误,请输入正整数!
')
elsebegin
Fori:
=0toLength(s1)-1dobegin
s2:
=sz[i]+s2;
end;
label2.Caption:
=s2;
end;
end;
end;
9.苹果每只5元,梨3元,桔子3元,100元买100种水果,有多少种?
。
procedureTForm1.Button1Click(Sender:
TObject);
varx,y,z,i:
integer;
begin
i:
=0;
forx:
=0to20dobegin
fory:
=0to100div3dobegin
forz:
=0to100div3dobegin
If5*x+3*y+3*z=100Then
begin
ListBox1.Items.Add('苹果'+IntToStr(x)+'只'+''+'梨'+IntToStr(y)+'只'+''+'桔子'+IntToStr(z)+'只');
i:
=i+1;
end;
end;
end;
end;
Edit1.Text:
=IntToStr(i);
end;
10计算1-1000的水仙花数.水仙花数满足条件a*a*a+b*b*b+c*c*c=abc
procedureTForm1.Button1Click(Sender:
TObject);
vara,b,c:
integer;
st:
string;
begin
fora:
=0to9dobegin
forb:
=0to9dobegin
forc:
=1to9dobegin
if(a*a*a+b*b*b+c*c*c=100*a+10*b+c)thenbegin
st:
=inttostr(100*a+10*b+c)+'='+inttostr(a)+'^3+'+inttostr(b)+'^3+'+inttostr(c)+'^3';
listbox1.items.Add(st);
end;
end;
end;
end;
end;
11.删除数组中某个字符。
procedureTForm1.Button1Click(Sender:
TObject);
varst,a,s,s1:
string;
zf:
array[0..20]ofstring;
n,j:
integer;
begin
st:
=edit1.Text;
a:
='';
s1:
='';
forn:
=1tolength(st)dobegin
a:
=copy(st,n,1);
zf[n]:
=a;
end;
s:
=edit2.text;
j:
=StrToInt(s);
forn:
=jtolength(st)-1dobegin
zf[n]:
=zf[n+1];
end;
forn:
=1tole