Lab 4: Complex Number
Lab 4-1: Complex Numbers (50%)
- 輸入:
- 以
double
格式輸入複數的實數及虛數部分,以空格分開 - 一行輸入一個複數
- 輸入 Ctrl+D 完成輸入
- Windows 請輸入 Ctrl+Z (會在螢幕上顯示
^Z
) 再輸入 Enter 完成輸入
- Windows 請輸入 Ctrl+Z (會在螢幕上顯示
- 以
- 輸出:
- 顯示複數儲存 Complex Class 的數字
- 一行輸入跟著輸出一個複數
- 檔名:
lab4-1_<學號>.cpp
(e.g.lab4-1_106062802.cpp
)
注意事項:
- 程式不會輸出任何使用者提示,只會輸出程式結果
- 請使用 pseudo code 提供的 main function 來處理輸入與輸出
- 程式需要於 10 秒內完成,所有的測資皆會保證於 10 秒內完成
Format
<real 1> <imag 1>⏎
<real 1> + <imag 1> i
<real 2> <imag 2>⏎
<real 2> + <imag 2> i
...
<real n> <imag n>⏎
<real n> + <imag n> i
^Z⏎
Example
$ ./a.out
1.0 0.0⏎
1 + 0 i
2.0 1.1⏎
2 + 1.1 i
-3.0 -2.2⏎
-3 + -2.2 i
^Z⏎
$
Pseudo Code
#include <iostream>
using namespace std;
class Complex
{
private:
// data members
// save the real and imaginary parts of the complex number
// with `double` precision
public:
// Constructor, initializes real and imaginary parts
// hint: as like as `modify` function in examples
// but use default constructor to implement
// print function
// note: be careful about the format of output
// especially the ` ` and newline (endl)
void print();
};
int main()
{
double real = 0.0, imag = 0.0;
while (cin >> real >> imag)
{
Complex c(real, imag);
c.print();
}
return 0;
}
Reference Code:
陳光齊 (110021102)
#include <iostream>
using namespace std;
class Complex
{
private:
// data members
// save the real and imaginary parts of the complex number
// with `double` precision
double m_real;
double m_imag;
public:
// Constructor, initializes real and imaginary parts
// hint: as like as `modify` function in examples
// but use default constructor to implement
Complex(double arg_real, double arg_imag);
// print function
// note: be careful about the format of output
// especially the ` ` and newline (endl)
void print();
};
Complex::Complex(double arg_real, double arg_imag)
: m_real(arg_real), m_imag(arg_imag)
{
}
void Complex::print()
{
cout << m_real << " + " << this->m_imag << " i" << endl;
}
int main()
{
double real = 0.0, imag = 0.0;
while (cin >> real >> imag)
{
Complex c(real, imag);
c.print();
}
return 0;
}
賴杰弘 (110021118)
#include <iostream>
using namespace std;
class Complex
{
private:
// data members
// save the real and imaginary parts of the complex number
// with `double` precision
double m_real;
double m_imag;
public:
// Constructor, initializes real and imaginary parts
// hint: as like as `modify` function in examples
// but use default constructor to implement
Complex(const double &arg_real = 0.0, const double &arg_imag = 0.0);
// Copy Constructor
Complex(const Complex &arg_complex);
// Destructor
~Complex(){
// do nothing
};
// print function
// note: be careful about the format of output
// especially the ` ` and newline (endl)
void print();
};
Complex::Complex(const double &arg_real, const double &arg_imag)
: m_real(arg_real), m_imag(arg_imag)
{
}
Complex::Complex(const Complex &arg_complex)
: m_real(arg_complex.m_real), m_imag(arg_complex.m_imag)
{
}
void Complex::print()
{
cout << m_real << " + " << m_imag << " i" << endl;
return;
}
int main()
{
double real = 0.0, imag = 0.0;
while (cin >> real >> imag)
{
Complex c(real, imag);
c.print();
}
return 0;
}