东北大学-C++选修课-课程实验总结

    以下内容由软件工程1607班王锋sir友情提供。

    仅供讨论、学习。

    转载、使用请注明出处。

实验一

1.1. 输人并运行所给的参考程序1,并将程序中的注释部分也输人计算机,体会和理解程序的基本格式规范。

建立一个控制台应用程序项目baseforml,向其中添加一个源程序文件sum.cpp。按照所给的程序代码输到计算机中,检查和调试程序,在确认没有发现错误之后,选择[Build]--[Build sum.exe]编译源程序,再选择[Build]-[Execute sum.exe]运行程序,并观察输出结果。若有问题,则需要重新检查程序。

#include <iostream>
using namespace std;
int add(int a,int b);
int main() {
	int x,y,sum;
	cout<<"Enter two numbers:\n";
	cin>>x>>y;
	sum=add(x,y);
	cout<<"The sum is:"<<sum<<endl;
	return 0;
}
int add(int a,int b) {
	int c;
	c=a+b;
	return c;
} 

1.2. 编写重载函数Maxl可分别求取两个整数,三个整数,两个双精度数,三个双精度数的最大值。

分别编写四个同名函数maxl,实现函数重载,在main()函数中测试函数功能。程序名:lab1_2.cpp

#include <iostream>
using namespace std;

int Max1(int a, int b);
int Max1(int a, int b, int c);
double Max1(double a, double b);
double Max1(double a, double b,double c);

int main () {
	int max1=Max1(1,2);
	int max2=Max1(1,2,3);
	double max3=Max1(1.0,2.0);
	double max4=Max1(1.0,2.0,3.0);
	cout<<max1<<endl<<max2<<endl<<max3<<endl<<max4<<endl;
	return 0;
}

int Max1(int a, int b) {
	int max=a;
	if (max < b) {
		max=b;
	}
	return max;
}

int Max1(int a, int b, int c) {
	int max = a;
	if (max < b) {
		max=b;
	}
	if (max < c) {
		max=c;
	}
	return max;
}

double Max1(double a, double b) {
	double max=a;
	if (max < b) {
		max=b;
	}
	return max;
}

double Max1(double a, double b,double c) {
	double max=a;
	if (max < b) {
		max=b;
	}
	if(max<c) {
		max=c;
	}
	return max;
}

1.3. 编写并测试3X3矩阵转置函数,使用数组保存3X3矩阵。

编写矩阵转置函数,输人参数为3X3整型数组,使用循环语句实现矩阵元素的行列对调,注意在循环语句中究竟需要对哪些元素进行操作,编写main ( )函数实现输入、输出。程序名:lab1_3.cpp

#include <iostream>
using namespace std;

void matrixTransposed(int matrix[3][3]);
int main() {
	int matrix[3][3];
	cout<<"Enter a 3x3 matrix, please. "<<endl;
	for(int i=0; i<3; i++) {
		for(int j=0; j<3; j++) {
			cin >> matrix[i][j];
		}
	}
	matrixTransposed(matrix);
	cout<<"The transposed matrix is: "<<endl;
	for(int i=0; i<3; i++) {
		for(int j=0; j<3; j++) {
			cout<<matrix[i][j]<<" ";
		}
		cout<<endl;
	}
	return 0;
}

void matrixTransposed(int matrix[3][3]) {
	for(int i=0; i<3; i++) {
		for(int j=0; j<i+1; j++) {
			int temp;
			temp=matrix[i][j];
			matrix[i][j]=matrix[j][i];
			matrix[j][i]=temp;
		}
	}
}

1.4. 使用动态内存分配生成动态数组来重新完成上题,使用指针实现函数的功能。

改写矩阵转置函数,参数为整型指针,使用指针对数组元素进行操作,在main ( )函数中使用new操作符分配内存生成动态数组。通过debug观察指针的内容及其所指的对象中的内容。程序名:lab1_4.cpp

#include <iostream>
using namespace std;

int** matrixTransposed(int **m,int row,int col);

