我似乎无法理解此程序的期望。谁能帮我?我是一个绝对无能的第一年学生。包括指针在内的C++作业
编写一个读取文本行并将它们附加到char缓冲区[1000]的程序。通过调用cin.get(ch)一次读取一个字符,其中ch是char类型的变量。使用输入重定向。阅读1,000个字符后停止。在阅读文本时,将所有换行符'\ n'替换为'\ 0'结束符。建立一个数组char * lines [100],以便该数组中的指针指向文本中行的开始。如果输入有更多的行,则只考虑100个输入行。然后以相反的顺序显示行,从最后一行输入行开始。 这是我到目前为止有:
#include <iostream>
using namespace std;
int main()
{
const int BUFFER_CAPACITY = 1000;
const int LINES_CAPACITY = 100;
char buffer[BUFFER_CAPACITY];
char* lines[LINES_CAPACITY];
int buffer_position = 0;
int lines_position = 0;
bool more = true;
bool newline = true;
while (more)
{
char c = cin.get();
if (cin.fail())
{
more = false;
}
else if (buffer_position >= BUFFER_CAPACITY) // at (or above) capacity
{
more = false;
}
if (newline)
{
if (lines_position < LINES_CAPACITY)
{
lines[lines_position] = buffer + buffer_position;
lines_position++;
newline = false;
}
else { more = false; }
}
if (more)
{
if (c == '\n')
{
buffer[buffer_position] = '\0';
buffer_position++;
newline = true;
}
else
{
buffer[buffer_position] = c;
buffer_position++;
}
}
}
buffer[BUFFER_CAPACITY - 1] = '\0';
for (int i = lines_position - 1; i >= 0; i--)
{
cout << lines[i] << endl;
}
return 0;
}
到目前为止你有什么?你几乎可以通过阅读问题陈述来编写代码。 –
推荐阅读:http://ericlippert.com/2014/03/21/find-a-simpler-problem/ – Steve