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

来自个人维基
跳转至: 导航搜索
 
(未显示1个用户的3个中间版本)
第1行: 第1行:
<noinclude>
+
<source lang="c">
{{Documentation}}
+
#include <iostream>
</noinclude>
+
#include <string>
 +
#include <vector>
 +
#include <iomanip>
  
== 圆角导航栏 ==
+
using namespace std;
  
<div style="border: 1px solid #ccc; border-radius: 12px; padding: 10px; background-color: #f9f9f9;">
+
class MenuItem {
  <!-- 主菜单项 -->
+
public:
  <ul style="list-style-type: none; padding: 0; margin: 0;">
+
    string name;
    <li style="display: inline-block; margin-right: 10px;">
+
    string link;
      <a href="{{{1|首页}}}" style="text-decoration: none; padding: 8px 12px; background-color: #4CAF50; color: white; border-radius: 8px;">首页</a>
+
    MenuItem(string name, string link) : name(name), link(link) {}
    </li>
+
};
    <li style="display: inline-block; margin-right: 10px;">
+
 
      <a href="{{{2|项目概览}}}" style="text-decoration: none; padding: 8px 12px; background-color: #4CAF50; color: white; border-radius: 8px;">项目概览</a>
+
class NavigationBar {
    </li>
+
private:
    <!-- 嵌套菜单 -->
+
    vector<MenuItem> menuItems;
    {{#if: {{{3|}}} |
+
 
    <li style="display: inline-block; margin-right: 10px; border-left: 2px solid #4CAF50; padding-left: 10px;">
+
public:
      <a href="{{{3|子菜单1}}}" style="text-decoration: none; padding: 8px 12px; background-color: #8BC34A; color: white; border-radius: 8px;">子菜单1</a>
+
    // 添加菜单项
     </li>
+
    void addItem(const string& name, const string& link) {
     <li style="display: inline-block; margin-right: 10px;">
+
        menuItems.push_back(MenuItem(name, link));
      <a href="{{{4|子菜单2}}}" style="text-decoration: none; padding: 8px 12px; background-color: #8BC34A; color: white; border-radius: 8px;">子菜单2</a>
+
    }
    </li>
+
 
    }}
+
    // 显示导航栏
    <li style="display: inline-block; margin-right: 10px;">
+
    void display() {
      <a href="{{{5|文档指南}}}" style="text-decoration: none; padding: 8px 12px; background-color: #4CAF50; color: white; border-radius: 8px;">文档指南</a>
+
        cout << "+----------------------------------------------------+" << endl;
    </li>
+
        cout << "|                   导航栏                        |" << endl;
    <li style="display: inline-block; margin-right: 10px;">
+
        cout << "+----------------------------------------------------+" << endl;
      <a href="{{{6|联系方式}}}" style="text-decoration: none; padding: 8px 12px; background-color: #4CAF50; color: white; border-radius: 8px;">联系方式</a>
+
 
     </li>
+
        for (size_t i = 0; i < menuItems.size(); ++i) {
  </ul>
+
            // 设置每个菜单项的显示格式
</div>
+
            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;
}