仅供讨论、学习。
转载、使用请注明出处。
实验一
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(); //关闭文件输入流
}
运行效果: