q8232356 于 2015.06.13 08:57 提问

c++初学者一枚,希望有好心人能够帮我解答这行代码的意思

{

cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input process terminated.\n";
if(i == 0)
return ptemp;
else
return --ptemp;
}

#include
const int Max = 5;

// function prototypes
double * fill_array(double *pfirst, double *pend);
void show_array( double *pfirst, double *pend);
void revalue(double *pfirst, double *pend, double r);

int main()
{
using namespace std;
double properties[Max];

``````double  *pos = fill_array(properties, properties+(Max-1));
show_array(properties, properties+(Max-1));
cout << "Enter revaluation factor: ";
double factor;
cin >> factor;
revalue(properties, properties+(Max-1), factor);
show_array(properties, properties+(Max-1));
cout << "Done.\n";
return 0;
``````

}

double * fill_array(double *pfirst, double *pend)
{
using namespace std;
double temp;
double * ptemp;
int i = 0;

``````for (ptemp = pfirst; ptemp <= pend; ptemp++)
{
cout << "Enter value #" << (i + 1) << ": ";
cin >> temp;

{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input process terminated.\n";
if(i == 0)
return ptemp;
else
return --ptemp;
}
else if (temp < 0)     // signal to terminate
break;
pfirst[i] = temp;
i++;
}
return --ptemp;
``````

}

// the following function can use, but not alter,
// the array whose address is pfirst
void show_array( double *pfirst, double *pend)
{
using namespace std;
double * ptemp;
int i = 0;
for (ptemp = pfirst; ptemp <= pend; ptemp++,i++)
{
cout << "Property #" << (i + 1) << ": \$";
cout << *ptemp << endl;
}
}

// multiplies each element of ptemp by r
void revalue(double *pfirst, double *pend, double r)
{
double *ptemp;
for (ptemp = pfirst; ptemp <= pend; ptemp++)
*ptemp *= r;
}

7个回答

devmiao      2015.06.13 09:12

return --ptemp;

return ptemp - 1;

happyparrot      2015.06.13 09:16

91program      2015.06.13 11:36

-- 运算符在前，先计算再返回。如果 -- 运算符在后，就没有意义了：先返回再减一。

a1321652782   2015.06.13 16:29

u012965373   2015.06.13 20:03

--ptemp;代表着ptemp - 1，刚才那么些运行快些，

crazy_father   2015.06.13 23:07

###如果lz的疑问是：--ptemp 是什么东西？

###如果lz的疑问是：--ptemp 放在这里的意义是个啥？

henuyx   2015.06.14 12:45

for里有个 ptemp++