2014-10-29 58 views
-1

你好我有一个任务,我不知道它是如何工作的。帕斯卡长度+圆?

我必须在pascal中制作一个程序,在其中编写一个单词,然后该程序找到该单词中的中间字母并将其显示在屏幕上。 例如:你有单词“dog”,程序会在屏幕上显示字母o,如果有4个字母,比如“tree”,那么程序将会到mid和+ 1,所以它会把我们的字母“e ”。 据我所知,我需要使用函数round(x)和Length(x),但我不知道如何正确使用它们以及如何定义VAR?

uses crt; 
var 
    x,x1: string; 
    z,y: real; 
    y1: integer; 
BEGIN 
    readln(x); 
    z:=length(x); 
    y:= z/2; 
    y1:= round(y) +1; 
    writeln; 
    x1:=copy(x, y1,1); 
    writeln(x1); 
    readkey; 
END. 

回答

1

您使用整数索引字符串,而不是real s。

var 
    Len, Mid: Integer; 
    TestString: string; 
    CenterChar: Char; 
begin 
    TestString := 'tree'; 
    Len := Length(TestString); 
    Mid := Len div 2; 

    // Your choice here 
    if not Odd(Len) then 
    Inc(Mid); 

    // or you can use this instead 
    { 
    if (Len mod 2 = 0) then 
    Inc(Mid); 
    } 

    CenterChar := TestString[Mid]; // Access string by index. No copy needed 
    WriteLn(CenterChar); 
end;