C语言习题1

  需要编写一个满足 Windows embedded 输出语句的程序用来调整文字信息等功能。该程序需要能够要求使用者输入任意文件后能自己识别特定的单词,句子,和特定的指令后,生成相对应的正确输出格式。

所输出的文件中,每一行的文字不需要有开头的空格(但是文件中是可以允许有空行存在的),所有行的文字要么需要有一个格式命令,要么有一个“单”词(single word), 要么是一些单词之间用一个空格几个空格来隔开。调用文字格式的语句需要用“点”来使用,后面加上两个字母组成的特殊函数 (比如 .br  .ce  .sp) 
每个单独的指令所输出的语句最长单位是60(长度为60,包括空格的长度)从输入文件的从每一行来读取,一次读取一个单词,判断这个单词的长度后尝试输出文字。如果某个单词的长度小于等于该行的所剩下的总长度,那么这个单词可以被输出并且该行所剩下的空间等于总空间减去该单词所占用的空间。 每两个单词之间需要加入空格,空格也会显示成输出为文件的一部分,自然而然空格也占用了“1”的空间,所以每个空格都要考虑总空间减去1的长度。如果某个单词的长度直接大于了所剩空间长度,那么可以直接输出语句,并且把该单词加入一个新的输入行。 

例如:

A

dog
chased
the
cat.

输出为:

A dog chased the cat.

如果输入文字为: 

A dog

chased the cat.
也会输出为上面的形式。
下面的例子就会输出为两行文字的形式:
A dog chased
a cat up the
tree. The cat
looked at the dog from
the top of the tree.
显示效果:
A dog chased a cat up the tree. The cat looked at the dog
from the top of the tree. 
因为从“from”开始,这个单词如果夹在第一行就会造成总长度超过60,所以输出为下一行的文字。

几种格式命令的介绍
.br   (break  停止输入文字行并且直接输出当前行的所有文字。 下一个单子会直接另起一行新的文字行。)
.fi   (start fill lines  开始从文件中提取单词并填充文字)
.sp n (blank lines    停止输入文字并且直接输出当前所有文字,同时会输出n个空白并且下一个单词会在n个空白后另起一行输出。如果n没有赋值或者值小于1,那么默认n为1
.ce n (centre lines   停止输入文字并且直接输出当前所有文字,接下来的n行输出将会完全按照原文件的格式来输出,并且全部都以居中对齐的形式【左右边缘向中间缩进】)

接下来的格式命令会导致每一行的文字允许长度发生改变【指的是从左到右的总长度,默认值左边是0,右边是60的单位为60长度的文字行】 改变边缘长度会导致每行的总长度。
.lm n  (left margin  设置所有文字行从当前输出后,自左向右产生n个空白单位长度,如果n的值没有确定,或者n小于0或大于60,那么默认n为0. 该代码会导致接下来的所有输出都会在左边先产生n个空格。
.rm n  (right margin  从右向左,道理同上,不过n的默认值为60【即最大单位长度】

.ti n  (temporary indent  停止输入文字并且直接输出当前所有文字,下一行开始会暂时产生n个空格【以当前的左长度为准,左长度默认为0,但是如果左起长度已经变化,则以变化后的长度起始算起】 之后所有文字继续以当前的左起始长度开始对齐。n 如果没有赋值,默认为0。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
 
 
#define BUFFSIZE 60
#define BLANK 32
#define COMMANDLEN 4
 
 
int func(const char* inputFile, const char* outputFile)
{
    char buff[BUFFSIZE];
    char command[COMMANDLEN];
    int NPos = 4;
    int spNValue = 0;
    char temp[3];
    int temp2,temp3,temp4,temp5;
    int i = 0;
    int j = 0;
    int ceFlag = 0;
    int ceNValue = 0;
    int leftBegin = 0;
    int rightBegin = 60;
    int tiFlag = 0;
    int tiNValue = 0;
    int remainValue = 0;
    char oneword[60];
    int leftBeginTemp = 0;
    int leftBeginChange = 0;
 
    FILE *pInputFile, *pOutputFile;
    pInputFile = fopen(inputFile, "r");
    pOutputFile = fopen(outputFile, "w+");
     
    if(!pInputFile && !pOutputFile)
    {
        printf("fopen error.\n");
        return -1;
    }
 
 
    while((fgets(buff, BUFFSIZE, pInputFile)) != NULL)
    {
        if(buff[0] == '.')
        {
            strncpy(command, buff, COMMANDLEN-1);
            command[COMMANDLEN-1] = '\0';
            if(!strcmp(command,".br"))
            {
                fputs("\r\n",pOutputFile);
                remainValue = 0;
            }
            if(!strcmp(command, ".fi"))
            {
                continue;
            }
            if(!strcmp(command, ".sp"))
            {
                int spNPos = NPos;
                for(i=0;isdigit(buff[spNPos]);i++)
                {
                    temp[0] = buff[spNPos];
                    temp[1] = '\0';
                    spNValue += atoi(temp);
                    spNPos += 2;
                }
                if( i == 0 )
                {
                    spNValue = 1;
                }
 
                for(i=0; i<spNValue; i++)
                {
                    fputc(BLANK, pOutputFile);
                }
                fputs("\r\n",pOutputFile);
                 
                spNValue = 0;
                remainValue = 0;
            }
            if(!strcmp(command, ".ce"))
            {
                fputs("\r\n",pOutputFile);
                ceFlag = 1;
                temp[0] = buff[NPos];
                temp[1] = '\0';
                ceNValue += atoi(temp);
            }
            if(!strcmp(command,".lm"))
            {
                fputs("\r\n",pOutputFile);
                leftBeginChange = 1;
                if(!isdigit(buff[NPos]))
                {
                    leftBegin = 0;
                }
                else
                {
                    temp[0] = buff[NPos];
                    if(isdigit(buff[NPos+1]))
                    {
                        temp[1] = buff[NPos+1];
                        temp[2] = '\0';
                    }else
                    {temp[1] = '\0';}
                    temp2 = atoi(temp);
                    if(temp2<0 || temp2 >60)
                    {
                        leftBegin = 0;
                    }
                    else
                        leftBegin = temp2;
                }
            }
            if(!strcmp(command,".rm"))
            {
                if(!isdigit(buff[NPos]))
                {
                    rightBegin = 60;
                }
                else
                {
                    temp[0] = buff[NPos];
                    if(isdigit(buff[NPos+1]))
                    {
                        temp[1] = buff[NPos+1];
                        temp[2] = '\0';
                    }else
                    {temp[1] = '\0';}
                    temp2 = atoi(temp);
                    if(temp2<0 || temp2 >60)
                    {
                        rightBegin = 60;
                    }
                    else
                        rightBegin = 60-temp2;
                }
            }
            if(!strcmp(command,".ti"))
            {
                tiFlag =1;
                fputs("\r\n",pOutputFile);
                remainValue = 0;
                if(!isdigit(buff[NPos]))
                {
                    tiNValue = 0;
                }
                else
                {
                    temp[0] = buff[NPos];
                    if(isdigit(buff[NPos+1]))
                    {
                        temp[1] = buff[NPos+1];
                        temp[2] = '\0';
                    }else
                    {temp[1] = '\0';}
                    tiNValue = atoi(temp);
                }
            }
        }
        else
        {
        if(ceFlag)
        {
            temp2 = strlen(buff);
            temp3 = (60-temp2)/2;
            for(j=0; j<temp3; j++)
            {
                fputc(BLANK, pOutputFile);
            }
            fputs(buff,pOutputFile);
            ceNValue--;
            if(ceNValue == 0)
            {    ceFlag = 0;}   
        }
        else
        {
            if(leftBeginChange != 0)
            {
                remainValue = 0;
                leftBeginTemp = leftBegin;
                while(leftBeginTemp != 0 && remainValue <= rightBegin)
                {
                    fputc(BLANK, pOutputFile);
                    remainValue++;
                    leftBeginTemp--;
                }
                leftBeginChange = 0;
            }
            if(tiFlag)
            {
                for(i = 0; i < tiNValue && remainValue <= rightBegin; i++)
                {
                    fputc(BLANK, pOutputFile);
                    remainValue++;
                }
                tiFlag = 0;
                tiNValue =0;
            }
             
            j=0;
            temp4 = strlen(buff); 
 
            for(i = 0; i< temp4; i++)
            {
                if(buff[i] != ' ' && buff[i] != 10)
                {
                    oneword[j] = buff[i];
                    j++;
                    continue;
                }
                oneword[j] = '\0';
                temp5 = strlen(oneword);
                temp5 += remainValue;
                if(temp5 <= rightBegin)
                {
                    fputs(oneword,pOutputFile);
                    fputc(BLANK, pOutputFile);
                    remainValue = temp5;
                }
                else
                {
                    fputs("\r\n",pOutputFile);
                    remainValue = 0;
                    leftBeginTemp =leftBegin;
                    while(leftBeginTemp != 0 && remainValue <= rightBegin)
                    {
                        fputc(BLANK, pOutputFile);
                        remainValue++;
                        leftBeginTemp--;
                    }
                    fputs(oneword,pOutputFile);
                    fputc(BLANK, pOutputFile);
                    remainValue += strlen(oneword);
                }
                j=0;
                 
            }  
        }
        }
    }
    fclose(pInputFile);
    fclose(pOutputFile);
    return 0;
}
 
void main()
{
    char *inputFileName = "input.txt";   //name of input file
    char *outputFileName = "output.txt";  //name of output file
    int n = func(inputFileName, outputFileName);
    printf("%d",n);
    system("pause");
}


input.txt

.br
My name is Bob.
My year is 24.
.fi
Your name if Hoj.
HH.
.sp 1
.sp 2
Here your tag.
.ce 3
A dog is coming.
You must go out of there.
Or your home will burn.
This is my pleasure.
But can you give me this bag, I love it so much.
.lm 5
You can, yeah, must do that.
Or you will die.
.rm 10
This is my iphone.
That is your nokia, hhh.
.ti 2
Jin tian tian qi hao qinglang.
Chu chu hua er xiang.
.lm 1
.rm 2
Wo Yeliangchen bufu.

output.txt


My name is Bob. My year is 24. Your name if Hoj. HH.  
 
Here your tag. 
                     A dog is coming.
                 You must go out of there.
                  Or your home will burn.
This is my pleasure. But can you give me this bag, I love it 
so much. 
     You can, yeah, must do that. Or you will die. This is my 
     iphone. That is your nokia, hhh. 
  Jin tian tian qi hao qinglang. Chu chu hua er xiang. 
 Wo Yeliangchen bufu.    



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章