C3L-Uva 272-TEX Quotes

平台:

UVa Online Judge

題號:

10037 - Bridge

題目連結:

https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem&problem=208

題目說明:

在TeX中,左双引号是“``”,右双引号是“''”。输入一篇包含双引号的文章,你的任务是
把它转换成TeX的格式。

範例輸入:

"To be or not to be," quoth the Bard, "that
is the question".
The programming contestant replied: "I must disagree.
To `C' or not to `C', that is The Question!" 
 

範例輸出:

 
``To be or not to be,'' quoth the Bard, ``that
is the question''.
The programming contestant replied: ``I must disagree.
To `C' or not to `C', that is The Question!''
 

解題方法:

逐个输入,逐个处理,直到遇到输入结束符EOF。

 

程式碼:

 1 #include <cstdio>
 2 
 3 int main() {
 4     int c = ' ';
 5     //true表示第1个引号,false表示第2个引号
 6     bool flag = true;
 7         //(c = std::getc(stdin))!= EOF等价于(c=getchar())!=EOF
 8     while ((c = std::getc(stdin))!= EOF) {
 9         if (c == '"') {
10             if (flag) {
11                 printf("``");
12             }
13             else {
14                 printf("''");
15             }
16             flag = !flag;
17         }
18         else {
19             putchar(c);
20         }
21     }
22     return 0;
23 }    

猜你喜欢

转载自www.cnblogs.com/lemonforce/p/13197395.html