文字排版

描述
给一段英文短文,单词之间以空格分隔(每个单词包括其前后紧邻的标点符号)。请将短文重新排版,要求如下:

每行不超过80个字符;每个单词居于同一行上;在同一行的单词之间以一个空格分隔;行首和行尾都没有空格。

输入
第一行是一个整数n,表示英文短文中单词的数目. 其后是n个以空格分隔的英文单词(单词包括其前后紧邻的标点符号,且每个单词长度都不大于40个字母)。

输出
排版后的多行文本,每行文本字符数最多80个字符,单词之间以一个空格分隔,每行文本首尾都没有空格。

样例输入:

84
One sweltering day, I was scooping ice cream into cones and told my four children they could "buy" a cone from me for a hug. Almost immediately, the kids lined up to make their purchases. The three youngest each gave me a quick hug, grabbed their cones and raced back outside. But when my teenage son at the end of the line finally got his turn to "buy" his ice cream, he gave me two hugs. "Keep the changes," he said with a smile. 

样例输出:

One sweltering day, I was scooping ice cream into cones and told my four
children they could "buy" a cone from me for a hug. Almost immediately, the kids
lined up to make their purchases. The three youngest each gave me a quick hug,
grabbed their cones and raced back outside. But when my teenage son at the end
of the line finally got his turn to "buy" his ice cream, he gave me two hugs.
"Keep the changes," he said with a smile.

C++的写法:

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    char words[1000][40]; //用于保存每一个单词
    int wordsLen[1000]; // 记录每一个单词的长度
    int n; // 需要处理的单词总数
    cin >> n;
    cin.get();//cin之后要用cin.get()读取换行符,
    for (int i = 0; i < n;i++) //输入单词数据,处理后得到每个单词的长度
    {
        char temp;
        for (int j = 0; j < 40;j++) {
            cin.get(temp);                 
                if (temp != ' '&&temp != '\n') {
                    words[i][j] = temp;
                    
                }
                else
                {
                    wordsLen[i] = j;
                    break;
                }
            }
        
    }
    //先输出第一个单词
    int length = wordsLen[0];
    for (int j = 0; j < wordsLen[0]; j++)
        cout << words[0][j];
    for (int i = 1; i < n; i++)
    {
        //如果该单词,连同前面的一个空格加入后不换行,则输出空格和该单词
        if (length + 1 + wordsLen[i] <= 80)
        {
            length = length + 1 + wordsLen[i];
            cout << ' ';
            for (int j = 0; j < wordsLen[i]; j++)
                cout << words[i][j];
        }
        else//如果该单词,连同前面的一个空格加入后换行,则输出回车和该单词,另外重置本行现有长度为单词长度
        {
            cout << endl;
            length = wordsLen[i];
            for (int j = 0; j < wordsLen[i]; j++)
                cout << words[i][j];
        }
    }
    return 0;
View Code

 

C#控制台:

样例输入:

One sweltering day, I was scooping ice cream into cones and told my four children they could "buy" a cone from me for a hug. Almost immediately, the kids lined up to make their purchases. The three youngest each gave me a quick hug, grabbed their cones and raced back outside. But when my teenage son at the end of the line finally got his turn to "buy" his ice cream, he gave me two hugs. "Keep the changes," he said with a smile. 

样例输出:

****************************
83个单词,每行最多80个字符
****************************
Onesweltering day, I was scooping ice cream into cones and told my four
children they could "buy" a cone from me for a hug. Almost immediately, the
kids lined up to make their purchases. The three youngest each gave me a quick
hug, grabbed their cones and raced back outside. But when my teenage son at
the end of the line finally got his turn to "buy" his ice cream, he gave me
two hugs. "Keep the changes," he said with a smile.

 

using System;
namespace TextTypeSetting
{
    class Program
    {
        public static  string FinaString(string str,int maxWidth)
        {
            string[] array = new string[200];//单词数组
            array = str.Split(' ');
            int len;
            len = array.Length;
            string strLine = "";
            string kong = "";
            int tot = 1;//行数 
            Console.WriteLine("****************************");
            Console.WriteLine("{0}个单词,每行最多{1}个字符", len - 1, maxWidth);
            Console.WriteLine("****************************");
            for (int j = 0; j < len; j++)
            {
                if (j == 0)
                {
                    array[j] = array[0]+" ";
                }
                else
                {
                    array[j] = array[j] + " ";
                }
            }
            for (int i = 0; i < len; i++)
            {
                if (strLine.Length + array[i].Length >= maxWidth)
                {
                    if (strLine == "")
                    {
                        continue;
                    }
                    Console.Write(strLine);
                    Console.WriteLine();
                    strLine = kong;
                    strLine = strLine + array[i];
                    tot++;
                }
                else
                {
                    strLine = strLine + array[i];
                }
            }
            return strLine;

        }
        public static void Main(string[] args)
        {
            string strRead = "";
            int maxWidth = 80;//每行最大字符数
            strRead = Console.ReadLine();
            string finaString=  FinaString(strRead,maxWidth);
            Console.WriteLine(finaString);
        }
    }
}

PascalScript:

样例:

%5PES %35VIS 10%SP  %20FT %30PRES

 function FinaString(var str:string;var maxWidth:int):string;                           
   var  
        i:int;
        j:int;
        line:int;//行数
        strafter:string;
         a:array of string;//动态数组
         a1:array of string;//动态数组             
      b:array of string;//动态数组                  
         k:int ; m:int;  n:int;
            lengthOfArray:int;            
                          tmp:string;                               
          strLine:string;
          kong:string;
           tot:int;//行数
           p:int;                         
       begin         
                  tot:=1;              
                   strLine:='';          
                  kong:='';              
                   tmp:='';                
                        
                  str:=replacetext(str,' ',',');
                  str:= str+','  ;         //字符串加个逗号                                                                     
                        i:=  length(str);                                 
                  setlength(a,i);//动态更新数组
                        setlength(a1,i);//动态更新数组                      
                       setlength(b,15);   //记录了15行  ,这里应该远少于15行                                                                                             
                 // setlength(b,i);                      
                           k:=0;    //逗号的个数                                        
                       FOR j:=1 TO i do    
                         begin                            
                           if    str[j] =','  then
                           begin
                           k:=k+1;                                                                             
                           end;                                     
                        //  Memo2.text:=str+sComponent+inttostr(k);
                           //Memo2.text:=  str+substring(sComponent,',',1);                                  
                       end;
                                lengthOfArray:=k;                                                                           
                              for m:=1 to k do
                               begin
                                a[m]:=substring(str,',',m);                                        
                               end;                            
                                         str:='';                                          
                                           for i:=1 TO  lengthOfArray do
                                          begin
                                                                                             
                                               //str:=str+a[j];
                                                 if  i=1 then
                                                   begin                                                               
                                                     a1[i]:= a[1]+' ';
                                                     end  
                                                     else
                                                     begin
                                                            a1[i]:=  a[i]+' ' ;                                                         
                                                     end;                                                   
                                           end   ;      //现在a1数组中放了   a1[1]=aaa  a1[2]=bbb ...  长度为lengthOfArray  (单词个数)

                                           ///////////////////////////////////////////////////////////////  
                                                  p:=0;                                             
                                                   for i:=1 to lengthOfArray do
                                             begin
                                                                                                                                      
                                                   if length(strLine)+ length(a1[i])>=maxWidth  then
                                                   begin
                                                                                                                                                                               
                                                             if   strLine='' then
                                                                 begin
                                                                    continue;                                                                       
                                                                 end   ;
                                                                   p:=p+1;                                                                                                                                                             
                                                                         b[p]:=strLine+#10;          //b数组存放 被切割的行                                                                                           
                                                                           
                                                                        strLine:= kong;
                                                                        strLine:= strLine + a1[i];
                                                                         //tot:= tot+1;   行数                                                                                                                                                      
                                                     end
                                                     else
                                                     begin
                                                             strLine:= strLine + a1[i];                                                         
                                                     end;                                                               
                                             end;
                                              ///////////////////////////////////////////////////////////////




                                                
                                                      for i:=1 to p do
                                             begin
                                               tmp:=tmp+b[i];                                                                                                                                                                          
                                             end;  
                                              
                                            ///////////////////////////////////////////////////////////////  
                                             
                          
                                  //////////////////////////////////////////////////////////////                                            
                                
                                                                                                                                                                     
                                                       //result:= tmp+strLine+inttostr(p);
                                                       result:=  tmp+strLine;//+inttostr(p);  //p为>maxWidth的行数                                                                                               
       end;

 

 

 

 

原文链接:https://blog.csdn.net/panda711/article/details/50750620

GitHub:   https://github.com/RookieBoy666/TextTypeSetting

 

posted on 2019-12-11 09:54  RookieBoy666  阅读(424)  评论(0编辑  收藏  举报