本文整理自网络,侵删。
程序结构
program程序名; 单元引用; 常量说明; 类型定义; 变量定义; 过程和函数的定义; begin … end.
1)程序开头关键字:program其后程序名称(Pascal标识符) 2)说明部分:本程序使用单元(说明、类型、变量..的定义) 3)程序开头begin,结尾end。每个语句‘;’结尾,end前分号可省略 关键字和标识符(65个)表2-1 24页 1)标准常量。false、true… 2)标准类型。integer、boolean、real 3)标准函数。sin、cos、abs… 4)标准过程。get、put… 5)标准文件。input、output
自定义标识符
1)大小写、英文、数字、下划线构成长度不超过255 2)开头必须:英文或下划线 3)不得用关键字 4)最好不要与标准标识符同名。 说明: 1)见名知义:age―-年龄 2)长度适中:studentname―-stu_name 3)标识符不分大小写 分隔符: 分号:分隔语句 逗号:分隔数据 注解作用:调试作用、可读性、//单行、{多行}、(也行)
数据类型 表2-2 25页
1)标准类型(可直接使用):整型integer、实型real、字符型char、字符串型string、布尔型boolean 其他12个要先定义再使用 2)顺序类型:整型、字符型、布尔型、枚举型、子界型 整型数据类型 表2-3 16页 常量(constant):直接常量和符号常量 直接:100,1.1,true 符号:const <常量名>:[<类型名>]=<表达式1>; pi=3; //常量定义 pi=123; //错误(不能重新定义) enterchar:char=#13; //定义常量并定义类型 xingming:string[8]=’zhangsan’; 变量(variable):变量名、类型、变量的值 var <变量名>:<类型名1>; type month=1..12; var x,y,z:real; //用逗号隔开 i,j,k:integer; //定义整型 m1,m2:month; //month类型已定义 days:array[1..12] of integer; //数组类型
运算符:
8位:0000 0000 and 与(左右都成立) 8 and 7 结果:0 0000 1000 and 0000 0111 结果:0(位置对应1取1) or 或(其中一个成立) 6 or 3结果:7 0000 0110 or 0000 0011 结果:7(有1取1:0000 0111) not 非(反一下) not 6 结果:-7; not 0000 0110 结果:1111 1001 表示-7(1111 1111=-1) xor 异或(同时成立或同时失败 才成立) 6 xor 3结果:5 0000 0110 xor 0000 0011 结果:5(取单独1:0000 0101) shl 左移 9 shl 1结果:18 0000 1001 左移1:0001 0010 结果:16+2=18(相当于*2) shr 右移 27 shr 2结果:6 0001 1011 右移1:000001 10 结果:27 div2平方 前提:必须整型 div 求整除的商 mod 求余数 字符串运算符:(格式) <字符表达式>|<字符串表达式>+<字符表达式>|<字符串表达式> 关系运算符 =、>、>=、<、<=、<>(不等于)、in(于..内) ‘ab’>’ac’?C:返回false,从左到右比较a相同,比较b和c 3<>4?C:3不等于4,返回false 3 in [2,3]?C:3是否在集合[2,3]内,返回true 逻辑运算符 not(非)、and(与)、or(或)、xor(异或) var a:boolean; false and a?C:为false,不会计算a的值 true or a?C:为true,不会计算a的值 运算符的优先级: 1)括号() 2)函数 3)not、+、-(正负符号) 4)乘法类:*、/、div、mod、and、shl、shr 5)加法类:+、-、or、xor 6)=、>、>=、<、<=、<>、in 建议:人为加括号提高可读性
常用函数与过程
关于函数的使用:
function 方法名(参数):返回类型,调用的时候直接方法名+参数就行, 例如:
write(Concat(s1,s2,s3));1――?C这是拼接字符串,输出为s1+s2+s3
于SysUtils单元中定义的(会自动别其他单元引用)
1)绝对值函数
function abs(x);1返回参数的绝对值 2)平方函数
function sqr(x:Extended):Extended;function sqr(x:Integer):Integer;1一般使用第一种,参数为实型表达式:返回x*x 3)平方根函数
function sqrt(x:Extended):Extended;1-返回非负数x的平方根 4)三角函数 正弦函数:
function sin(x:Extended):Extended;1余弦函数:
function cos(x:Extended):Extended;1反正切函数:
function arctan(x:Extended):Extended;15)取整函数
function round(x:Extended):Int64;1对x四舍不入取整
function trunc(x:Extended):Int64;1返回最大的且不大于x本身的整数。 6)指数函数
function exp(x:Real):Real;1返回e的x方 7)对数函数
function ln(x:real):real;1返回自然对数ln(x) 8)随机函数
function random[(Range:Integer)];1返回一个大于或等于0且小于输入的值:range的随机整数 若无参数a=random,返回大于等于0,小于1的值 9)随机过程
procedure randomize;110)π函数
function pi:Extended;1返回π值3.14159…; 11)pi:=3.14….; 例1:随机数
randomize;n:=round(random(26));或n:=round(random*26);例2:求sin(60)。n:=sin(60*pi/180);12345字符处理函数与过程
调用函数方式: 1)去掉function 2)括号中给指定参数值 如大小写转换UpperCase(‘a’) 1)大小写转换函数
function UpperCase(const s:string):string;1将s字符串中所有小写字母转化为大写字母
function LowerCase(const s:string):string;1将s字符串中所有大写字母转化为小写字母 2)比较字符串大小的函数
function CompareStr(const s1,s2:string):Integer;1?C>后者大,返回负整数;一样返回0;前者大,正整数 (测试结果为:很有意思); 3)拼接字符串
function Concat(s1,s2,s3...):string;14)查找函数
function Pos(substr,s:string):integer;1查找前者是在后者中的位置,没找到返回0; (温习一下const: const(修饰词) a:string=’2’; 变量:变量类型=具体数值) 5)取字符串函数
function Copy(s:String;index,count:integer):string;1取s中从index到count之间的字符串(第一个为1不是0,包括index和count) 6)求字符串长度
function Length(s:string):integer;1返回s字符串的长度 7)去掉不可见字符
去左(left)
function trimleft(const s:string):string;1去右(right)
function trimright(const s:string):string;1前后
function trim(s:string):string;18)删除子字符串的过程
procedure delete(s:string;index,count:integer);1删除从第index到count的字符。count>s长度,删除index后面全部
9)插入字符串
procedure insert(source:string;s:string;index:integer);1将source插入单s中的第index位置
日期时间函数过程
1)日期时间函数
function Now:TDateTime;1返回当前日期时间,如:
edit1.Text:=datetimetostr(now);1结果:2017-12-15 15:38:00
2)日期函数
function Data:TDateTime;edit1.Text:=datetimetostr(Date)12或者
edit1.Text:=datetostr(Date);1结果:2017-12-15
3)时间函数
function Time:TDateTime; 1function GetTime:TDateTime;1edit1.Text:=timetostr(time)1或
edit1.Text:=timetostr(gettime);1结果:10:46:50 4)星期函数
function DayOfWeek(Date:TDateTime):Integer;edit1.Text:=inttostr(dayofweek(date));12结果:星期日为1,星期六为7。 今天星期5,返回6 5)日期与时间格式的函数
function FormatDateTime(const format:string;DateTime:TDateTime):string;1format格式化字符串表2-8 37页
类型转换函数与过程
1)日期转化为字符串
function DateToStr(Date:TDateTime):string;12)时间转化为字符串
function TimeToStr(Time:TDateTime):string;13)时间、日期转化为字符串
function DateTimeToStr(DateTime:TDateTime):string;14)时、分、秒、毫秒转化为时间类型
function EncodeTime(Hour,Min,Sec,MSec:Word):TDateTime;15)时间类型转化为时、分、秒、毫秒
procedure DecodeTime(Time:TDateTime,Hour,Min,Sec,MSec:Word;16)年、月、日转化为日期类型
function EncodeDate(Year,Month,Day:Word):TDateTime;17)日期类型转化为年、月、日
procedure DecodeDate(Date:TDateTime;Year,Month,Day:Word);18)字符串型转化为数值型
procedure Val(s;var v;var code:integer);19)整型转化为字符串型
function IntToStr(Value:integer):string;1function IntToStr(Value:Int64):string;1整型可以是integer或int64 10)字符串型转化为整型
function StrToInt(const s:string):integer;1function StrToInt64(const s:string):int64;111)浮点型转化为字符串型
function FloatToStr(Value:Extended):string;112)字符串型转化为浮点型
function StrToFloat(const s:string):Extended;113)字符串型转化为布尔型
function StrToBool(const s:string):boolean;114)布尔型转化为字符串型
function BoolToStr(b:boolean;UseBoolStrs:boolean=false):string;1其他函数与过程
1)ASCII码转化为字符
function Chr(x:Byte):Char;1将x转化为字符,如chr(97)位a字符 2)取序号函数
function Ord(x);1如1的序号为1,a的序号是a的ASCII的值97,false的序号为0,true的序号为1; 3)前导函数
function Pred(x);1b的前导是a,2的前导是1; 4)后继函数
function Succ(x);1b的后继是c,2的后继是3.false的后继是true
语句
(细节:end之前的;可以省略不写) 1.赋值语句(’:=’为赋值符号)
var i;...i:=1...12或
var st:string;...st:='聪明的程序员使用delphi';label1.Caption:=st...1232.空语句(设计目的:需要需要而已) 单独一个分号就是一个空语句。
var i;...i:=12;;13.读语句 read(变量,变量…); readln(变量,变量…); 区别:前者必须有参数,后者不必须。 4.读语句 write(项1,项2); writeln(项1,项2); writeln();//换行 区别:后者换行 (项可以是常数、变量、函数、表达式) 5.goto语句(少用) 1)线定义后使用 2)不可以从构造语句(循环)外转到构造与句内
相关阅读 >>
Delphi twebbrowser:确定带有框架的页面何时完成
更多相关阅读请进入《Delphi》频道 >>