科目名稱:程式設計 | 開課系所:資訊工程 學系 | 任課教師 |
吳坤熹 |
||
系所別: |
年級: |
學號: |
姓名: |
考試日期 |
2008.4.17 |
(考試時間: 8:10-8:30)
Open book; turn off computer & mobile phone
#include <iostream>
using std::cout;
using std::endl;
class Complex
{
double Re; // real part
double Im; // imaginary part
public:
Complex(double a=0.0, double b=0.0):
Re(a), Im(b) { }
Complex operator* (Complex b)
{
return Complex(Re*b.Re - Im*b.Im, Re*b.Im + Im*b.Re);
}
void Print(void)
{
cout << Re;
if (Im > 0)
cout << "+" << Im << "i";
else
if (Im<0)
cout << Im << "i";
cout << endl;
}
};
void main()
{
Complex a(0.0, 1.0);
Complex b(0.0, -1.0);
Complex c;
a.Print();
b.Print();
c = a * b;
c.Print();
// Note: "cout << 0.0 << 1.0;" will merely print out "01".
}
#include <iostream>
using std::cout;
using std::endl;
class Node
{
private:
int value;
Node* left;
Node* right;
public:
void Inorder(void)
{
if (this->left)
this->left->Inorder();
cout << value;
if (this->right)
this->right->Inorder();
}
void Preorder(void)
{
cout << value;
if (this->left)
this->left->Preorder();
if (this->right)
this->right->Preorder();
}
void Postorder(void)
{
if (this->left)
this->left->Postorder();
if (this->right)
this->right->Postorder();
cout << value;
}
Node(int v, Node* L = 0, Node* R = 0) // Constructor
: value(v), left(L), right(R)
{ }
friend Node* init(void); // Friend function (P.354)
};
Node* init(void)
{
Node* three = new Node(3);
Node* five = new Node(5);
Node* six = new Node(6, five, three);
Node* two = new Node(2);
Node* seven = new Node(7, two, six);
Node* four = new Node(4);
Node* nine = new Node(9, four);
Node* eight = new Node(8, nine);
Node* one = new Node (1, seven, eight);
return one;
}
void main()
{
Node* root = init() ;
root->Inorder(); cout << endl;
root->Preorder(); cout << endl;
root->Postorder(); cout << endl;
}