当前位置:网站首页>Experiment 7 class construction and static member function

Experiment 7 class construction and static member function

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

A. Triangle class ( Structure and destruction )

Title Description

Define a triangle class CTriangle, Attribute contains three sides and triangle type , Where the triangle type is saved in a string . Triangle types are as follows :

an isosceles triangle :isosceles triangle
right triangle :right triangle
Isosceles right triangle :isosceles right triangle
Equilateral triangle :equilateral triangle
General triangle :general triangle
Can't make a triangle :no triangle

The conditions for judging right triangle : The square length of one of the three sides of a triangle is equal to the sum of the square lengths of the other two sides

Class behavior includes constructing 、 destructor 、 Calculate the area, etc . Where the constructor will set the length of three edges , And judge whether the three sides can form a triangle 、 And set the triangle type . The destructor clears the length of the three edges 0, And set the triangle type to none

The triangle area is calculated as follows
 Insert picture description here

Input

Number of groups of test data

The first set of edges 1 The first set of edges 2 The first set of edges 3

The second set of edges 1 The second set of edges 2 The second set of edges 3

Output

The first triangle type , area

The second triangle type , area

If you don't form a triangle , There is no need to output area

Area precision to the decimal point 1 position

sample input

3
3.0 2.0 6.0
3.0 4.0 5.0
1.0 1.0 1.414

sample output

no triangle
right triangle, 6.0
isosceles right triangle, 0.5

Reference code

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
class CTriangle
{
    
	double a;
	double b;
	double c;
	string type;
	public:
	CTriangle(){
    }
	CTriangle(double x1,double x2,double x3);
	bool istriangle()
	{
    
		if(type[0]=='n') return false;
		else return true;
	}
	double area()
	{
    
		double s;
		double p;
		p=(a+b+c)/2;
		s=sqrt(p*(p-a)*(p-b)*(p-c));
		return s;
	}
	~CTriangle()
	{
    
		a=0;
		b=0;
		c=0;
		type="none";
	}
	void print()
	{
    
		if(istriangle()) cout<<type<<", "<<fixed<<setprecision(1)<<area()<<endl;
		else cout<<type<<endl;
		
	}
		
};
CTriangle::CTriangle(double x1,double x2,double x3)
	{
    
		a=x1;
		b=x2;
		c=x3;
		type="0";
		if(a+b>c&&a+c>b&&b+c>a&&fabs(a-b)<c&&fabs(a-c)<b&&fabs(b-c)<a)
		{
    
			int flag1=0,flag2=0;
			// right triangle  
			if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a) 
			{
    
				type="right triangle";
				flag1=1;
			}	
			else if(a==b||a==c||b==c) 
			{
    
				if(a==b&&b==c) type="equilateral triangle";// Equilateral triangle  
				else // An ordinary isosceles triangle  
				{
    
					type="isosceles right triangle";
					flag2=1;
				}	
			}
			else if(flag1&&flag2) type="isosceles right triangle";
			else type="general triangle";	
		}
		else type="no triangle";
	}
int main()
{
    
	int t;
	cin>>t;
	double x1,x2,x3;
	
	while(t--)
	{
    
		cin>>x1>>x2>>x3;
		CTriangle tri(x1,x2,x3);
		tri.print();
	}
}

B. Equation( Classes and objects + structure )

Title Description

Create a class Equation, Expression equation ax2+bx+c=0. Class contains at least the following methods :

1、 No arguments structure (abc The default value is 1、1、0) With a parameterized constructor , For initialization a、b、c Value ;

2、set Method , For modification a、b、c Value

3、getRoot Method , Find the root of the equation .

The root formula of the quadratic equation of one variable is as follows :
 Insert picture description here
The solution of the quadratic equation of one variable can be divided into three cases , as follows :
 Insert picture description here

Input

Enter the number of groups of test data t

The first group a、b、c

The second group a、b、c

Output

The root of the output equation , The result is after the decimal point 2 position

stay C++ in , The reference code for outputting the specified precision is as follows :

#include

#include // Must contain this header file

using namespace std;

void main( )

