1. 程式人生 > >英文排版系統C語言實現

英文排版系統C語言實現

英文排版系統C語言實現

程式碼:

#include <stdio.h>
#include <stdlib.h>
#include<string.h>

void route(char Text[],int *start,int *end,int column);
void output(char cpy[],int column);
int countspace(char cpy[]);

int main()
{
    system("color 0B");
    Menu();
    int start,end,column,length;
    int i;
    char Text[10000],ch;
    printf("\nPlease input or copy the text you want to transform:\n\n");
    gets(Text);
    printf("\n\nPlease input the column(column>=25):");
    scanf("%d",&column);
    system("cls");
    printf("\n");
    printf("Transformed text:\n\n");
    start=0;
    end=column-1;
    length=strlen(Text);
    if(length<column)
    {
        printf("%s",Text);
        return 0;
    }
    while(end<length)
    {
        route(Text,&start,&end,column);
    }
    for(i=start; i<length; i++)
    {
        printf("%c",Text[i]);
    }
    printf("\n\nInput 'c'  to continue or input ESC to quit!");
    while(1)
    {
        ch=getch();
        if(ch=='c')
        {
            getchar();
            system("cls");
            return main();
        }
        else if(ch==27)
        {
            return 0;
        }
        else
        {
            ;
        }
    }

}
void route(char Text[],int *start,int *end,int column)
{
    int i,j;
    char cpy[500]="";
    while(Text[*end]!=' '&&Text[*end]!=','&&Text[*end]!='!'&&Text[*end]!='.')//代表每行最後一個字元是字母
        *end=*end-1;
    if(Text[*end]==' ')
    {
        for(i=*start,j=0; i<*end; i++,j++)
        {
            cpy[j]=Text[i];
        }
    }
    else
        for(i=*start,j=0;i<=*end;i++,j++)
            cpy[j]=Text[i];
    *start=*end+1;
    *end=*start+column;
    output(cpy,column);
}
void output(char cpy[],int column)
{
    char strout[500]="";
    int i,j=0;
    int spacecount,difference,space0,spacex;
    spacecount=countspace(cpy);
    difference=column-strlen(cpy);
    space0=(column-strlen(cpy))/spacecount;
    spacex=(column-strlen(cpy))%spacecount;
    for(i=0; i<column; i++)
    {
        strout[i]=' ';
    }
    if(difference>0)
    {
        for(i=0,j=0; i<strlen(cpy); i++)
        {
            if(cpy[i]==' ')
            {
                j+=space0;
                if(spacex>0)
                {
                    j++;
                    spacex--;
                }
            }
            strout[j]=cpy[i];
            j++;
        }
        printf("%s\n",strout);
    }
    else
    {
        printf("%s\n",cpy);
    }
}
int countspace(char cpy[])
{
    int i,sum=0;
    for(i=0; i<strlen(cpy); i++)
    {
        if(cpy[i]==' ')
        {
            sum++;
        }
    }
    return sum;
}
void Menu()
{
    char ch;
    printf("                                  【【English text form transformation system】】\n");
    printf("                           ==============================================================");
    printf("\n                                                   1.Start");
    printf("\n                                                   2.Quit\n");
    printf("                           ==============================================================");
    printf("\n\nInput the number you want to operate:");
    while(1)
    {
        ch=getch();
        if(ch=='1')
        {

            system("cls");
            return;
        }
        else if(ch=='2')
        {
            exit(0);
        }
        else
        {
            ;
        }
    }
}

備註: 大家在開始執行程式後,貼上大量文字的時候注意一下,因為在codeblocks的黑框環境下它會卡,所以就點黑框上方的白色邊界裡面的編輯—>貼上 即可貼上入大量待排版的文字了O(∩_∩)O。