1. 程式人生 > 實用技巧 >C3L-Uva 272-TEX Quotes

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 }