2024年12月完成(补充提交)

This commit is contained in:
Cx330 committed 2025-03-29 23:02:48 +08:00
commit 4e2bf47db0
39 files changed
+2106

No files matched your search

@@ -0,0 +1,45 @@
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "split.h"
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
// 函数:反转字符串
string reverseString(const string& s) {
return string(s.rbegin(), s.rend());
}
int main() {
// 读取输入文本
cout << "输入一段文本: ";
string input;
std::getline(cin, input);
// 拆分文本为单词
vector<string> words = split(input);
// 对每个单词进行反转
for (auto& word : words) {
word = reverseString(word);
}
// 按照反转后的单词排序
std::sort(words.begin(), words.end());
// 输出排序后的单词,保持与原输入格式一致
for (size_t i = 0; i < words.size(); ++i) {
if (i > 0) {
cout << " "; // 单词间加空格
}
cout << words[i];
}
cout << endl;
return 0;
}
@@ -0,0 +1,43 @@
#include <cctype>
#include <string>
#include <vector>
#include "split.h"
using std::vector;
using std::string;
#ifndef _MSC_VER
using std::isspace;
#endif
vector<string> split(const string& s)
{
vector<string> ret;
typedef string::size_type string_size;
string_size i = 0;
// invariant: we have processed characters `['original value of `i', `i)'
while (i != s.size()) {
// ignore leading blanks
// invariant: characters in range `['original `i', current `i)' are all spaces
while (i != s.size() && isspace(s[i]))
++i;
// find end of next word
string_size j = i;
// invariant: none of the characters in range `['original `j', current `j)' is a space
while (j != s.size() && !isspace(s[j]))
++j;
// if we found some nonwhitespace characters
if (i != j) {
// copy from `s' starting at `i' and taking `j' `\-' `i' chars
ret.push_back(s.substr(i, j - i));
i = j;
}
}
return ret;
}
@@ -0,0 +1,8 @@
#ifndef GUARD_split_h
#define GUARD_split_h
#include <vector>
#include <string>
std::vector<std::string> split(const std::string&);
#endif