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

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