#include <iostream>
const int Max{ 5 };
double* fill_array(double* ptrBegin, double* ptrEnd);
void show_array(const double* ptrBegin, const double* ptrEnd);
void revalue(double r, double* ptrBegin, double* ptrEnd);
int main()
{
double properties[Max];
double* pEnd = fill_array(properties, properties + Max);
show_array(properties, pEnd);
if (pEnd - properties > 0)
{
std::cout << "请输入系数:";
double factor;
while ( !(std::cin >> factor))
{
std::cin.clear();
while (std::cin.get() != '\n')
{
continue;
}
std::cout << "输入有误,请输入有效数字: ";
}
revalue(factor, properties, pEnd);
show_array(properties, pEnd);
}
return 0;
}
double* fill_array(double* ptrBegin, double* ptrEnd)
{
double temp;
double* ptr { nullptr };
unsigned int i{};
for (ptr = ptrBegin; ptr != ptrEnd; ptr++)
{
std::cout << "输入第 " << i + 1 << " 号数字:";
std::cin >> *ptr;
if (!std::cin)
{
std::cin.clear();
while (std::cin.get() != '\n')
{
continue;
}
std::cout << "输入有误, 输入过程终止。" << std::endl;
break;
}
else if(*ptr < 0)
{
break;
}
i++;
}
return ptr;
}
void show_array(const double* ptrBegin, const double* ptrEnd)
{
const double* ptr{ nullptr };
unsigned int i{};
for (ptr = ptrBegin; ptr != ptrEnd; ++ptr)
{
std::cout << "第 " << i + 1 << " 号数字:";
std::cout << *ptr << std::endl;
i++;
}
}
void revalue(double r, double* ptrBegin, double* ptrEnd)
{
double* ptr{ nullptr };
for (ptr = ptrBegin; ptr != ptrEnd; ++ptr)
{
*ptr *= r;
}
}