类定义(包括数据成员和成员函数功能描述)#include<iostream> #include<fstream> #include<iomanip> using namespace std; double M[3][3]; double N[10][10]; bool ReadMatrix() { int i, j; ifstream Nfile("d:\N矩阵.txt"); if (!Nfile) return false; ifstream Mfile("d:\M矩阵.txt"); if (!Mfile) { Nfile.close(); return false; } for (i = 0;i < 10;i++) for (j = 0;j < 10;j++) Nfile >> N[i][j]; for (i = 0;i < 3;i++) for (j = 0;j < 3;j++) Mfile >> M[i][j]; Mfile.close(); Nfile.close(); return true; } double algorithms1(int I, int J) { double Mij, Nij; double a, b; int i, j, in, jn; a = 0; b = 0; for (i = 0;i <= 2;i++) for (j = 0;j <= 2;j++) { Mij = M[i][j]; in = I - i - 1; jn = J - j - 1; if (in < 0 || jn < 0 || in>9 || jn>9) Nij = 0; else Nij = N[in][jn]; a = a + Mij * Nij; b = b + Mij; } if (b != 0) return a / b; else return 0; } double algorithms2(int I, int J) { double Mij, Nij; double a, b; int i, j, in, jn; a = 0; b = 0; for (i = 0;i <= 2;i++) for (j = 0;j <= 2;j++) { Mij = M[i][j]; in = I - i - 1; jn = J - j - 1; if (in < 0 || jn < 0 || in>9 || jn>9) Nij = 0; else Nij = N[9 - in][9 - jn]; a = a + Mij * Nij; b = b + Mij; } if (b != 0) return a / b; else return 0; } int main() { int i, j; double v1, v2; char c; if (!ReadMatrix()) { cout << "打开文件出错,程序退出" << endl; return -1; } cout << "读入矩阵数据成功,请输入I:"; cin >> i; cout << endl << "请输入J:"; cin >> j; cout << "输入的I=" << i << "输入的J= " << j << endl; v1 = algorithms1(i, j); cout << "算法1的结果=" << v1 << endl; v2 = algorithms2(i, j); cout << "算法2的结果=" << v2 << endl; return 0; }
时间: 2024-01-31 22:03:04 浏览: 169
vc++2008编译不了#include<iostream.h>头文件
4星 · 用户满意度95%
这段代码中并没有定义任何类,只是一些全局变量和函数。如果要将其改为类定义,可以将全局变量M和N作为类的数据成员,并将ReadMatrix()、algorithms1()和algorithms2()作为类的成员函数。具体实现如下:
```c++
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
class MatrixCalculator {
private:
double M[3][3];
double N[10][10];
public:
bool ReadMatrix() {
int i, j;
ifstream Nfile("d:\N矩阵.txt");
if (!Nfile) return false;
ifstream Mfile("d:\M矩阵.txt");
if (!Mfile) {
Nfile.close();
return false;
}
for (i = 0;i < 10;i++)
for (j = 0;j < 10;j++)
Nfile >> N[i][j];
for (i = 0;i < 3;i++)
for (j = 0;j < 3;j++)
Mfile >> M[i][j];
Mfile.close();
Nfile.close();
return true;
}
double algorithms1(int I, int J) {
double Mij, Nij;
double a, b;
int i, j, in, jn;
a = 0;
b = 0;
for (i = 0;i <= 2;i++)
for (j = 0;j <= 2;j++) {
Mij = M[i][j];
in = I - i - 1;
jn = J - j - 1;
if (in < 0 || jn < 0 || in>9 || jn>9)
Nij = 0;
else
Nij = N[in][jn];
a = a + Mij * Nij;
b = b + Mij;
}
if (b != 0)
return a / b;
else
return 0;
}
double algorithms2(int I, int J) {
double Mij, Nij;
double a, b;
int i, j, in, jn;
a = 0;
b = 0;
for (i = 0;i <= 2;i++)
for (j = 0;j <= 2;j++) {
Mij = M[i][j];
in = I - i - 1;
jn = J - j - 1;
if (in < 0 || jn < 0 || in>9 || jn>9)
Nij = 0;
else
Nij = N[9 - in][9 - jn];
a = a + Mij * Nij;
b = b + Mij;
}
if (b != 0)
return a / b;
else
return 0;
}
};
int main() {
MatrixCalculator mc;
int i, j;
double v1, v2;
char c;
if (!mc.ReadMatrix()) {
cout << "打开文件出错,程序退出" << endl;
return -1;
}
cout << "读入矩阵数据成功,请输入I:";
cin >> i;
cout << endl << "请输入J:";
cin >> j;
cout << "输入的I=" << i << "输入的J= " << j << endl;
v1 = mc.algorithms1(i, j);
cout << "算法1的结果=" << v1 << endl;
v2 = mc.algorithms2(i, j);
cout << "算法2的结果=" << v2 << endl;
return 0;
}
```
这样,我们就将这段代码改写成一个包含一个类的程序,使其更加面向对象。
阅读全文