東北大學秦皇島分校通訊工程中外合作2020級C/CPP實驗8
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.
相關文章
- 秦皇島開票-秦皇島開票
- 2020CCPC秦皇島補題
- 秦皇島開票
- ccpc2020秦皇島Kingdom‘s Power(貪心)
- 差分:2020CCPC秦皇島 D Exam Results
- 關於秦皇島哪裡可以開餐飲發票-秦皇島百度派
- 2017秦皇島CCPC E - String of CCPC
- 2018CCPC秦皇島站總結
- The 2017 CCPC秦皇島站 L- One-Dimensional Maze
- 誰知道秦皇島哪裡可以開飛機票哪裡可以買到假飛機票報銷哪裡可以做假的飛機票-秦皇島攜程網
- cpp 實驗5
- cpp 實驗4
- C語言開發東北大學20級大作業—活力長者社群(附原始碼)C語言原始碼
- 會戰港城!動感地帶5G校園先鋒賽河北賽區秦皇島城市晉級賽即將來臨
- 港城戰罷新秀崛起!動感地帶5G校園先鋒賽河北賽區秦皇島城市晉級賽圓滿結束
- 工程數學實驗5
- 工程數學實驗四
- 2020-10-8學習C
- 6.15 工程數學實驗三
- 6.15 工程數學實驗四
- 6.15 工程數學實驗二
- 6.15 工程數學實驗一
- 實驗八 程式間通訊
- 中級通訊工程師證書會過期嗎工程師
- 初倪人工之智慧殺手鐧,北大院士打破資訊孤島
- C# NModbus RTU通訊實現C#
- C語言實現TCP通訊C語言TCP
- HTTPS通訊的C++實現HTTPC++
- 如何實現 “defer”:Go vs Java vs C/CPPGoJava
- 實在智慧攜手東北大學,共同培養數字化轉型人才
- 通訊安全重重考驗,阿里雲通訊如何打造企業級“安全感”?阿里
- 2024/6/5 工程數學 實驗四
- FM-RDS工廠/鋼鐵廠無線廣播-秦皇島佰工鋼鐵有限公司無線智慧廣播應用分析
- 《憶秦娥·婁山關》————毛澤東
- C#實現ADH815通訊C#
- c++ 中.h 和.cppC++
- Java工程師【2020升級版】Java工程師
- 中級通訊工程師考試,你想了解的都在這裡工程師