1 条题解

  • 0
    @ 2025-5-30 12:24:48
    #include <iostream>
    #include <string>
    #include <vector>
    #include <queue>
    #include <sstream>
    #include <iomanip>
    #include <algorithm>
    
    using namespace std;
    
    struct Table {
        vector<char> headFormat;
        vector<string> headLine;
        vector<vector<string> > body;
        vector<int> colWidths;
    };
    
    class InstruensFabulam {
    private:
        queue<Table> chartTableList;
        Table currentTable;
    
    public:
        void start() {
            string input;
            while (getline(cin, input)) {
                if (input == "*") break;
                lineParse(input);
            }
            currentTable = calculateColumnWidth(currentTable);
            chartTableList.push(currentTable);
    
            while (!chartTableList.empty()) {
                drawThisTable(chartTableList.front());
                chartTableList.pop();
            }
        }
    
        void lineParse(const string& strLine) {
            if (isHeadFormat(strLine)) {
                if (!currentTable.headLine.empty()) {
                    currentTable = calculateColumnWidth(currentTable);
                    chartTableList.push(currentTable);
                }
                currentTable = Table();
                currentTable.headFormat.assign(strLine.begin(), strLine.end());
            } else {
                insertToCurrentTable(strLine);
            }
        }
    
        void insertToCurrentTable(const string& strLine) {
            if (currentTable.headLine.empty()) {
                currentTable.headLine = splitAndStore(strLine);
            } else {
                currentTable.body.push_back(splitAndStore(strLine));
            }
        }
    
        vector<string> splitAndStore(string str) {
            vector<string> result;
            size_t pos;
            while ((pos = str.find('&')) != string::npos) {
                result.push_back(str.substr(0, pos));
                str = str.substr(pos + 1);
            }
            result.push_back(str);
            return result;
        }
    
        bool isHeadFormat(const string& str) {
            return !str.empty() && (str[0] == '<' || str[0] == '>' || str[0] == '=');
        }
    
        Table calculateColumnWidth(Table table) {
            int cols = table.headLine.size();
            table.colWidths = vector<int>(cols);
            for (int i = 0; i < cols; ++i)
                table.colWidths[i] = table.headLine[i].size();
            for (size_t i = 0; i < table.body.size(); ++i) {
                for (size_t j = 0; j < table.body[i].size(); ++j) {
                    if ((int)table.body[i][j].size() > table.colWidths[j])
                        table.colWidths[j] = table.body[i][j].size();
                }
            }
            for (int i = 0; i < cols; ++i)
                table.colWidths[i] += 2; // for 1 space padding on each side
            return table;
        }
    
        void drawThisTable(Table& table) {
            int cols = table.colWidths.size();
            int totalWidth = 1; // start with @
            for (int i = 0; i < cols; ++i)
                totalWidth += table.colWidths[i] + 1;
            drawBorder(totalWidth, '@', '-');
    
            drawRow(table.headLine, table.headFormat, table.colWidths);
    
            drawSeparator(table.colWidths);
    
            for (size_t i = 0; i < table.body.size(); ++i)
                drawRow(table.body[i], table.headFormat, table.colWidths);
    
            drawBorder(totalWidth, '@', '-');
        }
    
        void drawBorder(int width, char edge, char fill) {
            cout << edge;
            for (int i = 0; i < width - 2; ++i)
                cout << fill;
            cout << edge << endl;
        }
    
        void drawSeparator(const vector<int>& colWidths) {
            cout << "|";
            for (size_t i = 0; i < colWidths.size(); ++i) {
                for (int j = 0; j < colWidths[i]; ++j)
                    cout << "-";
                if (i != colWidths.size() - 1)
                    cout << "+";
            }
            cout << "|" << endl;
        }
    
        void drawRow(const vector<string>& row, const vector<char>& format, const vector<int>& widths) {
            cout << "|";
            for (size_t i = 0; i < widths.size(); ++i) {
                string cell = (i < row.size() ? row[i] : "");
                int pad = widths[i] - cell.size();
                if (format[i] == '<') {
                    cout << " " << cell << string(pad - 1, ' ');
                } else if (format[i] == '>') {
                    cout << string(pad - 1, ' ') << cell << " ";
                } else { // center
                    int left = pad / 2;
                    int right = pad - left;
                    cout << string(left, ' ') << cell << string(right, ' ');
                }
                cout << "|";
            }
            cout << endl;
        }
    };
    
    int main() {
        InstruensFabulam app;
        app.start();
        return 0;
    }
    • 1

    信息

    ID
    579
    时间
    1000ms
    内存
    10MiB
    难度
    10
    标签
    递交数
    2
    已通过
    1
    上传者