delphi问题 一个小函数

来源:百度知道 编辑:UC知道 时间:2024/05/10 06:11:02
function tform1.zhuan_2byte(const str:string):string;
var ss,s1:string;
len,sss:integer;
begin
sss:=strtoint(str);
if sss>=100 then sss:=sss-100 ;
else
if sss>=200 then sss:=sss-200;
s1:=inttostr(sss);
len:=strlen(pchar(s1));
case len of
1:result:='0'+s1;
2:result:=s1;
end;
end;

这是我在一个程序里看到的函数
我觉得这个函数有问题 就是else和后面的if then没有用
请问是不是这样?
还有再问一下
pchar是什么?和char有什么区别?
strlen和length有什么区别?
result的用法
谢谢
if sss>=100 then sss:=sss-100
else
if sss>=200 then sss:=sss-200;

谢谢回答 其实我的意思是
第二个IF THEN是在第一个的else里 就是说 第二个if then是判断的sss<100的情况 所以不可能出现
SSS>=200 所以第二个 if then 没有用

请问我的想法对不对?

function tform1.zhuan_2byte(const str:string):string;
var ss,s1:string;
len,sss:integer;
begin
sss:=strtoint(str);//函数参数的str 转换成整数
if sss>=100 then sss:=sss-100 ;//如果这个数大于100
else
if sss>=200 then sss:=sss-200;
s1:=inttostr(sss);//将sss这个整数转换成字符串
len:=strlen(pchar(s1));//求字符串的长度
case len of //根据长度进行拼接.
1:result:='0'+s1;
2:result:=s1;
end;
end;

pchar是什么?和char有什么区别?
pchar相当于是C语言里面string,就是字符串结尾的地方有个ascii 0
char:是一个字符.
strlen和length有什么区别?
length用来求pascal的字符串的长度的.
strlen用来求C的字符串长度.
主要区别是:
pascal字符串 末尾没有Ascii 0,整个字符串的长度它放在另外一个位置.所以length只要去读另外那个位置就能得到字符串的长度.
strlen:就必须遍历所有的字符,看字符等不等于ascii 0.
区别很大的.
result的用法
result就是返回值.没有多大讲究

第一个 if then 是判断 200 以内的数,
第二个 if then 是判断 200 以外的数。
而且:
if sss>=100 then sss:=sss-100 //;<----这个分号不能有
else
if sss>=200 then sss:=sss-200;