東北大學秦皇島分校通訊工程中外合作2020級C/CPP實驗8

-NEBULA-發表於2020-12-10

1.閱讀並分析下面的程式,說明類的功能,並在電腦中錄入、執行進行驗證(要有執行的截圖)
1.Read and analyze the following program, interpret the functions of the class, and input it into computer, run it to check your conclusions

#include <iostream>
using namespace std;

class LinearEquation
{ 
public:
    LinearEquation(double newA, double newB, double newC, double newD, double newE, double newF);
    bool isSolvable();
    double getX();
    double getY();
private:
    double a, b, c, d, e, f;
};

LinearEquation::LinearEquation(double newA, double newB, double newC,double newD, double newE, double newF) 
{
    a = newA;
    b = newB;  
    c = newC;  
    d = newD;
    e = newE;  
    f = newF; 
}

bool LinearEquation::isSolvable() 
{
  return a * d - b * c != 0;
}

double LinearEquation::getX() 
{
    double x = (e * d - b * f) / (a * d - b * c);
    return x; 
}

double LinearEquation::getY() 
{
    double y = (a * f - e * c) / (a * d - b * c);
    return y;
}

int main( ) 
{
    cout << "Enter a, b, c, d, e, f: ";
    double a, b, c, d, e, f;
    cin >> a >> b >> c >> d >> e >> f;

    LinearEquation equation(a, b, c, d, e, f);

    if (equation.isSolvable()) 
	{
        cout << "x is " << equation.getX( ) << " and y is " << equation.getY( ) << endl;
    }
    else 
	{
        cout << "The equation has no solution" << endl;
    }
    return 0;
}

2.閱讀並分析下面的程式,說明類的功能,並在電腦中錄入、執行進行驗證(要有執行的截圖)
2.Read and analyze the following program, interpret the functions of the class, and input it into computer, run it to check your conclusions

#include <iostream>
using namespace std;

class myDate 
{
private:
	int year;
	int month;
	int day;
	
	bool isLeapYear(int y) const 
	{
		if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
			return true;
		else
			return false;
	}
	
	bool isLegalDate(int y, int m, int d) const 
	{
		if(m > 12 || m < 1 || d < 1 || d > 31)
			return false;
		else if(m == 2)
			if(!isLeapYear(y) && d > 28)
				return false;
			else if (isLeapYear(y) && d > 29)
				return false;
		else if((m == 4 || m == 6 || m == 9 || m == 11) && day > 30)
			return false;
		else 
			return true;
	}
	
public:
	myDate(int y = 2020, int m = 9, int d = 1) 
	{
		if(isLegalDate(y, m, d)) 
		{
			year  = y;
			month = m;
			day   = d;
		}
		else 
		{
			year  = 2020;
			month = 9;
			day   = 1;
		}
	}
	
	void setDate(int y, int m, int d) 
	{
		if(isLegalDate(y, m, d)) 
		{
			year  = y;
			month = m;
			day   = d;
		}
		else 
		{
			year  = 2020;
			month = 9;
			day   = 1;
		}	
	} 
	void outputDate( ) const 
	{
		cout << "(" << year << ", " << month << ", " << day << ")";
	}
};

int main( ) 
{
	myDate d1(2020, 1, 1);
	
	cout << "d1 is legal and d1 = ";
	d1.outputDate( );
	cout << endl;
	
	myDate d2(2020, 1, 32);
	cout << "\nd2 is illegal and the default d2 = ";
	d2.outputDate( );
	cout << endl;
	
	cout << "\nset d2 as a new date (2002, 1, 1), then d2 = ";
	d2.setDate(2002, 1, 1);
	d2.outputDate( );
	cout << endl;
	
	myDate d3(2020, 13, 10);
	cout << "\nd3 is illegal and the default d3 = ";
	d3.outputDate( );
	cout << endl;
	
	cout << "\nset d3 as a new date (2020, 2, 29), then d3 = ";
	d3.setDate(2020, 2, 29);
	d3.outputDate( );
	cout << endl;
	
	cout << "\nif set d3 as a illegal date (2019, 2, 29), then d3 = ";
	d3.setDate(2019, 2, 29);
	d3.outputDate( );
	cout << endl;
}

3.Design a class named QuadraticeEquation for a quadratic equation a x2 + b x + c = 0. The class contains:
(1)Data field a, b, and c that represent three coefficients (係數).
(2)A constructor for the arguments for a, b, and c.
(3)A function named getDiscriminant( ) that returns the discriminant (判別式), which is b2 - 4ac.
(4)The functions named getRoot1( ) and getRoot2( ) for returning two roots of the equation:
在這裡插入圖片描述

These functions are useful only if the discriminant is nonnegative. Let these functions return 0 if the discriminant is negative.
Finally, write main function and, in which, define a objects named equation, to test the class.

4.Write a class named Cylinder to describe a cylinder (圓柱體), which includes radius of the bottom circle and height. Please
(1) Adds constructor with default parameters’ value as (1.0, 1.0) to initialize the member data;
(2) Add a public member function named getSurfArea to return the surface area;
(3) Add a public member function named getVolume to return the volume.
Finally, write main function and, in which, define two objects named c1 and c2, to test the member functions you have added.

相關文章