北郵機試 | bupt oj | 98. IP數據包解析

心得

我真就喜歡無腦分割字符串。。。
利用while(1)死循環,對char類型的兩種情況的處理。
另外,有個易錯點:對於char類型,在格式轉換時,是不好直接和c<'10’比較的,頂多比到9,這我都能犯錯。。裂開

題目

IP數據包解析
時間限制 1000 ms 內存限制 65536 KB

題目描述
我們都學習過計算機網絡,知道網絡層IP協議數據包的頭部格式如下:
在這裏插入圖片描述

其中IHL表示IP頭的長度,單位是4字節;總長表示整個數據包的長度,單位是1字節。

傳輸層的TCP協議數據段的頭部格式如下:

在這裏插入圖片描述

頭部長度單位爲4字節。

你的任務是,簡要分析輸入數據中的若干個TCP數據段的頭部。 詳細要求請見輸入輸出部分的說明。

輸入格式
第一行爲一個整數T,代表測試數據的組數。

以下有T行,每行都是一個TCP數據包的頭部分,字節用16進製表示,以空格隔開。數據保證字節之間僅有一個空格,且行首行尾沒有多餘的空白字符。

保證輸入數據都是合法的。

輸出格式
對於每個TCP數據包,輸出如下信息:

Case #x,x是當前測試數據的序號,從1開始。

Total length = L bytes,L是整個IP數據包的長度,單位是1字節。

Source = xxx.xxx.xxx.xxx,用點分十進制輸出源IP地址。輸入數據中不存在IPV6數據分組。

Destination = xxx.xxx.xxx.xxx,用點分十進制輸出源IP地址。輸入數據中不存在IPV6數據分組。

Source Port = sp,sp是源端口號。

Destination Port = dp,dp是目標端口號。

對於每個TCP數據包,最後輸出一個多餘的空白行。

具體格式參見樣例。

請注意,輸出的信息中,所有的空格、大小寫、點符號、換行均要與樣例格式保持一致,並且不要在任何數字前輸出多餘的前導0,也不要輸出任何不必要的空白字符。

輸入樣例
2
45 00 00 34 7a 67 40 00 40 06 63 5a 0a cd 0a f4 7d 38 ca 09 cd f6 00 50 b4 d7 ae 1c 9b cf f2 40 80 10 ff 3d fd d0 00 00 01 01 08 0a 32 53 7d fb 5e 49 4e c8
45 00 00 c6 56 5a 40 00 34 06 e0 45 cb d0 2e 01 0a cd 0a f4 00 50 ce 61 e1 e9 b9 ee 47 c7 37 34 80 18 00 b5 81 8f 00 00 01 01 08 0a 88 24 fa c6 32 63 cd 8d
以第一行爲例(爲16進制數)一位16進制數需要4位2進制數表示,TCP報文一行32位

ip首部

45 00 00 34 //0034爲總長度 52

7a 67 40 00

40 06 63 5a

0a cd 0a f4 //源地址 10.205.10.244

7d 38 ca 09 //目的地址 125.56.202.9

tcp首部

cd f6 00 50 //源端口cdf6即52726 目的端口0050即80

b4 d7 ae 1c

9b cf f2 40

80 10 ff 3d

fd d0 00 00

01 01 08 0a

32 53 7d fb

5e 49 4e c8

輸出樣例

Case #1
Total length = 52 bytes
Source = 10.205.10.244
Destination = 125.56.202.9
Source Port = 52726
Destination Port = 80
 
Case #2
Total length = 198 bytes
Source = 203.208.46.1
Destination = 10.205.10.244
Source Port = 80
Destination Port = 52833

————————————————
題目解析原文鏈接:https://blog.csdn.net/stone_fall/article/details/88583161

AC代碼

#include <bits/stdc++.h>

using namespace std;

int change(char a)
{
	if(a-'0'>=0&&a-'0'<10) return a-'0';//媽的腦殘!告訴你了char別寫10!!只能寫到 9 啊,f******k 
	else return a-'a'+10;
}

int turn(string str)
{
	int answer=0;
	for(int i=0;i<str.size();i++)
	{
		answer=answer*16;
		answer+=change(str[i]);
	}
	return answer;
}

int main()
{
	int T;
	scanf("%d",&T);
	int casenumber=0;
	getchar();
	while(T--)
	{
		vector<string>myvector;
		string str="";
		while(1)
		{
			char c;
			scanf("%c",&c);
			if(c==' ')
			{
				myvector.push_back(str);
				str="";
			}
			else if(c=='\n')
			{
				break;
			}
			else 
			{
				str=str+c;
			}
		}
		myvector.push_back(str);
		printf("Case #%d\n",++casenumber);
		string temp=myvector[2]+myvector[3];
		int answer=turn(temp);
		printf("Total length = %d bytes\n",answer);
		printf("Source = %d.%d.%d.%d\n",turn(myvector[12]),turn(myvector[13]),turn(myvector[14]),turn(myvector[15]));
		printf("Destination = %d.%d.%d.%d\n",turn(myvector[16]),turn(myvector[17]),turn(myvector[18]),turn(myvector[19]));
		string temp2=myvector[20]+myvector[21];
		printf("Source Port = %d\n",turn(temp2));
		string temp3=myvector[22]+myvector[23];
		printf("Destination Port = %d\n",turn(temp3));
		printf("\n"); 
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章