寒假作业最后成品

main.cpp

#include <iostream>
#include <fstream>
#include "x.h"
#define GU 10001
using namespace std;

int main() {
	char name1[50];
	char name2[50];
	ifstream file;
	ifstream fin; // 先不打开文件, 等待输入
	scanf("%s ",name1);
	cin>>name2;
	file.open(name1,ios::in);
	fin.open(name2,ios::in); // fstream的open函数用来打开一个文件
	if(!file||!fin)
	{
		cout<<"打开错误"<<endl;
		return 0;
	} 
	
	long long int num1[GU], num2[GU], num3[GU], num4[GU], num5[GU];//rule的分点十进制原ip
	long long int num11[GU], num21[GU], num31[GU], num41[GU], num51[GU];//rule的分点十进制终ip
	int duan1[GU], duan2[GU], duan3[GU], duan4[GU];//端点
	char ds1[GU], ds2[GU], ds3[GU], ds4[GU];//十六进制
	int dp1[GU], dp2[GU], dp3[GU], dp4[GU];
	long long int ip11[GU], ip21[GU], ip1[GU], ip2[GU]; //十进制ip
	int sum1[GU], sum2[GU];//0x转化为十进制

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


	ofstream fout;
	fout.open("res.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()) {
		ans1=-1;
		fin >> ans1 >> ans2 >> ans3 >> ans4 >> ans5;
		if(ans1!=-1) {
			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";
		}
	}
	system("pause"); 
	return 0;
}

x.h

#ifndef __X_H__
#define __X_H__
	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);
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;
}
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;
		}
} 
#endif

check.cpp

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

int main() {
	int a, b;
	int flag = 0;
	int i=1;
	char name1[50];
	char name2[50];
	ifstream file;
	ifstream fin; // 先不打开文件, 等待输入
	scanf("%s ",name1);
	cin>>name2;
	file.open(name1,ios::in);
	fin.open(name2,ios::in); // fstream的open函数用来打开一个文件
	if(!file||!fin)
	cout<<"打开错误"<<endl;
	while ( ! fin.eof() ) {
		file >> a;
		fin >> b;
		if (a != b) {
			flag++;
			cout << i << "\n";
		}
		if(flag>10)
		break;
		i++;
	}
	cout << flag ;
	system("pause");
	return 0;

}
posted @ 2022-02-16 13:35  帝芬尼  阅读(29)  评论(0编辑  收藏  举报