1、统计1到200以内的素数
#include "stdafx.h"
#include "iostream.h"
#include "cmath"
int main()
{
int nNumber = 1,
int nSqrt;
int i = 2;
while(nNumber <= 200)
{
nSqrt = sqrt(nNumber);
while(i <= nSqrt)
{
if(nNumber % i == 0)
{
break;
}
i++;
}
if(i > nSqrt)
{
cout<< nNumber << "\t";
}
i = 2;
nNumber = nNumber + 2;
}
}
2、寻找矩阵中最小的数
寻找矩阵中最小的值并且打印所在行数和列数
#include "stdafx.h"
#include "iostream.h"
void OutputElement(int *pArray, int nCol, int nRow)
{
int nColIndex;
int nRowIndex;
int nNumber = pArray[0];
for(int i=0; i<nRow; i++)
{
for(int j=0; j<nCol-1; j++)
{
if (nNumber > pArray[(i)*nCol + (j)])
{
nColIndex = j;
nRowIndex = i;
nNumber = pArray[(i)*nCol + (j)];
}
}
}
cout << "最小元素: " << nNumber << endl
<< "行号: " << nRowIndex << endl
<< "列号: " << nColIndex << endl;
}
int main(int argc, char* argv[])
{
int nArray[4][4] = {
{10, 30, 5, 47},
{20, 87, 101, 56},
{2, 14, 25, 36},
{87, 56, 10, 20}
};
OutputElement((int *)&nArray[0], 4, 4);
return 0;
}
3、按值、引用传递
按值传递,不会影响原来的值。
#include "stdafx.h"
#include "iostream.h"
void OutputNumber(int nNumber)
{
cout << nNumber << endl; //输出数值
nNumber = 20; //修改参数
}
int main(int argc, char* argv[])
{
int nNum = 10; //定义一个变量
OutputNumber(nNum); //调用OutputNumber函数
cout << nNum << endl; //输出变量值
return 0;
}
按引用传递,传递的是参数的地址,修改会影响原值。
#include "stdafx.h"
#include "iostream.h"
void OutputNumber(int &nNumber) //设置引用类型参数
{
cout << nNumber << endl; //输出结果
nNumber = 20; //修改参数值,将影响到实际参数
}
int main(int argc, char* argv[])
{
int nNum = 10; //定义一个变量
OutputNumber(nNum); //调用OutputNumber函数
cout << nNum << endl; //输出变量值
return 0;
}
编写函数的时候,如果需要采用引用方式传递,使用指针和引用作为参数类型都是ok的,各有优缺点。比如指针需要检查是否为空;因为必须初始化为一个对象,并且不能在指向其它对象。
4、数组参数
在c++中,数组参数是有争议的,如果将数组作为参数,实际上是指向第一个元素的指针被传递到了函数中。
#include "stdafx.h"
#include "iostream.h"
void Sort(int nArray[5]) //定义一个排序函数,使用数组作为参数
{
int nTmp = 0; //定义一个临时变量
for(int i=0; i<5; i++) //利用冒泡法排序
{
for(int j=0; j<5-i;j++)
{
if (nArray[j]>nArray[j+1]) //交换数组元素
{
nTmp = nArray[j];
nArray[j] = nArray[j+1];
nArray[j+1] = nTmp;
}
}
}
cout << "排序之后" <<endl;
for(i=0; i<5; i++) //输出排序后的结果
{
cout << nArray[i] << endl;
}
}
int main(int argc, char* argv[])
{
//即使定义一个6个元素的也不会报错
int nList[] = {78, 98, 75, 86, 78}; //定义一个数组
Sort(nList); //调用Sort函数
return 0;
}
5、定义重载函数
编译器能够根据传递的参数的类型来确定调用哪一个Add函数。
#include "stdafx.h"
#include "iostream.h"
int Add(int nPlus, int nSummand) //定义第一个重载的Add函数
{
cout << "整数加法运算" << endl;
return nPlus + nSummand; //返回结果
}
double Add(double dbPlus, double dbSummand) //定义第2个重载的Add函数
{
cout << "实数加法运算" << endl;
return dbPlus + dbSummand; //返回结果
}
int main(int argc, char* argv[])
{
int nRet = Add(10, 30); //调用一个版本的Add函数,实现两个整数相加
double dbRet = Add(10.5, 20.5); //调用2个版本的Add函数,实现两个实数相加
return 0;
}
6、隐藏重载函数
可以发现,在main函数中执行的Add(10, 30),调用的是float参数类型的Add函数,因为在main函数中声明了float参数类型的Add函数,导致与该函数同名的所有Add函数被隐藏。
#include "stdafx.h"
#include "iostream.h"
int Add(int nPlus, int nSummand) //定义一个重载函数,实现两个整数加法运算
{
cout << "整数加法运算" << endl;
return nPlus + nSummand; //返回运算结果
}
double Add(double dbPlus, double dbSummand) //定义一个重载函数,实现两个实数加法运算
{
cout << "实数加法运算" << endl;
return dbPlus + dbSummand; //返回运算结果
}
int main(int argc, char* argv[])
{
float Add(float fPlus, float fSummand); //声明函数Add
int nRet = Add(10, 30); //调用Add函数
return 0;
}
float Add(float fPlus, float fSummand) //实现两个单精度数值的加法运算
{
cout << "单精度加法运算" << endl;
return fPlus + fSummand; //返回运算结果
}
这篇博客探讨了C++编程中的几个关键概念:如何找出1到200之间的素数,寻找矩阵中的最小元素及其位置,理解按值和按引用传递参数的区别,以及如何定义和使用重载函数。此外,还介绍了数组参数的传递方式和隐藏重载函数的情况。
11万+

被折叠的 条评论
为什么被折叠?



