稀疏矩阵三元组存储 c实现
1 #include 2 #include 3 typedef struct Triple 4 { 5 int row,column; 6 float value; 7 }Triple; 8 struct Triple TripleTable[36]; 9 struct Triple TransposeTable[36]; 10 float matrix[6][6]; 11 float transposeMatrix[6][6]; 12 int POS[7]; 13 int NUM[7]; 14 int main() 15 { 16 printf("******************************************************************************\n"); 17 printf("*****************************welcome******************************************\n"); 18 printf("******************************************************************************\n"); 19 printf("This code is used for operating sparse matrix.\n"); 20 printf("******************************************************************************\n"); 21 int i,j,row,column; 22 for(i = 0; i < 6; i++) 23 for(j = 0;j < 6;j++) 24 matrix[i][j] = 0; 25 printf("Now please input the number of your matrix's rows and columns:\n"); 26 scanf("%d",&row); 27 scanf("%d",&column); 28 printf("******************************************************************************\n"); 29 printf("OK.Your matrix has %d rows and %d columns.\nNext please input the matrix line by line:\n",row,column); 30 TripleTable[0].row = row; 31 TripleTable[0].column = column; 32 TripleTable[0].value = 0.0; 33 for(i = 0; i < row; i++) 34 for(j = 0;j < column;j++) 35 { 36 scanf("%f",&matrix[i][j]); 37 if(matrix[i][j]!=0) 38 { 39 TripleTable[0].value++; 40 NUM[i+1]++; 41 TripleTable[(int)TripleTable[0].value].row = i+1; 42 TripleTable[(int)TripleTable[0].value].column = j+1; 43 TripleTable[(int)TripleTable[0].value].value = matrix[i][j]; 44 } 45 46 } 47 printf("******************************************************************************\n"); 48 printf("Your matrix is:\n"); 49 for(i = 0; i < row; i++) 50 for(j = 0;j < column;j++) 51 { 52 if(j==0) 53 printf("\n"); 54 printf("%f ",matrix[i][j]); 55 } 56 printf("\n******************************************************************************\nAnd the tripletable is:\n"); 57 for(i = 0; i < ((int)TripleTable[0].value)+1; i++) 58 { 59 printf("\n"); 60 printf("%d ",TripleTable[i].row); 61 printf("%d ",TripleTable[i].column); 62 printf("%f ",TripleTable[i].value); 63 } 64 printf("\n******************************************************************************\nThe NUM is:\n"); 65 NUM[0] = 2; 66 for(i = 1; i < row+1 ;i++) 67 printf("NUM(%d)=%d ",i,NUM[i]); 68 POS[0] = 0; 69 printf("\n******************************************************************************\nThe POS is:\n"); 70 for(i = 1;i < row+1;i++) 71 { 72 POS[i] = POS[i-1] + NUM[i-1]; 73 printf("POS(%d)=%d ",i,POS[i]); 74 } 75 for(i = 0; i < ((int)TripleTable[0].value)+1; i++) 76 { 77 if(i==0) 78 { 79 TransposeTable[i].column = TripleTable[i].column; 80 TransposeTable[i].row = TripleTable[i].row; 81 TransposeTable[i].value = TripleTable[i].value; 82 } 83 else 84 { 85 TransposeTable[i].row = TripleTable[i].column; 86 TransposeTable[i].column = TripleTable[i].row; 87 TransposeTable[i].value = TripleTable[i].value; 88 } 89 } 90 91 printf("\n******************************************************************************\nThe transposetable is:\n"); 92 for(i = 0; i < (int)TransposeTable[0].value+1; i++) 93 { 94 printf("\n"); 95 printf("%d ",TransposeTable[i].row); 96 printf("%d ",TransposeTable[i].column); 97 printf("%f ",TransposeTable[i].value); 98 if(i!=0) 99 { 100 transposeMatrix[TransposeTable[i].row][TransposeTable[i].column] = TransposeTable[i].value; 101 } 102 } 103 printf("\n******************************************************************************\nThe transposematrix is:\n"); 104 for(i = 0;i < TransposeTable[0].row;i++) 105 for(j = 0;j < TransposeTable[0].column;j++) 106 { 107 if(j==0) 108 printf("\n"); 109 printf("%f ",transposeMatrix[i][j]); 110 } 111 printf("\n*********************************Thanks***************************************\n"); 112 return 0; 113 }