Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Основная";
Текущий архив: 2004.12.05;
Скачать: [xml.tar.bz2];

Вниз

Переносы   Найти похожие ветки 

 
Yanis ©   (2004-11-17 18:02) [0]

Хочу(читай "нужно") написать функцию вывода текста в консоли с переносом слова, если оно не умещается в строке размером, например 50 символов. Я кое-что набросал, но функция работает криво, да и текст все равно обрезается. Оченб нуждаюсь в помощи и советах.

Вот на чём я сейчас остановился (т.е. застрял):
(слова разделяются одиночным пробелом)


const
 s =
   "Previously, LeftStr, RightStr, and MidStr each had an AnsiString parameter type and return type, and did not "+
   "support MBCS strings.  Each of these functions has been replaced by a pair of overloaded functions, one that "+
   "takes and returns AnsiString, and one that takes and returns WideString. The new functions correctly handle "+
   "MBCS strings. This change breaks code that uses these functions to store and retrieve byte values in AnsiStrings. "+
   "Such code should be updated to use the new byte-level functions described below.";

var
 i, LastPos:Integer;
 _S:String;

begin
 for i := 1 to Length(S) do
   begin
   end;

 _S := "";
 for i := 1 to Length(S) do
   begin
     if S[i] = " " then
       begin
         LastPos := i;
       end;

     if (S[i] <> " ") and ((i mod 15) = 0) and (S[i+1] <> " ") then
       begin
         insert(#13,_S,LastPos);
         _S[LastPos] := #10;
         _S := _S+S[i];
       end
     else
       _S := _S+S[i];
   end;

 write(_S);
 readln;
end.


 
Poirot ©   (2004-11-17 18:06) [1]

Я бы для начала предложил похоить отладчиком:) Очень хорпошо видно, что так, а что не так:)


 
Yanis ©   (2004-11-17 19:21) [2]

Я уже второй день хожу. Другие проги подобного типа сразу же "прогрибались", а здесь немогу никак. Может есть класический алгоритм? Поделитесь.


 
Yanis ©   (2004-11-17 23:51) [3]

Всем спасибо. Я так и знал, что где-где, а уж на любимом форуме мне помогут. Но я не в обиде. Ведь здесь все ради патрепаться сидят (см.: http://delphimaster.net/view/15-1100708166/)

Вот что у меня получилось:


procedure PrintFormatText(var S:String);
var
 s1:string;
 i,j:integer;
begin
 // удаляем конец символы конца
 // строки и перевода каретки
 DeleteEol(S);

 I:=0;
 J:=1;

 while ( j <= length(s)) do
   begin
     s1:="";

     while not (s[j] in [" ",".",","]) do
       begin
         s1:=s1+s[j];
         inc(j);
       end;

// MaxInLine - кол-во символов в строке
     if i+length(s1) > MaxInLine then
       begin
         writeln;
         i:=0;
         write(s1);
       end
     else
       begin
         write(s1+s[j]);
         inc(j);
       end;
       
     inc(i,length(s1)+1);
   end;
end;


 
_Lucky_   (2004-11-18 23:38) [4]

а вот что получилось у меня, если конечно я правильно понял, что надо было сделать :)

Uses Crt;

Const
nMaxStrLen = 50;

S =
 "Previously, LeftStr, RightStr, and MidStr each had an"+
"AnsiString parameter type and return type, and did not "+
  "support MBCS strings.  Each of these functions has been" +
  "replaced by a pair of overloaded functions, one that "+
  "takes and returns AnsiString, and one that takes and returns" +
"WideString. The new functions correctly handle "+
  "MBCS strings. This change breaks code that uses these" +
"functions to store and retrieve byte values in AnsiStrings. "+
  "Such code should be updated to use the new byte-level" +
"functions described below.";

Var
sNewStr : String;

Procedure ReadFile (Var sFileName, sResult : String);
Var
fIn : Text;
  sRes : String;
Begin
  sResult := "";
    Assign (fIn, sFileName);
    ReSet (fIn);
    While Not EOF (fIn) Do
    Begin
         ReadLn (fIn, sRes);
         sResult := sResult + sRes;
  End;
    Close (fIn);
End;     {ReadFile}

Procedure ReBuildFile (sFileName : String; nStrSize : Word; Var sStrRes : String);
Var
  nCurrLen : Word;
  nStrLen : Word;
  nSpacePos : Word;
  sIn, sOut : String;
  bExit : Boolean;

