The following describes how to solve the problem of Joseph PHUs by using struct:
After learning the struct, we learned that the member pointer of the struct itself can point to the address of its own object, so we can easily think of solving this mathematical problem, it is no longer appropriate to describe with struct. It can be used to describe the circular linked list perfectly.
The Code is as follows:
// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.
# Include <iostream>
# Include <string>
Using namespace std;
Struct Children
{
Int number;
Children * next;
};
Void show (Children * point, int num) // The output function of the link chain.
{
For (int I = 1; I <= num; I ++)
{
Cout <point-> number <",";
Point = point-> next;
}
}
Void main ()
{
Int num; // Total number of children
Int interval; // number selected
Cout <"Enter the total number of children :";
Cin> num;
Cout <"Enter the selected number :";
Cin> interval;
Children * Joseph PHUs = new Children [num]; // sets the starting point pointer of the circle and dynamically opens up heap space for data storage.
Children * point = Joseph; // the pointer used for the initial linked list. The starting address is the same as that of the Joseph pointer.
For (int I = 1; I <= num; I ++)
{
Point-> number = I;
Point-> next = Joseph PHUs + I % num; // use the + 1 modulo method to set the next pointer of the node. At the end, the next pointer is automatically directed to the first node to form a chain link.
Point = point-> next; // move the location to the next ELE. Me node, that is, the next child location.
}
Show (point, num );
Children * cut_point;
Point = & Joseph PHUs [num-1]; // set the starting pointer to the last node, which starts from 0 when it enters the loop, so that the unnecessary node can be detached.
Int k = 0; // deliberately set a k to observe the number of while Loops
While (point-> next! = Point) // The nodes that need to be abandoned are continuously searched through Loops
{
K ++;
For (int I = 0; I <interval; I ++) // locate the node to be abandoned
{
Cut_point = point; // store the truncated position pointer
Point = cut_point-> next; // move the point pointer to the abandoned node, which is also related to the while loop termination condition.
}
Cut_point-> next = point-> next; // sets the truncated next pointer to discard the next pointer at the node, and disconnects the discarded node, that is, the unnecessary node.
Cout <"k:" <k <endl;
}
Cout <"final winner:" <endl <point-> number <endl <point-> next <endl;
Delete [] Joseph;
Cin. get ();
Cin. get ();
}
The hard part of this Code is the setting of the termination condition of the while loop. If the reader is not able to understand this part, pay attention to the following scheme to help learning.
The structure solution is very important. It is the foundation for us to fully understand the abstract problem of object-oriented programming. We must understand it before learning the following knowledge. We must take it seriously.
This code is more readable than the previous program, but it is still not easy to understand!
To make it easier to learn and understand, we have two children in a circle and set the number of records to 1.
The above two solutions to the Joseph's problem refer to the Code as a solution. The second solution is superior to the first one in terms of structure expression, but both of them are pure.Procedural ProgrammingAlthough the program is short, it is difficult to understand, and the program is not highly readable. Before we learn object-oriented programming, smart people may break down various steps into several functions to solve the problem.
The idea can be roughly divided into the following six parts:
1. Create a structure
2. initialize the total number of children and the number of children
3. initialize the linked list and create a link chain
4. start to get the winner by repeating the number of children
5. Output winner
6. Return heap memory space
From the table, this program can write six functions to process these six processes separately for ease of reading. Indeed, the readability of the modified program is greatly improved, however, there are still some shortcomings. The program is completely exposed and anyone can modify the program. Some program authors in the program do not want the objects that the user can modify to be exposed, and each object cannot be protected, there is no guarantee that the program will not be accidentally modified during operation. For users, they still need to have the ability to solve the problem of Joseph. Once the program becomes increasingly popular ,, every programmer involved in development needs to read all the parts of the program, and the program does not have the Black Box effect at all, causing great trouble for collaborative development by many people, almost everyone has done the same repetitive work. In this way, to solve a small branch problem, write a function, and finallyThere are many functions to solve local problems.The combined program is calledStructured Program DesignStructured Programming is more readable than procedural programming, but programs cannot be separated to solve major problems, when using them in main functions, this function always calls that function. If you are not the author of these functions, it is difficult to use them correctly and conveniently, in addition, the problem of variable name duplication in programs is also a headache .......
So how does Object-Oriented Programming solve these problems?
The idea of object-oriented programming is as follows:
Program = Object + object ..........
This combination
You can divide the problem into the following methods for design (as shown in)
Attachment:20052241612351. RAR
We can see that:
Object-Oriented ProgrammingIt is composed of classes. A class must have class objects. Interactions between programs are mainly performed through the relationship between objects.
Because we break down the Joseph PHUs problem into the Joseph PHUs class and the ring class, in the main function, you only need to use the Joseph PHUs class to design its object and clearly understand the external interface function of the Joseph PHUs class, that is, the method for operating the object initial, as for the internal implementation of Joseph PHUs, users do not need to know how to operate with the Ring class. They only need to know what interfaces and interface functions are, such a program is designed to ensure the security of all types of member data. The main function code call is extremely simple only when the operation of the object and the method of the object is established. Once the class needs to be modified, you only need to modify the class body, and the main function does not need to make any changes, so that it is good to do what people do and who do not conflict with each other.
The code of the program is as follows. I have compressed the project file and used it as an attachment for downloading this post. I hope the readers can read it carefully to understand the characteristics and intentions of object-oriented oop programming.
Main Program test4.cpp
-----------------------------------------------------------
// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.
# Include <iostream>
# Include "Joseph. h"
Using namespace std;
Void main ()
{
Joseph;
A. initial ();
Cin. get ();
Cin. get ();
}
Joseph. h
-----------------------------------------------------------
// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.
Class Joseph PHUs
{
Public:
Joseph PHUs (int num = 10, int