一个使用数组区间的实例

#include <iostream>
const int MAX = 5;
double * fill_array(double *, double *);
void show_array(double *, double *);
void revalue(double, double *, double *);
using namespace std;

int main() {
	double properties[MAX];

	double * pf = fill_array(properties, properties + MAX);
	show_array(properties, pf);
	if (!(pf == &properties[0]))    //pf指向最后一个数据的后面一个位置
	{
		cout << "Enter evaluation factor: ";
		double factor;
		while (!(cin >> factor)) {
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input; Please enter a number: ";
		}
		revalue(factor, properties, pf);
		show_array(properties, pf);
	}
	cout << "Done!\n";
	system("pause");
	return 0;
}

double * fill_array(double * begin, double * end) {
	double * pt;
	double temp;
	int i = 0;
	for (pt = begin; pt != end; pt++)
	{
		cout << "Enter value #" << i+1<< " :";
		cin >> temp;
		if (!cin) {
			cin.clear();
			while (cin.get() != '\n')
				continue;
			cout << "Bad input; input process terminated.\n";
			break;
		}
		else if (temp < 0)
			break;
		*pt = temp;
		i++;
	}
	return begin + i;
}

void show_array(double * begin, double * end) {
	double * ps;
	int j = 0;
	for (ps = begin; ps != end; ps++,j++) {
		cout << "Property #" << j + 1 << ": $";
		cout << *ps << endl;
	}
}

void revalue(double r,double * begin, double * end) 
{
	double * pv;
	for (pv = begin; pv != end; pv++)
		(*pv) *= r;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值