Lab 4-2: Complex Number Calculation (35%)
- 輸入:
- 以
double
格式輸入複數的實數及虛數部分,以空格分開,一行輸入一個複數 - 以
char
格式輸入複數的運算子,包含+
、-
、*
、/
,一行輸入一個運算子 - 複數與運算子以交錯的方式輸入,一行輸入一個複數接著一個運算子,倒數兩行為複數與完成輸入指令
- 輸入 Ctrl+D 完成輸入
- Windows 請輸入 Ctrl+Z (會在螢幕上顯示
^Z
) 再輸入 Enter (Format 中的⏎
) 完成輸入
- Windows 請輸入 Ctrl+Z (會在螢幕上顯示
- 以
- 輸出:
- 顯示運算複數的結果
- 檔名:
lab4-2_<學號>.cpp
(e.g.lab4-2_106062802.cpp
)
注意事項:
- 程式不會輸出任何使用者提示,只會輸出程式結果
- 使用者不需要處理錯誤輸入
- 請使用 pseudo code 提供的 main function 來處理輸入與輸出
- 程式需要於 10 秒內完成,所有的測資皆會保證於 10 秒內完成
Format
<real 1> <imag 1>⏎
<op 1>⏎
<real 2> <imag 2>⏎
<op 2>⏎
...
<real n-1> <imag n-1>⏎
<op n-1>⏎
<real n> <imag n>⏎
^Z⏎
<real result> + <imag result> i
Example
$ ./a.out
1.0 0.0⏎
^Z⏎
1 + 0 i
$
$ ./a.out
1.0 0.0⏎
+⏎
2.0 1.1⏎
-⏎
-3.0 -2.2⏎
*⏎
4.3 2.1⏎
/⏎
-1.2 -3.4⏎
^Z⏎
-8.74846 + 2.46231 i
$
Pseudo Code
#include <iostream>
#include <string>
#include <sstream>
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();
// add function
// subtract function
// multiply function
// divide function
};
int main()
{
string input;
Complex result_complex;
char op = ' ';
while (getline(cin, input))
{
// input is a operation
if (input == "+" || input == "-" || input == "*" || input == "/")
{
op = input[0];
continue;
}
// input is a complex number
else
{
stringstream ss(input);
double real, imag;
ss >> real >> imag;
Complex current_complex(real, imag);
switch (op)
{
case '+':
result_complex = result_complex.add(current_complex);
break;
case '-':
result_complex = result_complex.sub(current_complex);
break;
case '*':
result_complex = result_complex.mul(current_complex);
break;
case '/':
result_complex = result_complex.div(current_complex);
break;
case ' ':
result_complex = current_complex;
break;
default:
cerr << "Error: unknown operation" << endl;
return 1;
}
}
}
result_complex.print();
return 0;
}
Reference Code:
陳光齊 (110021102)
#include <iostream>
#include <sstream>
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 = 0.0, double arg_imag = 0.0);
Complex(const Complex &arg_c);
// print function
// note: be careful about the format of output
// especially the ` ` and newline (endl)
void print() const;
double get_real() const;
double get_imag() const;
void set_real(double &real);
void set_imag(double &imag);
Complex add(const Complex &arg_c);
Complex sub(const Complex &arg_c);
Complex mul(const Complex &arg_c);
Complex div(Complex &arg_c);
Complex const_mul(double arg_x);
Complex conj();
};
Complex::Complex(double arg_real, double arg_imag)
: m_real(arg_real), m_imag(arg_imag)
{
}
Complex::Complex(const Complex &arg_c)
: m_real(arg_c.get_real()), m_imag(arg_c.get_imag())
{
}
void Complex::print() const
{
cout << m_real << " + " << this->m_imag << " i" << endl;
}
double Complex::get_real() const
{
return m_real;
}
double Complex::get_imag() const
{
return m_imag;
}
void Complex::set_real(double &real)
{
m_real = real;
}
void Complex::set_imag(double &imag)
{
m_imag = imag;
}
Complex Complex::add(const Complex &arg_c)
{
Complex c_temp;
c_temp.m_real = m_real + (arg_c.get_real());
c_temp.m_imag = m_imag + (arg_c.get_imag());
return c_temp;
}
Complex Complex::sub(const Complex &arg_c)
{
Complex c_temp;
c_temp.m_real = m_real - (arg_c.get_real());
c_temp.m_imag = m_imag - (arg_c.get_imag());
return c_temp;
}
Complex Complex::mul(const Complex &arg_c)
{
Complex c_temp;
c_temp.m_real = m_real * (arg_c.get_real()) - m_imag * (arg_c.get_imag());
c_temp.m_imag = m_imag * (arg_c.get_real()) + m_real * (arg_c.get_imag());
return c_temp;
}
Complex Complex::div(Complex &arg_c)
{
double c = arg_c.get_real();
double d = arg_c.get_imag();
Complex c_this(m_real, m_imag);
Complex c_temp;
arg_c = arg_c.conj();
c_temp = c_this.mul(arg_c);
c = c * c + d * d;
c = 1 / c;
c_temp = c_temp.const_mul(c);
return c_temp;
}
Complex Complex::const_mul(double arg_x)
{
Complex c_temp;
c_temp.m_real = m_real * arg_x;
c_temp.m_imag = m_imag * arg_x;
return c_temp;
}
Complex Complex::conj()
{
Complex c_temp;
c_temp.m_real = m_real;
c_temp.m_imag = -m_imag;
return c_temp;
}
int main()
{
string input;
Complex result_complex;
char op = ' ';
while (getline(cin, input))
{
// input is a operation
if (input == "+" || input == "-" || input == "*" || input == "/")
{
op = input[0];
continue;
}
// input is a complex number
else
{
stringstream ss(input);
double real, imag;
ss >> real >> imag;
Complex current_complex(real, imag);
switch (op)
{
case '+':
result_complex = result_complex.add(current_complex);
break;
case '-':
result_complex = result_complex.sub(current_complex);
break;
case '*':
result_complex = result_complex.mul(current_complex);
break;
case '/':
result_complex = result_complex.div(current_complex);
break;
case ' ':
result_complex = current_complex;
break;
default:
cerr << "Error: unknown operation" << endl;
return 1;
}
}
}
result_complex.print();
return 0;
}
賴杰弘 (110021118)
#include <iostream>
#include <string>
#include <sstream>
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();
// add function
Complex add(const Complex &arg_complex);
// subtract function
Complex sub(const Complex &arg_complex);
// multiply function
Complex mul(const Complex &arg_complex);
// divide function
Complex div(const Complex &arg_complex);
};
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;
}
Complex Complex::add(const Complex &arg_complex)
{
Complex complex;
complex.m_real = m_real + arg_complex.m_real;
complex.m_imag = m_imag + arg_complex.m_imag;
return complex;
}
Complex Complex::sub(const Complex &arg_complex)
{
Complex complex;
complex.m_real = m_real - arg_complex.m_real;
complex.m_imag = m_imag - arg_complex.m_imag;
return complex;
}
Complex Complex::mul(const Complex &arg_complex)
{
Complex complex;
complex.m_real = (m_real * arg_complex.m_real) - (m_imag * arg_complex.m_imag);
complex.m_imag = (m_real * arg_complex.m_imag) + (m_imag * arg_complex.m_real);
return complex;
}
Complex Complex::div(const Complex &arg_complex)
{
Complex complex;
complex.m_real = ((m_real * arg_complex.m_real) + (m_imag * arg_complex.m_imag))
/ ((arg_complex.m_real * arg_complex.m_real) + (arg_complex.m_imag * arg_complex.m_imag));
complex.m_imag = ((m_imag * arg_complex.m_real) - (m_real * arg_complex.m_imag))
/ ((arg_complex.m_real * arg_complex.m_real) + (arg_complex.m_imag * arg_complex.m_imag));
return complex;
}
int main()
{
string input;
Complex result_complex;
char op = ' ';
while (getline(cin, input))
{
// input is a operation
if (input == "+" || input == "-" || input == "*" || input == "/")
{
op = input[0];
continue;
}
// input is a complex number
else
{
stringstream ss(input);
double real, imag;
ss >> real >> imag;
Complex current_complex(real, imag);
switch (op)
{
case '+':
result_complex = result_complex.add(current_complex);
break;
case '-':
result_complex = result_complex.sub(current_complex);
break;
case '*':
result_complex = result_complex.mul(current_complex);
break;
case '/':
result_complex = result_complex.div(current_complex);
break;
case ' ':
result_complex = current_complex;
break;
default:
cerr << "Error: unknown operation" << endl;
return 1;
}
}
}
result_complex.print();
return 0;
}