您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 纺织服装 > delphi的一些程序代码
1.程序实现求最大公约数。procedureTForm1.Button1Click(Sender:TObject);varm,n,t,r:longword;beginm:=strtoint(edit1.Text);n:=strtoint(edit2.Text);ifm=nthenbegint:=m;m:=n;n:=t;end;repeatr:=mmodn;m:=n;n:=r;untilr=0;Label3.Caption:=inttostr(m);end;2.程序实现求最小公倍数。procedureTForm1.Button1Click(Sender:TObject);varm,n,i,j:integer;beginm:=strtoint(edit1.Text);n:=strtoint(edit2.Text);j:=compare(m,n);ifm=jthenm:=n;fori:=1tojdobeginif(i*m)modj=0thenbreak;end;edit3.Text:=inttostr(i*m);end;functionTform1.compare(a:integer;b:integer):integer;beginifa=bthencompare:=aelsecompare:=b;end;procedureTForm1.Button2Click(Sender:TObject);beginedit1.text:='';edit2.text:='';edit3.text:='';end;3程序计算Sin(X)函数的值(不用delphiobjectpascal内部函数)procedureTForm1.Button1Click(Sender:TObject);vardone,n,negative:integer;x,sinx,pow,x1,factor:real;beginx:=strtofloat(edit1.Text);done:=1;n:=1;factor:=1;negative:=1;pow:=x;sinx:=pow/(factor*negative);whiledone=1dobeginn:=n+2;pow:=pow*x*x;negative:=negative*(-1);factor:=factor*n*(n-1);x1:=pow/(factor*negative);if(abs(x1)1e-3)thendone:=0elsesinx:=sinx+x1;end;edit2.Text:=floattostr(sinx);end;4.程序实现打印九九乘法表。procedureTForm1.Button1Click(Sender:TObject);vari,j:integer;s:string;beginfori:=1to9dobegins:='';forj:=1toidobegins:=s+''+inttostr(i)+'*'+inttostr(j)+'='+inttostr(i*J);end;listbox1.items.add(s);end;end;5.程序实现删除字符串中空格。procedureTForm1.Button1Click(Sender:TObject);vars1,s:string;k,i:integer;s2:string;begins:=edit1.Text;i:=length(s);fork:=1toidobegins1:=copy(s,k,1);if(s1='')thenbegins2:=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];begins:=uppercase(edit1.text);fori:=1tolength(s)dobegins0:=copy(s,i,1);if(s0='A')and(s0='Z')thenbeginform:=0to25dobeginif(chr(m+65)=s0)thenbeginA[m]:=A[m]+1;break;end;end;end;if(s0='0')and(s0='9')thenbeginidx:=strtoint(s0);b[idx]:=b[idx]+1;end;end;forj:=0to25dobeginif(A[j]0)thenbeginst:=chr(j+65)+':'+inttostr(A[j]);listbox1.items.add(st);end;end;fork:=0to9dobeginif(B[k]0)thenbeginst:=inttostr(k)+':'+inttostr(B[k]);listbox1.items.add(st);end;end;end;procedureTForm1.FormCreate(Sender:TObject);varm,n:integer;beginform:=0to25doA[m]:=0;forn:=0to9doB[n]:=0;end;7.程序实现数的阶乘。procedureTForm1.Button1Click(Sender:TObject);vars:real;n,i:integer;beginn:=strtoint(edit1.text);ifn0thenshowmessage('数据有误');s:=1;fori:=1tondobeginS:=i*S;end;edit2.Text:=floattostr(s);end;8.从键盘任意输入一个正整数,然后把该数的每位数字逆序输出。procedureTForm1.Button1Click(Sender:TObject);Vars1,s2:String;i,p:Integer;sz:Array[1..100]ofString;begins1:=Edit1.Text;p:=0;ifCopy(s1,1,1)='+'thenbeginFori:=1tolength(s1)-1dobeginsz[i]:=Copy(s1,i+1,1);if(sz[i]'0')or(sz[i]'9')thenp:=p+1;end;if(p0)thenShowmessage('输入错误,请输入正整数!')elsebeginFori:=1toLength(s1)-1dobegins2:=sz[i]+s2;end;label2.Caption:=s2;end;endelsebeginFori:=0tolength(s1)-1dobeginsz[i]:=Copy(s1,i+1,1);if(sz[i]'0')or(sz[i]'9')thenp:=p+1;end;if(p0)thenShowmessage('输入错误,请输入正整数!')elsebeginFori:=0toLength(s1)-1dobegins2:=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;begini:=0;forx:=0to20dobeginfory:=0to100div3dobeginforz:=0to100div3dobeginIf5*x+3*y+3*z=100ThenbeginListBox1.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=abcprocedureTForm1.Button1Click(Sender:TObject);vara,b,c:integer;st:string;beginfora:=0to9dobeginforb:=0to9dobeginforc:=1to9dobeginif(a*a*a+b*b*b+c*c*c=100*a+10*b+c)thenbeginst:=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;beginst:=edit1.Text;a:='';s1:='';forn:=1tolength(st)dobegina:=copy(st,n,1);zf[n]:=a;end;s:=edit2.text;j:=StrToInt(s);forn:=jtolength(st)-1dobeginzf[n]:=zf[n+1];end;forn:=1tolength(st)-1dobegins1:=s1+zf[n];end;edit3.Text:=s1;end;12把文件file1.txt中重复字符去掉后(即若有多个字符相同,则只保留1个)写入文件file2.tex。procedureTForm1.Button1Click(Sender:TObject);varzf,filename1,filename2:string;i,j,k:integer;beginfilename1:='c:\file1.txt';filename2:='c:\file2.txt';memo1.lines.loadfromfile(filename1);zf:=memo1.text;fori:=1tolength(zf)-1doforj:=i+1tolength(zf)doifzf[j]=zf[i]thenbeginfork:=jtolength(zf)-1dobeginzf[k]:=zf[k+1];end;delete(zf,length(zf),1);end;memo1.text:=zf;memo1.lines.savetofile(filename2);end;13文件file1.txt全部由英文字母构成,统计文本文件中各种英文字母个数。procedureTForm1.Button1Click(Sender:TObject);beginfilename:='c:\file.txt';memo1.lines.loadfromfile(filename);end;procedureTForm1.Button2Click(Sender:TObject);vara:array[0..25]ofinteger;i,idx:integer;vs,ch:String;st:string;Ch0:Array[0..2]ofChar;beginFori:=0To25doA[i]:=0;vs:=Memo1.Text;vs:=UpperCase(Vs);Fori:=1ToLength(Vs)dobeginCh:=Copy(vs,i,1);StrPLCOPY(Ch0,ch,1);If((ch='A')And(ch='Z'))ThenBeginidx:=ord(ch0[0
本文标题:delphi的一些程序代码
链接地址:https://www.777doc.com/doc-3260296 .html