第三次寒假作业成品二号

main.cpp

#include <iostream>
#include <fstream>
using namespace std;

int main() {
	long long int num1[10001], num2[10001], num3[10001], num4[10001], num5[10001];//rule的分点十进制原ip
	long long int num11[10001], num21[10001], num31[10001], num41[10001], num51[10001];//rule的分点十进制终ip
	int duan1[10001], duan2[10001], duan3[10001], duan4[10001];//端点
	char ds1[10001], ds2[10001], ds3[10001], ds4[10001];//十六进制
	int dp1[10001], dp2[10001], dp3[10001], dp4[10001];
	long long int ip11[10001], ip21[10001], ip1[10001], ip2[10001]; //十进制ip
	int sum1[10001], sum2[10001];//0x转化为十进制

	char s;//除去字符
	int s1;//除去字符
	int len = 0;
	long long int x;

	long long int IP(long long a, long long b, long long c, long long d, long long e, int len, int flag);
	int duan(int ds);
	
	ifstream fin("packet1.txt");
	ifstream file("rule.txt");
	ofstream fout;
	fout.open("output.txt");
	
	while (!file.eof()) {
		file >> s >> num1[len] >> s >> num2[len] >> s >> num3[len] >> s >> num4[len] >> s >> num5[len];
		file >> num11[len] >> s >> num21[len] >> s >> num31[len] >> s >> num41[len] >> s >> num51[len];
		file >> duan1[len] >> s >> duan2[len] >> duan3[len] >> s >> duan4[len];
		file >> s1 >> s >> ds1[len] >> ds2[len] >> s >> s1 >> s >> ds3[len] >> ds4[len];
		/*输入数据对rule进行处理*/


		ip1[len] = IP(num1[len], num2[len], num3[len], num4[len], num5[len], len, 1);
		ip11[len] = IP(num1[len], num2[len], num3[len], num4[len], num5[len], len, 0);
		ip2[len] = IP(num11[len], num21[len], num31[len], num41[len], num51[len], len, 1);
		ip21[len] = IP(num11[len], num21[len], num31[len], num41[len], num51[len], len, 0);
		
		if(ip1[len]>ip11[len])
		{
			x=ip1[len];
			ip1[len]=ip11[len];
			ip11[len]=x;
		} 
		
		if(ip2[len]>ip21[len])
		{
			x=ip2[len];
			ip2[len]=ip21[len];
			ip21[len]=x;
		}
		
		dp1[len] = duan(ds1[len]);
		dp2[len] = duan(ds2[len]);
		dp3[len] = duan(ds3[len]);
		dp4[len] = duan(ds4[len]);

		sum1[len] = 16 * dp1[len] + dp2[len];
		sum2[len] = 16 * dp3[len] + dp4[len];
		/*十六进制处理*/
		len++;
	}
	
	long long int ans1, ans2, ans3, ans4, ans5;
	int flag = 0;
	len--;
	flag = 0;

	while (!fin.eof()) {
		fin >> ans1 >> ans2 >> ans3 >> ans4 >> ans5;
		for (int j = 0; j < len; j++) {
			if (ans3 >= duan1[j] && ans3 <= duan2[j] && ans4 >= duan3[j]
				&& ans4 <= duan4[j]) {
				if ((sum2[j] != 255) || (sum1[j] == ans5 && sum2[j] == 255)) {
					if (ans1 >= ip1[j] && ans2 >= ip2[j]&&ans1 <= ip11[j] && ans2 <= ip21[j]) {
						flag = 1;
						fout << j << "\n";
						break;
					}
				}
			}
		}
		if (flag == 0)
			fout << -1 << "\n";
	}
	return 0;
}

IP.cpp

long long int IP(long long a,long long b,long long c,long long d,long long e,int len,int flag)
{
	int zw1[50], zw2[50], zw3[50], zw4[50];
	for (int i = 7; i >= 0; i--) {
			zw1[i] = a % 2;
			a /= 2;
		}
		for (int i = 15; i >= 8; i--) {
			zw1[i] = b % 2;
			b /= 2;
		}
		for (int i = 23; i >= 16; i--) {
			zw1[i] = c % 2;
			c /= 2;
		}
		for (int i = 31; i >= 24; i--) {
			zw1[i] = d % 2;
			d /= 2;
		}
		//网址转化
		for (int i = 0; i < e; i++) {
			zw2[i] = 1;
		}
		for (int i = 0; i <= 31; i++) {
			zw3[i] = zw1[i] & zw2[i];
			zw4[i] = zw3[i];
		}
		for (int i = e; i < 32; i++) {
			zw4[i] = 1;
		}

		//重新赋值
		int ss = 0, sss = 128;
		for (int i = ss; i <= ss + 7; i++) {
			a += zw3[i] * sss;
			sss /= 2;
		}
		ss += 8;
		sss = 128;
		for (int i = ss; i <= ss + 7; i++) {
			b += zw3[i] * sss;
			sss /= 2;
		}
		ss += 8;
		sss = 128;
		for (int i = ss; i <= ss + 7; i++) {
			c += zw3[i] * sss;
			sss /= 2;
		}
		ss += 8;
		sss = 128;
		for (int i = ss; i <= ss + 7; i++) {
			d += zw3[i] * sss;
			sss /= 2;
		}
		
		if(flag==1)
		return  16777216 * a + 65536 * b + 256 * c + d;
		else
		{
			a = 0;
			b = 0;
			c = 0;
			d = 0;
			ss = 0;
			sss = 128;
		for (int i = ss; i <= ss + 7; i++) {
			a += zw4[i] * sss;
			sss /= 2;
		}
		ss += 8;
		sss = 128;
		for (int i = ss; i <= ss + 7; i++) {
			b += zw4[i] * sss;
			sss /= 2;
		}
		ss += 8;
		sss = 128;
		for (int i = ss; i <= ss + 7; i++) {
			c += zw4[i] * sss;
			sss /= 2;
		}
		ss += 8;
		sss = 128;
		for (int i = ss; i <= ss + 7; i++) {
			d += zw4[i] * sss;
			sss /= 2;
		}
		return 16777216 * a + 65536 * b + 256 * c + d;
		}
} 

daun.cpp

int duan(int ds){
	int dp;
	switch (ds) {
			case 'A':
				dp = ds - 55;
				break;
			case 'B':
				dp = ds - 55;
				break;
			case 'C':
				dp = ds - 55;
				break;
			case 'D':
				dp = ds - 55;
				break;
			case 'E':
				dp = ds - 55;
				break;
			case 'F':
				dp = ds - 55;
				break;
			default:
				dp = ds - 48;
		}
	return dp;
}

check.cpp

#include <iostream>
#include <fstream>
using namespace std;

int main() {
	int a, b;
	int flag = 0;
	int i=1;
	ifstream file("ans1.txt");
	ifstream fil("output.txt");
	while ( ! fil.eof() ) {
		file >> a;
		fil >> b;
		if (a != b) {
			flag++;
			cout << i << "\n";
		}
		if(flag>10)
		break;
		i++;
	}
	
	cout << flag ;
	return 0;
}
posted @ 2022-02-09 23:31  帝芬尼  阅读(27)  评论(0编辑  收藏  举报