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.    



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