東北大學秦皇島分校通訊工程中外合作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.
相關文章
- 2018CCPC秦皇島站總結
- 實驗八 程式間通訊
- C# 通過socket實現UDP 通訊C#UDP
- 輕量級C#網路通訊元件StriveEngine —— C/S通訊開源demo(附原始碼)C#元件原始碼
- 中級通訊工程師證書會過期嗎工程師
- C語言實現TCP通訊C語言TCP
- C# NModbus RTU通訊實現C#
- 通訊安全重重考驗,阿里雲通訊如何打造企業級“安全感”?阿里
- HTTPS通訊的C++實現HTTPC++
- 如何實現 “defer”:Go vs Java vs C/CPPGoJava
- 通訊錄管理系統(C++實現)C++
- 汽車智聯網實驗五:序列通訊建模
- c++ 中.h 和.cppC++
- WebRTC音訊通話升級為視訊通話Web音訊
- 中級通訊工程師考試,你想了解的都在這裡工程師
- 8┃音視訊直播系統之 WebRTC 信令系統實現以及通訊核心並實現視訊通話Web
- C語言開發東北大學20級大作業—活力長者社群(附原始碼)C語言原始碼
- C#實現ADH815通訊C#
- c#實現最簡單的socket通訊C#
- 打工筆記--------------------------c#實現串列埠通訊筆記C#串列埠
- C#實現掃碼槍串列埠通訊C#串列埠
- 備考進行時!2020年中級通訊工程師傳輸與接入(無線)考試大綱工程師
- 【iCore3 雙核心板_FPGA】實驗十七:基於I2C匯流排的ARM與FPGA通訊實驗FPGA
- C# 串列埠通訊C#串列埠
- C# kvaser can 通訊C#
- ROS通訊方式(保姆級教程)ROS
- 【基礎島·第3關】浦語提示詞工程實踐
- QT:用QWebSocket實現webchannel,實現C++與HTML通訊QTWebC++HTML
- C/S通訊模型與B/S通訊模型介紹模型
- 網路通訊2:TCP通訊實現TCP
- C# 實現socket通訊程式(伺服器端)C#伺服器
- 基於OpenSSL的HTTPS通訊C++實現HTTPC++
- P2P通訊原理與實現(C++)C++
- 單核心,ospf子公司通訊詳細實驗步驟單核
- 【GMT43智慧液晶模組】例程三:CAN通訊實驗
- 程式間的8種通訊方式
- 實驗8.Vlan Hybrid實驗
- vue工程利用pubsub-js實現兄弟元件之間的通訊VueJS元件