实验目的:
熟悉c++的指针、引用,运算符new和delete。
熟悉c++的类和对象。
实验步骤与内容:
1、在main函数中接受从键盘输入的3个整数,调用下面的三个函数进行排序,然后输出排序后的结果。
- 实现函数sort1(int x, int y, int z),sort2(int *px, int *py, int * pz),sort3(int &x, int &y, int &z), 分别采用传值、传指针、传引用的方式接受参数,对三个整数按照从小到大顺序排序,输出排序后的结果。
- 分析三种传递参数的方式有什么不同,在main函数中哪种方式能正确地得到排序后的结果?
#include<iostream>
using namespace std;
//传值
void sort1(int a, int b, int c){
//sort(&x,&y,&z);
int temp;
if(a>b)
{
temp=a;a=b;b=temp;}
if(a>c)
{
temp=a;a=c;c=temp;}
if(b>c)
{
temp=b;b=c;c=temp;}
printf("这3个整数从小到大排列是:%d<%d<%d\n",a,b,c);
}
//传指针
void sort2(int *a, int *b, int *c){
int temp;
if(*a>*b)
{
temp=*a;*a=*b;*b=temp;}
if(*a>*c)
{
temp=*a;*a=*c;*c=temp;}
if(*b>*c)
{
temp=*b;*b=*c;*c