我正在做一个硬件作业,我的教授使用这段代码来测试我们的程序:
int main()
{
const int SZ1 = 10;
const int SZ2 = 7;
const int SZ3 = 5;
float array1[SZ1];
float array2[SZ2];
float array3[SZ3];
DisplayValues(SortValues(GetValues(array1, SZ1), SZ1), SZ1);
DisplayValues(SortValues(GetValues(array2, SZ2), SZ2), SZ2);
DisplayValues(SortValues(GetValues(array3, SZ3), SZ3), SZ3);
return EXIT_SUCCESS;
}
float *DisplayValues(float *p, size_t n)
{
float previous = *p, *ptr, *end = p + n;
setiosflags(ios_base::fixed);
for (ptr = p; ptr < end; ++ptr) // get each element
{
cout << *ptr << '\n';
if (ptr != p) // if not first element...
{
if (previous < *ptr) // ...check sort order
{
cerr << "Error - Array sorted incorrectly\n";
return NULL;
}
}
previous = *ptr; // save this element
}
resetiosflags(ios_base::fixed);
return p;
}
#endif
我用
float *GetValues(float *p, size_t n)
{
float input;
float *start = p;
cout << "Enter " << n << " float values separated by whitespace: \n";
while (scanf("%f", &input) == 1) {
*p++ = input;
}
return start;
}
按照他的指示从终端窗口获取输入,并使用 ctrl + d 输入 EOF 字符,以便第一次调用 DisplayValues(SortValues(GetValues(array1, SZ1), SZ1), SZ1) 发生。但是,当 DisplayValues(SortValues(GetValues(array2, SZ2), SZ2), SZ2); 叫做。这有什么原因或解决方法吗?谢谢。