ccf 201703-3 Markdown
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 const int maxn = 100+5; 5 char ans[maxn][maxn]; 6 7 void coutIn(char *a) 8 {///进行行内文字的转换 9 for(int i=0;i<strlen(a);i++) 10 { 11 if(a[i] == '_') 12 {//强调不会出现嵌套,每行中 `_` 的个数一定是偶数, 13 //且不会连续相邻。注意 `_Text_` 的前后不一定是空格字符。 14 cout<<"<em>"; 15 i++; 16 int num = 0; 17 char temp[maxn]; 18 while(a[i] != '_') 19 { 20 temp[num++] = a[i]; 21 i++; 22 } 23 temp[num] = '\0'; 24 i++;//忽略掉最后一个'_' 25 coutIn(temp); 26 cout<<"</em>"; 27 } 28 if(a[i] == '['){//处理超链接 29 //超级链接和强调可以相互嵌套,但每种格式不会超过一层。 30 char temp[maxn]; 31 int num = 0; 32 while(a[++i] != ']'){ 33 temp[num++] = a[i]; 34 } 35 temp[num] = '\0'; 36 cout<<"<a href=\""; 37 while(a[i++] != '('); 38 char temp2[maxn]; 39 int num2 = 0; 40 while(a[i] != ')'){ 41 temp2[num2++] = a[i]; 42 i++; 43 } 44 temp2[num2] = '\0'; 45 i++;//去掉最后的')' 46 coutIn(temp2); 47 cout<<"\">" ; 48 coutIn(temp); 49 cout<<"</a>"; 50 } 51 if(a[i] != '\0') 52 cout<<a[i]; 53 } 54 } 55 void coutList(char *a) 56 { 57 cout<<"<li>"; 58 int space = 0; 59 while(a[space] == ' ') space++; 60 coutIn(a+space); 61 cout<<"</li>"<<endl; 62 } 63 int main() 64 { 65 string str; 66 int Count=0; 67 68 while(getline(cin,str)) 69 { 70 int len = str.length(); 71 for(int i=0;i<=len;i++){//将读入的行存储 72 if(i == len) ans[Count][i] = '\0'; 73 else ans[Count][i] = str[i]; 74 } 75 Count++; 76 ///注意'\n’在字符数组中为'\000' 77 ///空格 会保留 78 } 79 80 ///处理字符数组 81 for(int i=0;i<Count;i++) 82 {//逐行进行处理 83 //标题行 84 if(ans[i][0] == '#') 85 { 86 int temp = 1; 87 while(ans[i][temp] == '#') 88 { 89 temp++; 90 } 91 cout<<"<h"<<temp<<">"; 92 ///输出行内字符 93 int space = 0; 94 while(ans[i][temp+space] == ' ') space++; 95 //每个标题区块只有一行,由若干个 `#` 开头, 96 //接着一个或多个空格,然后是标题内容,直到行末。 97 coutIn(ans[i]+temp+space); 98 cout<<"</h"<<temp<<">"<<endl; 99 continue; 100 } 101 else if(ans[i][0] == '\0')//空格行 102 {//输出时删除所有分隔区块的空行 103 continue; 104 } 105 else if(ans[i][0] == '*'){///处理列表 106 cout<<"<ul>"<<endl; 107 coutList(ans[i]+1);//输出第一行列表 108 //每行由 `*` 开头,接着一个或多个空格,然后是列表项目的文字,直到行末。 109 while(ans[i+1][0] == '*') 110 { 111 coutList(ans[++i]+1); 112 } 113 cout<<"</ul>"<<endl; 114 } 115 else{ 116 cout<<"<p>"; 117 coutIn(ans[i]); 118 while(ans[i+1][0] != '\0'){ 119 cout<<endl; 120 coutIn(ans[++i]); 121 } 122 cout<<"</p>"<<endl; 123 } 124 125 } 126 /* 127 for(int i=0;i<Count;i++) 128 printf("%s",ans[i]); 129 */ 130 return 0; 131 }