zoukankan      html  css  js  c++  java
  • 201703-3 markdown

    实现

    #include <iostream>
    #include <string>
    #include <cstring>
    
    using namespace std;
    
    string line;
    int line_index = 0;
    
    void handle_link() {
        ++line_index;
    
        char text[255]; int text_index = 0;
        while (line[line_index] != ']') {
            text[text_index++] = line[line_index++];
        }
    
        line_index+=2;
    
        char link[255]; int link_index = 0;
        while (line[line_index] != ')') {
            link[link_index++] = line[line_index++];
        }
        ++line_index;
    
    
        cout << "<a href="" ; 
        for (int i = 0;i < link_index;++i) {
            if (link[i] != '_') {
                cout << link[i];
            } else {
                cout << "<em>";
                ++i;
                while (link[i] != '_') {
                    cout << link[i++];
                }
                cout << "</em>";
            }
        }
        
        cout << "">";
    
        for (int i = 0;i < text_index;++i) {
            if (text[i] != '_') {
                cout << text[i];
            } else {
                cout << "<em>";
                ++i;
                while (text[i] != '_') {
                    cout << text[i++];
                }
                cout << "</em>";
            }
        }
        cout << "</a>";
    }
    
    void handle_emp() {
        ++line_index;
        cout << "<em>";
    
        while (line_index < line.size()) {
            switch (line[line_index])
            {
                case '_':
                    ++line_index;
                    cout << "</em>";
                    return;
                case '[':
                    handle_link();
                    break;
                default:
                    cout << line[line_index++];
                    break;
            }
        }
    }
    
    void handle_line() {
    	while (line_index < line.size()) {
    	    switch (line[line_index])
    	    {
    	        case '_':
    	            handle_emp();
    	            break;
    	        case '[':
    	            handle_link();
    	            break;
    	        default:
    	            if (line_index == line.size()) {
    	                return;
    	            } else {
    	                cout << line[line_index++];
    	            }
    	            break;
    	        if (line_index == line.size()) { 
    	            return;
    	        }
    	    }
    	
    	}
    }
    
    int main() {
        
        while (getline(cin,line)){ 
    
            line_index = 0;
    
            handle_begin:;  
    
            if (line.empty()) {
                continue;
            } else if (line[0] == '#') {
                int sharp_cnt = 0;
                while(line[line_index++] == '#') {
                    sharp_cnt+=1;
                }
                cout << "<h" << sharp_cnt << ">";
    
                while (isspace(line[line_index])) {
                    ++line_index;
                }
                handle_line();
                cout << "</h" << sharp_cnt << ">" << endl;
            } else if (line[0] == '*') {
                cout << "<ul>" << endl;
    
                handle_list:;
    
                ++line_index;
                while(isspace(line[line_index])) {
                    ++line_index;
                }
    
                cout << "<li>";
                handle_line();
                cout << "</li>" << endl;
    
                if (getline(cin,line)) {
                    line_index = 0;
                    if (line.empty()||line[0] != '*') {
                        cout << "</ul>" << endl;
                        goto handle_begin;
                    } else {
                        goto handle_list;
                    }
                }
                cout << "</ul>";
                break;
            } else {
                cout << "<p>";
                
                handle_content:;
    
                handle_line();
    
                if (getline(cin,line)) {
                    line_index = 0;
                    if (line.empty() || line[0] == '*' || line[0] == '#') {
                        cout << "</p>" << endl;
                        goto handle_begin;
                    } else {
                        cout << endl;
                        goto handle_content;
                    }
                }
                cout << "</p>";
                break;
            }
    
        }
    }
    
    
  • 相关阅读:
    锐捷交换机密码恢复
    adobe cs3系列产品官方帮助网页(中文)
    网页设计视频教程
    锐捷交换机、路由器配置手册
    WINPE下如何直接删除联想隐藏分区?
    IWMS实现频道页面的方法
    SATA、SCSI、SAS区别与特点
    自定义系统必备
    自己写的Web服务器
    尝试MVP模式
  • 原文地址:https://www.cnblogs.com/amonqsq/p/13610991.html
Copyright © 2011-2022 走看看