要交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. MySQL的四种外键

    来自:某一位网友的博客    转自:http://blog.csdn.net/cnjsnt_s/article/details/5548280 具体使用时需要参考:http://blog.csdn.n ...

  2. 51Nod 1554 欧姆诺姆和项链 (KMP)

    题意:中文题. 析:首先要使用KMP的失配函数 f ,对于长度为 i 的串,如果存在循环节那么  i % (i-f[i]) == 0,循环节的长度就是 i - f[i] ,当然次数就是 i / (i- ...

  3. Struts2的多方法动态Action

    原创 一个简单的Action里面的核心方法只有一个execute(); 定义多个核心方法的Action,有如下3种方式: 为Action配置method属性 动态方法调用 使用通配符映射方式 动态方法 ...

  4. nginx中级应用-续

    1.server下配置的每个location,都需要有自己的一套代理配置 即要么加入: root  某个目录 要么加入 proxy_pass 某个地址;  proxy_redirect off; # ...

  5. ASP.NET Core2利用Jwt技术在服务端实现对客户端的身份认证

    背景 在微服务架构下,一般都会按不同的业务或功能将整个系统切分成不同的独立子系统,再通过REST API或RPC进行通讯并相互调用,形成各个子系统之间的串联结构.在这里,我们将采用REST API的通 ...

  6. [Postgres]Postgres单用户模式执行VACUUM

    # su - postgres $ /usr/local/pgsql/bin/postgres --single -D /usr/local/pgsql/data [不能VACUUM的数据库] Pos ...

  7. [LeetCode题解]: Sort Colors

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an a ...

  8. RabbitMQ与.net core(三) fanout类型Exchange 与 消息的过期时间 与 队列的存活时间

    上一篇我们讲了关于direct类型的Exchange,这一片我们来了解一下fanout类型的Exchange. 1.Exchange的fanout类型 fanout类型的Exchange的特点是会把消 ...

  9. ASP.NETMVC4 分页组合查询解决方法

    本人新手刚在webform转到mvc   像linq  ef啥的,都是不会的不行不行的,不会就问群友,找资料 今天本屌遇到了一个分页组合查询的问题,解决了2个小时,把代码共享给大家 话不多话,直接上代 ...

  10. JavaScript类型检测汇总

        曾经我以为JavaScript中的类型检测只要使用 typeof 或 instanceof 就可以通通解决.后来我发现我是too young too naive啊!早说过JavaScript是 ...