科目名稱:程式設計 | 開課系所:資訊工程 學系 | 考試日期 | 2017.6.21 | ||
系所別: |
年級: |
學號: |
姓名: |
考試時間 | 08:30-10:30 |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 |
// Transposition Cipher - Scytale
// The input key specifies the diameter of the scytale
#include <iostream>
#include <cstring>
#include <cmath>
using std::cout;
using std::endl;
#define MAX_WIDTH 9
#define MAX_MESSAGE_LENGTH 200
void decrypt(char* ciphertext, char* plaintext, int key);
int main()
{
char matrix[MAX_WIDTH * MAX_WIDTH + 1];
char plaintext[ MAX_MESSAGE_LENGTH];
char ciphertext[ MAX_MESSAGE_LENGTH ] =
"H crfaoptsrcwr eok oylmettof roeu hs";
char *p = plaintext;
unsigned key = 6;
decrypt(ciphertext, plaintext, key);
std::cout << plaintext << '\n';
return 0;
}
void decrypt(char* ciphertext, char* plaintext, int height)
{
char* pc = plaintext;
int i, j, offset = 0;
int length = strlen(ciphertext);
int width = ceil(1.0 * length / height);
for (j=0; j<width; j++)
for (i=0; i<height; i++)
{
plaintext[i * width + j] = ciphertext[i + height * j] ;
}
plaintext[ width * height ] = '\0';
}
(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).
// Static data members (P.311)
#include <iostream>
using std::cout;
using std::endl;
class CNode
{
public:
int value;
static CNode* previous;
CNode* pLastNode;
CNode(int v) :
value(v)
{ previous = NULL; pLastNode = this; }
};
CNode* CNode::previous = NULL;
int main()
{
CNode a(3), b(5), c(7), d(9);
CNode* p;
int n = 0;
for (n=0, p=d.pLastNode; p!=NULL; p=p->previous)
n++;
cout << n << endl;
return 0;
}
(10%) If we replace the OnDraw() function in P.669 as below, what result will be shown on the screen after you run the program?
// CView::OnDraw() (P.669)
void CSketcherView::OnDraw(CDC* pDC)
{
CSketcherDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
const float R = 200.0f;
const int x0 = R, y0 = R;
int degree;
float theta;
const CPoint origin = CPoint(x0, y0);
CPoint points[12];
for (int i=0; i<12; i++) {
degree = 30 * i;
theta = degree * M_PI / 180;
points[i] = CPoint(R*cos(theta), R*sin(theta));
}
pDC->MoveTo( points[0] + origin );
for (int i=1; i<=12; i++) {
pDC->LineTo( points[5*i % 12] + origin );
}
}
(10%) We want to develop an MFC program which allows the user to input a message and then print out the encrypted Caesar cipher.
However, the following code cannot be compiled successfully. Please point out which line of the code must be revised, and how should it be revised.// GetWindowText()
CString encrypt(CString s, int key)
{
CString result = _T("");
char c;
for (int i=0; i<s.GetLength(); i++)
{
c = s.GetAt(i) + key;
result = result + c;
}
return result;
}
void CF10View::OnBnClickedButton1()
{
CString message, cipher;
message = GetDlgItem(IDC_TXT_MESSAGE)->GetWindowText( );
cipher = encrypt(message, 3);
GetDlgItem(IDC_TXT_CIPHER)->SetWindowText( cipher );
}