int main() {
	int row,col;
	cout<<"Enter the row of the row*col matrix, please: "<<endl;
	cin>>row;
	cout<<"Enter the col of the row*col matrix, please: "<<endl;
	cin>>col;
	cout<<"Enter a "<<row<<"x"<<col<<" matrix, please."<<endl;
	//动态开辟一个二维数组
	int **m=new int*[row];
	for(int i=0; i<row; i++) {
		m[i]=new int[col];
	}
	//读入矩阵
	for(int i=0; i<row; i++) {
		for(int j=0; j<col; j++) {
			cin>>m[i][j];
		}
	}
	//转置矩阵
	int **mt=new int*[col];
	for(int i=0; i<col; i++) {
		mt[i]=new int[row];
	}
	mt=matrixTransposed(m,row,col);
	//输出结果
	cout<<"The transposed matrix is: "<<endl;
	for(int i=0; i<col; i++) {
		for(int j=0; j<row; j++) {
			cout<<mt[i][j]<<" ";
		}
		cout<<endl;
	}
	//内存回收处理
	delete []m;
	delete []mt;
	return 0;
}

int** matrixTransposed(int **m,int row,int col) {
	int **temp=new int*[col];
	for(int i=0; i<col; i++) {
		temp[i]=new int[row];
	}
	for(int i=0; i<col; i++) {
		for(int j=0; j<row; j++) {
			temp[i][j]=m[j][i];
		}
	}
	return temp;
}

1.5. 编写程序,读写指定的文件,在每一行前加行号后,将结果输出到屏幕。

编写程序lab1_5.cpp,使用void main(int argc, char argv[])函数中的参数传递操作的文件名,定义ofstream的对象对文件进行操作,使用get()或getline()成员函数读入数据,使用输出流对象输出数据到屏幕。


#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<cassert>
using namespace std;

void printTxt(string file);
int main(int argc,char* argv[]) { //路径名不能有空格哦~~ 不然我会挂掉给你看~~
	string filename=argv[1];
	printTxt(filename);
	return 0;
}
void printTxt(string file) {
	ifstream infile;
	infile.open(file.data());   //将文件流对象与文件连接起来
	assert(infile.is_open());   //若失败,则输出错误消息,并终止程序运行

	string c;
	int i=0;
	while (true) {
		i++;
		getline(infile,c);
		if(infile.eof())
			break;
		cout<<i<<":"<<c<<endl;
	}
	infile.close();             //关闭文件输入流
}

实验二

2.1. 下面是“平面上点”类的定义:

class CPoint

{

private:

int x,  y;

static int nCount;   // nCount用于保存点的个数

public:

CPoint(int px=0, int py=0);

CPoint(CPoint&);

~CPoint();

int GetX();

int GetY();

void SetX(int);

void SetY(int);

void ShowPoint();

};

请完成该类中各成员函数的定义。

2.2. 下面是“平面上线段”类的定义:

class CLine

{

private:

CPoint pt1, pt2;  //pt1pt2分别代表该线段的起点和终点

public:

CLine();

CLine(int x1,int y1,int x2,int y2);

CLine(CPoint p1,CPoint p2);

double Distance();  //计算该线段长度的成员函数

void ShowLine();

};

请完成该类中各成员函数的定义。并利用VC调试工具观察含有组合关系类的构造函数和析构函数的执行情况。

2.3. 以成员函数的方式,重载题目1中的CPoint类的“+”运算符,返回的CPoint类对象的横纵坐标值分别等于原两点的横纵坐标值之和;以全局函数的方式重载CPoint类的“-”运算符,返回的CPoint类对象的横纵坐标值分别等于原两点的横纵坐标值之差。

2.4. 对于题目1中的CPoint类,重载“>>”运算符使得像cin等输入流对象能够写CPoint类对象,再重载“<<”运算符使得像cout等输出流对象能够输出CPoint类对象。

#include<iostream>
#include<cmath>
using namespace std;

