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
@@ -0,0 +1,41 @@
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "urls.h"
using std::cin;
using std::cout;
using std::endl;
using std::sort;
using std::string;
using std::vector;
int main() {
// 输入文本
cout << "输入一段文本: ";
string input;
std::getline(cin, input);
// 提取所有 URL
vector<string> urls = find_urls(input);
// 筛选出以 http:// 或 https:// 开头的 URL
vector<string> http_urls;
for (const string& url : urls) {
if (url.find("http://") == 0 || url.find("https://") == 0) {
http_urls.push_back(url);
}
}
// 按字母顺序排序
sort(http_urls.begin(), http_urls.end());
// 输出结果
cout << "排序后的 HTTP URLs:" << endl;
for (const string& url : http_urls) {
cout << url << endl;
}
return 0;
}
@@ -0,0 +1,101 @@
#include <algorithm>
#include <cctype>
#include <string>
#include <vector>
#include "urls.h"
using std::find;
using std::find_if;
#ifndef _MSC_VER
using std::isalnum;
using std::isalpha;
using std::isdigit;
#endif
using std::search;
using std::string;
using std::vector;
bool not_url_char(char);
string::const_iterator
url_end(string::const_iterator, string::const_iterator);
string::const_iterator
url_beg(string::const_iterator, string::const_iterator);
vector<string> find_urls(const string& s)
{
vector<string> ret;
typedef string::const_iterator iter;
iter b = s.begin(), e = s.end();
// look through the entire input
while (b != e) {
// look for one or more letters followed by `://'
b = url_beg(b, e);
// if we found it
if (b != e) {
// get the rest of the \s-1URL\s0
iter after = url_end(b, e);
// remember the \s-1URL\s0
ret.push_back(string(b, after));
// advance `b' and check for more \s-1URL\s0s on this line
b = after;
}
}
return ret;
}
string::const_iterator
url_end(string::const_iterator b, string::const_iterator e)
{
return find_if(b, e, not_url_char);
}
bool not_url_char(char c)
{
// characters, in addition to alphanumerics, that can appear in a \s-1URL\s0
static const string url_ch = "~;/?:@=&$-_.+!*'(),";
// see whether `c' can appear in a \s-1URL\s0 and return the negative
return !(isalnum(c) ||
find(url_ch.begin(), url_ch.end(), c) != url_ch.end());
}
string::const_iterator
url_beg(string::const_iterator b, string::const_iterator e)
{
static const string sep = "://";
typedef string::const_iterator iter;
// `i' marks where the separator was found
iter i = b;
while ((i = search(i, e, sep.begin(), sep.end())) != e) {
// make sure the separator isn't at the beginning or end of the line
if (i != b && i + sep.size() != e) {
// `beg' marks the beginning of the protocol-name
iter beg = i;
while (beg != b && isalpha(beg[-1]))
--beg;
// is there at least one appropriate character before and after the separator?
if (beg != i && !not_url_char(i[sep.size()]))
return beg;
}
// the separator we found wasn't part of a \s-1URL\s0; advance `i' past this separator
i += sep.size();
}
return e;
}
@@ -0,0 +1,9 @@
#ifndef GUARD_urls_h
#define GUARD_urls_h
#include <vector>
#include <string>
std::vector<std::string> find_urls(const std::string& s);
#endif
@@ -0,0 +1,37 @@
// source file for `Student_info'-related functions
#include "Student_info.h"
using std::istream; using std::vector;
bool compare(const Student_info& x, const Student_info& y)
{
return x.name < y.name;
}
istream& read(istream& is, Student_info& s)
{
// read and store the student's name and midterm and final exam grades
is >> s.name >> s.midterm >> s.final;
read_hw(is, s.homework); // read and store all the student's homework grades
return is;
}
// read homework grades from an input stream into a `vector<double>'
istream& read_hw(istream& in, vector<double>& hw)
{
if (in) {
// get rid of previous contents
hw.clear();
// read homework grades
double x;
while (in >> x)
hw.push_back(x);
// clear the stream so that input will work for the next student
in.clear();
}
return in;
}
@@ -0,0 +1,19 @@
#ifndef GUARD_Student_info
#define GUARD_Student_info
// `Student_info.h' header file
#include <iostream>
#include <string>
#include <vector>
struct Student_info {
std::string name;
double midterm, final;
std::vector<double> homework;
};
bool compare(const Student_info&, const Student_info&);
std::istream& read(std::istream&, Student_info&);
std::istream& read_hw(std::istream&, std::vector<double>&);
#endif
@@ -0,0 +1,41 @@
#include <stdexcept>
#include <vector>
#include "grade.h"
#include "median.h"
#include "Student_info.h"
using std::domain_error; using std::vector;
// compute a student's overall grade from midterm and final exam grades and homework grade
double grade(double midterm, double final, double homework)
{
return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}
// compute a student's overall grade from midterm and final exam grades
// and vector of homework grades.
// this function does not copy its argument, because `median' does so for us.
double grade(double midterm, double final, const vector<double>& hw)
{
if (hw.size() == 0)
throw domain_error("student has done no homework");
return grade(midterm, final, median(hw));
}
double grade(const Student_info& s)
{
return grade(s.midterm, s.final, s.homework);
}
// predicate to determine whether a student failed
bool fgrade(const Student_info& s)
{
return grade(s) < 60;
}
bool pgrade(const Student_info& s)
{
return !fgrade(s);
}
@@ -0,0 +1,14 @@
#ifndef GUARD_grade_h
#define GUARD_grade_h
#include <vector>
#include "Student_info.h"
double grade(double, double, double);
double grade(double, double, const std::vector<double>&);
double grade(const Student_info&);
bool pgrade(const Student_info&);
bool fgrade(const Student_info&);
#endif
@@ -0,0 +1,57 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include "Student_info.h"
#include "grade.h"
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
// 定义常量:及格分数
const double PASS_GRADE = 60.0;
int main() {
vector<Student_info> students;
Student_info record;
// 读取学生信息
cout << "期中成绩占比20%、期末成绩占比40%,平时成绩占比40%):" << endl;
cout << "输入学生信息(姓名、期中成绩、期末成绩,平时成绩):" << endl;
cout << "输入Ctrl+z后回车退出。" << endl;
while (read(cin, record)) {
students.push_back(record);
}
// 将学生分为及格和不及格两组
vector<Student_info> pass, fail;
for (const auto& student : students) {
if (grade(student) >= PASS_GRADE) {
pass.push_back(student);
}
else {
fail.push_back(student);
}
}
// 按名字排序
std::sort(pass.begin(), pass.end(), compare);
std::sort(fail.begin(), fail.end(), compare);
// 输出及格学生信息
cout << "合格学生:" << endl;
for (const auto& student : pass) {
cout << student.name << ": 总成绩 = " << grade(student) << endl;
}
// 输出不及格学生信息
cout << "不合格学生:" << endl;
for (const auto& student : fail) {
cout << student.name << ": 总成绩 = " << grade(student) << endl;
}
return 0;
}
@@ -0,0 +1,30 @@
// source file for the `median' function
#include <algorithm> // to get the declaration of `sort'
#include <stdexcept> // to get the declaration of `domain_error'
#include <vector> // to get the declaration of `vector'
using std::domain_error; using std::sort; using std::vector;
#include "median.h"
// compute the median of a `vector<double>'
// note that calling this function copies the entire argument `vector'
double median(vector<double> vec)
{
#ifdef _MSC_VER
typedef std::vector<double>::size_type vec_sz;
#else
typedef vector<double>::size_type vec_sz;
#endif
vec_sz size = vec.size();
if (size == 0)
throw domain_error("median of an empty vector");
sort(vec.begin(), vec.end());
vec_sz mid = size/2;
return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
}
@@ -0,0 +1,9 @@
#ifndef GUARD_median_h
#define GUARD_median_h
// `median.h'--final version
#include <vector>
double median(std::vector<double>);
#endif