标签:i++ mat pos cpp parse tran space pre div
SparsrMatrix.h
#pragma once #include<iostream> using namespace std; class Tri { public: int row; int col; int value; }; class SparseMatrix { public: int sRow; int sCol; int sTerm; Tri* elements;//to save terms. SparseMatrix(int r = 10, int c = 10, int term = 2) { sRow = r; sCol = c; sTerm = term; elements = new Tri[sTerm]; } void transpose(SparseMatrix& B) { int index_B = 0; if (sTerm > 0) { for (int i = 0; i < sCol; i++) { for (int j = 0; j < sTerm; j++) { if (elements[j].col == i) { B.elements[index_B].col = elements[j].row; B.elements[index_B].row = elements[j].col; B.elements[index_B].value = elements[j].value; index_B++; } } } } } void show() { for (int i = 0; i < sTerm; i++) { cout << elements[i].row << ‘,‘ << elements[i].col << ‘,‘ << elements[i].value << endl; } } };
main.cpp
#include"SparseMatrix.h" int main() { SparseMatrix A, B; A.elements[0].row = 1; A.elements[0].col = 3; A.elements[0].value = 6; A.elements[1].row = 5; A.elements[1].col = 1; A.elements[1].value = 8; A.show(); cout << endl; cout << "----------------------------" << endl; cout << endl; A.transpose(B); B.show(); return 0; }
标签:i++ mat pos cpp parse tran space pre div
原文地址:https://www.cnblogs.com/SlowIsFast/p/12684498.html