{ double a =3.141596;

cout<<fixed<<setprecision(3)<<a<<endl; // Output after decimal point 3 position

sample input

3
2 4 2
2 2 2
2 8 2

sample output

x1=x2=-1.00
x1=-0.50+0.87i x2=-0.50-0.87i
x1=-0.27 x2=-3.73

Reference code

#include<iostream>
#include<iomanip> 
#include<cmath>
using namespace std;
class Equation
{
    
	double a;
	double b;
	double c;
	public:
	Equation()
	{
    
		a=1;b=1;c=0;	
	}
	Equation(double a1,double a2,double a3)
	{
    
		a=a1;
		b=a2;
		c=a3;
	}	
	void set(double a1,double a2,double a3)
	{
    
		a=a1;
		b=a2;
		c=a3;
	}	
	int num_root()
	{
    
		int judge=0;
		if(b*b-4*a*c<0) judge=0;
		else if(b*b-4*a*c==0) judge=1;
		else judge=2; 
		return judge;
	}
	void getroot();	
};
void Equation::getroot()
{
    
	double x1,x2,x1_i,x2_i;
	double res1,res2;
	res1=-b/(2*a);
	if(num_root()==0) // No real roots  
	{
    
		x1=res1;
		x2=res1;
		x1_i=sqrt(4*a*c-b*b)/(2*a);// The discriminant is negative  
		x2_i=-sqrt(4*a*c-b*b)/(2*a); 
		cout<<"x1="<<fixed<<setprecision(2)<<x1<<'+'<<fixed<<setprecision(2)<<x1_i<<"i x2=";
		cout<<fixed<<setprecision(2)<<x2<<fixed<<setprecision(2)<<x2_i<<'i'<<endl;
	}
	else if(num_root()==1)
	{
    
		x1=res1;
		x2=res1;
		cout<<"x1=x2="<<fixed<<setprecision(2)<<x1<<endl;
	}
	else 
	{
    
		x1=res1+sqrt(b*b-4*a*c)/(2*a);
		x2=res1-sqrt(b*b-4*a*c)/(2*a);
		cout<<"x1="<<fixed<<setprecision(2)<<x1<<' ';
		cout<<"x2="<<fixed<<setprecision(2)<<x2<<endl;
	}
}
int main()
{
    
	int t;
	cin>>t;
	double a1,a2,a3;
	while(t--)
	{
    
		cin>>a1>>a2>>a3;
		Equation equ1(a1,a2,a3);
		equ1.getroot();
	}
}

C. Book borrowing ( An array of objects + structure )

Title Description

Suppose that the titles of the books in the library are different , There are several copies of the same book to borrow .

Define Book classes CBook, Data members include : Claim number 、 Title 、 Number of collections 、 Available quantity . Methods include :

Constructors : Initialize data members according to parameters .

Borrow books : The parameter is the title of the book . If the quantity of the book that can be borrowed is greater than or equal to 1, The request number is returned ; otherwise , Return to empty string .

The main function is dynamically defined CBook Array , Initialize library collection information . Enter borrowing information , Demand for each loan , Give the result according to the example .

Input

The types of books in the library n

n Book information : Claim number Title Number of collections Available quantity

Number of book borrowing requirements m

m The title of this book

Output

For each borrowing demand , Output the results according to the sample .

Output blank lines .

Output information of all library collections .

Output the number of books lent The number of books remaining in the collection

sample input

4
TP312JA-43/L99 Java Language programming course  3 0
TP312PH/Q68b PHP7 Kernel analysis  3 2
TP311.561-43/L93 Python University Course  3 1
TP311.5-43/M18a1(2)  Fundamentals of software engineering  3 3
4
Java Language programming course 
 Fundamentals of software engineering 
Python University Course 
Python University Course 

sample output

Java Language programming course   The book has been fully lent out 
 Fundamentals of software engineering   Claim number : TP311.5-43/M18a1(2)
Python University Course   Claim number : TP311.561-43/L93
Python University Course   The book has been fully lent out 

TP312JA-43/L99 Java Language programming course  3 0
TP312PH/Q68b PHP7 Kernel analysis  3 2
TP311.561-43/L93 Python University Course  3 0
TP311.5-43/M18a1(2)  Fundamentals of software engineering  3 2
 Lending books : 8 Ben    The rest of the collection : 4 Ben 

Reference code

#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
using namespace std;
class CBook
{
    
	string sbn;
	string name;
	int num_have;
	int num_borr;
	public:
		CBook(){
    }
		CBook(string s1,string s2,int n1,int n2)
		{
    
			sbn=s1;
			name=s2;
			num_have=n1;
			num_borr=n2;
		}
		void set(string s1,string s2,int n1,int n2)
		{
    
			sbn=s1;
			name=s2;
			num_have=n1;
			num_borr=n2;
		}
		string borrow()
		{
    
			string temp;
			if(num_borr>=1) 
			{
    
				num_borr--; 
				return sbn;
			} 
			else 
			{
    
				temp="\0";
				return temp;
			}
		}
		string get_name()
		{
    
			return name;
		}
		int get_numbor()
		{
    
			return num_borr;
		}
		int get_numhav()
		{
    
			return num_have;
		}
		void get_all()
		{
    
			cout<<sbn<<' '<<name<<' '<<num_have<<' '<<num_borr<<endl;
		}
};

int main()
{
    
	int n;
	cin>>n;
	int m,num1,num2;
	string s1,s2,temp1,temp2;
	CBook *p=new CBook[n];
	for(int i=0;i<n;i++)
	{
    
		cin>>s1>>s2>>num1>>num2;
		p[i].set(s1,s2,num1,num2);
	}
	cin>>m;
	for(int i=0;i<m;i++)
	{
    
		cin>>temp1;
		cout<<temp1<<' ';
		for(int j=0;j<n;j++)
		{
    
			if(p[j].get_name()==temp1) 
			{
    
				temp2=p[j].borrow();
				if(temp2=="\0") cout<<" The book has been fully lent out "<<endl;
				else cout<<" Claim number : "<<temp2<<endl;
				break;
			}
		}	
	}
	cout<<endl; 
	int sum=0,sum_left=0,sum_bor=0;
	for(int i=0;i<n;i++)
	{
    
		p[i].get_all();
		sum+=p[i].get_numhav();
		sum_left+=p[i].get_numbor();
	}
	sum_bor=sum-sum_left;
	cout<<" Lending books : "<<sum_bor<<" Ben  "<<" The rest of the collection : "<<sum_left<<" Ben "<<endl; 
	delete []p;
}

D. Mobile services ( structure + Copy structure + Pile up )

Title Description

Design a class to realize the function of mobile phone . It contains private properties : Number type 、 number 、 Number status 、 Shutdown date ; Contains methods : structure 、 Copy structure 、 Print 、 downtime .

1、 The number type indicates the user category , Use only a single letter ,A Means the government ,B It means enterprise 、C Means personal

2、 The number is 11 An integer , Use a string to represent

3、 The number status is represented by a number ,1、2、3 They are in use 、 Unused 、 Discontinue use

4、 Downtime date is a date object pointer , During initialization, the member points to null , This date class contains the private attribute year, month, day , And constructors and print functions


5、 The function of constructor is to accept foreign parameters , And set each attribute value , And output prompt information , Look at the sample output

6、 The function of copy structure is to copy the information of existing objects , And output prompt information , Look at the sample output .

Think about how downtime should be copied , How to copy without downtime ?? How to copy when it is down ??

7、 The printing function is to output all the attributes of the object , See the example for the output format

8、 The shutdown function is to disable the current number , The parameter is the downtime date , No return value , The operation is to change the status to disabled , And create the downtime date pointer as a dynamic object , And set the shutdown date according to the parameters , Finally, output prompt information , Look at the sample output


requirement : Realize the function of number backup in the main function , Copy all the information of the existing virtual mobile phone number , And change the number type to D Represents a backup ; Add a letter to the end of the mobile phone number X

Input

First line input t Express t Numbers

Second line input 6 Parameters , Including number type 、 number 、 state 、 Years of downtime 、 month 、 Japan , Space off

Input in sequence t That's ok

Output

Each sample outputs three lines , Output the original number information in turn 、 Back up the number information and the information after the shutdown of the original number

Dash between each example ( four ) Split up , Look at the sample output

sample input

2
A 15712345678 1 2015 1 1
B 13287654321 2 2012 12 12

sample output

Construct a new phone 15712345678
 type = Institutions || number =15712345678||State= In use 
Construct a copy of phone 15712345678
 type = Backup || number =15712345678X||State= In use 
Stop the phone 15712345678
 type = Institutions || number =15712345678||State= Discontinue use  || Shutdown date =2015.1.1
----
Construct a new phone 13287654321
 type = Enterprises || number =13287654321||State= Unused 
Construct a copy of phone 13287654321
 type = Backup || number =13287654321X||State= Unused 
Stop the phone 13287654321
 type = Enterprises || number =13287654321||State= Discontinue use  || Shutdown date =2012.12.12
----

Reference code

#include<iostream>
#include<string>
using namespace std;
class date {
    
	private:
		int y, m, d;
	public:
		date(int a, int b, int c) 
		{
    
			y = a;
			m = b;
			d = c;
		}
		void print_date() 
		{
    
			cout << y << '.' << m << '.' << d << endl;
		}
};
class phone {
    
	public:
		char type;
		string number;
		int mod;
		date *bir;
		phone(char a, string b, int c, date *p) {
    
			type = a;
			number = b;
			mod = c;
			bir = p;
			cout << "Construct a new phone " << number << endl;
		}
		phone(const phone &a) {
    
			type = 'D';
			number = a.number;
			mod = a.mod;
			bir = new date(*a.bir);
			cout << "Construct a copy of phone " << number << endl;
		}
		
		void stop() // Shutdown operation  
		{
    
			cout<<"Stop the phone "<<number<<endl;
			mod=3;
			print();
			cout<<"|| Shutdown date =";
			bir->print_date();
			cout<<"----"<<endl;
		}
		void print() 
		{
    
			switch (type) {
    
				case 'A':
					cout << " type = Institutions " << "||";
					break;
				case 'B':
					cout << " type = Enterprises " << "||";
					break;
				case 'C':
					cout << " type = personal " << "||";
					break;
				case 'D':
					cout << " type = Backup " << "||";
					number.append("X");
					break;
			}
			cout << " number =" << number << "||";
			switch (mod) {
    
				case 1:
					cout << "State= In use "<<endl;
					break;
				case 2:
					cout << "State= Unused "<<endl;
					break;
				case 3:
					cout << "State= Discontinue use  ";
					break;
			}

		}
		~phone()
		{
    
			if(bir!=NULL)
			delete bir;
		}
};
int main() {
    
	int t,y, m, d;
	cin >> t;
	while (t--) 
	{
    
		char type;
		string num;
		int mod;
		cin >> type >> num >> mod>> y >> m >> d;
		date *p = new date(y, m, d);
		phone ph1(type, num, mod, p);
		ph1.print();
		phone ph2(ph1);// Call copy construct  
		ph2.print();
		ph1.stop();
	}
}

E. Top grade ( Static members )

Title Description

The definition of student class is as follows :

class Student {

private:

int id;// Student number

int score; // achievement

static int maxscore;// The highest score

static int maxid;// Student number with the highest score

public:

Student(int ti=0,int ts=0)

:id(ti), score(ts)

{}

static void findMax(Student & st); // Looking for the highest grades and student numbers

static int getMaxScore(); // Return to the highest score

static int getMaxID();// Return the student number with the highest score

};

Enter a group of student numbers and grades , Use the above static members to find the highest score and corresponding student number

Input

First line input n Express n A student

Then the input n That's ok , Each row contains two data , Indicates student number and grade

Output

Call the static member function to output the student number and the highest score , See the sample format

sample input

3
1002 68
1023 54
1045 32

sample output

1002--68

Reference code

#include<iostream>
using namespace std;
class Student
{
    
	private:
		int id;
		int score;
		static int maxscore;// The highest score 
		static int maxid;// Student number with the highest score 
	public:
		Student(int ti=0,int ts=0):id(ti),score(ts) {
    }
		static void findMax(Student & st); // Looking for the highest grades and student numbers 
		static int getMaxScore()// Return to the highest score 
		{
    
			return maxscore;
		}
		 
		static int getMaxID()// Return the student number with the highest score 
		{
    
			return maxid;
		}	
		void set()
		{
    
			maxscore=score;
			maxid=id;
		}
		void print()
		{
    
			cout<<maxid<<"--"<<maxscore<<endl;
		}
};
int Student::maxscore=0;
int Student::maxid=0;
void Student::findMax(Student &st)
{
    
	if(maxscore<st.score) 
	{
    
		st.set();
	}
	
}
int main()
{
    
	int n;
	cin>>n;
	int id1,sco1;
	Student *q=new Student[n];
	for(int i=0;i<n;i++)
	{
    
		cin>>id1>>sco1;
		q[i]=Student(id1,sco1); 
	}
	q[0].set();
	for(int i=0;i<n-1;i++)
	{
    
		for(int j=i+1;j<n;j++)
		{
    
			q[i].findMax(q[j]);
		}
	}
	q[0].print();
	delete []q;
}
原网站

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