Summary of the second winter vacation

Posted by KenGR on Wed, 26 Jan 2022 05:12:56 +0100

Summary of the second winter vacation

Main code analysis

/*ip conversion*/
		for (int i = 7; i >= 0; i--) {
			zw1[i] = num1[len] % 2;
			num1[len] /= 2;
		}
		for (int i = 15; i >= 8; i--) {
			zw1[i] = num2[len] % 2;
			num2[len] /= 2;
		}
		for (int i = 23; i >= 16; i--) {
			zw1[i] = num3[len] % 2;
			num3[len] /= 2;
		}
		for (int i = 31; i >= 24; i--) {
			zw1[i] = num4[len] % 2;
			num4[len] /= 2;
		}
		//Website conversion
		for (int i = 0; i < num5[len]; i++) {
			zw2[i] = 1;
		}
		for (int i = 0; i <= 31; i++) {
			zw3[i] = zw1[i] & zw2[i];
			zw4[i] = zw3[i];
		}
		for (int i = num5[len]; i < 32; i++) {
			zw4[i] = 1;
		}

		//Reassign
		int ss = 0, sss = 128;
		for (int i = ss; i <= ss + 7; i++) {
			num1[len] += zw3[i] * sss;
			sss /= 2;
		}
		ss += 8;
		sss = 128;
		for (int i = ss; i <= ss + 7; i++) {
			num2[len] += zw3[i] * sss;
			sss /= 2;
		}
		ss += 8;
		sss = 128;
		for (int i = ss; i <= ss + 7; i++) {
			num3[len] += zw3[i] * sss;
			sss /= 2;
		}
		ss += 8;
		sss = 128;
		for (int i = ss; i <= ss + 7; i++) {
			num4[len] += zw3[i] * sss;
			sss /= 2;
		}
		ip1[len] = 16777216 * num1[len] + 65536 * num2[len] + 256 * num3[len] + num4[len];

Here, I use to first convert the 127.0.0.1 web address into binary, process it according to the x value of / x, then conduct and operation, and finally assign value for calculation

Advantages: none, but meet the definition

Disadvantages: large amount of code

switch (ds1[len]) {
			case 'A':
				dp1[len] = ds1[len] - 55;
				break;
			case 'B':
				dp1[len] = ds1[len] - 55;
				break;
			case 'C':
				dp1[len] = ds1[len] - 55;
				break;
			case 'D':
				dp1[len] = ds1[len] - 55;
				break;
			case 'E':
				dp1[len] = ds1[len] - 55;
				break;
			case 'F':
				dp1[len] = ds1[len] - 55;
				break;
			default:
				dp1[len] = ds1[len] - 48;
		}
        

The protocol number cannot be processed. Since it is impossible (probably I won't), each bit can only be processed from one character to integer, and then converted

Finally, there is the problem of data comparison. Because the code is too long, it won't be released. The most important thing is the matching of ip addresses, / 32 is equal to, / 31 is equal to two data, and the other is to make a judgment within a range. Therefore, the second method of calculating ip network width can reduce the amount of calculation and write less in the judgment process (another 100 lines faster)

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

int main() {
	int a[10001], b[10001];
	int len = 0;
	ifstream file("ans1.txt");
	while ( ! file.eof() ) {
		file >> a[len];
		len++;
	}
	len = 0;
	ifstream fil("output.txt");
	while ( ! fil.eof() ) {
		fil >> b[len];
		len++;
	}
	int flag = 0;
	len --;
	for (int i = 0; i < len; i++) {
		if (a[i] != b[i]) {
			flag++;
			cout << i << "\n";
		}
	}
	cout << flag ;
	return 0;
}

Then, the check code can compare the results with ans. the code should be very simple. I won't introduce it much. However, it's good to see the number of errors from thousands to 173, then to 11 and finally to 0


Running time

It takes about 3 seconds for a packet, which is quite long

learning process

First understand the basic concepts, then input and output of files, ip calculation methods, etc., write code (the first version), protocol number problem (the second version), transmission protocol problem (the third version), bug of / 31 (the fourth version is also the final version)

The biggest problem that needs to be solved is that the demand is not understood

1. Modularity

I don't modularize everything. In fact, there is only one file. I don't think it's necessary. In the case of small projects, it's a single person. I don't feel anything wrong with the algorithm in the whole coding process, except that the processing of input data is very uncomfortable

2. Code quantity

If you use scanf and printf, you will reduce a lot of code. ip computing using the second method will be much less.

3.main.c. problems

The question is that I can't change it. I suggest you open my file directly and change it directly. [funny]

My github: https://github.com/gudiffany/gudiffany