| C++ 實習測試: 稀疏矩陣 (Sparse Matrix) 類別設計 |
| 時間: 110 分鐘 (09:20-11:10, 11:10 上傳時間截止) |
|
如上圖,概念上稀疏矩陣是一個很大的矩陣 (很多列、很多行) 但是裡面絕大部份數值都是 0
|
| 就是下面這個 C 程式了 (希望你沒有什麼程式恐懼症,它看起來比較長、有點討厭的原因應該是為了實作比較有效率的資料結構的關係,不過是程式而已,程式做的工作多,人需要做的工作就少,生活就愉快一點!) #include <stdio.h>
#include <malloc.h> // malloc(), free()
struct Entry
{
int row;
int col;
int value;
};
void printMatrix(struct Entry matrix[]);
void transposeMatrix(const struct Entry A[], struct Entry B[]);
void multiplyMatrix(const struct Entry A[], const struct Entry B[], struct Entry C[]);
int main()
{
int i;
struct Entry A[100]={0}, At[100]={0}, B[100]={0}, Bt[100]={0}, C[100]={0};
scanf("%d%d%d", &(A[0].row), &(A[0].col), &(A[0].value));
for (i=1; i<=A[0].value; i++)
scanf("%d%d%d", &(A[i].row), &(A[i].col), &(A[i].value));
A[i].row = -1; // extra entry for multiplyMatrix()
scanf("%d%d%d", &(B[0].row), &(B[0].col), &(B[0].value));
for (i=1; i<=B[0].value; i++)
scanf("%d%d%d", &(B[i].row), &(B[i].col), &(B[i].value));
B[i].row = -1; // extra entry for multiplyMatrix()
if ((A[0].row != B[0].row)||(A[0].col != B[0].col))
{
printf("Matrix A and B are not of the same dimension!\n");
return 1;
}
printf("\n");
printf("Matrix A:\n");
printMatrix(A);
printf("Matrix B:\n");
printMatrix(B);
transposeMatrix(B, Bt);
printf("Transpose Matrix B:\n");
printMatrix(Bt);
multiplyMatrix(A, Bt, C);
printf("Matrix A * Matrix B':\n");
printMatrix(C);
transposeMatrix(A, At);
printf("Transpose Matrix A:\n");
printMatrix(At);
multiplyMatrix(At, B, C);
printf("Matrix A' * Matrix B:\n");
printMatrix(C);
getchar(); getchar();
return 0;
}
void printMatrix(struct Entry matrix[])
{
int i;
printf("%d * %d matrix with %d non-zero item:\n",
matrix[0].row, matrix[0].col, matrix[0].value);
for (i=1; i<=matrix[0].value; i++)
printf("<%d,%d:%d>\n", matrix[i].row, matrix[i].col, matrix[i].value);
printf("====================\n");
}
void transposeMatrix(const struct Entry A[], struct Entry B[])
{
int i, iB=0, *start = (int *) malloc(sizeof(int)*(A[0].col+1));
B[iB].row = A[0].col; B[iB].col = A[0].row; B[iB++].value = A[0].value;
for (start[0]=1, i=1; i<=A[0].col; i++)
start[i] = 0;
for (i=1; i<=A[0].value; i++)
start[A[i].col+1]++; // counting element's in a row
for (i=1; i<=A[0].col; i++)
start[i] += start[i-1]; // accumulating
for (i=1; i<=A[0].value; i++)
{
iB = start[A[i].col]++;
B[iB].row = A[i].col;
B[iB].col = A[i].row;
B[iB].value = A[i].value;
}
free(start);
B[B[0].value+1].row = -1; // extra entry for multiply to test end of B1's row
}
void multiplyMatrix(const struct Entry A[], const struct Entry B[],
struct Entry C[])
{
int iA, iC, iA1, iB1, iA1row, iB1row, sum;
struct Entry *B1 = (struct Entry *)
malloc(sizeof(struct Entry)*(B[0].value+2));
transposeMatrix(B, B1);
C[0].row = A[0].row;
C[0].col = B[0].col;
C[0].value = 0;
iA = iC = 1;
while (iA <= A[0].value)
{
sum = 0;
iA1 = iA; iB1 = 1;
iA1row = A[iA1].row; iB1row = B1[iB1].row;
while (iB1 <= B1[0].value)
{
if (A[iA1].col == B1[iB1].col)
{
sum += A[iA1].value * B1[iB1].value;
iA1++; iB1++;
}
else if (A[iA1].col > B1[iB1].col)
iB1++;
else // if (A[iA1].col < B1[iB1].col)
iA1++;
if (A[iA1].row != iA1row)
while (B1[iB1].row == iB1row) iB1++;
if (B1[iB1].row != iB1row)
{
if (iB1 <= B1[0].value)
iA1 = iA;
else
while (A[iA1].row == iA1row) iA1++;
if (sum!=0)
{
C[0].value++;
C[iC].row = iA1row;
C[iC].col = iB1row;
C[iC++].value = sum;
sum = 0;
}
iB1row = B1[iB1].row;
}
}
iA = iA1;
}
free(B1);
}
|
輸入測試資料3 5 7 0 1 3 0 3 1 1 0 1 1 2 3 1 3 4 2 2 -2 2 4 2 3 5 7 0 0 3 0 1 1 1 0 -1 1 3 2 2 0 5 2 1 2 2 4 -1 程式輸出結果Matrix A: 下面是這次考試的詳細要求 |
|
考試時間: 110分鐘 (11:10 截止上傳,為了避免截止時間系統以及網路的延遲,請提早 10 分鐘上傳,確保你已經上傳一個版本了)
|
| 將所完成的 project (只需保留 .cpp, .h, .sln 以及 .vcxproj 檔案即可; 刪除掉 .suo, .sdf, .filters, .users, debug\ 資料匣, 以及 ipch\ 資料匣下的所有內容) 以 zip/rar/7zip 程式將整個資料匣壓縮起來, 在作業與實習繳交頁面中,選擇 「2018-03-27 2A考試一」 上傳 |
範例程式碼 我把上傳的截止時間重設為 3/28 (三) 21:00 如果你覺得實習課時時間不夠,可以再點加油把它完成。 |
|
回
C++ 物件導向程式設計課程 首頁 製作日期: 03/27/2018 by 丁培毅 (Pei-yih Ting) E-mail: pyting@mail.ntou.edu.tw TEL: 02 24622192x6615 海洋大學 電機資訊學院 資訊工程學系 Lagoon |