online_judge_1475

这道题目我测试了好多次都没问题,但是就是无法AC,十分愤怒。我编程习惯十分差,谁要是发现Bug。求指点!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct SString
{
    char e[200];
}SString;
typedef struct IP
{
    int a;
    int b;
    int c;
    int d;
}IP;
typedef struct S
{
    int len;
    IP source;
    IP destination;
    int source_port;
    int destinstion_port;
}S;
int tran(char e)
{
    if(e>='0'&&e<='9') return e-'0';
    else if(e>='a'&&e<='z') return e-'a'+10;
    return 0;
}
int main()
{
    int n,i;
    SString *str;
    S *Node;
    int x;
    while(scanf("%d",&n)!=EOF)
    {
        getchar();
        str=(SString *)malloc(n*sizeof(SString));
        Node=(S *)malloc(n*sizeof(S));
        for(i=0;i<n;i++)
            gets(str[i].e);
        for(i=0;i<n;i++)
        {
            x=4*tran(str[i].e[1]);
            Node[i].len=16*256*tran(str[i].e[6])+256*tran(str[i].e[7])+\
            16*tran(str[i].e[9])+tran(str[i].e[10]);
            Node[i].source.a=tran(str[i].e[36])*16+tran(str[i].e[37]);
            Node[i].source.b=tran(str[i].e[39])*16+tran(str[i].e[40]);
            Node[i].source.c=tran(str[i].e[42])*16+tran(str[i].e[43]);
            Node[i].source.d=tran(str[i].e[45])*16+tran(str[i].e[46]);
            Node[i].destination.a=tran(str[i].e[48])*16+tran(str[i].e[49]);
            Node[i].destination.b=tran(str[i].e[51])*16+tran(str[i].e[52]);
            Node[i].destination.c=tran(str[i].e[54])*16+tran(str[i].e[55]);
            Node[i].destination.d=tran(str[i].e[57])*16+tran(str[i].e[58]);
            Node[i].source_port=4096*tran(str[i].e[3*x])+256*tran(str[i].e[3*x+1])+\
            16*tran(str[i].e[3*x+3])+tran(str[i].e[3*x+4]);
            Node[i].destinstion_port=4096*tran(str[i].e[3*x+6])+256*tran(str[i].e[3*x+7])+\
            16*tran(str[i].e[3*x+9])+tran(str[i].e[3*x+10]);
        }
        for(i=0;i<n;i++)
        {
            printf("Case #%d\n",i+1);
            printf("Total length = %d bytes\n",Node[i].len);
            printf("Source = %d.%d.%d.%d\n",Node[i].source.a,\
                   Node[i].source.b,Node[i].source.c,Node[i].source.d);
            printf("Destination = %d.%d.%d.%d\n",Node[i].destination.a,\
                   Node[i].destination.b,Node[i].destination.c,Node[i].destination.d);
            printf("Source Port = %d\n",Node[i].source_port);
            printf("Destination Port = %d\n\n",Node[i].destinstion_port);
        }
        free(str);
        free(Node);
    }
    return 0;
}




posted on 2015-01-27 23:26  赛亚人  阅读(95)  评论(0编辑  收藏  举报