“模板:返回上一级”的版本间的差异

来自个人维基
跳转至: 导航搜索
 
第1行: 第1行:
<noinclude>
+
<source lang="c">
{{Documentation}}
+
#include <iostream>
</noinclude>
+
#include <string>
 +
#include <vector>
 +
#include <iomanip>
  
<table style="border: none; background-color: #f9f9f9; border-radius: 15px; padding: 5px;">
+
using namespace std;
  <tr>
+
 
     <td style="padding: 5px; text-align: center; border-radius: 10px;">
+
class MenuItem {
      <a href="{{{1|首页}}}" style="text-decoration: none; background-color: #4CAF50; color: white; padding: 10px 20px; border-radius: 10px;">首页</a>
+
public:
     </td>
+
    string name;
     <td style="padding: 5px; text-align: center; border-radius: 10px;">
+
    string link;
      <a href="{{{2|项目概览}}}" style="text-decoration: none; background-color: #4CAF50; color: white; padding: 10px 20px; border-radius: 10px;">项目概览</a>
+
     MenuItem(string name, string link) : name(name), link(link) {}
     </td>
+
};
     {{#if: {{{3|}}} |
+
 
    <td style="padding: 5px; text-align: center; border-radius: 10px; border-left: 3px solid #4CAF50;">
+
class NavigationBar {
      <a href="{{{3|子菜单1}}}" style="text-decoration: none; background-color: #8BC34A; color: white; padding: 10px 20px; border-radius: 10px;">子菜单1</a>
+
private:
    </td>
+
    vector<MenuItem> menuItems;
    <td style="padding: 5px; text-align: center; border-radius: 10px;">
+
 
      <a href="{{{4|子菜单2}}}" style="text-decoration: none; background-color: #8BC34A; color: white; padding: 10px 20px; border-radius: 10px;">子菜单2</a>
+
public:
    </td>
+
     // 添加菜单项
    }}
+
     void addItem(const string& name, const string& link) {
    <td style="padding: 5px; text-align: center; border-radius: 10px;">
+
        menuItems.push_back(MenuItem(name, link));
      <a href="{{{5|文档指南}}}" style="text-decoration: none; background-color: #4CAF50; color: white; padding: 10px 20px; border-radius: 10px;">文档指南</a>
+
    }
     </td>
+
 
     <td style="padding: 5px; text-align: center; border-radius: 10px;">
+
     // 显示导航栏
      <a href="{{{6|联系方式}}}" style="text-decoration: none; background-color: #4CAF50; color: white; padding: 10px 20px; border-radius: 10px;">联系方式</a>
+
     void display() {
     </td>
+
        cout << "+----------------------------------------------------+" << endl;
  </tr>
+
        cout << "|                   导航栏                        |" << endl;
</table>
+
        cout << "+----------------------------------------------------+" << endl;
 +
 
 +
        for (size_t i = 0; i < menuItems.size(); ++i) {
 +
            // 设置每个菜单项的显示格式
 +
            cout << "| " << setw(20) << left << menuItems[i].name
 +
                << " | " << setw(25) << left << menuItems[i].link << " |" << endl;
 +
        }
 +
 
 +
        cout << "+----------------------------------------------------+" << endl;
 +
    }
 +
};
 +
 
 +
int main() {
 +
    NavigationBar navbar;
 +
      
 +
    // 添加菜单项
 +
     navbar.addItem("首页", "http://example.com");
 +
    navbar.addItem("项目概览", "http://example.com/project");
 +
    navbar.addItem("文档指南", "http://example.com/docs");
 +
    navbar.addItem("联系方式", "http://example.com/contact");
 +
 
 +
     // 显示导航栏
 +
    navbar.display();
 +
 
 +
    return 0;
 +
}
 +
 
 +
</source>

2024年12月1日 (日) 00:05的最后版本

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
 
using namespace std;
 
class MenuItem {
public:
    string name;
    string link;
    MenuItem(string name, string link) : name(name), link(link) {}
};
 
class NavigationBar {
private:
    vector<MenuItem> menuItems;
 
public:
    // 添加菜单项
    void addItem(const string& name, const string& link) {
        menuItems.push_back(MenuItem(name, link));
    }
 
    // 显示导航栏
    void display() {
        cout << "+----------------------------------------------------+" << endl;
        cout << "|                    导航栏                        |" << endl;
        cout << "+----------------------------------------------------+" << endl;
 
        for (size_t i = 0; i < menuItems.size(); ++i) {
            // 设置每个菜单项的显示格式
            cout << "| " << setw(20) << left << menuItems[i].name 
                 << " | " << setw(25) << left << menuItems[i].link << " |" << endl;
        }
 
        cout << "+----------------------------------------------------+" << endl;
    }
};
 
int main() {
    NavigationBar navbar;
 
    // 添加菜单项
    navbar.addItem("首页", "http://example.com");
    navbar.addItem("项目概览", "http://example.com/project");
    navbar.addItem("文档指南", "http://example.com/docs");
    navbar.addItem("联系方式", "http://example.com/contact");
 
    // 显示导航栏
    navbar.display();
 
    return 0;
}