英文排版系统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。