要交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++的更多相关文章

  1. Python3 透明网桥算法

    import time #定义网桥1 b1 = {} port_list1 = [1, 2] #主机列表 L1 = ['a','b','c'] L2 = ['d','e'] L = [L1,L2] d ...

  2. 【最大团转最大点独立集(匈牙利算法+时间戳优化)】BZOJ2744-[HEOI2012]朋友圈

    [题目大意] 有两个国家A和B.存在以下朋友关系: 1.A国:每个人都有一个友善值,当两个A国人的友善值a.b,如果a xor b mod 2=1,那么这两个人都是朋友,否则不是: 2.B国:每个人都 ...

  3. 关于 centos 7系统,iptables透明网桥实现【转载请注明】

    首先建立网桥:(使用bridge)    示例 桥接eth0 与 eth1 网口 /sbin/modprobe bridge /usr/sbin/brctl addbr br0 /sbin/ifup ...

  4. 关于 centos 7系统,iptables透明网桥实现

    首先建立网桥:(使用bridge)    示例 桥接eth0 与 eth1 网口 /sbin/modprobe bridge /usr/sbin/brctl addbr br0 /sbin/ifup ...

  5. 计算机网络 Computer Networks​ 期末复习总提纲

    平时不学习,期末火葬场. 一周时间靠王道考研和各路 pdf 自学计网,留下的提纲都在这里了.全是干货.全文 pdf 可以在这里下载:http://cloud.billc.io/s/xNHarppQPG ...

  6. nat模式、路由模式,网桥模式

    路由器的几种连接方式 NAT英文全称是“Network Address Translation”,中文意思是“网络地址转换”,它是一个IETF(Internet Engineering Task Fo ...

  7. 如何计算CDS view里两个时间戳之间的天数间隔

    ABAP透明表里的时间戳,数据类型为dec: 有个需求:计算这两个时间戳之间的天数间隔,丢弃时间戳年-月-日8位后面的小时:分钟:秒. 举个例子:如果时间戳是20180918173132,丢弃1731 ...

  8. TCP/IP(三)数据链路层~2

    一.局域网 1.1.局域网和以太网的区别和联系 局域网:前面已经介绍了,其实就是学校里面.各个大的公司里,自己组件的一个小型网络,这种就属于局域网. 以太网:以太网(Ethernet)指的是由Xero ...

  9. GPON和820.1p学习及资料(zt)

    1)hw的两个PPT不错,GPON技术基础.ppt和10G-GPON技术基础.ppt, 介绍了GPON的知识背景,标准的名称,帧协议. 尤其是详细对比了10G-PON和G-PON的区别,以及演进的道路 ...

随机推荐

  1. YII2 自动 created_at created_by updated_by updated_at

    use yii\behaviors\TimestampBehavior; use yii\behaviors\BlameableBehavior; use yii\db\Expression; /** ...

  2. Spring框架总结(一)

    名词解释: 框架就是组件的集合.比如:Struts.Spring.Hibernate就是组件的集合 组件就是常用的功能包封装成工具类. 常用组件: Dom4j/Xpath.DBUtils.C3p0.B ...

  3. C++学习--入口函数

    在学习第一个C++程序的时候发现控制台程序的入口函数是int _tmain而不是main,查了资料才发现_tmain()是为了支持unicode所使用的main一个别名,宏定义在<stdafx. ...

  4. sql五大类中的 DTL 数据事务语言

    DTL,数据事务语言 事务的定义:就是指一组相关的SQL操作,我们所有的操作都是事务中的. 注意:在数据库中,执行业务的基本单位是[事务],不是以某一条SQL.    数据库在默认情况下,事务是都打开 ...

  5. 【微服务架构】SpringCloud之Ribbon(四)

    一:Ribbon是什么? Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起.Ribbon客户端组件提供一系列完善的配置项如连接 ...

  6. <a>标签的用法以及[@text_cut]

    <a href="${a.url}" target="_blank">[@text_cut s=a.title len=titLen append= ...

  7. bootstrap css排版

    smart-form 单行元素: 一般用div包含,class="row" 列元素:用section包含,class="col col-x"(section带有 ...

  8. [Erlang03]Erlang有哪些好用的静态分析工具?

    1. dialyzer Dialyzer starts its analysis from either debug-compiled BEAM bytecode or from Erlang sou ...

  9. Centos部署Abp zero常见问题及处理

    多租户切换,多语言切换异常 解决: 修改nginx配置,在nginx.conf中 增加 #多租户问题 ignore_invalid_headers off; 修改应用程序Logo异常处理 异常: Sy ...

  10. Entity Framework 6 暂停重试执行策略

    EF6引入一个弹性连接的功能,也就是允许重新尝试执行失败的数据库操作.某些复杂的场景中,可能需要启用或停用重试执行的策略,但是EF框架暂时尚未提供直接的设置开关,将来可能会加入这种配置.幸运的是,很容 ...