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

    仅供讨论、学习。

    转载、使用请注明出处。

实验一

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<<"the max number of (1,2) is: "<<max1<<endl
    <<"the max number of (1,2,3) is: "<<max2<<endl
    <<"the max number of (1.0,2.0) is: "<<max3<<endl
    <<"the max number of (1.0,2.0,3.0) is: "<<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();             //关闭文件输入流
}

运行效果:

 

  • 35
    点赞
  • 106
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
吉林大学C实验课是一门专门针对计算机科学与技术专业的实践课程。通过这门课程,学生将有机会接触并了解计算机科学领域中的实际问题,并通过实验的方式来解决这些问题。这门课程的目标是让学生能够熟练掌握计算机科学与技术的实践能力,为将来工作或学术研究打下坚实的基础。 在吉林大学C实验课中,学生将进行一系列的实验项目,以提高他们的实践能力。这些实验项目涵盖了计算机科学与技术的不同领域,例如软件开发、网络安全、数据库应用等。通过实验项目,学生将能够学习和掌握计算机领域中的先进技术和工具,同时也培养他们的团队合作和问题解决能力。 吉林大学C实验课的教学方法包括课堂教学和实验室实践。在课堂教学中,教师将向学生介绍实验项目的背景和目标,讲解相关的理论知识和实验步骤。学生将通过听课和课后学习来掌握这些知识。在实验室实践中,学生将亲自动手进行实验项目,实践他们在课堂上学到的知识和技能。同时,教师将提供指导和支持,确保学生能够顺利完成实验项目。 通过参加吉林大学C实验课,学生将能够深入了解和实践计算机科学与技术的领域,提高他们的实践能力和创新能力。这将为他们未来的就业或学术研究提供有力的支持。此外,吉林大学C实验课还将培养学生的团队合作和沟通能力,让他们能够更好地适应和应对现实工作环境中的挑战。 总之,吉林大学C实验课是一门重要的实践课程,它将帮助学生提高他们的实践能力和创新能力,为他们未来的就业或学术研究奠定坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@wefree

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值