⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 change.cpp

📁 我学习C++ Primer Plus过程中写下的课后作业的编程代码
💻 CPP
字号:
//7.修改程序清单7.7中的3个数组处理函数,使之使用两个指针参数来表示区间。
//fill_array()函数不返回实际读取了多少个数字,而是返回一个指针,该指针
//指向最后被填充的位置;其他的函数可以将该指针作为第二个参数,以标识数据结尾。

#include <iostream>
const int Max=5;

//function prototypes
double* fill_array(double* ar,double* limit);
void show_array(const double* ar,double* n);
void revalue(double r,double* ar,double* n);

int main()
{
	using namespace std;
	double properties[Max];
	double *size = fill_array(properties,properties+Max);
	show_array(properties,size);
	cout<<"Enter revaluation factor: ";
	double factor;
	cin>>factor;
	revalue (factor,properties,size);
	show_array(properties,size);
	cout<<"Done.\n";
	return 0;
}

double* fill_array(double* ar,double* limit)
{
	using namespace std;
	double temp;
	int i=0;
	for(double *p=ar; p<limit; p++,i++)
	{
		cout<<"Enter value#"<<(i+1)<<": ";
		cin>>temp;
		if(!cin)	//bag input
		{
			cin.clear();
			while(cin.get()!='\n')
				continue;
			cout<<"Bad input;input process terminated.\n";
			break;
		}
		else if(temp<0)
			break;
		*p = temp;
	}
	return p;
}

// the following function can use,but not alter,
// the array whose address is ar
void show_array(const double* ar,double* n)
{
	using namespace std;
	int i=0;
	for(const double *p=ar;p<n;p++,i++)			//注意const的用法
	{
		cout<<"Property #"<<(i+1)<<": $";
		cout<<*p<<endl;
	}
}

//multiplies each element of ar[] by r
void revalue(double r,double* ar,double* n)
{
	for(double *p=ar;p<n;p++)
		*p *=r;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -