计算机网络透明网桥算法时间戳c++

要交CG的兄弟们别抄啊,撞上了严nan谁都不会放过的

 

 

好久没写博客了,这次是老师布置的作业,做出来一种,觉得写得很不好,第一种方法把情况都写死在代码里了。

上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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;
}

  

 

第二种就较友好点,我把网桥的数量可以自定义,我认为这样才是真正的实现了学习的需求

上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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;
    }  
    }
}

  

  

 

 

 

 

 

 

接下来做的是要加上时间戳,我对时间函数的调用不是很熟悉,就只是实现了这个功能,没有追求精确无误,功能没有问题,但是不是严格得按照打印输出一样显示超过五秒就删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#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();
    }<br>
}

  

 

 

posted on   妄想症T  阅读(353)  评论(0编辑  收藏  举报

编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示