版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
ObjectPascal編程基礎講義項目文件-主程序programProject1;
usesForms,Unit1in'Unit1.pas'{Form1};
{$R*.res}
beginApplication.Initialize;Application.CreateForm(TForm1,Form1);Application.Run;end.
單元文件單元文件unitUnit1;interfaceusesWindows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls;typeTForm1=class(TForm)Label1:TLabel;Edit1:TEdit;Label2:TLabel;Edit2:TEdit;Edit3:TEdit;單元文件Button1:TButton;Button2:TButton;procedureButton1Click(Sender:TObject);procedureButton2Click(Sender:TObject);private{Privatedeclarations}public{Publicdeclarations}end;
varForm1:TForm1;
implementation{$R*.dfm}單元文件procedureTForm1.Button1Click(Sender:TObject);varsno,sname:string;beginsno:=edit1.Text;sname:=edit2.Text;edit3.Text:='welcometodelphi:'+sno+sname;end;procedureTForm1.Button2Click(Sender:TObject);beginForm1.Close;end;end.
變量說明procedureTForm1.Button1Click(Sender:TObject);vareno,ename:String;//字符串型(隱含最大長度256個字符)age:Integer;//整型
sal:Real;//實型married:Boolean;//布爾型ch:Char;//字符型,變量只存放一個字符cnt:word;//無符號16位整型begin
…
…end;
運算符ObjectPascal常用的運算符:算術運算符:+、-、*、/、div(整除)、mod(求余)布爾運算符:and(與)、or(或)、not(非)字符串運算符:+(字符串拼接)集合運算符:+(并)、-(差)、*(交)、in(屬于)關系運算符:=、<>(不等于)、>、<、<=、>=控制語句-分支語句If條件then語句1else語句2;在第一個編輯框中輸入星期幾,然后按OK按鈕,如果輸入的是saturday或sunday,則在第2個編輯框中顯示一條信息,并把窗體變黃??刂普Z句-分支語句procedureTForm1.Button1Click(Sender:TObject);beginif(Edit1.text='saturday')or(Edit1.text='sunday')thenbeginedit2.text:='Whyareyouworkingtoday!';//顯示一條信息
Form1.Color:=clYellow;//將窗體顏色置為黃色
endelsebeginedit2.text:='workday';Form1.Color:=clBtnFace;//將窗體顏色置為隱含顏色
end;end;條件語句嵌套例條件語句嵌套procedureTForm1.Button1Click(Sender:TObject);vargrade:integer;begingrade:=strtoint(edit1.Text);//將輸入的分數(shù)賦值給變量gradeif(grade>=85)and(grade<=100)thenedit2.Text:='優(yōu)秀'
elseif(grade>=60)and(grade<85)then//語句嵌套
edit2.Text:='通過'
elseif(grade>=0)and(grade<60)then//再次嵌套
edit2.Text:='不通過'
elseedit2.Text:='成績有誤';end;設置多分支if(grade>=85)and(grade<=100)thenedit2.Text:='優(yōu)秀'//分支1elseif(grade>=60)and(grade<85)thenedit2.Text:='通過'//分支2elseif(grade>=0)and(grade<60)thenedit2.Text:='不通過'//分支3elseedit2.Text:='成績有誤';//其他分支while循環(huán)procedureTForm1.Button1Click(Sender:TObject);vari,n,sum:integer;beginsum:=0;i:=1;n:=strtoint(edit1.Text);//讀入正整數(shù)nwhilei<=ndo//當循環(huán)beginifodd(i)thensum:=sum+i;i:=i+1;end;edit2.Text:=inttostr(sum);//將結(jié)果在編輯框中顯示end;For循環(huán)procedureTForm1.Button1Click(Sender:TObject);vari,j:integer;beginedit2.text:='';//清空edit2j:=StrToInt(edit1.text);讀入edit1中的值
fori:=1tojdoedit2.text:=edit2.text+IntToStr(i);end;異常處理procedureTForm1.OkClick(Sender:TObject);vari:integer;beginedit2.text:='';tryfori:=1tostrtoint(edit1.text)doedit2.text:=edit2.text+inttostr(i);
exceptshowmessage('輸入有錯');
exit;
end;end;數(shù)組implementation{$R*.dfm}varcount:integer=1;//此處定義的變量在單元中是全程變量
array1:array[1..10]ofinteger;
procedureTForm1.Button1Click(Sender:TObject);“輸入確認”vari,j,temp:integer;beginifcount=1thenedit2.Text:='';//當輸入第1個數(shù)時,輸出編輯框清空
array1[count]:=strtoint(edit1.Text);//讀入的數(shù)據(jù)存入數(shù)組
edit1.Text:='';//數(shù)據(jù)讀入后,編輯框清空
edit1.SetFocus;//將光標置于輸入編輯框數(shù)組
count:=count+1;//計數(shù)器加1
ifcount>10then//如果已輸入10個數(shù),開始排序輸出
begin//輸出處理開始fori:=1to9do//排序
forj:=1to10-idoif(array1[j]>array1[j+1])thenbegintemp:=array1[j];array1[j]:=array1[j+1];array1[j+1]:=temp;end;
數(shù)組fori:=1to10do//輸出
edit2.Text:=edit2.Text+inttostr(array1[i])+',';count:=1;end;//輸出處理結(jié)束label2.Caption:=inttostr(count);//編輯框下顯示要輸入第幾個數(shù)end;end.記錄type
TEmployee=record//定義一個職工記錄類型
eno:string[5];//職工號
ename:String[10];//
職工名
age:integer;//
年齡
sal:Real;//工資
marriedBoolean;//
婚否
end;
varemp1,emp2:TEmployee;emp1.age:=23;edit1.text:=emp1.ename;文件文件-定義記錄類型implementation{$R*.dfm}typeTEmpRec=record//定義一個職工記錄類型
ename:String[10];//定長字符串a(chǎn)ge:integer;sal:real;end;文件-打開文件var//定義單元內(nèi)的全局變量
emprec:TEmprec;//定義一個記錄型變量
EmpFile:TEmpRec;//定義文件變量
last:longint;//定義一個長整型變量,
procedureTForm1.Button3Click(Sender:TObject);begin//寫功能區(qū)中的打開文件tryAssignFile(EmpFile,'d:\emp.$$$');//將一個文件名指定給文件型變量。//此后該變量即代表一個文件
reset(EmpFile);//打開已有文件(若文件不存在將發(fā)生例外轉(zhuǎn)移文件-打開文件last:=(EmpFIle);//取文件大小
seek(Emp);//將指針移到文件尾
exceptrewrite(EmpFile);//創(chuàng)建并打開新文件
end;end;文件-寫procedureTForm1.Button1Click(Sender:TObject);Begin//寫文件emprec.ename:=edit1.Text;emprec.age:=strtoint(edit2.Text);emprec.sal:=strtofloat(edit3.Text);
write(Emp);//將職工記錄寫入文件
edit1.Clear;//清除編輯框(調(diào)用edit的clear方法)
edit2.Clear;edit3.Clear;end;關閉文件procedureTForm1.Button5Click(Sender:TObject);//寫功能區(qū)中的關閉文件beginclosefile(EmpFile);//關閉文件end;讀文件procedureTForm1.Button2Click(Sender:TObject);beginifnoteof(EmpFile)then//函數(shù)eof判斷是否已到文件尾
beginread(Emp);//讀一個記錄
edit4.text:=emprec.ename;edit5.text:=inttostr(emprec.age);edit6.text:=floattostr(emprec.sal);endelseshowmessage('文件讀完');end;程序塊程序塊可分為兩部分:可選的聲明區(qū)(在語句區(qū)之前)語句區(qū)(begin…end)聲明區(qū)變量聲明(var)常量
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度國際貨物檢驗檢疫服務英文版合同
- 2025年度數(shù)據(jù)中心工程監(jiān)理合同執(zhí)行標準
- 2025-2030年中國電子線號印字機行業(yè)深度研究分析報告
- 瑤族服飾研究報告總結(jié)與反思
- 2025年企業(yè)臨時工勞動服務合同模板
- 2025年度互聯(lián)網(wǎng)金融服務合作合同范本
- 中國鐘閥爐頂項目投資可行性研究報告
- 2022-2027年中國企業(yè)直播服務行業(yè)發(fā)展監(jiān)測及投資戰(zhàn)略研究報告
- 國學社申請書
- 京劇藝術戲曲發(fā)音吐字方法
- 銷售人員薪資提成及獎勵制度
- 2023年宏觀經(jīng)濟學考點難點
- 先兆流產(chǎn)課件-課件
- 黑龍江申論真題2021年(鄉(xiāng)鎮(zhèn))
- 山體排險合同模板
- 醫(yī)保專(兼)職管理人員的勞動合同(2篇)
- 特殊感染手術的配合與術后處理課件
- 檢驗科生物安全工作總結(jié)
- 《ESPEN重癥病人營養(yǎng)指南(2023版)》解讀課件
- 《金屬與石材幕墻工程技術規(guī)范》jgj1332001-2021112401384
- 即時通訊系統(tǒng)建設方案
評論
0/150
提交評論