计算机网络透明网桥算法时间戳c++
要交CG的兄弟们别抄啊,撞上了严nan谁都不会放过的
好久没写博客了,这次是老师布置的作业,做出来一种,觉得写得很不好,第一种方法把情况都写死在代码里了。
上代码
#include<iostream>
#include<map>
using namespace std;
int main(){
map<char,int>mp1;
map<char,int>mp2;
while(true){
cout<<"请输入源站点、目标站点、1号站接收的接口和2号站接收的接口以空格隔开"<<endl;
char destinatios,source;
int inport1,inport2;
cin>>source>>destinatios>>inport1>>inport2;
mp1[source]=inport1;
mp2[source]=inport2;
if(mp1[destinatios]==0&&mp2[destinatios]==0){
cout<<"查表失败,向所有站点广告"<<endl;
continue;
}
if(inport1==mp1[destinatios]){
cout<<"站点1不需要转发!"<<endl;
continue;
mp1[source]=inport1;
}else cout<<"站点1要转发!"<<endl;
if(inport2==mp2[destinatios]){
cout<<"站点2不需要转发!"<<endl;
mp2[source]=inport2;
continue;
}else cout<<"站点2要转发!"<<endl;
}
return 0;
}


第二种就较友好点,我把网桥的数量可以自定义,我认为这样才是真正的实现了学习的需求
上代码
#include<iostream>
#include<map>
#include<vector>
using namespace std;
int main(){
int n;
cout<<"请输入桥数,默认从左到右为0 1 2 n-1"<<endl;
cout<<"请输入源站点左右桥号、起始位置、目标位置。"<<endl;
cout<<"假设网络的两个端点都是网桥,即所有源站点均在两个网桥中间"<<endl;
cin>>n;//桥数
vector<map<char,int> >v(n);
while(true){
char destinatios,source;
int start1,start2;
cin>>start1>>start2>>source>>destinatios;
int f=0;
if(v[start1][destinatios]==0&&v[start2][destinatios]==0){
cout<<"-----------查找失败!向所有站点广播-----------"<<endl;
for(int i=0;i<=start1;i++)
v[i][source]=2;
for(int i=start2;i<n;i++)
v[i][source]=1;
continue;
}
if(v[start1][destinatios]==2){
cout<<"-----------"<<start1<<"号站点不需要转发-----------"<<endl;
f=1;
}
if(v[start2][destinatios]==1){
cout<<"-----------"<<start2<<"号站点不需要转发-----------"<<endl;
if(f==1){
cout<<"-----------目标站点就在"<<start1<<"和"<<start2<<"之间-----------"<<endl;
continue;
}
}
if(v[start1][destinatios]==1){
while(v[start1][destinatios]==1){
cout<<"-----------"<<start1<<"号站点已向左转发-----------"<<endl;
v[start1][source]=2;
start1--;
}
continue;
}
if(v[start2][destinatios]==1){
while(v[start2][destinatios]==1){
cout<<"-----------"<<start2<<"号站点已向右转发-----------"<<endl;
v[start2][source]=1;
start2--;
}
continue;
}
}
}


接下来做的是要加上时间戳,我对时间函数的调用不是很熟悉,就只是实现了这个功能,没有追求精确无误,功能没有问题,但是不是严格得按照打印输出一样显示超过五秒就删除
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <time.h>
#include<iostream>
#include<map>
#include<vector>
#include<time.h>
using namespace std;
int n;
vector<map<char,int> >v(100);
vector<map<char,int> >shijian(100);
void ff(){
for(int k=0;k<n;k++){
for(map<char,int>::iterator it=shijian[k].begin();it!=shijian[k].end();it++){
clock_t start;
start = clock();
char sou=it->first;
if(start-it->second-shijian[k][sou]>5*60){
v[k][sou]=0;
cout<<sou<<"在"<<k<<"号站点的时间大于5秒,记录已被清除"<<endl;
}
}
}
}
int main(){ cin>>n; while(true){
clock_t start;
char des,sou;
int start1,start2;
cin>>start1>>start2>>sou>>des;
int f=0;
if(v[start1][des]==0&&v[start2][des]==0){
cout<<"查找失败,向所有人广播!"<<endl;
for(int i=0;i<=start1;i++){
v[i][sou]=2;
start = clock();
shijian[i][sou]=start;
cout<<i<<"号站点"<<sou<<"的最新时间是"<<shijian[i][sou]<<endl;
}
for(int j=start2;j<n;j++){
v[j][sou]=1;
start = clock();
shijian[j][sou]=start;
cout<<j<<"号站点"<<sou<<"的最新时间是"<<shijian[j][sou]<<endl;
}
ff();
continue;
}
if(v[start1][des]==2){
cout<<"-------------"<<start1<<"号站点不需要转发----------"<<endl;
f=1;
}
if(v[start2][des]==1){
cout<<"------------"<<start2<<"号站点不需要转发------------"<<endl;
if(f==1){
cout<<"------------目标站点就在"<<start1<<"和"<<start2<<"之间----------"<<endl;
ff();
continue;
}
}
if(v[start1][des]==1){
while(v[start1][des]==1){
cout<<"----------"<<start1<<"号站点已向左转发---------"<<endl;
v[start1][sou]=2;
start = clock();
shijian[start1][sou]=start;
cout<<start1<<"号站点"<<sou<<"的最新时间是"<<shijian[start1][sou]<<endl;
start1--;
}
ff();
continue;
}
if(v[start2][des]==1){
while(v[start2][des]==1){
cout<<"----------"<<start2<<"号站点已向右转发---------"<<endl;
v[start2][sou]=1;
start = clock();
shijian[start2][sou]=start;
cout<<start2<<"号站点"<<sou<<"的最新时间是"<<shijian[start2][sou]<<endl;
start2--;
}
ff();
continue;
}
ff();
}
}

