C++ cin practice and data abstraction practice

Recently I wrote a program for the mentally retarded to find the Greatest Common Divisor (GCD, Greatest Common Divisor). It has not been possible to distinguish the similarities and differences between data encapsulation and data abstraction, but it can still be implemented. The specific thing is to hide the data of the class (variables that store numbers) and key member functions (specific algorithms), and expose the constructor and destructor for operations in the main program (as an interface).

Because this class is very simple, give it two numbers, it tells you GCD, so you don't have to talk nonsense, call the algorithm directly in the constructor, but you need to perform input data check before calling the algorithm, otherwise there will be unexpected and ridiculous errors. The main body of the main program is written in a do while loop. It is necessary to query the screen after each execution, and judge whether to exit the loop and end the main program according to the results of the screen input.

The standard input stream cin is commonly used for input in C++. In this practice, several commonly used functions are used to complete input judgment

cin buffer

The input of cin will enter a buffer for storage, following the LILO principle, and cin will use tabs, spaces and carriage returns as input separators.

#include <iostream>
using namespace std;

int main(){
    
    

	while(1){
    
    

		cout << "Say something to me! " << endl;

		char input[100];

		cin >> input;

		cout << input << endl;

	}

	return 0;

}

The test results of this example are as follows:

Say something to me!
Where is my space???
Where
Say something to me!
is
Say something to me!
my
Say something to me!
space???
Say something to me!

To solve this problem, you can use the cin.getline() member function, which can read the entire line of input, and can specify the separator, the default is carriage return.

cin.fail()

Cin will check the input data and the data type of the incoming variable. When an error occurs, cin.fail() will return true.

cin.clear ()

The function of cin.clear() is to clear the error state of cin.fail(). Since input check and re-input are often placed in a loop body, if the homing is not performed, it will cause an endless loop. Examples of the use of cin.fail() and cin.clear() are shown in the constructor of the class in the following example.

In the early days of learning, I mistaken the function of cin.clear() for clearing buffer data, but it is not. This is my first point of confusion.

cin.ignore (1000, '\ n')

The function of cin.ignore() is to clear the buffer data or ignore the buffer data. The first parameter is the number of bytes to be ignored, and the second data is the character specified to stop the ignore behavior. If you enter "qwerty\n" and specify to ignore 3 characters, "rty\n" will flow into the next variable specified by cin. If you specify to ignore 1000 characters, it will be ignored until after'\n'. The next input behavior has an impact.

When confusing cin.clear(), I mistakenly thought that this meant that the next time cin only reads the first few input bytes and ignores the subsequent bytes.

Routine

The following is a routine:

#include <iostream>
using namespace std;

class GCD{
    
    
private:

	int tempG;	//较大数
	int tempS;	//较小数
	int tempM;	//余数

	int num1;	//存放输入数字
	int num2;

	int gcd;	//存放最大公约数

	//成员函数 计算最大公约数
	void Euclidian();

	//成员函数,显示计算结果
	void display();

public:

	GCD(void);				//构造
	virtual ~GCD(void);		//析构
	
};

//构造时执行输入判断、计算以及结果输出
GCD::GCD(void){
    
    
	cout << "Please enter two positive integer numbers. Seperate them using tab, space or return:" << endl;
	cin >> num1 >> num2;

	while ((num1 < 1) || (num2 < 1) || (cin.fail()))
	{
    
    
		cout << "Please enter two POSITIVE integer NUMBERS!" << endl;
		cin.clear();			//清除错误状态
		cin.ignore(100, '\n');		//忽略前序输入
		cin >> num1 >> num2;
	}
	if (num1 > num2){
    
    
		tempG = num1;
		tempS = num2;
	}
	else{
    
    
		tempG = num2;
		tempS = num1;
	}
		Euclidian();
		display();
}

//析构以释放内存
GCD::~GCD(void){
    
    }

//成员函数,计算最大公约数
void GCD::Euclidian(){
    
    
	do{
    
    

		tempM = tempG % tempS;
		tempG = tempS;
		tempS = tempM;

	} while (tempM != 0);

	gcd = tempG;
}

//成员函数,显示结果
void GCD::display(){
    
    
	cout << "The greatest common divisor of " << num1 << " and " << num2 << " is: " << gcd << endl;
	cout << "--------------------------------------------------------" << endl;
}

int main(){
    
    
	char f;
	do{
    
    
		GCD obj;
		obj.~obj();

		cout << "Do you wish to continue? (Y/N)" << endl;
		cin >> f;

		int ff = 0;

		do{
    
    
			switch (f){
    
    
			case 'Y':
				ff = 0;
				continue;
			case 'N':
				ff = 0;
				continue;
			default:
				cout << "You are so bad! (Y/N)" << endl;
				ff = 1;
				//cin.clear(); 此处并未对cin.fail()进行判断,不用使用cin.clear()
				cin.ignore(1000, '\n');
				cin >> f;
			}
		} while (ff == 1);

	} while ( f == 'Y');
	return 0;
}

The test results are as follows

Please enter two positive integer numbers. Seperate them using tab, space or return:
1232 232
The greatest common divisor of 1232 and 232 is: 8
--------------------------------------------------------
Do you wish to continue? (Y/N)
what if i mess with you				//主程序中对输入的判断
You are so bad! (Y/N)
Y
Please enter two positive integer numbers. Seperate them using tab, space or return:
gs 2						//构造函数对输入的判断
Please enter two POSITIVE integer NUMBERS!
-2 7						//构造函数对输入的判断
Please enter two POSITIVE integer NUMBERS!
23232116 2322
The greatest common divisor of 23232116 and 2322 is: 2
--------------------------------------------------------
Do you wish to continue? (Y/N)
N
Press any key to continue . . .

Follow-up question

Please enter two positive integer numbers. Seperate them using tab, space or return:
111111111111111111111111111111111 1
Please enter two POSITIVE integer NUMBERS!
1111111111111 6
Please enter two POSITIVE integer NUMBERS!
111111111111 6
Please enter two POSITIVE integer NUMBERS!
11111111111 6
Please enter two POSITIVE integer NUMBERS!
1111111111 6
The greatest common divisor of 1111111111 and 6 is: 1
--------------------------------------------------------
Do you wish to continue? (Y/N)
Ndfsdfsdfs
Press any key to continue . . .

The data is too long; the identification of this Y/N judgment cannot be realized.

Guess you like

Origin blog.csdn.net/weixin_54940900/article/details/113533847