//平面上的点类
class CPoint {
	private:
		int x,  y;
	public:
		static int nCount;   // nCount用于保存点的个数
		CPoint(int px=0, int py=0) { //构造函数
			x=px;
			y=py;
			nCount++;
		};
		CPoint(CPoint& p) { //拷贝构造
			x=p.x;
			y=p.y;
			nCount++;
//			cout<<"copy constructor is invoked."<<endl;
		};
		~CPoint() { //析构函数
			nCount--;
//			cout<<"Deconstruction finished"<<endl;
		};
		int GetX() {
			return x;
		};
		int GetY() {
			return y;
		};
		void SetX(int px) {
			x=px;
		};
		void SetY(int py) {
			y=py;
		};
		void ShowPoint() {
			cout<<"<"<<x<<","<<y<<">"<<endl;
		};
		CPoint operator+(CPoint pt);
		friend istream& operator>>(istream& is, CPoint& p);
		friend ostream& operator<<(ostream& os, CPoint& p);
};
int CPoint::nCount = 0;
//平面上的直线类
class CLine {
	private:
		CPoint pt1, pt2;  //pt1和pt2分别代表该线段的起点和终点
	public:
		CLine() {
		};
		CLine(int x1,int y1,int x2,int y2)
			:pt1(x1,y1),pt2(x2,y2) {
		};
		CLine(CPoint p1,CPoint p2)
			:pt1(p1) ,pt2(p2) {
		};
		double Distance() { //计算该线段长度的成员函数
			int x_x=pt1.GetX() -pt2.GetX() ;
			int y_y=pt1.GetY() -pt2.GetY() ;
			double dis=pow((double)(pow(x_x,2)+pow(y_y,2)),0.5);
			return dis;
		};
		void ShowLine() {
			cout<<"<"<<pt1.GetX()<<","<<pt1.GetY() <<">"
			    <<"-->"
			    <<"<"<<pt2.GetX() <<","<<pt2.GetY() <<">"
			    <<endl;
		};
};

CPoint CPoint::operator+(CPoint pt) { //成员函数重载+
	CPoint sumPoint;
	sumPoint.SetX(pt.GetX() +this->GetX());
	sumPoint.SetY(pt.GetY() +this->GetY());
	return sumPoint;
}

CPoint operator- (CPoint pt1, CPoint pt2) { //全局重载-
	CPoint difPoint;
	difPoint.SetX(pt1.GetX() -pt2.GetX() );
	difPoint.SetY(pt1.GetY() -pt2.GetY() );
	return difPoint;
}

istream& operator>>(istream& is, CPoint& p) { //成员函数重载》
	is>>p.x >>p.y;
	return is;
}

ostream& operator<<(ostream& os, CPoint& p) { //成员函数重载《
	os<<"<"<<p.x<<","<<p.y<<">";
	return os;
}
int main() {
	//验证点类的正确实现
	CPoint a(1,2),b(3,4),c(a);
	cout<<"the first point is:a";
	a.ShowPoint() ;
	cout<<"the second point is:b";
	b.ShowPoint() ;
	cout<<"the third point is:c";
	a.ShowPoint() ;
	cout<<"the nCount is:"<<CPoint::nCount<<endl;

	//验证线类的正确实现
	CLine d,e(1,1,1,1),f(a,b);
	cout<<"the first line is:";
	d.ShowLine() ;
	cout<<"the second line is:";
	e.ShowLine();
	cout<<"the third line is:";
	f.ShowLine() ;
	cout<<"the third line's distance is:";
	cout<<f.Distance() <<endl;

	//验证+,-重载运行符的正确实现
	CPoint sumAB,difAB;
	sumAB=a+b;
	difAB=a-b;
	cout<<"the result of a+b is: ";
	sumAB.ShowPoint();
	cout<<"the result of a-b is: ";
	difAB.ShowPoint() ;

	//验证 》,《 重载运算符的实现
	CPoint overloadCP;
	cout<<"Enter two numbers to make a point: "<<endl;
	cin>>overloadCP;
	cout<<"Your point is: ";
	cout<<overloadCP<<endl;
	return 0;
}

实验三

待续...


欢迎批评指正不足之处~







猜你喜欢

转载自blog.csdn.net/qq_38672855/article/details/80710766