C++primer plus第六版课后编程题答案 7.7

7.7

//7.修改程序清单7.7中的3个数组处理函数,使之使用两个指针参数来表示区间.
//fill_array()函数不返回实际读取了多少个数字,而是返回一个指针,该指针
//指向最后被填充的位置;其他的函数可以将该指针作为第二个参数,以标识
//数据结尾。

#include <iostream>
using namespace std;

double *Fill_array(double *arr);因为是对数组进行操作,所以可以不用返回值;
void const Show_array(const double *arr,double *Epoint);
void Reverse_array(double *arr,double *Epoint);
const int MAX=10;
void main77()
{
	double arr[MAX];
	double *end=Fill_array(arr);//使end指向最后一个指针
	Show_array(arr,end);
	Reverse_array(arr,end);
	system("pause");

}

double *Fill_array(double *arr) 
{
	double *p=arr;
	int i=0;//用于记录输入了多少个数字
	for(i=0;i<MAX;i++)
	{
		cout<<"\nPlease enter the "<<i+1<<" double values:";
		if((cin>>*(arr+i)))//如果输入非法值,跳出循环
		{
			p++;
			continue;
		}
		else
			break;
		
	}
	cout<<"\n you had input "<<i<<"  values!"<<endl;

	return p;
}

void const Show_array(const double *arr,double *end)
{
	const double *p=arr;
	cout<<"the array is:"<<endl;
	for(int i=0;i<MAX&&p<end;i++)
		cout<<*(p+i)<<"  ";
	cout<<"\nshow end!"<<endl;
}
void Reverse_array(double *arr,double *end)
{
	 double *p=arr;
	int temp;
	cout<<"\nnow Reverse the array!"<<endl;
	for(int i=1;i<(end-arr)/2&&p<end;i++)
	{
		temp=*(p+i);
		*(p+i)=*(end-i-1);
		*(end-i)=temp;
	}
	cout<<"this is the new array:"<<endl;
	Show_array(arr,end);
}

被这道题卡了好几天,因为不是很透彻地理解了关于函数指针的这一部分,只好上网找了下答案。

感谢http://blog.sina.com.cn/s/blog_4e6b6c2f01000a0c.html帖子对我的帮助。


发现这个有些bug,现修改如下,如果编译器不支持c++11的,可以将nullptr换为NULL即可。

#include <iostream>  
using namespace std;  
  
double *Fill_array(double *arr);因为是对数组进行操作,所以可以不用返回值;  
void const Show_array(const double *arr,double *Epoint);  
void Reverse_array(double *arr,double *Epoint);  
const int MAX=10;  
int main()
{  
    double arr[MAX];  
    double *end=Fill_array(arr);//使end指向最后一个指针  
    Show_array(arr,end);  
    Reverse_array(arr,end);  
  
}  
  
double *Fill_array(double *arr)   
{  
    double *p=arr;  
    int i=0;//用于记录输入了多少个数字  
    for(i=0;i<MAX;i++)  
    {  
        cout<<"\nPlease enter the "<<i+1<<" double values:";  
        if((cin>>*(arr+i))){//如果输入非法值,跳出循环    
		p=arr+i;//使p指向当前被填充的位置
            continue;  
        }  
        else{
		if(i==0)//如果一个都不输入,返回一个空指针
			return nullptr;
            else
			break;
	}
          
    }  
    cout<<"\n you had input "<<i<<"  values!"<<endl;  
  
    return p;  
}  
  
void const Show_array(const double *arr,double *end)  
{  
    const double *p=arr;  
   
     if(end==nullptr)
		cout<<"没有输入元素"<<endl;
     else{
	 cout<<"the array is:"<<endl;  
	 for(;p<=end;p++)
		cout<<*p<<"  ";	
	}
    cout<<"\nshow end!"<<endl;  
}  
void Reverse_array(double *arr,double *end)  
{  
     
    if(end==nullptr)
		cout<<"数组内没有元素!"<<endl;
    else{
		cout<<"\nnow Reverse the array!"<<endl;  
	double *p=arr; 
	double *e=end; 	
	int temp; 
	//注意循环条件
	for(;p<e;p++,e--){
		temp=*p;
		*p=*e;
		*e=temp;
     }
    cout<<"this is the new array:"<<endl;  
    Show_array(arr,end);  
     }
}  
测试如下:

不输入任何元素:

输入四个元素:


输入5个元素


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值