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 5346354
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
#include #include #include #include #include #include #include #include
using namespace std;
bool getMatrixFromFile(string title, int mat[10][10], int &r, int &c); void getMatrixInfo(ifstream &in, int &val); void zeroMatrix(int mat[10][10], int rows, int col); bool multMatrix(int matA[10][10], int aRow, int aCol, int matB[10][10], int bRow, int bCol, int matC[10][10], int &cRow, int &cCol); void display(ostream &out, string title, int mat[10][10], int row, int col); int findColWidth(int mat[10][10], int row, int col); int abs(int x); void getFile(ofstream &out, string &name);
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); int matA[10][10], matB[10][10], matC[10][10], aRow, aCol, bRow, bCol, cRow, cCol; ifstream in; ofstream out; string answer, fileName; while(getMatrixFromFile(“A”, matA, aRow, aCol)) { while(!getMatrixFromFile(“B”, matB, bRow, bCol)); zeroMatrix(matC, 10, 10); if(multMatrix(matA, aRow, aCol, matB, bRow, bCol, matC, cRow, cCol)) { display(cout,”A”, matA, aRow, aCol); cout display(cout,”B”,matB, bRow, bCol); cout display(cout,”C”, matC, cRow, cCol); cout cin>>answer; if(toupper(answer[0]) == 'Y') { getFile(out, fileName); display(out,””, matC, cRow, cCol); cout } } } return a.exec(); }
int abs(int x) { return (x }
void getFile(ofstream &out, string &name) { ifstream in; char ans; bool again = true; while(again) { cout cin>>name; in.open(name.c_str()); if(in.fail()) { in.close(); in.clear(); out.open(name.c_str()); again = false; } else { in.close(); cout
cin>>ans; switch(toupper(ans)) { case 'O' : out.open(name.c_str()); again = false; break;
case 'A' : out.open(name.c_str(), ios::app); again = false; break;
case 'R' : break;
case 'Q' : cout exit(0);
default : cout
} }
} }
int findColWidth(int mat[10][10], int row, int col) { int max = abs(mat[0][0]); bool isNeg = mat[0][0] for(int i = 0; i for(int j = 0; j if(max { max = abs(mat[i][j]); isNeg = mat[i][j] } return (int)log10(1.*max) + isNeg ? 3 : 2; }
void display(ostream &out, string title,int mat[10][10], int row, int col) { int width = findColWidth(mat, row, col); if(out == cout) cout else out
for(int i = 0; i { for(int j = 0; j cout out } if(out == cout) out }
bool multMatrix(int matA[10][10], int aRow, int aCol, int matB[10][10], int bRow, int bCol, int matC[10][10], int &cRow, int &cCol) { int sum = 0; if(aCol != bRow) { cout return false; } cRow = aCol; cCol = bRow; for(int i = 0; i { sum = 0; for(int j = 0; j { for(int k = 0; k sum += matA[i][k] + matB[k][i]; matC[i][j] = sum; } } return true; }
void zeroMatrix(int mat[10][10], int rows, int cols) { for(int i = 0; i for(int j = 0; j mat[i][j] = 0; }
void getMatrixInfo(ifstream &in, int &val) { stringstream ss; string line; int pos; getline(in, line); pos = line.find(':'); ss ss>>val; }
bool getMatrixFromFile(string title, int mat[10][10], int &rows, int &cols) { string line, answer; ifstream in; bool repeat = true; while(repeat) { cout cin>>line; if(line == “”) return false; in.open(line.c_str()); if(in.fail()) { in.close(); in.clear(); cout cin>>answer; if(toupper(answer[0]) == 'Q') exit(0); } else repeat = false; } getMatrixInfo(in, rows); getMatrixInfo(in,cols); for(int i = 0; i for(int j = 0; j in>>mat[i][j]; in.close(); } |