当前位置:网站首页>Experiment four categories and objects

Experiment four categories and objects

2022-06-12 00:10:00 Jun Yehan

A. An array of objects ( Classes and objects )

Title Description

In class, we talked about the concept of class type , For example, in question 1, we have the abstract concept of student class , Thousands of students have the same attributes , But for a specific student , He / She has her own distinct personality , For example, Wang Hai, a computer major , Li Hui from the school of Information Engineering , So we are defining a variable of this class :Studentstudent; Suppose the class contains the name 、 Student number 、 Gender 、 The College 、 Contact number, etc ; In the process of program running , Put variables student Given different values, it can indicate whether it is Wang Hai or Li Hui , Try to define an array of students ( For example, the size of four elements , Think about how the four objects are initialized ? The constructor is called ?), Then input four different student attributes to initialize the students in the array ( It's best to define an input function ), Then output the name of each object of the student object array .

Input

Enter the size of the array elements

Enter the attribute values of each object on each line in turn

Output

Each line outputs the name of a student class object

sample input

3
tom 2013333333  male   school of computing  13766666666
Marry 2012222222  Woman   School of Information Engineering  13555555555
John  2014444444  male   School of Management  13888888888

sample output

tom
Marry
John

Reference code

#include<iostream>
#include<string>
using namespace std;
class Student
{
    
	public:
	void set()
	{
    
		cin>>name>>no>>sex>>major>>number;
	}
	void Print()
	{
    
		cout<<name<<endl;
	}
	
	private:
	string name;
	string no;
	string sex;
	string major;
	string number;
		
};
int main()
{
    
	
	int n;
	cin>>n;
	Student *p=new Student[n];
	for(int i=0;i<n;i++)
	{
    
		p[i].set();	
	}
	for(int i=0;i<n;i++) p[i].Print();
	delete []p;
}

B. Student class definition ( Classes and objects )

Title Description

The center of object-oriented programming is to abstract objective things into pieces of code in the program world , The main body of the campus is students , General students contain many attributes , Such as name 、 Student number 、 My college 、 major 、 Gender 、 address 、 contact number ...... wait , There are these properties , Need to manipulate their actions , For example, read the name 、 Set name 、 Read the student number 、 Set up student number ...... wait , This is the attribute and method we talk about in class , For properties and methods , We also have access control restrictions , Marked as public、private、protected etc. , According to the above information , Please give a complete definition of student class :Student, And test the output n Each attribute value of this kind of object .

Input

The first line indicates that you want to enter n Objects

Enter the attribute values of different objects in subsequent lines , One line per object .

Output

Output the respective properties of different objects

Each object occupies one line

sample input

2
WangHai 2014150112 CSSE ComputerScience male South215 13760222222
LiBin 2013151292 CSSE SoftwareEngineering female South318 13677777777

sample output

WangHai 2014150112 CSSE ComputerScience male South215 13760222222
LiBin 2013151292 CSSE SoftwareEngineering female South318 13677777777

Reference code

#include<iostream>
#include<string>
using namespace std;
class Student
{
    
	public:
	void set()
	{
    
		cin>>name>>no>>major1>>major2>>sex>>address>>number;
	}
	void Print()
	{
    
		cout<<name<<" "<<no<<" "<<major1<<" "<<major2<<" "<<sex<<" "<<address<<" "<<number<<endl;
	}
	
	private:
	string name;
	string no;
	string major1;
	string major2;
	string sex;
	string address;
	string number;
		
};
int main()
{
    
		
	int n;
	cin>>n;
	Student *p=new Student[n];
	for(int i=0;i<n;i++)
	{
    
		p[i].set();	
	}
	for(int i=0;i<n;i++) p[i].Print();
	delete []p;
	
}

C. Gobang simple implementation ( Classes and objects )

Title Description

Gobang is a pure strategy chess game played by two people , Each side uses black and white pieces , On the intersection of the straight line and the horizontal line of the chessboard , First formed 5 Subwired wins . Suppose that the chessboard is fifteen ways (15×15) The board , Input o It means that a white child falls into a trap ,u It means that sunspots fall .

Using object-oriented idea to realize Gobang game contains multiple classes . The class and object of this exercise , Merge rule class and checkerboard class , Only the checkerboard class is defined , Realize the placement function of Gobang , Do not accept repentance .

Chessboard :

attribute :15*15 The board .
Method : The initial chessboard is empty .
Accept the fall , Determine whether it is legal .
According to the rules , Modify the chessboard status .
According to the rules , Determine whether the five child connection .
Output checkerboard status .

According to the title , Add other methods . Cannot add attribute .
notes 1: The rule for determining whether it is legal is :① It is considered illegal when the drop is outside the chessboard ,② It is considered illegal when there are chess pieces in the falling position . In addition to the above two cases , Other circumstances shall be deemed legal .

notes 2: The determination of five child connection is carried out after each drop , If you get a sunspot or a white boy, you will win , Then the following drop operation will not be carried out ( but OJ It is required to complete the reading ).

notes 3: The drop point may be illegal , The input data guarantees black 、 Baizi falls at intervals , Don't worry about sunspots 、 The problem of the order in which white seeds fall . That is, when one side falls , If it's legal , The input data will be handed over to the other party immediately ; If it's illegal , Input data will make this side continue to fall , When there is a legal situation , The input data will be handed over to the other party immediately .

notes 4: Input data to ensure that the final chessboard has at least one piece , There will be no chess pieces on the whole board .

Input

The first 1 That's ok : Number of tests t ( 1 <= t <= 15)

Each group of test data represents a Gobang game , The format is :

The... In each group 1 That's ok : Number of falls n (1 <= n <= 300)

The... In each group 2 Go to the first place n + 1 That's ok : Move later ,o or u, x, y, They represent the falling square and the falling coordinates respectively ( The falling coordinates are consistent with the screen coordinates , x, y Integers , The legal coordinates are (1,1) To (15,15))

Output

For each group of test data , Output chessboard final state (# Said the spots ,@ Said an albino ),

Output Gobang game results :

Bai Zisheng 、 Sunspot wins 、 Baizi continues 、 Sunspot continue .

The output of each group of test data is separated by blank lines .

sample input

2
11
u 8 8
o 7 9
u 9 9
o 10 10
u 7 9
u 8 10
o 11 11
u 8 9
o 8 7
u 8 11
o 9 8
11
u 8 8
o 10 6
u 8 9
o 8 7
u 10 8
o 11 8
u 8 11
o 11 7
u 8 12
o 10 7
u 8 10

sample output

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 @ 0 0 0 0 0 0 
0 0 0 0 0 0 @ # # # # 0 0 0 0 
0 0 0 0 0 0 0 @ # 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 @ 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 @ 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
 Sunspot continue 

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 @ # # # # # 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 @ @ # 0 0 0 0 0 0 0 
0 0 0 0 0 0 @ @ 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
 Sunspot wins 

PS: This question is really a bit troublesome , There are many conditions to consider ....

Reference code

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
class CBoard {
    
private:
    char board[16][16];
public:
    CBoard() {
    
        for (int i = 1; i <= 15; i++)
            for (int j = 1; j <= 15; j++)
                board[i][j] = '0';
    }
    int Getche(char type, int i, int j) {
     // Output the result after receiving the drop  1→ legal ,2→ illegal ,3→ Sunspot wins ,4→ Bai Zisheng 
        int flag = 1;
        if (i <= 15 && i >= 1 && j <= 15 && j >= 1 && board[i][j] == '0') {
    
            flag = Update(type, i, j);
        }
        else {
    
            flag = 2;
        }
        return flag;
    }
    int Update(char type, int i, int j) {
     // After updating the data , Output results  1→ legal ,2→ illegal ,3→ Sunspot wins ,4→ Bai Zisheng 
        if (type == 'u') board[i][j] = '#';
        else board[i][j] = '@';
        int flag = Judge(type, i, j);
        return flag;
    }
    int Judge(char type, int i, int j) {
     // Judge output results  1→ legal ,2→ illegal ,3→ Sunspot wins ,4→ Bai Zisheng 
        char chess = (type == 'u' ? '#' : '@');
        int flag = 1;
        int tmpi = i, tmpj = j;
        int count = 0;
        // From the top down 
        while (tmpi > 0 && board[tmpi][tmpj] == chess) {
    
            tmpi--;
            count++;
        }
        tmpi = i + 1;
        while (tmpi < 16 && board[tmpi][tmpj] == chess) {
    
            tmpi++;
            count++; 
        }
        if (count >= 5) {
    
            flag = (chess == '#' ? 3 : 4);
            goto end;
        }
        // Horizontal 
        tmpi = i;
        count = 0;
        while (tmpj > 0 && board[tmpi][tmpj] == chess) {
    
            tmpj--;
            count++;
        }
        tmpj = j + 1;
        while (tmpj < 16 && board[tmpi][tmpj] == chess) {
    
            tmpj++;
            count++;
        }
        if (count >= 5) {
    
            flag = (chess == '#' ? 3 : 4);
            goto end;
        }

        tmpj = j;
        count = 0;
        while (tmpi > 0 && tmpj > 0 && board[tmpi][tmpj] == chess) {
    
            tmpi--; tmpj--;
            count++;
        }
        tmpi = i + 1;
        tmpj = j + 1;
        while (tmpi < 16 && tmpj < 16 && board[tmpi][tmpj] == chess) {
    
            tmpi++; tmpj++;
            count++;
        }
        if (count >= 5) {
    
            flag = (chess == '#' ? 3 : 4);
            goto end;
        }

        tmpj = j; tmpi = i;
        count = 0;
        while (tmpi < 16 && tmpj > 0 && board[tmpi][tmpj] == chess) {
    
            tmpi++; tmpj--;
            count++;
        }
        tmpi = i - 1;
        tmpj = j + 1;
        while (tmpi > 0 && tmpj < 16 && board[tmpi][tmpj] == chess) {
    
            tmpi--;
            tmpj++;
            count++;
        }
        if (count >= 5) {
    
            flag = (chess == '#' ? 3 : 4);
            goto end;
        }
    end:
        return flag;
    }
    void Print() {
    
        for (int i = 1; i <= 15; i++) {
    
            for (int j = 1; j <= 15; j++) {
    
                cout << board[i][j] << ' ';
            }
            cout << endl;
        }
    }
};

int main() {
    
    int n;
    cin >> n;
    for (int n_ = 0; n_ < n; n_++){
    
        CBoard board;
        int t, i, j, flag = 1;
        char type;
        cin >> t;
        for (int z = 0; z < t; z++) {
              
			cin >> type >> i >> j;
            if (flag == 1 || flag == 2)
                flag = board.Getche(type, i, j);

        }
        if (n_ != 0) cout << endl;
        board.Print();
        if (flag == 3) cout << " Sunspot wins " << endl;
        else if (flag == 4) cout << " Bai Zisheng " << endl;
		else {
    
			if (flag == 1) {
    
				if (type == 'u') cout << " Baizi continues " << endl;
				else cout << " Sunspot continue " << endl; 
			}
			else {
    
				if (type == 'o') cout << " Baizi continues " << endl;
				else cout << " Sunspot continue " << endl; 
			}
		} 
    }
    return 0;
}

D. Passbook class definition ( Classes and objects )

Title Description

Define a passbook class CAccount, The passbook class has an account number (account, long)、 full name (name,char[10])、 balance (balance,float) Data members such as , Deposit can be realized (deposit, Successful operation prompt “saving ok!”)、 Withdraw money (withdraw, Successful operation prompt “withdraw ok!”) And check the balance (check) The operation of , The withdrawal amount must be within the balance range , Otherwise prompt “sorry! over limit!”.

Write the main function , Create the object of this class and test , Enter account 、 full name 、 After balance , Query balance by 、 deposit 、 Check the balance 、 Withdraw money 、 Call the class method in the order of querying the balance and output .

Input

The account number of the first passbook 、 full name 、 balance

Amount of deposit

Withdrawal amount

The account number of the second passbook 、 full name 、 balance

Amount of deposit

Withdrawal amount

Output

The account balance of the first passbook

Deposit operation results

The account balance

Withdrawal operation results

The account balance

The account balance of the second passbook

Deposit operation results

The account balance

Withdrawal operation results

The account balance

sample input

9111 Tom 1000
500
1000
92220 John 500
500
1500

sample output

Tom's balance is 1000
saving ok!
Tom's balance is 1500
withdraw ok!
Tom's balance is 500
John's balance is 500
saving ok!
John's balance is 1000
sorry! over limit!
John's balance is 1000

Reference code

#include<iostream>
#include<string>
using namespace std;
class CAccount
{
    
	public:
	void set()
	{
    
		cin>>account>>name>>balance>>cunkuan>>qukuan;
	}
	void check()// Output query balance  
	{
    
		cout<<name<<"'s balance is "<<balance<<endl;
	}
	void deposit()
	{
    
		balance+=cunkuan;
		cout<<"saving ok!"<<endl;
	}
	void withdraw()
	{
    
		if(qukuan>balance) cout<<"sorry! over limit!"<<endl;
		else
		{
    
			balance-=qukuan;
			cout<<"withdraw ok!"<<endl;
		 } 
	} 
	private:
		long account;
		string name;
		float balance;
		float cunkuan;
		float qukuan;
	
		
};
int main()
{
    
	CAccount p[5];
	for(int i=0;i<2;i++)
	{
    
		p[i].set();	
	}
	for(int i=0;i<2;i++)
	{
    
		p[i].check();
		p[i].deposit() ;
		p[i].check() ;
		p[i].withdraw() ;
		p[i].check() ;
	} 
	
}

E. The fattest Garfield ( Classes and objects + Array )

Title Description

There are a group of cats , Each cat has its own name and weight .

Use classes to describe cats , Name and weight are private properties , Request to add attribute get Method . Other functions can be defined as needed

Create a dynamic cat object array , Store the name and weight of each cat

Sort the array in ascending order according to the cat's weight , And output the name of each cat after sorting

The numerical values involved in the topic are treated as integers

Input

First line input n Express n Cats

On the second line, enter the name and weight of a cat

Input in sequence n That's ok

Output

Output one line , Output the name of the sorted cat

sample input

4
chocolate 1500
water 400
cheese 3000
vegetable 200

sample output

vegetable water chocolate cheese

Reference code

#include<iostream>
#include<string>
using namespace std;
class Cat
{
    
	public:
	void set()
	{
    
		cin>>name>>weight;
	}
	int get_wei()// Get weight  , Because weight is a private attribute , Must use get Sort by  
	{
    
		return weight;
	}
    void get_nam()
    {
    
    	cout<<name;
	}
	private:
		string name;
		int weight;
	
		
};
void Sort(Cat &a,Cat &b)// Sort by reference 
	{
    
		Cat temp; 
		if(a.get_wei() >b.get_wei()) // The comparison is weight  
		{
    
			temp=a;
			a=b;
			b=temp;
		}
	} 
int main()
{
    
	int n;
	cin>>n;
	Cat *p=new Cat[n];
	for(int i=0;i<n;i++)
	{
    
		p[i].set() ;// In the data  
	}
	for(int i=1;i<n;i++)// Bubble sort  
	  for(int j=0;j<n-i;j++)
	  {
    
	  	Sort(p[j],p[j+1]);
	  } 
	for(int i=0;i<n;i++) 
	  {
    
	  	p[i].get_nam();
	  	if(i<n-1) cout<<" ";
	  } 
	delete []p;
	
}
原网站

版权声明
本文为[Jun Yehan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011530098905.html