科目名稱:資訊系統 與網路導論 | 開課系所:資訊工程 學系 | 任課教師 |
吳坤熹 |
||
系所別: |
年級: |
學號: |
姓名: |
考試日期 |
2011.1.5 |
Open book; turn off computer & mobile phone
(考試時間: 16:30-16:45)
// Ex7_01.cpp
#include <iostream>
using std::cout;
using std::endl;
// Definition of a struct to represent rectangles
struct RECTANGLE
{
int Left; // Top-left point
int Top; // coordinate pair
int Right; // Bottom-right point
int Bottom; // coordinate pair
};
long Area(const RECTANGLE aRect);
void MoveRect(RECTANGLE aRect, int x, int y);
int main(void)
{
RECTANGLE Hut1, Hut2;
Hut1.Left = 70;
Hut1.Top = 10;
Hut1.Right = Hut1.Left + 25;
Hut1.Bottom = 30;
Hut2 = Hut1; // Define Hut2 the same as Hut1
MoveRect(Hut2, 10, 90); // Now move it to the right position
cout << "Coordinates of Hut2 are "
<< Hut2.Left << "," << Hut2.Top << " and "
<< Hut2.Right << "," << Hut2.Bottom;
cout << endl
<< "The area of Hut2 is "
<< Area(Hut2) << endl;
return 0;
}
// Function to calculate the area of a rectangle
long Area(const RECTANGLE aRect)
{ return (aRect.Right - aRect.Left)*(aRect.Bottom - aRect.Top);
}
// Function to Move a Rectangle
void MoveRect(RECTANGLE aRect, int x, int y)
{
int length(aRect.Right - aRect.Left); // Get length of rectangle
int width(aRect.Bottom - aRect.Top); // Get width of rectangle
aRect.Left = x;
aRect.Top = y; // to new position
aRect.Right = x + length; // Get bottom-right point as
aRect.Bottom = y + width; // increment from new position
return;
}
(10%) Determine
whether the following code has syntax erros or not. If it is correct,
predict its output. If it is incorrect, point out the mistake(s).
// Ex7_01.cpp
// Exercising structures in the yard
#include <iostream>
using std::cout;
using std::endl;
// Definition of a struct to represent rectangles
struct ListElement
{
int value;
ListElement* pNext;
};
int main(void)
{
ListElement LE5 = { 50, 0 };
ListElement LE4 = { 40, &LE5 };
ListElement LE3 = { 30, &LE4 };
ListElement LE2 = { 20, &LE3 };
ListElement LE1 = { 10, &LE2 };
ListElement* p = &LE1;
while (p != NULL)
{
cout << p->value << endl;
p = p->pNext;
}
return 0;
}