3、编写简单的矩阵类
1)编写一个表示矩阵的类Matrix。矩阵元素为double类型。
2)改写默认构造函数,生成一个空矩阵,(不分配内存,矩阵大小为0*0)。
3)添加一个带参数的构造函数,传入参数为矩阵的大小,该构造分配矩阵内存,并初始化矩阵元素为0。矩阵元素按行序存储。
4)在析构函数中释放内存。
5)编写拷贝构造函数和拷贝赋值函数(operator=)。(注意用深拷贝)
6)编写成员函数SetValue,实现对矩阵的第i行第j列赋值。
7)编写成员函数GetValue,返回矩阵的第i行第j列的值。
8)编写成员函数GetRowCount,GetColCount,分别获取矩阵的行数和列数。
9)编写成员函数Multiply,实现矩阵的乘法运算,该函数的参数是一个Matrix对象的引用,返回一个新的Matrix对象,该对象是两个矩阵相乘的结果。如果矩阵维度不匹配,返回一个空矩阵。
10)编写成员函数Transpose,该函数返回一个新的矩阵,该矩阵是原矩阵的转置。
#include <iostream>
using namespace std;
/*在下面添加你的代码
类的声明和实现代码都写在下方*/
class Matrix {
private:
int rows;//矩阵的行数
int cols;//矩阵的列数
double *pValue;//矩阵的数值
public:
/*改写默认构造函数*/
Matrix() {
rows = cols = 0;
pValue = nullptr;
}
/*构造函数*/
Matrix(const int numRows, const int numCols) {
rows = numRows;
cols = numCols;
pValue = new double[numRows*numCols];
memset(pValue, 0, sizeof(double)*(numRows*numCols));//置0
}
/*在下方编写拷贝构造函数*/
Matrix(const Matrix &a) {
rows = a.rows;
cols = a.cols;
delete[]pValue;
pValue = new double[a.rows*a.cols];
memset(pValue, 0, sizeof(double)*(rows*cols));//置0
int i = 0;
for (int r = 0; r < a.rows; r++)
for (int c = 0; c < a.cols; c++) {
pValue[i++] = a.pValue[i++];
}
}
Matrix& operator=(const Matrix &a) {//拷贝赋值函数
if (this == &a)
return *this;
delete[]pValue;
pValue = new double[a.rows*a.cols];
int i = 0;
for (int r = 0; r < a.rows; r++)
for (int c = 0; c < a.cols; c++) {
pValue[i++] = a.pValue[i++];
}
return *this;
}
/*析构函数*/
~Matrix() {
delete[]pValue;
}
/*在下方编写其它成员函数*/
void SetValue(int i, int j, double value) {//对第i行j列赋值
if (i < 0 || i >=rows || j < 0 || j >= cols)
return;
pValue[cols*i + j] = value;
}
double GetValue(int i, int j) {//取第i行第j列值
if (i < 0 || i >= rows || j < 0 || j >= cols)
return -999;//!!!!
return pValue[cols*i + j];
}
int GetRowCount() {//获取行数
return rows;
}
int GetColCount() {//获取列数
return cols;
}
Matrix Multiply(Matrix &a) {//矩阵相乘函数
Matrix b;
if (cols != a.GetRowCount())//矩阵维数不匹配
return b;
else {
b.rows = rows;
b.cols = a.cols;//新矩阵的行数为第一个矩阵的行数,列数为第二个矩阵的列数
b.pValue = new double[b.rows*b.cols];
int i = 0;
double sum = 0;
for (int r = 0; r < b.rows; r++)
for (int c = 0; c < b.cols; c++) {
for (int c1 = 0, r1 = 0; c1 < cols, r1 < rows; c1++, r1++) {
sum += pValue[cols*r1 + c1] * a.pValue[a.rows*c1 + r1];
}
b.pValue[i++] = sum;
}
return b;
}
}
Matrix Transpose() {//求矩阵转置
Matrix c;
if (0 == rows || 0 == cols || nullptr == pValue) {
return c;//!!!
}
Matrix b(cols, rows);
for(int r=0;r<cols;r++)
for (int c = 0; c < rows; c++) {
b.pValue[rows*r + c] = pValue[cols*c + r];
}
return b;
}
/*Display函数不用改写*/
void Display() const {
if (0 == rows || 0 == cols || nullptr == pValue) {
cout << "Empty Matrix" << endl;
}
else {
int i = 0;
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c) {
cout << pValue[i++] << ' ';
}
cout << endl;
}
}
}
};
/*上述代码编写完成之后,请把下面的宏改为:
#define TEST 1
*/
#define TEST 1
/*=======================================================*/
/*以下代码不要修改*/
/*=======================================================*/
#define PI 3.1415926
int main(void) {
cout << name << " " << ID << endl;
#if TEST
Matrix Non;
cout << "Non rows = " << Non.GetRowCount() << ", cols=" << Non.GetColCount() << endl;
Matrix R(2, 2);
double theta = PI / 4;
R.SetValue(0, 0, cos(theta));
R.SetValue(1, 0, -sin(theta));
R.SetValue(0, 1, -R.GetValue(0, 1));
R.SetValue(1, 1, R.GetValue(0, 0));
R.Display();
Matrix v(2, 1);
v.SetValue(0, 0, sqrt(2.0) / 2);
v.SetValue(1, 0, sqrt(2.0) / 2);
Matrix w = R.Multiply(v);
w.Display();
Matrix vt = v.Transpose();
Matrix wn = R.Multiply(vt);
wn.Display();
#endif // TEST
return 0;
}
4、编写简单的字符串String类
1)编写一个表示字符串的类String。
2)改写默认构造函数,生成一个空串,(不分配内存)。
3)添加一个带参数的构造函数,根据字符串常量构造一个字符串对象;String s(“HelloWorld”)。
4)在析构函数中释放内存。
5)编写拷贝构造函数和拷贝赋值函数(operator=)。(注意用深拷贝)
6)编写成员函数GetLength,返回字符串的长度。
7)编写成员函数bool GetChar(int index, char &ch),获取指定位置index处的字符,通过函数参数ch传出,如果index在合理范围内,返回true,否则返回false。
8)编写成员函数bool SetChar(int index, const char ch),把字符串位置index处的字符改变为ch,如果index在合理范围内,返回true,否则返回false。
9)编写成员函数Concat(const String &s),把字符串s拼接到当前对象表示的字符串后面,即:String a(“Hello”); String b(“World”); 则,a.Concat(b);之后,a中的内容变为”HelloWorld”。
10)编写成员函数Display,在控制台输出该字符串的内容。
#include <iostream>
#include<string.h>
using namespace std;
class String {
private:
int length;//字符串的长度
char *pData;//字符串的内容,注意:字符串以'\0'结尾
public:
/*改写默认构造函数*/
String() {
length = 0;//长度置0
pData = nullptr;
}
/*构造函数*/
String(const char *s) {
length = strlen(s);
pData = new char[length + 1];
strcpy(pData, s);
}
/*在下方编写拷贝构造函数*/
String(const String &s) {
length = s.length;
delete[]pData;
pData = new char[length + 1];
strcpy(pData, s.pData);
}
/*在下方编写拷贝赋值运算operator=*/
String& operator=(const String &s) {
if (this == &s)
return *this;
delete[]pData;
pData = new char[length + 1];
strcpy(pData, s.pData);
return *this;
}
/*析构函数*/
~String() {
delete[]pData;
}
/*在下方编写其它成员函数*/
int GetLength() { return length; }//返回字符串长度
bool GetChar(int index, char &ch) {//获取指定位置的index处的字符
if (index<0 || index>length)
return false;
else {
for (char *p = pData; p <= pData + index; p++)
ch = *p;
return true;
}
}
bool SetChar(int index, const char ch) {//把字符串位置index处的字符改变为ch
if (index<0 || index>length)
return false;
else {
for (char *p = pData; p <= pData + index; p++)
*p = ch;
}
}
String &Concat(const String &s) {//把字符串s拼接到当前对象表示的字符串后面
char *ppData = new char[s.length + length + 1];
int i = 0;
for (; i < length ; i++) {
ppData[i] = pData[i];
}
for (i = length; i < s.length + length; i++) {
ppData[i] = s.pData[i-length];
}
ppData[i] = '\0';
length = length + s.length;
pData = ppData;
return *this;
}
/*Display函数不用改写*/
void Display() const {
if (0 != pData)
std::cout << pData;
std::cout << endl;
}
};
/*上述代码编写完成之后,请把下面的宏改为:
#define TEST 1
*/
#define TEST 1
/*=======================================================*/
/*以下代码不要修改*/
/*=======================================================*/
int main(void) {
std::cout << name << " " << ID << endl;
#if TEST
String s1;
s1.Display();
String s2("Hello World!");
s2.Display();
cout << s2.GetLength() << endl;
String s3(" C++ Programming.");
s3.Display();
cout << s3.GetLength() << endl;
s2.Concat(s3);
s2.Display();
cout << s2.GetLength() << endl;
char ch = 0;
if (s2.GetChar(0, ch)) {
s2.SetChar(0, 'h');
s2.Display();
}
String s4 = String("My God!");
s4.Display();
#endif // TEST
return 0;
}
5、有理数Rational类的编写
编写一个表示有理数的类Rational。(有理数就是分数,包含分子与分母,均为整数)。要求:
-
- 定义一个命名空间Numeric,在该空间中定义类Rational;
- 编写默认构造函数,构造一个有理数0;
- 编写带参数列表的构造函数Rational (int, int ),要求使用初始化列表;
- 编写复制构造函数;
- 编写赋值操作=;
- 编写四个友元函数add、sub、mul、div,对两个Rational对象表示的有理数分别进行相加、相减、相乘、相除运算;(例:Rational x(1,2),y(1,3);分别表示有理数12,13 ,则Rational z = add(x,y);之后,z表示12+13=56 )
- 重载上述四个函数,实现有理数与整数的相加、相减、相乘、相除运算;(例:Rational x(1,2);表示有理数12 ,则Rational z = add(x,1),之后,z表示12+1=32 )
- 编写成员函数getValue(),返回用浮点数表示的有理数,要求写成常函数。(例:Rational x(1,2);表示有理数12 ,则x.getValue()返回0.5)
- 编写友元函数lessThan,比较两个有理数的大小,返回bool类型。(例:Rational x(1,2),y(1,3);则bool b = lessThan(x,y);之后b为false)
- 编写main函数,使用using namespace Numeric;来访问Rational::Rational类。编写代码测试Rational类。
- 在main函数中,随机生成10个有理数,形成一个有理数数组,并利用lessThan函数以及任意一种排序算法,对这10个有理数进行从小到大排序,输出排序结果
-
#include<iostream> #include<stdlib.h> #include<time.h> using namespace std; namespace Numeric {//Numeric命名空间 class Rational { private: int Numerator;//分子 int Denominator;//分母 public: Rational() { Numerator = 0; Denominator = 1; }//无参构造函数 Rational(int _Nume, int _Deno) :Numerator(_Nume), Denominator(_Deno) {}//带参数构造函数 Rational(const Rational&q) {//复制构造函数 Numerator = q.Numerator; Denominator = q.Denominator; } Rational &operator=(const Rational & q) {//赋值操作 Numerator = q.Numerator; Denominator = q.Denominator; return *this; } friend Rational add(const Rational&q1, const Rational&q2) {//求和函数 Rational q; q.Denominator = q1.Denominator*q2.Denominator; q.Numerator = q1.Numerator*q2.Denominator + q2.Numerator*q1.Denominator; return q; } friend Rational sub(const Rational&q1, const Rational&q2) {//求差函数 Rational q; q.Denominator = q1.Denominator*q2.Denominator; q.Numerator = q1.Numerator*q2.Denominator - q2.Numerator*q1.Denominator; return q; } friend Rational mul(const Rational&q1, const Rational&q2) {//求积函数 Rational q; q.Denominator = q1.Denominator*q2.Denominator; q.Numerator = q1.Numerator*q2.Numerator; return q; } friend Rational div(const Rational&q1, const Rational&q2) {//求商函数 Rational q; if (q2.Numerator != 0) { q.Denominator = q1.Denominator*q2.Numerator; q.Numerator = q1.Numerator*q2.Denominator; } else exit(-2); return q; } friend Rational add(const Rational&q1, const int&q2) {//重载后的求和函数 Rational q; q.Denominator = q1.Denominator; q.Numerator = q1.Numerator + q2 * q1.Denominator;//新有理数的分母不变,分子为原来的分子加上有理数与分母积的和 return q; } friend Rational sub(const Rational&q1, const int&q2) {//重载后的求差函数 Rational q; q.Denominator = q1.Denominator; q.Numerator = q1.Numerator - q2 * q1.Denominator;//新有理数的分母不变,分子为原来的分子加上有理数与分母积的差 return q; } friend Rational mul(const Rational&q1, const int&q2) {//重载后的求积函数 Rational q; q.Denominator = q1.Denominator; q.Numerator = q1.Numerator*q2;//新的有理数的分母不变,分子为原分子与整数的积 return q; } friend Rational div(const Rational&q1, const int&q2) {//重载后的求商函数 Rational q; if (q2 != 0) {//除数不能为0 Rational temp(1, q2);//temp为q2的倒数 q.Denominator = q1.Denominator*temp.Denominator; q.Numerator = q1.Numerator*temp.Numerator; } else exit(-2); return q; } void set(const int a, const int b) {//给私有成员赋值 if (b != 0) { Numerator = a; Denominator = b; } } float getValue()const {//返回用浮点数表示的有理数,用const修饰 float q = (Numerator*1.0) / (Denominator); return q; } friend bool lessThan(const Rational&q1, const Rational&q2) {//比较函数 float a = q1.getValue(); float b = q2.getValue(); if (a < b) return true; else return false; } int comdiv()const {//求分子分母的最大公约数 int temp; int a = Denominator; int b = Numerator; if (a > b) { temp = a; a = b; b = temp; } while (a != 0) { temp = b % a; b = a; a = temp; } return b; } void display() { int commom = comdiv(); cout << Numerator / commom << "/" << Denominator / commom <<"\t"; } };//Rational类 }//命名空间 using namespace Numeric; void sort(Rational a[], int size) {//排序函数(选择排序) Rational temp;//临时变量,用作与交换时的中介 Rational qmin;//假设为最小的有理数 int i, j; for(i=0;i<size;++i) for (j = i + 1; j < size; ++j) { qmin = a[i]; if (lessThan(a[j],a[i]) ) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } int main() { Rational a; cout << "Construct Three Rational Numbers:" << endl; cout << "a="; a.display(); Rational b(1, 6); Rational c(2, 8); Rational d = add(b, c); cout << "b="; b.display(); cout << "c="; c.display(); cout << endl; cout << endl; cout << "Perform four operations:" << endl; cout << "b+c="; d.display(); d = sub(b, c); cout << "b-c="; d.display(); d = mul(b, c); cout << "b*c="; d.display(); d = div(b, c); cout << "b/c="; d.display();//对加减乘除的测试 cout << endl; cout << endl; a = b; cout << "Test assignment operators:" << endl; cout << "a="; a.display();//对赋值运算的测试 cout << endl; cout << endl; float x = a.getValue(); float y = b.getValue(); float z = c.getValue(); cout << "Convert to floating point:" << endl; cout << "a=" << x << endl; cout << "b=" << y << endl; cout << "c=" << z << endl; cout << endl; cout << "Determine the size relationship:" << endl; if (lessThan(a, b)) { cout << "a<b" << endl; } else { cout << "a>=b" << endl; } if (lessThan(a, c)) { cout << "a<c" << endl; } else { cout << "a>=c" << endl; }//对判定是否小于的函数的测试 cout << endl; cout << "Testing of Overload Functions:" << endl; d = add(a, 8); cout << "a+8="; d.display(); d = sub(a, 8); cout << "a-8="; d.display(); d = mul(a, 8); cout << "a*8="; d.display(); cout << "\t"; d = div(a, 8); cout << "a/8="; d.display();//以上为对加减乘除的重载函数的测试 cout << endl; Rational r[10]; srand(time(0)); int i; for (i = 0; i < 10; ++i) { r[i].set(rand()%21,rand()%21);//限定随机数的范围 } cout << "Original array:" << endl; for (i = 0; i < 10; ++i) { cout<<"r" << "[" << i << "]="; r[i].display(); cout << endl; } cout << endl;//输出原数组 sort(r, sizeof(r) / sizeof(Rational)); cout << "After sorting" << endl; for (i = 0; i < 10; ++i) { cout << "r" << "[" << i << "]="; r[i].display(); cout << endl; }//输出排序后的数组 cout << endl; return 0; }