#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <conio.h> using namespace std; struct port { char name[16]; int status;//1:激活 0:禁用 char ip[16]; char type[4]; //端口类型LAN WAN }; //定义5个端口变量; struct port port1, port2, port3, port4,port5; void show_port_status(struct port port) { printf("名称[%s]\t状态[%s]\tIP[%s]\t类型[%s]\n", port.name, port.status == 0 ? "禁用" : "激活", port.ip, port.type); } void show_ports(void) { system("cls"); printf("---端口状态---\n"); printf("PORT1:\t"); show_port_status(port1); printf("PORT2:\t"); show_port_status(port2); printf("PORT3:\t"); show_port_status(port3); printf("PORT4:\t"); show_port_status(port4); printf("PORT5:\t"); show_port_status(port5); system("pause"); } void set_port1(void) { system("cls"); printf("---设置PORT1端口---\n"); printf("请输入端口名称: "); scanf_s("%s", port1.name,sizeof(port1.name)); printf("请输入端口的状态:[0:禁止] [1:激活] "); scanf_s("%d", &port1.status); printf("请输入端口的类型:[LAN 或 WAN] "); scanf_s("%s", port1.type,sizeof(port1.type)); printf("请输入端口的IP地址: "); scanf_s("%s", port1.ip,sizeof(port1.ip)); } void set_port2(void) { system("cls"); printf("---设置PORT2端口---\n"); printf("请输入端口名称: "); scanf_s("%s", port2.name, sizeof(port2.name)); printf("请输入端口的状态:[0:禁止] [1:激活] "); scanf_s("%d", &port2.status); printf("请输入端口的类型:[LAN 或 WAN] "); scanf_s("%s", port2.type, sizeof(port2.type)); printf("请输入端口的IP地址: "); scanf_s("%s", port2.ip, sizeof(port2.ip)); } void set_port3(void) { system("cls"); printf("---设置PORT3端口---\n"); printf("请输入端口名称: "); scanf_s("%s", port3.name,sizeof(port3.name)); printf("请输入端口的状态:[0:禁止] [1:激活] "); scanf_s("%d", &port3.status); printf("请输入端口的类型:[LAN 或 WAN] "); scanf_s("%s", port3.type,sizeof(port3.type)); printf("请输入端口的IP地址: "); scanf_s("%s", port3.ip,sizeof(port4.ip)); } void set_port4(void) { system("cls"); printf("---设置PORT4端口---\n"); printf("请输入端口名称: "); scanf_s("%s", port4.name,sizeof(port4.name)); printf("请输入端口的状态:[0:禁止] [1:激活] "); scanf_s("%d", &port4.status); printf("请输入端口的类型:[LAN 或 WAN] "); scanf_s("%s", port4.type,sizeof(port4.type)); printf("请输入端口的IP地址: "); scanf_s("%s", port4.ip,sizeof(port4.ip)); } void set_port5(void) { system("cls"); printf("---设置PORT5端口---\n"); printf("请输入端口名称: "); scanf_s("%s", port5.name,sizeof(port5.name)); printf("请输入端口的状态:[0:禁止] [1:激活] "); scanf_s("%d", &port5.status); printf("请输入端口的类型:[LAN 或 WAN] "); scanf_s("%s", port5.type,sizeof(port5.type)); printf("请输入端口的IP地址: "); scanf_s("%s", port5.ip,sizeof(port5.ip)); } void Create_Account(void) { system("cls"); string new_account; string new_password; cout<<"请输入要创建的账号名称:"; cin >> new_account; cout << "请输入要创建的账号密码:"; cin >> new_password; cout << "您已成功创建新账号!\n"; cout << "新账号名为:"<<new_account<<"\n密码为:"<< new_password<< endl; system("pause"); } void Ip_Manage(void) { system("cls"); printf("------IP管理中------\n"); printf("待实现...\n\n"); system("pause"); } void input_error(void) { system("cls"); printf("\n\n输入错误!\n\n"); printf("\n\n按任意键后,请重新输入\n\n"); system("pause"); } void set_ports(void) { int n=0; system("cls"); printf("请选择要设置的端口:"); cin.ignore((numeric_limits<std::streamsize>::max)(), '\n'); cin >> n; switch (n){ case 1: set_port1(); break; case 2: set_port2(); break; case 3: set_port3(); break; case 4: set_port4(); break; case 5: set_port5(); break; default: break; } system("pause"); } void Port_Manage(void) { char n; while (1) { system("cls"); printf("1. 查看端口\n"); printf("2. 设置端口\n"); printf("3. 返回主菜单\n"); printf("请选择: "); cin.ignore((numeric_limits<std::streamsize>::max)(), '\n'); scanf_s("%c", &n,sizeof(n)); if (n == '1') { show_ports(); } else if (n == '2') { set_ports(); } else if (n == '3') { break; } else { input_error(); } } } void show_menu(void) { system("cls"); printf("\n---交换机后台管理---\n"); printf("1.创建账号\n"); printf("2.IP管理\n"); printf("3.端口管理\n"); printf("4.退出\n"); printf("请选择:"); } void login(void) { string username; string password; while (1){ system("cls"); printf("请输入用户名:"); cin >> username; printf("请输入密码:"); cin >> password; if ((username== "admin") && (password =="123456")) { break; } else { printf("用户名或密码错误!请重新输入!\n"); system("pause"); } } } int main(void) { int n=0; login(); bool is_quit = false; while (!is_quit){ show_menu(); scanf_s("%d", &n); switch (n) { case 1:Create_Account(); break; case 2:Ip_Manage(); break; case 3:Port_Manage(); break; case 4: is_quit = true; break; default: input_error(); break; } } return 0; }标签:实战,void,端口,C++,port,交换机,printf,sizeof,scanf From: https://www.cnblogs.com/smartlearn/p/16839356.html