2022 object oriented programming (Fuzhou University) winter vacation assignment 2

Posted by JP128 on Mon, 31 Jan 2022 00:38:10 +0100

Which course does this assignment belong to 2022 object oriented programming (Fuzhou University)
What are the requirements for this assignment 2022 object oriented programming (Fuzhou University) winter vacation assignment 2
The goal of this assignment The router operates the data packet according to the corresponding rules. Now it gives the rule set and input data packet, and outputs the best matching rules of the corresponding data packet
Job text See below
Other references IP address classification and CIDR division method
Processing method of "error C2280 trying to use deleted function" when fstream class is used in VS2015

Github warehouse please click here The complete source codes of C and C + + programs have been uploaded to GitHub warehouse

What to learn to complete this assignment

  • How to import data from and export data to a file
  • How do different source files relate to the same program
  • Understanding of CIDR representation of IP address block

learning process

Reading data from and outputting data to a file (Visual Studio 2022 compiler, c file):

/*Reading data from and outputting data to a file*/
#include <stdio.h>
#include <stdlib.h>

int main()
{
	FILE* fp, * out;
	char z[9854] = {};
	char input[2022] = {};//The full path to the file where the data is stored
	char output[2022] = {};//The full path where the data output file is stored
	int i;
	printf("Please enter the full path of the data source file:\n");
	gets(input);
	printf("Please enter the full path of the data output file:\n");
	gets(output);
	fp = fopen(input, "r");//"r" means to open the file in read-only mode
	out = fopen(output, "w+");//"w +" means to open the read-write file. If the file exists, the file length will be cleared to zero, that is, the content of the file will disappear. If the file does not exist, the file is created.
	while ((fscanf(fp, "%s", z)) != EOF)//Read the corresponding type of data from the file by line, and EOF means to end the cycle after reading to the end of the file
	{
		fprintf(out, "%s\n", z);//Output the corresponding type of data to a file
	}
	fclose(fp);//Close the file corresponding to the file pointer
	fclose(out);
	system("pause");
	return 0;
}

This program is completely implemented in C language by using fopen, fclose, fscanf, fgets, fprintf and other file functions in C language.
After completing the C language code, I also tried to use some operations on the file in the fsstream of C + + and achieved success. The core part is as follows (Visual Studio 2022 compiler, cpp file):

#include <iostream>
#include <fstream>

char rule_packet[9854] = {};//The full path where the rule set file is stored
char packet[9854] = {};//The full path to the save dataset file
char output[9855] = {};//Store the full path of the data matching result file

cout << "Please enter the full path of the rule set file (space free, no quotation marks):" << endl;
gets_s(rule_packet);
cout << "Please enter the full path of the dataset file (can contain spaces and no quotation marks):" << endl;
gets_s(packet);
cout << "Please enter the full path of the data matching result file. If the file does not exist, it will automatically create a new file (which can contain spaces and quotation marks):" << endl;
gets_s(output);

ifstream fp_rule(rule_packet, ios::in | ios::_Nocreate);//in means input from file_ Nocreate means that if the file does not exist, the file will not be created
fp_rule >> a >> b >> c;
ifstream fp_packet(packet, ios::in | ios::_Nocreate);
ofstream fp_out(output, ios::out);//out means output to the file, and the original contents of the file will be emptied
fp_out << a << b << c;

For the CIDR representation of IP address, I mainly refer to this CSDN blog post: IP address classification and CIDR division method
Basic use of classes in C + +

