gpt4 book ai didi

c++使用指针以相反的顺序显示句子

转载 作者:搜寻专家 更新时间:2023-10-31 01:50:32 26 4
gpt4 key购买 nike

我正在尝试构建一个程序,从用户那里获取简短的句子并以相反的顺序显示它们。不幸的是,我刚开始使用 c++,我需要知道这样做。例如:如果用户输入:

I like the red color
blue is also nice
and green is lovely
but I don't like orange

输出:

but I don't like orange
and green is lovely
blue is also nice
I like the red color

提前致谢!

#include<iostream>
#include<string>
using namespace std;

const int SIZE= 500;

int main()
{

const int SIZE = 500;
char mystring[SIZE];
int i;
for(i=0; i<SIZE; i++)
{

cout<<"Enter a string: ";
cin.getline(mystring, SIZE);
} while (mystring != 0);

char * reverse= new char[strlen(mystring) + 1];
char *p1 = mystring+ strlen(mystring);
char *p2 = reverse;


while(p1 != mystring)
{
p1--;
*p2= *p1;
p2++;
}

*p2 = '\0';
cout << reverse<<endl;


system("PAUSE");
return 0;
}

最佳答案

您打算采用以下算法来解决这个问题:

  1. 将文件加载到缓冲区中,以空字符终止。
  2. 将指针 p 定位到最后一个缓冲区槽的位置。
  3. p 没有指向缓冲区的开始时,执行以下操作:
    • 如果字符是换行符('\n')则
      1. 将字符串 过去 换行符 (p+1) 发送到标准输出。
      2. 用空字符覆盖p指向的换行符。
    • p 递减一个字符位置。
  4. 上面的循环结束后还剩下一行:第一行。将它发送到标准输出,你就完成了。

或者我被引导相信。需要考虑的重要事项如下:

  1. 该算法是否适用于空文件?
  2. 该算法是否适用于仅包含换行符的文件?
  3. 该算法是否适用于没有尾随换行符的多行文件?
  4. 该算法是否适用于没有尾随换行符的单行文件?
  5. 该算法是否适用于带有尾随换行符的多行文件?
  6. 该算法是否适用于带有尾随换行符的单行文件?

话虽如此,这是一个潜在的候选人:

#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
using namespace std;

int main(int argc, char *argv[])
{
// assume the file to reverse-print is the first
// command-line parameter. if we don't have one
// we need to leave now.
if (argc < 2)
return EXIT_FAILURE;

// will hold our file data
std::vector<char> data;

// open file, turning off white-space skipping
ifstream inf(argv[1]);
inf.seekg(0, inf.end);
size_t len = inf.tellg();
inf.seekg(0, inf.beg);

// resize buffer to hold (len+1) chars
data.resize(len+1);
inf.read(&data[0], len);
data[len] = 0; // terminator

// walk the buffer backwards. at each newline, send
// everything *past* it to stdout, then overwrite the
// newline char with a nullchar (0), and continue on.
char *start = &data[0];
char *p = start + (data.size()-1);
for (;p != start; --p)
{
if (*p == '\n')
{
if (*(p+1))
cout << (p+1) << endl;
*p = 0;
}
}

// last line (the first line)
cout << p << endl;

return EXIT_SUCCESS;
}

输入

I like the red color
blue is also nice
and green is lovely
but I don't like orange

输出

but I don't like orange
and green is lovely
blue is also nice
I like the red color

一种相当简单的方法

很多更简单的方法可以做到这一点,我会在评论中解释每个步骤。您很可能不能使用这样的东西,但重要的是您了解什么时候可以使用:

#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
using namespace std;

int main(int argc, char *argv[])
{
// assume the file to reverse-print is the first
// command-line parameter. if we don't have one
// we need to leave now.
if (argc < 2)
return EXIT_FAILURE;

// collection that will hold our lines of text
vector<string> lines;

// read lines one at a time until none are returned
// pushing each line in to our vector.
ifstream inf(argv[1]);
string line;
while (getline(inf, line))
lines.push_back(line);
inf.close();

// a LOT happens in the next single line of code, and
// I will try to describe each step along the way.
//
// we use std::copy() to copy all "items" from
// a beginning and ending iterator pair. the
// target of the copy is another iterator.
//
// our target iterator for our formatted ouput
// is a special iterator class designed to
// perform an output-stream insertion operation
// (thats the << operator) to the stream it is
// constructed with (in our case cout) using each
// item we give it from our copy-iteration. to use
// this class the "copied" item must support the
// traditional insertion operator <<, which of
// course, std::string does. after each item is
// written, the provided suffix (in our case \n)
// is written as well. without this all the lines
// would be ganged together.
//
// lastly, to glue this together (and the whole
// reason we're here), we use a pair of special
// iterators designed to work just like the regular
// begin() and end() iterators you're familiar with,
// when traversing forward in a sequence, but these
// ones, rbegin() and rend(), move from the last
// item in the sequence to the first item, which is
// *exactly* what we need.

copy(lines.rbegin(), lines.rend(),
ostream_iterator<string>(cout, "\n"));

// and thats it.
return EXIT_SUCCESS;
}

输入

I like the red color
blue is also nice
and green is lovely
but I don't like orange

输出

but I don't like orange
and green is lovely
blue is also nice
I like the red color

更新:合并用户输入

为第二个版本合并用户输入的示例是:

#include <iostream>
#include <iterator>
#include <vector>
using namespace std;

int main(int argc, char *argv[])
{
// collection that will hold our lines of text
vector<string> lines;
do
{ // prompt the user
cout << "Sentance (<enter> to exit): ";
string line;
if (!getline(cin, line) || line.empty())
break;
lines.push_back(line);
} while (true);

// send back to output using reverse iterators
// to switch line order.
copy(lines.rbegin(), lines.rend(),
ostream_iterator<string>(cout, "\n"));

return EXIT_SUCCESS;
}

关于c++使用指针以相反的顺序显示句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14906323/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com