算法竞赛入门经典:第五章 基础题目选解 5.2 Tex

来源:互联网 发布:天津网络大学 编辑:程序博客网 时间:2024/06/01 03:59
/*在Tex中,双引号是``,右引号是"。输入一篇包含双引号的文章,你的任务是把它转换成TeX的格式。输入:"To be or not to be," quoth the Bard,"that is the question".输出:``To be or not to be,'' quoth the Bard,``thar is the question."思路:关键是判断一个 引号到底是左引号 还是右引号,然后打印出来。可以用栈实现,直接用标志变量,数组:1表示左括号,0表示右括号*//*关键:1 对于输入的每个字符需要判断用 while(EOF != (ch = getchar()) )2 标记变量用 q = !q*/#include <stdio.h>#include <stdlib.h>#include <memory.h>#define MAXSIZE 1024#define LEFTBRA '['#define RIGHTBRA ']'char* judgeBracket(char* str){int iMark[MAXSIZE];//标记数组memset(iMark,2,MAXSIZE);int iFlag = 0;//左括号标记为1,右括号标记为0for(int i = 0; str[i] != '\0' ; i++){if('\"' == str[i]){iMark[i] = iFlag;iFlag = !iFlag;if(1 == iFlag){str[i] = LEFTBRA;}else{str[i] = RIGHTBRA;}}}return str;}void judgeBracket(){char ch;int iFlag = 1;while(EOF != (ch = getchar())){if('\"' ==  ch){iFlag ? putchar(LEFTBRA) : putchar(RIGHTBRA);iFlag = !iFlag;}else//别遗漏{putchar(ch);}}}int main(int argc,char* argv[]){/*char str[MAXSIZE];gets(str);printf("%s\n",judgeBracket(str));*/judgeBracket();system("pause");return 0;}

0 0
原创粉丝点击