Begin
{ReadFile (sFileName, sIn);}
  sIn := S;
  writeln (length (S), " ", length (sIn));
  sOut := "";

  bExit := False;
  nCurrLen := 0;
  While Not bExit Do
  Begin
   nSpacePos := Pos (" ", sIn);
 If nSpacePos > 0 Then
  Begin
         If (nCurrLen + nSpacePos) > nStrSize Then
            Begin
               sOut := sOut + #13 + #10;
                 nCurrLen := 0;
              End;
         sOut := sOut + Copy (sIn, 1, nSpacePos);
           Delete (sIn, 1, nSpacePos);
           nCurrLen := nCurrLen + nSpacePos;
  End;
     nStrLen := Length (sIn);
     If (nStrLen > 0) And (nSpacePos = 0) Then
      Begin
         If (nCurrLen + nStrLen) > nStrSize Then
               sOut := sOut + #13 + #10;
         sOut := sOut + Copy (sIn, 1, nStrLen);
           bExit := True;
        End;
  End;
  sStrRes := sOut;
End;     {ReBuildFile}

Begin
clrscr;
ReBuildFile ("d:\text.txt", nMaxStrLen, sNewStr);
  write (sNewStr);
  readkey;
End.


хотя приведенный текст у меня тоже обрезается, но это потому что в string больше 255 (в пасе) не влазеет


 
Игорь Шевченко ©   (2004-11-18 23:59) [5]

А вот что у меня получилось:

procedure StrBreakApart(const S, Delimeter : string; Parts : TStrings);
var
 CurPos: integer;
 CurStr: string;
begin
 Parts.clear;
 Parts.BeginUpdate();
 try
   CurStr:= S;
   repeat
     CurPos:= Pos(Delimeter, CurStr);
     if (CurPos>0) then begin
       Parts.Add(Copy(CurStr, 1, Pred(CurPos)));
       CurStr:= Copy(CurStr, CurPos+Length(Delimeter),
                   Length(CurStr)-CurPos-Length(Delimeter)+1);
     end else
       Parts.Add(CurStr);
   until CurPos=0;
 finally
   Parts.EndUpdate();
 end;
end;

procedure TfMain.Button1Click(Sender: TObject);
begin
 DestMemo.Lines.Text := WrapText (SrcMemo.Lines.Text);
end;

function TfMain.WrapText(const S: string): string;
const
 PartLimit = 50;
var
 List: TStrings;
 I, PartLen: Integer;
begin
 List := TStringList.Create;
 try
   StrBreakApart (S, " ", List);
   PartLen := 0;
   for I:=0 to Pred(List.Count) do begin
     if Length(List[I]) + PartLen + 1 > PartLimit then begin
       Result := Result + #13;
       PartLen := 0;
     end;
     if PartLen <> 0 then begin
       Result := Result + " ";
       Inc(PartLen);
     end;
     Result := Result + List[I];
     Inc(PartLen, Length(List[I]));
   end;
 finally
   List.Free;
 end;
end;



На входе:
Applications often need to manage lists of character strings. Examples include items in a combo box, lines in a memo, names of fonts, and names of rows and columns in a string grid. The VCL and CLX provide a common interface to any list of strings through an object called TStrings and its descendant TStringList. TStringList implements the abstract properties and methods introduced by TStrings, and introduces properties, events, and methods to


На выходе:
Applications often need to manage lists of
character strings. Examples include items in a
combo box, lines in a memo, names of fonts, and
names of rows and columns in a string grid. The
VCL and CLX provide a common interface to any list
of strings through an object called TStrings and
its descendant TStringList. TStringList implements
the abstract properties and methods introduced by
TStrings, and introduces properties, events, and
methods to


 
Yanis ©   (2004-11-19 00:42) [6]

Я уже сделал, но все равно спасибо. Это я к себе в копилку.


 
VMcL ©   (2004-11-19 07:20) [7]

Эта... а SysUtils.WrapText не подойдет?


 
Yanis ©   (2004-11-19 08:31) [8]

Вобщем подойдёт. (См. Игорь Шевченко (18.11.04 23:59) [5])

А вообще задача должна быть решена на Паскале.

Может еще у коги-нибудь варианты решения есть? Было бы интересно посмотреть.


 
Игорь Шевченко ©   (2004-11-19 11:19) [9]

VMcL ©   (19.11.04 07:20) [7]

Вот же - век живи, век учись. Спасибо :)



Страницы: 1 вся ветка

Форум: "Основная";
Текущий архив: 2004.12.05;
Скачать: [xml.tar.bz2];

Наверх





Память: 0.48 MB
Время: 0.038 c
1-1100878506
PC
2004-11-19 18:35
2004.12.05
"Полный контроль"


14-1100520707
syte_ser78
2004-11-15 15:11
2004.12.05
Как я провел выходные


3-1099571426
Mih
2004-11-04 15:30
2004.12.05
Вопрос поиска.


14-1100679552
NewDelpher
2004-11-17 11:19
2004.12.05
Зачем нужна JAVA и надо ли на неё переходить?


1-1100862998
Dolphin001
2004-11-19 14:16
2004.12.05
msEquation





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский