专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > C语言

C 语言程序: 编写一个 Windows embedded 输出语句的程序用来调整文字信息等功能。 [有关问题点数:40分]

发布时间:2011-06-28 09:55:58 文章来源:www.iduyao.cn 采编人员:星星草
C 语言程序求救: 编写一个 Windows embedded 输出语句的程序用来调整文字信息等功能。 [问题点数:40分]
请多多指教!        
        需要编写一个满足 Windows embedded 输出语句的程序用来调整文字信息等功能。该程序需要能够要求使用者输入任意文件后能自己识别特定的单词,句子,和特定的指令后,生成相对应的正确输出格式。
所输出的文件中,每一行的文字不需要有开头的空格(但是文件中是可以允许有空行存在的),所有行的文字要么需要有一个格式命令,要么有一个“单”词(single word), 要么是一些单词之间用一个空格几个空格来隔开。调用文字格式的语句需要用“点”来使用,后面加上两个字母组成的特殊函数 (比如 .br  .ce  .sp) 如果需要一次使用单个格式命令来完成多项指令,需要在它们之间加入一次空格(比如 .sp 1 4 5)
每个单独的指令所输出的语句最长单位是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  停止输入文字行并且直接输出当前行的所有文字。 下一个单子会直接另起一行新的文字行。)
.nf  (no fill  停止输入文字并且直接输出当前所有文字,并且会导致接下来的所有输出语句均按照原本的格式输出【其中,任何格式命令都会继续执行,但是不会输出】)
.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[2];
int temp2,temp3,temp4;
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];

FILE *pInputFile = fopen(inputFile, "r");
    FILE *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);
}
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;
}
if(!strcmp(command, ".ce"))
{
ceFlag = 1;
temp[0] = buff[NPos];
temp[1] = '\0';
ceNValue += atoi(temp);
}
if(!strcmp(command,".lm"))
{
if(!isdigit(buff[NPos]))
{
leftBegin = 0;
}
else
{
temp[0] = buff[NPos];
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];
temp[1] = '\0';
temp2 = atoi(temp);
if(temp2<0 || temp2 >60)
{
leftBegin = 60;
}
else
leftBegin = 60-temp2;
}
}
if(!strcmp(command,".ti"))
{
tiFlag =1;
fputs("\r\n",pOutputFile);
if(!isdigit(buff[NPos]))
{
tiNValue = 0;
}
else
{
temp[0] = buff[NPos];
temp[1] = '\0';
tiNValue = atoi(temp);
}
}
}

if(ceFlag)
{
for(i = 0; i < ceNValue; i++)
{
fgets(buff, BUFFSIZE, pInputFile);
temp2 = strlen(buff);
temp3 = (60-temp2)/2;
for(i=0; i<temp3; i++)
{
fputc(BLANK, pOutputFile);
}
fputs(buff,pOutputFile);
}
ceFlag = 0;
ceNValue = 0;
}
else
{
while(leftBegin != 0 && remainValue <= rightBegin)
{
fputc(BLANK, pOutputFile);
remainValue++;
}
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] != ' ')
{
oneword[j] = buff[i];
i++;
j++;
}
else
{
oneword[j] = '\0';
if(remainValue <= rightBegin)
{
fputs(oneword,pOutputFile);
fputc(BLANK, pOutputFile);
remainValue += strlen(oneword);
remainValue += 1;
}
else
{
fputs("\r\n",pOutputFile);
remainValue = 0;
while(leftBegin != 0 && remainValue <= rightBegin)
{
fputc(BLANK, pOutputFile);
remainValue++;
}
fputs(oneword,pOutputFile);
fputc(BLANK, pOutputFile);
remainValue += strlen(oneword);
remainValue += 1;
}
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
    func(inputFileName, outputFileName);
}

------解决思路----------------------
没准楼主要参考
FLEX+BISON
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: