寫出 C 函數 read_matrix,print_matrix 和 search,他們分別讀取三項式到一個新的稀疏矩陣,印出稀疏矩陣中的元素,以及在稀疏矩陣中找尋一個值。
稀疏矩陣的輸入,輸出,搜尋及轉置
課本p77,EX.1 read matrix, print matrix, search 及 sparse matrix 的轉置(transpose) 轉置的演算法(algorithm)可以參考課本 p71 program2.7 或 p62 program 2.8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
#include<stdio.h> #define MAX_TERMS 101 #define MAX_COL 50 /* maximun number of columns + 1 */ typedef struct { int col; int row; int value; } term; term a[MAX_TERMS]; term b[MAX_TERMS]; void read_matrix(int maximun); void print_matrix(int maximun); void search_matrix(int maximun); void fast_transpose(); int main(void) { int i, maximun=0; for(i = 0; i < MAX_TERMS; i++) { a[i].col = 0; a[i].row = 0; a[i].value = 0; } while(i != 0) { printf("1). 輸入稀疏矩陣\n"); printf("2). 列印稀疏矩陣\n"); printf("3). 搜尋矩陣元素\n"); printf("4). 轉置稀疏矩陣\n"); printf("0). 離開\n"); printf("請選擇選項: "); scanf("%d", &i); switch(i) { case 1: printf("請問您想輸入的稀疏矩陣的元素個數: "); scanf("%d", &maximun); read_matrix(maximun); break; case 2: print_matrix(maximun); break; case 3: search_matrix(maximun); break; case 4: fast_transpose(); break; default: break; } printf("\n\n\n"); } return 0; } void read_matrix(int maximun) { int i; for(i = 0; i < maximun; i++) { printf("請問該元素位於第幾 row : "); scanf("%d" ,&a[i].row); printf("請問該元素位於第幾 column: "); scanf("%d" ,&a[i].col); printf("請問該元素位的值: "); scanf("%d" ,&a[i].value); if(i<maximun-1) printf("請繼續輸入下一個元素..\n"); } printf("輸入完成\n"); } void print_matrix(int maximun) { printf("列印矩陣..\n"); printf("矩陣元素 row col value \n"); for(int i = 0; i < maximun; i++) printf(" a[%d] %d %d %d\n", i , a[i].row, a[i].col, a[i].value); } void search_matrix(int maximun) { int num; bool judge = 0; printf("請輸入您想搜尋的元素,本程式會列出他的 row 和 col 值\n"); printf("想搜尋的數字為: "); scanf("%d", &num); for(int i = 0; i < maximun; i++) if(a[i].value == num) { printf("a[%d], row = %d, col = %d, value= %d\n", i , a[i].row, a[i].col, a[i].value); judge = 1; } if(!judge) printf("抱歉,搜尋沒有結果\n"); } void fast_transpose() { /* the transpose of a is placed in b */ int row_terms[MAX_COL], starting_pos[MAX_COL]; int i, j, num_cols = a[0].col, num_terms = a[0].value; b[0].row = num_cols; b[0].col = a[0].row; b[0].value = num_terms; if(num_terms > 0) { for(i = 0; i < num_cols; i++) row_terms[i] = 0; for(i = 1; i <= num_terms; i++) row_terms[a[i].col]++; starting_pos[0] = 1; for(i = 1; i < num_cols; i++) starting_pos[i] = starting_pos[i-1] + row_terms[i-1]; for(i = 1; i <= num_terms; i++) { j = starting_pos[a[i].col]++; b[j].row = a[i].col; b[j].col = a[i].row; b[j].value = a[i].value; } } printf("轉置矩陣完成"); } |
轉不出來 好奇怪
輸出結果還是跟原本一樣…..
這是怎麼回事?