程序处理结果分析#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; }
时间: 2024-04-28 12:19:50 浏览: 176
这段代码是一个矩阵计算器的程序,可以读取两个矩阵文件,分别为3x3的M矩阵和10x10的N矩阵。程序中定义了两个算法,algorithms1和algorithms2,均接受两个参数I和J,用来计算矩阵中第I行第J列的值。其中,algorithms1使用的是普通的方式计算,而algorithms2使用的是将N矩阵上下翻转后再进行计算。在main函数中,程序会提示用户输入I和J的值,并调用algorithms1和algorithms2计算得出结果,最后输出到屏幕上。
相关问题
将以下程序补充完整 #include <iostream> #include <fstream> #include <iomanip> using namespace std; template <typename ElemType> class myArrayList { private: int mSize; int mLen; ElemType *mpArr; public: myArrayList(int n); myArrayList(ElemType *a,int n); void show(); ElemType getMax(); //以下函数由同学完成 //void sort(); //myArrayList(myArrayList<ElemtType> &other);//拷贝构造函数 //mymyArrayList<ElemType> & operator =(mymyArrayList<ElemType> &other) }; template <typename ElemType> myArrayList<ElemType>::myArrayList(int n) { this->mSize=n; this->mLen=0; this->mpArr=new ElemType[mSize]; } template <typename ElemType> myArrayList<ElemType>::myArrayList(ElemType *a,int n) { this->mSize=n; this->mLen=n; this->mpArr=new ElemType[mSize]; for(int i=0;i<mLen;i++) mpArr[i]=a[i]; } template <typename ElemType> void myArrayList<ElemType>::show() { for(int i=0;i<mLen;i++) cout<<setw(4)<<mpArr[i]; cout<<endl; } template <typename ElemType> ElemType myArrayList<ElemType>::getMax() { ElemType max; max=mpArr[0]; for(int i=1;i<mLen;i++) if(max<mpArr[i]) max=mpArr[i]; return max; } //Student.h class Student { private: int mId; float height; int score; public: Student(int id=0,float h=0,int s=0):height(h),mId(id),score(s) { } friendbool operator <(Student &a1,Student &a2) { if(a1.height<a2.height) return true; else return false; } friend ostream &operator <<(ostream &out,Student &s) { out<<"ID:"<<s.mId<<" Height:"<<s.height<<" Socre:"<<s.score<<endl; return out; } }; //主程序 int main() { int a[]={1,2,3,5,7,9,12,8}; double b[]={1,2.5,3.6,5,7,9,12.8,8}; myArrayList <int> list1(a,8); list1.show(); cout<<"max="<<list1.getMax()<<endl; myArrayList <double> list2(b,8); list2.show(); cout<<"max="<<list2.getMax()<<endl; Student s[3]={Student(1,175,80),Student(2,178,90),Student(3,195,83)},s1; myArrayList <Student> list3(s,3); list3.show(); cout<<"max="<<list3.getMax()<<endl; } 说明: 将以下程序补充完整 //void sort(); //myArrayList(myArrayList<ElemtType> &other);//拷贝构造函数 //mymyArrayList<ElemType> operator =(mymyArrayList<ElemType> &other) //修改补充 Student类,要求按成绩排序(从高到低)
将以下程序补充完整:
```
template <typename ElemType>
void myArrayList<ElemType>::sort()
{
for(int i=0; i<mLen-1; i++)
{
for(int j=0; j<mLen-i-1; j++)
{
if(mpArr[j] > mpArr[j+1])
{
ElemType temp = mpArr[j];
mpArr[j] = mpArr[j+1];
mpArr[j+1] = temp;
}
}
}
}
template <typename ElemType>
myArrayList<ElemType>::myArrayList(myArrayList<ElemType> &other)
{
this->mSize = other.mSize;
this->mLen = other.mLen;
this->mpArr = new ElemType[mSize];
for(int i=0; i<mLen; i++)
{
mpArr[i] = other.mpArr[i];
}
}
template <typename ElemType>
myArrayList<ElemType> & myArrayList<ElemType>::operator =(myArrayList<ElemType> &other)
{
if(this == &other)
return *this;
delete []mpArr;
this->mSize = other.mSize;
this->mLen = other.mLen;
this->mpArr = new ElemType[mSize];
for(int i=0; i<mLen; i++)
{
mpArr[i] = other.mpArr[i];
}
return *this;
}
//修改补充 Student类,要求按成绩排序(从高到低)
class Student
{
private:
int mId;
float height;
int score;
public:
Student(int id=0,float h=0,int s=0):height(h),mId(id),score(s) { }
friend bool operator <(Student &a1,Student &a2)
{
if(a1.score > a2.score)
return true;
else
return false;
}
friend ostream &operator <<(ostream &out,Student &s)
{
out<<"ID:"<<s.mId<<" Height:"<<s.height<<" Score:"<<s.score<<endl;
return out;
}
};
```
说明:
- `sort()`函数实现排序功能,采用冒泡排序算法;
- `myArrayList`类的拷贝构造函数和赋值运算符重载函数均实现深拷贝功能;
- `Student`类的 `<` 运算符重载函数改为按成绩从高到低排序。
注释函数#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; }
这是一个C++程序,实现了一个矩阵计算器类MatrixCalculator,其中包含两个算法algorithms1和algorithms2,可以根据输入的矩阵数据以及用户输入的I和J值,计算出相应的结果。程序首先读入矩阵数据,然后获取用户输入的I和J值,接着调用算法1和算法2进行计算,并输出结果。算法1是根据输入的I和J值,以及3x3的矩阵M和10x10的矩阵N,计算出一个加权平均值,其中权重由矩阵M的元素决定,矩阵N的元素作为值参与计算。算法2与算法1类似,差别在于它使用了矩阵N中与算法1相反方向的元素进行计算。
阅读全文
相关推荐
