class rule
{
public:
	int in(ifstream& fp_rule);//The copy (assignment) constructor of fstream is a deleted function, "&" means to set the formal parameter of the function to the reference type to prevent error reporting
	class rule* create_chain_table(ifstream& fp_rule);//The private part variables can be called through the member function. The member function can only be called in the public part
	void visit_chain_table(rule* p);
	void visit_processed_chain_table(rule* p, ifstream& fp_packet, ofstream& fp_out);
	
private:
	char ip0bin[37], ip1bin[37], z0[3], z1[3];
	unsigned int ip0min, ip0max, ip1min, ip1max;
	int ip01,ip02,ip03,ip04,ip0wei;
	int ip11,ip12,ip13,ip14,ip1wei;
	int d01, d02, d11, d12, x0, x1;
	char x;
	rule* next;
};
void rule::visit_processed_chain_table(rule* p, ifstream& fp_packet, ofstream& fp_out)
{
	unsigned int ip0, ip1, d0, d1, y, count, flag;
	rule* pt = p;//Store the pointer of the first node
	while (fp_packet >> ip0 >> ip1 >> d0 >> d1 >> y)
	{
		for (p = pt, count = 0, flag = 0; p->next; p = p->next)
		{
			if (rule_match(ip0, ip1, d0, d1, y, p->ip0min, p->ip0max, p->ip1min, p->ip1max, p->d01, p->d02, p->d11, p->d12, p->x0, p->x1))
			{
				fp_out << count << endl;
				flag = 1;
				break;
			}
			count++;//Counter, indicating which rules are currently [rules are numbered from 0]
		}
		if (!flag)fp_out << "-1\n";
	}
}
rule* head, * p;
p->visit_processed_chain_table(p, fp_packet, fp_out);

Only part of the code used by the class is shown here. Please move to GitHub warehouse for the complete code.

Programming ideas

Convert the CIDR dotted decimal IP address of the rule set into 32 binary numbers and store them in the character array. Take the remainder of the four numbers from right to left and fill them in the character array in reverse order. Every 8-bit binary number is a group of operations. Fill 0 in front of less than 8 bits. Some codes are as follows:

/*ip_bin Is a character array. The complete code has been uploaded to GitHub*/
char* p;
for (p = ip_bin + 32; ip4; ip4 /= 2, p--)
	*p = ip4 % 2 + '0';
if (p != ip_bin + 24)
	for (; p > ip_bin + 24; p--)*p = '0';
for (; ip3; ip3 /= 2, p--)
	*p = ip3 % 2 + '0';
if (p != ip_bin + 16)
	for (; p > ip_bin + 16; p--)*p = '0';

After that, 32 binary digits are converted into a decimal number by bit operation. The code is as follows:

void IP_Switch_From_BIN_To_DEC(unsigned int *p,char x[])
{
	int i, m;
	*p = 0;
	for (i = 1, m = 31; i <= 32; i++, m--)
		*p += (x[i] - '0') * (1 << m);//Convert binary IP address to decimal by bit operation
}

Then determine the number of network prefix according to the number after "/" in CIDR representation of IP address, and then confirm the number of host number. Set the host number to 0 and 1 respectively to obtain the minimum address and maximum address of IP address block respectively.
Then determine the matching protocol number range according to the protocol number representation and matching rules.
Finally, match with the packet to find the best matching rule.
[Note: the complete C + + and C language codes have been uploaded to GitHub and will not be displayed here]

Test procedure

There are still serious bugs in the starting program, which can not output the correct results. Sometimes the error can not be found. The statement by statement debugging mode will be used to observe the value of each variable in real time.
After the serious bug of the program is repaired and the correct results can be output, the use mode of the program is partially improved and debugged.
Program debugging process written in C language:

Debugging process of program and simple file comparison program written in C + +

Difficulties encountered

  • The use of C + + featured classes is very unskilled. Solution: refer to the examples in the C + + programming book, write a program debugging by imitating the examples in the book, and try to use the member function to access the private part of the class
  • Various strange problems are encountered in the use of linked lists and file streams.
  • When writing programs using C + + classes, try to define FP as ifstream_ Rule and FP defined as ofstream_ When the variable of out is passed to the class member function as a parameter, an error message of "trying to call the deleted function" appears. Solution: refer to CSDN this article Processing method of "error C2280 trying to use deleted function" when fstream class is used in VS2015 , change the formal parameter to the reference type (plus "&") to class rule * rule:: create_ chain_ table(ifstream& fp_rule)

Some defects

  • There is no algorithm optimization, pure violence algorithm, and the program matching time is too long
  • Using C + + classes and linked lists of classes is too cumbersome
  • Failed to successfully separate the relevant modules of classes in the code written in C + +
  • Failed to parse the time complexity of the program
  • Failed to install the title, which requires the implementation to execute and input files using the command line, so an interactive interface is written as before

My ability is limited. There must be various undetected problems in the code. You are welcome to criticize and correct.