您的位置:首页 > 其它

九度oj 题目1475:IP数据包解析

2015-08-28 14:32 417 查看
链接
http://ac.jobdu.com/problem.php?pid=1475
千万要注意使用gets,getline时多出来的回车换行的读取。

在这题里,将'\n'使用scanf读到一个char里会wrong asnwer,必须使用get读到一个char[]里!

还有一点就是,要判断T的文件输入流是否结束,否则也会wrong answer!

上两点应是oj题通用的结论!以后尽量这样用。减少排错时间。

ac代码

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <algorithm>

#include <math.h>

#include <queue>

#include <stack>

#include <map>

#define INF 2<<20

#define MAX_INDEX 105

using namespace std;

int stoi(char *s,int n);

int getport(char *s,int n);

int getaddress(char *s,int n);

int main()

{

    int T;

    while(scanf("%d",&T)!=EOF)

    {

        char tp;

        char s[1000];

        gets(s);

        //scanf("%c",&tp);

        int flag = 1;

        while(T--)

        {

            gets(s);

            int IHL = stoi(s,1)*4*3;

            int Totallength  = stoi(s,2*3)*16*16*16+stoi(s,2*3+1)*16*16+stoi(s,3*3)*16+stoi(s,3*3+1);

            int add1,add2,add3,add4;

            printf("Case #%d\nTotal length = %d bytes\n",flag++,Totallength);

            add1 = getaddress(s,12*3);

            add2 = getaddress(s,13*3);

            add3 = getaddress(s,14*3);

            add4 = getaddress(s,15*3);

            printf("Source = %d.%d.%d.%d\n",add1,add2,add3,add4);

            add1 = getaddress(s,16*3);

            add2 = getaddress(s,17*3);

            add3 = getaddress(s,18*3);

            add4 = getaddress(s,19*3);

            printf("Destination = %d.%d.%d.%d\n",add1,add2,add3,add4);

            add1 = getport(s,IHL);

            add2 = getport(s,IHL+6);

            printf("Source Port = %d\n",add1);

            printf("Destination Port = %d\n\n",add2);

        }

    }

    return 0;

}

int stoi(char *s,int n)

{

    if((*(s+n))>='0'&&(*(s+n))<='9')

        return *(s+n)-'0';

    else

        return *(s+n)-'a'+10;

}

int getport(char *s,int n)

{

    return stoi(s,n)*16*16*16 + stoi(s,n+1)*16*16 + stoi(s,n+3)*16 + stoi(s,n+4);

}

int getaddress(char *s,int n)

{

    return stoi(s,n)*16 + stoi(s,n+1);

}

wa代码

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <algorithm>

#include <math.h>

#include <queue>

#include <stack>

#include <map>

#define INF 2<<20

#define MAX_INDEX 105

using namespace std;

int stoi(char *s,int n);

int getport(char *s,int n);

int getaddress(char *s,int n);

int main()

{

    int T;

    while(scanf("%d",&T)!=EOF)

    {

        char tp;

        char s[1000];

        //gets(s);

        scanf("%c",&tp);

        int flag = 1;

        while(T--)

        {

            gets(s);

            int IHL = stoi(s,1)*4*3;

            int Totallength  = stoi(s,2*3)*16*16*16+stoi(s,2*3+1)*16*16+stoi(s,3*3)*16+stoi(s,3*3+1);

            int add1,add2,add3,add4;

            printf("Case #%d\nTotal length = %d bytes\n",flag++,Totallength);

            add1 = getaddress(s,12*3);

            add2 = getaddress(s,13*3);

            add3 = getaddress(s,14*3);

            add4 = getaddress(s,15*3);

            printf("Source = %d.%d.%d.%d\n",add1,add2,add3,add4);

            add1 = getaddress(s,16*3);

            add2 = getaddress(s,17*3);

            add3 = getaddress(s,18*3);

            add4 = getaddress(s,19*3);

            printf("Destination = %d.%d.%d.%d\n",add1,add2,add3,add4);

            add1 = getport(s,IHL);

            add2 = getport(s,IHL+6);

            printf("Source Port = %d\n",add1);

            printf("Destination Port = %d\n\n",add2);

        }

    }

    return 0;

}

int stoi(char *s,int n)

{

    if((*(s+n))>='0'&&(*(s+n))<='9')

        return *(s+n)-'0';

    else

        return *(s+n)-'a'+10;

}

int getport(char *s,int n)

{

    return stoi(s,n)*16*16*16 + stoi(s,n+1)*16*16 + stoi(s,n+3)*16 + stoi(s,n+4);

}

int getaddress(char *s,int n)

{

    return stoi(s,n)*16 + stoi(s,n+1);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: