c++高级程序设计语言实验八引用和指针的比较

实验名称

引用和指针的比较

实验目的

  1. 掌握引用和指针使用时的不同。
  2. 掌握指针作为函数参数的应用。

实验任务

  1. 定义一个函数exchange,交换两个整型变量的值。分别使用指针的方法和使用引用的方法实现。ad08_01 ad08_02

  1. 9.3 第203页 ex08_03

编写程序,调用传递引用的参数,实现两个字符串变量的交换:

例如开始: char *ap=“hello”;

           char *bp=“how are you”;

交换后的结果使得ap和bp指向的内容分别是

ap:“how are you”     bp:“hello”

  1. 9.1 第198页 修正版第202页 ex09_01

读下列程序。

  1. 将其改写为传递引用参数。
  2. 说出其功能。
  3. 将findmax()函数改写为非递归函数(重新考虑参数的个数)。

/*

请输入10个数据:

33 91 54 67 82 37 85 63 19 68

最大的数是:91

它的下标是:1  `

*/

#include <iostream>

using namespace std;

int const size=10;

void findmax(int *a,int n,int i,int *pk);

int main() {

    int a[size];

    int n=0;

    cout<< "请输入"<<size<<"个数据:\n";

    for( int i=0; i<size; i++)

        cin>>a[i];

    findmax(a,size,0,&n);

    cout<<"最大的数是:"<<a[n]<<endl;

    cout<<"它的下标是:"<<n<<endl;

    return 0;

}

void findmax(int *a,int n,int i,int *pk) {

    if (i<n) {

        if (a[i]>a[*pk])

            *pk=i;

        findmax(a,n,i+1,&(*pk));

    }

}

  1. 9.2 第199页 修正版第203页 ex09_02

读下列程序,该程序生成有10个整数的安全数组。要把值放入数组中,使用put()函数;然后取出该值,使用get()函数;put()和get()中若遇下标越界则立刻终止程序运行。其运行结果为后面所示,请完成两个未写出的函数定义。

#include<iostream>

using namespace std;

int& put(int n);  //put value into the array

int get(int n);       //obtain a value from the array

int vals[10];

int error=-1;

int main() {

    put(0)=10; //put values into the array

    put(1)=20;

    put(9)=30;

    cout<<get(0)<<endl;

    cout<<get(1)<<endl;

    cout<<get(9)<<endl;

    put(12)=1; //out of range

    return 0;

}

运行结果要求为:

10

20

30

range error

实验内容

  1. ad08_01

#include<iostream>

using namespace std;

void exchange(int &a,int &b)

{

   int temp=a;

   a=b;

   b=temp;

}

int main()

{

  

   int a=1,b=2;

   cout << "a= "<<a<< endl;

   cout <<"b= "<<b<< endl;

   exchange(a,b);

      cout << "a= "<<a<< endl;

      cout <<"b= "<<b<< endl;

  

   return 0;

}

  1. ad08_02

#include<iostream>

using namespace std;

void exchange(int *a,int *b)

{

   int temp=*a;

   *a=*b;

   *b=temp;

}

int main()

{

  

   int a=1,b=2;

   cout << "a= "<<a<< endl;

   cout <<"b= "<<b<< endl;

   int *p1=&a;

   int *p2=&b;

   exchange(p1,p2);

      cout << "a= "<<a<< endl;

      cout <<"b= "<<b<< endl;

  

   return 0;

}

  1. ex08_06

#include<iostream>

using namespace std;

char* strcpy(char* dest,const char* source)

{

   for(int i=0;i<sizeof(source);i++)

   {

      dest[i]=source[i];

   }

   return dest;

}

int main()

{

   char* source="abc";

   cout <<"source="<< source<< endl;

   char* c1;

   c1=strcpy(c1,source);

   cout <<"c1="<< c1<< endl;

   return 0;

}

  1. ex09_01

//功能:把参数修改为数组中最大的数的下标

#include <iostream>

using namespace std;

int const size=10;

void findmax(int *a,int size,int &pk);

int main() {

   int a[size];

   int n=0;

   cout<< "请输入"<<size<<"个数据:\n";

   for( int i=0; i<size; i++)

      cin>>a[i];

   findmax(a,size,n);

   cout<<"最大的数是:"<<a[n]<<endl;

   cout<<"它的下标是:"<<n<<endl;

   return 0;

}

void findmax(int *a,int size,int &pk) {

   int temp=a[0];

   for(int i=0;i<size;i++)

   {

      if(temp<a[i])

      {

      temp=a[i];

      pk=i; 

      }

   }

}

  1. ex09_02

#include<iostream>

using namespace std;

int& put(int n); //put value into the array

int get(int n);     //obtain a value from the array

int vals[10];

int error=-1;

int main() {

   put(0)=10; //put values into the array

   put(1)=20;

   put(9)=30;

   cout<<get(0)<<endl;

   cout<<get(1)<<endl;

   cout<<get(9)<<endl;

   put(12)=1; //out of range

   return 0;

}

int& put(int n)

{

   if(n>=10)

   {

      cout <<"out of range"<< endl;

   }

   return vals[n]; 

}

int get(int n)

{

      if(n>=10)

      {

          cout <<"out of range"<< endl;

      }

   return vals[n];

}

小结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值