1 #define _CRT_SECURE_NO_WARNINGS 2 #include <iostream> 3 #define _CRT_SECURE_NO_WARNINGS 4 using namespace std; 5 6 int arr[11][11]; 7 8 struct TripleArray 9 { 10 int row; 11 int col; 12 int val; 13 }; 14 //三元表 15 16 int main() 17 { 18 int row = 1; 19 int col = 0; 20 21 //文件读取矩阵操作 22 FILE* fp = fopen("text.txt", "r"); 23 int a = fgetc(fp); 24 while (a != EOF) 25 { 26 if (a != ' ' && a != '\n') 27 { 28 int sum = a - '0'; 29 a = fgetc(fp); 30 while (a - '0' >= 0 && a - '0' <= 9) 31 { 32 sum = sum * 10 + a - '0'; 33 a = fgetc(fp); 34 } 35 col++; 36 arr[row][col] = sum; 37 } 38 else if (a == '\n') 39 { 40 row++; 41 col = 0; 42 } 43 a = fgetc(fp); 44 } 45 46 int num = 1; 47 TripleArray Mari[200]; 48 //三元数组 49 50 for (int i = 1; i <= row; i++) 51 { 52 for (int j = 1; j <= col; j++) 53 { 54 if (arr[i][j] != 0) 55 { 56 Mari[num].col = j; 57 Mari[num].row = i; 58 Mari[num].val = arr[i][j]; 59 num++; 60 } 61 } 62 } 63 64 cout << "三元组是:" << endl; 65 for (int i = 1; i < num; i++) 66 cout << Mari[i].row << " " << Mari[i].row << " " << Mari[i].val << endl; 67 68 cout << "矩阵转置" << endl; 69 for (int i = 1; i < num; i++) 70 { 71 int t = Mari[i].col; 72 Mari[i].col = Mari[i].row; 73 Mari[i].row = t; 74 } 75 76 //可以选择将矩阵按行排序一下 77 }
标签:fp,11,存储,SECURE,int,矩阵,三元组 From: https://www.cnblogs.com/saucerdish/p/17793034.html