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

来自个人维基
跳转至: 导航搜索
 
(未显示1个用户的5个中间版本)
第1行: 第1行:
<noinclude>
+
<source lang="c">
{{Documentation}}
+
#include <iostream>
</noinclude>
+
#include <string>
 +
#include <vector>
 +
#include <iomanip>
  
== 返回上一级 ==
+
using namespace std;
  
[[File:arrow-back.svg|right|30px]]  <!-- 可选择显示一个箭头图标 -->
+
class MenuItem {
 +
public:
 +
    string name;
 +
    string link;
 +
    MenuItem(string name, string link) : name(name), link(link) {}
 +
};
  
<span style="background-color:#4CAF50; color:white; padding: 10px 20px; border-radius: 12px; text-decoration: none; font-weight: bold; display: inline-block;">
+
class NavigationBar {
     [[{{{1|}}}|{{{2|返回上一级}}}]]
+
private:
</span>
+
    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;
 +
}
 +
 
 +
</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;
}