Chapter I fundamentals of C + + Programming - 1.6 pointer

   this article starts with some troublesome knowledge in C and C + +: Pointer.
  back to the previous article: Chapter 1 Fundamentals of C + + Programming - 1.5 learn to use Array and Vector in C + + Remaining problems. First, the upper limit is six series. After guessing in turn, the program does not end as expected; Second, this method displays six groups of elements in the same order every time. Our purpose is to increase the flexibility of the program.
   this article will indirectly access each node by discarding the way specified by name through pointer vector to achieve the purpose of transparency. We can manipulate pointers (representing a specific memory address) without directly manipulating objects. I learned how to define an object:

1. Pointer

int ival=1024;

   pointer contains the memory address of a specific type of object. If we want to define a pointer of a specific type, we must add * after the type name:

int *pi;//The pointer type is an int object

  if we want to go to the memory address of the object instead of the object value, we should use the address operator&

int *pi=&ival;//Set the initial test value of pi to the memory address where ival is located

   if we want to access an object indicated by the pointer, we can withdraw the pointer, that is, obtain the object "located on the memory address indicated by the smart", and use * before the pointer:

if(*pi!=1024)//Read vial value
	*pi=1024;//Write value to vial

  pointer summary:
1. Pointer meaning description

pi;//Represents the memory address represented by pi
*pi;//Represents the value of vial

2. The pointer may not point to any object, and an error may occur when the program runs, that is, a wild pointer. A pointer that does not point to any object. Its address is generally 0, which is also called null pointer and wild pointer, so we can initialize the pointer:

int *pi=0;

  we define six vector objects (representing six series):

vector<int> fibonacci,Lucas,Pell,Triangular,Square,Pentagonal;

   we use a pointer to point to a vector with "element type int", in the form of:
  type_of_object_pointer_to *name_of_pointer_object
  type_of_object_pointer_to: data type
  name_of_pointer_object: object name
   if we point to vector < int >, we will name me pv, and the initial value is 0.

int *pv=0;

  this method can point to the vector of the sequence in turn, which is cumbersome and will sacrifice the transparency of the program. Then, another solution is to store the memory address of each sequence in the vector, and we can access these sequences transparently through indexing:

const int seq_cnt=6;
//Defines a pointer array with a length of 6
//Each pointer points to the vector < int > object
vector<int> *seq_addrs[seq_cnt]={

  where, seq_addrs indicates array, and the element type is vector < int > *, seq_addrs[0] indicates the address of fibonacci vector. Therefore, we can access each vector by index value instead of Name:

vector<int> *current_vec=0;
for(int i=0;i<seq_cnt;i++){
	current_vec=seq_addrs[i];//All elements to be displayed through current_vec access

  finally, it mainly solves the problem of sequence. Previously, users always guessed in the order of fibonacci, Lucas, Pell, Triangular, Square and Pentagonal. We want the sequence to appear randomly. Here we use the rand() and srand() functions in the C language standard library:

#include <cstdlib>
srand(seq_cnt);//The parameter of srand() is the seed of the random number generator
seq_index=rand()%seq_cnt;//rand() will return an integer of "the maximum number that can be represented by 0-int". Our index is between 0-5, and the index adopts the method of remainder.

   the two functions must use the cstdlib header file

2. Case update

  the following is the code improved according to the previous article:

#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;
int main() {
	int usr_guess;      //User guessed number
	bool num_seq = true; //Show next series
	bool guess_again = true; //The user wants to guess again
	int guess_num = 0;  //Total number of guesses
	int guess_right = 0;//Number of user guesses
	char usr_rsp;      //The user's answer is recycled
	char try_again;    //The user's answer is used for external circulation
	double usr_score = 0.0;//Score ratio, double double precision type
	const int max_tries = 3;//Set the maximum number of guesses

	const int seq_size = 18;//Set the maximum length of the sequence

	int elem_seq[seq_size] = { //Each sequence stores the first three numbers for guessing
		1,2,3, //Fibonacci
		3,4,7, //Lucas (Lucas)
		2,5,12, //Pell
		3,6,10, //Triangular
		4,9,16, //Square
		5,12,22 //Pentagonal
	//Store the first three numbers of each sequence into the vector
	vector<int> fibonacci(elem_seq, elem_seq + 3);
	vector<int> lucas(elem_seq+3, elem_seq + 6);
	vector<int> pell(elem_seq+6, elem_seq + 9);
	vector<int> Triangular(elem_seq+9, elem_seq + 12);
	vector<int> Square(elem_seq+12, elem_seq + 15);
	vector<int> Pentagonal(elem_seq+15, elem_seq + 18);

	const int max_seq = 6;
	string seq_names[seq_size] = {
	//Store the address of each sequence into seq_addrs array
	vector<int> *seq_addrs[max_seq] = {
	vector<int> *current_vec = 0;
	int seq_index;

	while (num_seq == true) {
		int try_cnt = 0;//Compare the number of guesses with the maximum number
		bool got_it = false; //Is the user right

		//Randomize the sequence
		seq_index = rand() % max_seq;
		current_vec = seq_addrs[seq_index];//Get sequence

		//Start guessing numbers
		cout << "The first two elements of the squence are: "
			<< (*current_vec)[0]<< ","
			<< (*current_vec)[1] << "."
			<< "\nWhat is the next element?\n";

		//The user guessed wrong and wanted to guess again
		while (guess_again == true && got_it == false && (try_cnt++ <= max_tries))

			std::cout << "please input your num:" << endl;
			std::cin >> usr_guess;

			//If you guess right
			if (usr_guess == (*current_vec)[2]) {

				std::cout << "Your guess is right!"
					<< (*current_vec)[2]
					<< " is the next element in the "
					<< seq_names[seq_index]
					<< " sequence.\n";
				got_it = true;
			//User guessed wrong
			else {
				//Judge the number of guesses switch
				switch (try_cnt)
					std::cout << "Oops!Nice guess but not quiye it! \n" << endl;
					std::cout << "Hmm.Sorry.Wrong a second time.\n" << endl;
					std::cout << "Ah,this is harder than it looks.\n" << endl;
					std::cout << "It must be getting pretty frustrating by now! \n" << endl;
				//Do you want to try again
				std::cout << "Error!Want to try again?(y/n):";
				std::cin >> usr_rsp;
				if (usr_rsp == 'N' || usr_rsp == 'n')
					guess_again = false;
		}//End of inner circulation

		std::cout << "want to try another sequence again?(y/n):";
		std::cin >> try_again;
		if (try_again == 'N' || try_again == 'n')
			num_seq = false;

	}//End of external circulation
	return 0;

Tags: C++ visualstudio

Posted by stevenye on Mon, 02 May 2022 00:38:47 +0300