北邮新OJ98

http://code.bupt.edu.cn/problem/p/98/

这题其实很蛋疼,在九度OJ一直过不了,但是在北邮OJ相同的代码直接AC

主要就是考虑数据包长度不是固定的即可

当时被九度的判题整疯了,代码也写的很长很繁琐

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
 
void P(char str[],int n)
{
    //int len=strlen(str);
    cout<<"Case #"<<n<<endl;
     
    //求数据包长度
    int temp1,temp2,temp3,temp4;
    if('0'<=str[6]&&str[6]<='9')
        temp1=str[6]-'0';
    else if('a'<=str[6]&&str[6]<='z')
        temp1=str[6]-'a'+10;
    else
        temp1=str[6]-'A'+10;
     
    if('0'<=str[7]&&str[7]<='9')
        temp2=str[7]-'0';
    else if('a'<=str[7]&&str[7]<='z')
        temp2=str[7]-'a'+10;
    else
        temp2=str[7]-'A'+10;
         
    if('0'<=str[9]&&str[9]<='9')
        temp3=str[9]-'0';
    else if('a'<=str[9]&&str[9]<='z')
        temp3=str[9]-'a'+10;
    else
        temp3=str[9]-'A'+10;
         
    if('0'<=str[10]&&str[10]<='9')
        temp4=str[10]-'0';
    else if('a'<=str[10]&&str[10]<='z')
        temp4=str[10]-'a'+10;
    else
        temp4=str[10]-'A'+10;
         
    int T_len=temp1*4096+temp2*256+temp3*16+temp4;
    cout<<"Total length = "<<T_len<<" bytes"<<endl;
     
    //求源IP
    int a,b;
    if('0'<=str[36]&&str[36]<='9')
        a=str[36]-'0';
    else if('a'<=str[36]&&str[36]<='z')
        a=str[36]-'a'+10;
    else
        a=str[36]-'A'+10;
         
    if('0'<=str[37]&&str[37]<='9')
        b=str[37]-'0';
    else if('a'<=str[37]&&str[37]<='z')
        b=str[37]-'a'+10;
    else
        b=str[37]-'A'+10;  
    temp1=a*16+b;
     
    if('0'<=str[39]&&str[39]<='9')
        a=str[39]-'0';
    else if('a'<=str[39]&&str[39]<='z')
        a=str[39]-'a'+10;
    else
        a=str[39]-'A'+10;
         
    if('0'<=str[40]&&str[40]<='9')
        b=str[40]-'0';
    else if('a'<=str[40]&&str[40]<='z')
        b=str[40]-'a'+10;
    else
        b=str[40]-'A'+10;
    temp2=a*16+b;
     
    if('0'<=str[42]&&str[42]<='9')
        a=str[42]-'0';
    else if('a'<=str[42]&&str[42]<='z')
        a=str[42]-'a'+10;
    else
        a=str[42]-'A'+10;
         
    if('0'<=str[43]&&str[43]<='9')
        b=str[43]-'0';
    else if('a'<=str[43]&&str[43]<='z')
        b=str[43]-'a'+10;
    else
        b=str[43]-'A'+10;
    temp3=a*16+b;
     
    if('0'<=str[45]&&str[45]<='9')
        a=str[45]-'0';
    else if('a'<=str[45]&&str[45]<='z')
        a=str[45]-'a'+10;
    else
        a=str[45]-'A'+10;
         
    if('0'<=str[46]&&str[46]<='9')
        b=str[46]-'0';
    else if('a'<=str[46]&&str[46]<='z')
        b=str[46]-'a'+10;
    else
        b=str[46]-'A'+10;
    temp4=a*16+b;
    cout<<"Source = "<<temp1<<"."<<temp2<<"."<<temp3<<"."<<temp4<<endl;
     
    //目的IP
    if('0'<=str[48]&&str[48]<='9')
        a=str[48]-'0';
    else if('a'<=str[48]&&str[48]<='z')
        a=str[48]-'a'+10;
    else
        a=str[48]-'A'+10;
    if('0'<=str[49]&&str[49]<='9')
        b=str[49]-'0';
    else if('a'<=str[49]&&str[49]<='z')
        b=str[49]-'a'+10;
    else
        b=str[49]-'A'+10;
    temp1=a*16+b;
     
    if('0'<=str[51]&&str[51]<='9')
        a=str[51]-'0';
    else if('a'<=str[51]&&str[51]<='z')
        a=str[51]-'a'+10;
    else
        a=str[51]-'A'+10;
    if('0'<=str[52]&&str[52]<='9')
        b=str[52]-'0';
    else if('a'<=str[52]&&str[52]<='z')
        b=str[52]-'a'+10;
    else
        b=str[52]-'A'+10;
    temp2=a*16+b;
     
    if('0'<=str[54]&&str[54]<='9')
        a=str[54]-'0';
    else if('a'<=str[54]&&str[54]<='z')
        a=str[54]-'a'+10;
    else
        a=str[54]-'A'+10;
    if('0'<=str[55]&&str[55]<='9')
        b=str[55]-'0';
    else if('a'<=str[55]&&str[55]<='z')
        b=str[55]-'a'+10;
    else
        b=str[55]-'A'+10;
    temp3=a*16+b;
     
    if('0'<=str[57]&&str[57]<='9')
        a=str[57]-'0';
    else if('a'<=str[57]&&str[57]<='z')
        a=str[57]-'a'+10;
    else
        a=str[57]-'A'+10;
    if('0'<=str[58]&&str[58]<='9')
        b=str[58]-'0';
    else if('a'<=str[58]&&str[58]<='z')
        b=str[58]-'a'+10;
    else
        b=str[58]-'A'+10;
    temp4=a*16+b;
    cout<<"Destination = "<<temp1<<"."<<temp2<<"."<<temp3<<"."<<temp4<<endl;
     
    //IP头的长度
    int len;
    if('0'<=str[1]&&str[1]<='9')
        len=str[1]-'0';
    else if('a'<=str[1]&&str[1]<='z')
        len=str[1]-'a'+10;
    else
        len=str[1]-'A'+10;
    len=len*4*2;//以一个符号为单位
    int st=len/2*3;//st是TCP的起始地址
         
    //源端口
    int s;
    if('0'<=str[st]&&str[st]<='9')
        temp1=str[st]-'0';
    else if('a'<=str[st]&&str[st]<='z')
        temp1=str[st]-'a'+10;
    else
        temp1=str[st]-'A'+10;
    if('0'<=str[st+1]&&str[st+1]<='9')
        temp2=str[st+1]-'0';
    else if('a'<=str[st+1]&&str[st+1]<='z')
        temp2=str[st+1]-'a'+10;
    else
        temp2=str[st+1]-'A'+10;
    if('0'<=str[st+3]&&str[st+3]<='9')
        temp3=str[st+3]-'0';
    else if('a'<=str[st+3]&&str[st+3]<='z')
        temp3=str[st+3]-'a'+10;
    else
        temp3=str[st+3]-'A'+10;
    if('0'<=str[st+4]&&str[st+4]<='9')
        temp4=str[st+4]-'0';
    else if('a'<=str[st+4]&&str[st+4]<='z')
        temp4=str[st+4]-'a'+10;
    else
        temp4=str[st+4]-'A'+10;
    s=temp1*4096+temp2*256+temp3*16+temp4;
    cout<<"Source Port = "<<s<<endl;
     
    //目的端口
    st=st+6;
    int d;
    if('0'<=str[st]&&str[st]<='9')
        temp1=str[st]-'0';
    else if('a'<=str[st]&&str[st]<='z')
        temp1=str[st]-'a'+10;
    else
        temp1=str[st]-'A'+10;
    if('0'<=str[st+1]&&str[st+1]<='9')
        temp2=str[st+1]-'0';
    else if('a'<=str[st+1]&&str[st+1]<='z')
        temp2=str[st+1]-'a'+10;
    else
        temp2=str[st+1]-'A'+10;
    if('0'<=str[st+3]&&str[st+3]<='9')
        temp3=str[st+3]-'0';
    else if('a'<=str[st+3]&&str[st+3]<='z')
        temp3=str[st+3]-'a'+10;
    else
        temp3=str[st+3]-'A'+10;
    if('0'<=str[st+4]&&str[st+4]<='9')
        temp4=str[st+4]-'0';
    else if('a'<=str[st+4]&&str[st+4]<='z')
        temp4=str[st+4]-'a'+10;
    else
        temp4=str[st+4]-'A'+10;
    d=temp1*4096+temp2*256+temp3*16+temp4;
    cout<<"Destination Port = "<<d<<endl;  
}
 
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        getchar();
        for(int i=1;i<=n;i++)
        {
            char str[1001];
            gets(str);
            P(str,i);
            cout<<endl;
        }
        //cout<<endl;
    }
     
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值