计算机网络透明网桥算法时间戳c++的更多相关文章
- Python3 透明网桥算法
import time #定义网桥1 b1 = {} port_list1 = [1, 2] #主机列表 L1 = ['a','b','c'] L2 = ['d','e'] L = [L1,L2] d ...
- 【最大团转最大点独立集(匈牙利算法+时间戳优化)】BZOJ2744-[HEOI2012]朋友圈
[题目大意] 有两个国家A和B.存在以下朋友关系: 1.A国:每个人都有一个友善值,当两个A国人的友善值a.b,如果a xor b mod 2=1,那么这两个人都是朋友,否则不是: 2.B国:每个人都 ...
- 关于 centos 7系统,iptables透明网桥实现【转载请注明】
首先建立网桥:(使用bridge) 示例 桥接eth0 与 eth1 网口 /sbin/modprobe bridge /usr/sbin/brctl addbr br0 /sbin/ifup ...
- 关于 centos 7系统,iptables透明网桥实现
首先建立网桥:(使用bridge) 示例 桥接eth0 与 eth1 网口 /sbin/modprobe bridge /usr/sbin/brctl addbr br0 /sbin/ifup ...
- 计算机网络 Computer Networks 期末复习总提纲
平时不学习,期末火葬场. 一周时间靠王道考研和各路 pdf 自学计网,留下的提纲都在这里了.全是干货.全文 pdf 可以在这里下载:http://cloud.billc.io/s/xNHarppQPG ...
- nat模式、路由模式,网桥模式
路由器的几种连接方式 NAT英文全称是“Network Address Translation”,中文意思是“网络地址转换”,它是一个IETF(Internet Engineering Task Fo ...
- 如何计算CDS view里两个时间戳之间的天数间隔
ABAP透明表里的时间戳,数据类型为dec: 有个需求:计算这两个时间戳之间的天数间隔,丢弃时间戳年-月-日8位后面的小时:分钟:秒. 举个例子:如果时间戳是20180918173132,丢弃1731 ...
- TCP/IP(三)数据链路层~2
一.局域网 1.1.局域网和以太网的区别和联系 局域网:前面已经介绍了,其实就是学校里面.各个大的公司里,自己组件的一个小型网络,这种就属于局域网. 以太网:以太网(Ethernet)指的是由Xero ...
- GPON和820.1p学习及资料(zt)
1)hw的两个PPT不错,GPON技术基础.ppt和10G-GPON技术基础.ppt, 介绍了GPON的知识背景,标准的名称,帧协议. 尤其是详细对比了10G-PON和G-PON的区别,以及演进的道路 ...
随机推荐
- QT学习之多线程
[为什么要用多线程?] 传统的图形用户界面应用程序都只有一个执行线程,并且一次只执行一个操作.如果用户从用户界面中调用一个比较耗时的操作,当该操作正在执行时,用户界面通常会冻结而不再响应.这个问题可以 ...
- (转) c/c++调用libcurl库发送http请求的两种基本用法
libcurl主要提供了两种发送http请求的方式,分别是Easy interface方式和multi interface方式,前者是采用阻塞的方式发送单条数据,后者采用组合的方式可以一次性发送多条数 ...
- Unable to locate JAR/zip in file system as specified by the driver definition: ojdbc14.jar
eclipse的配置错误,把当前包删除,重新导入一个包.然后设置与需要的数据库对应,就可以了
- java中super的用法
在Java中,super关键字有2个用法,一个是访问父类的函数,一个是访问父类的变量,总体来说,就是一个功能,访问父类的成员. 代码如下: class Person { String name ; i ...
- CodeForces 690C2 Brain Network (medium)(树上DP)
题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离. 析:就是一个树上DP,用两次BFS或都一次DFS就可以搞定.但两次的时间是一样的. 代码如下: #include<bits/std ...
- oracle中的表空间(tablespace)、方案(schema)、段(segment)、区(extent)、块(block)
数据文件和日志文件是数据库中最重要的文件.它们是数据存储的地方.每个数据库至少有一个与之相关的数据文件,通常情况下不只一个,有很多.数据在数据文件中是如何组织的?要了解这些内容我们首先必须理解什么是表 ...
- 【小梅哥SOPC学习笔记】Altera SOPC嵌入式系统设计教程
Altera SOPC嵌入式系统设计教程 第1章 概述 SOPC(System On Programmable Chip,可编程的片上系统)是Altera公司提出来的一种灵活.高效的SOC解决方案.它 ...
- fiddler-实现https抓包
1. fiddler设置-fiddler options-https项进行设置,如下: 2. ie代理设置:连接-局域网设置 3. 下载fiddler根证书,ie浏览器上打开地址:http://1 ...
- BT Tracker的原理及.Net Core简单实现Tracker Server
最近很忙,自上次Blog被盗 帖子全部丢失后也很少时间更新Blog了,闲暇在国外站点查阅资料时正好看到一些Tracker 的协议资料,也就今天记录并实践了下,再次分享给大家希望可以帮到需要的小伙伴. ...
- leetcode 23. 合并K个排序链表 JAVA
题目: 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [ 1->4->5, 1->3->4, 2->6 ] 输出: ...