实验一

任务一

  • 运行截图
    renwu1

  • 总结
    reverse不产生新的内存,而reverse_copy必须提前准备一个大的内存

    rotate:它的作用是将 [first, middle) 范围内的元素移动到序列的末尾,并将 [middle, last) 范围内 的元素移动到序列的开头。
    first:指向序列起始位置的迭代器。
    middle:指向旋转的“分界点”的迭代器。
    last:指向序列结束位置(最后一个元素的下一位)的迭代器

任务二

+运行截图
image

+总结
generate的主要作用是用指定的函数生成的值来填充一个区间。
优势主要是性能更高、效率更好(遍历次数更少)。
完全等同。

任务三

+运行截图
image
+总结
func 是一个字符变换函数。

tolower(c):将传入的大写字母字符 c 转换为小写字母。如果传入的字符不是大写字母(例如数字、标点或本来就是小写),则原样返回。

toupper(c):将传入的小写字母字符 c 转换为大写字母。如果传入的字符不是小写字母,则原样返回。

4个参数的意义:

first1:源序列的起始迭代器

last1:源序列的结束迭代器

d_first (或 result):目标序列的起始迭代器,即结果要存放的位置

unary_op:一个一元函数(或 Lambda 表达式、仿函数),用于对源序列中的每个元素执行转换操作

区别:节省了空间。

任务四

+代码`

include

include

include

using namespace std;
bool is_palindrome(const std::string &s);
bool is_palindrome_ignore_case(const std::string &s);

int main() {
using namespace std;
string s;

// 多组输入,直到按下Ctrl+Z结束测试
while(cin >> s) {
cout << boolalpha
<< "区分大小写: " << is_palindrome(s) << "\n"
<< "不区分大小写: " << is_palindrome_ignore_case(s) << "\n\n";
}
}

// 函数is_palindrome定义

bool is_palindrome(const string &s) {
string t=s;
reverse(t.begin(),t.end());
return (s==t);
}

// 函数is_palindrome_ignore_case定义
bool is_palindrome_ignore_case(const string &s) {

std::string t=s;
for (char &c:t)
c=static_cast(tolower(static_cast(c)));
std::string q=s;
for (char &c:q)
c=static_cast(tolower(static_cast(c)));
reverse(t.begin(),t.end());

return (q==t);
}`

+运行截图
77f4bfbdcf531e77b124408664eb21db

任务五

+代码`

include

include

include

using namespace std;
std::string dec2n(int x, int n = 2);

int main() {
int x;
while(std::cin >> x) {
std::cout << "十进制: " << x << '\n'
<< "二进制: " << dec2n(x) << '\n'
<< "八进制: " << dec2n(x, 8) << '\n'
<< "十二进制: " << dec2n(x, 12) << '\n'
<< "十六进制: " << dec2n(x, 16) << '\n'
<< "三十二进制: " << dec2n(x, 32) << "\n\n";
}
}

// 函数dec2n定义
std::string dec2n(int x, int n ){
string s;
char c;
int k=0;
if(x==0)s='0';
while(x!=0){
k=x%n;
if(k<10)c='0'+k;
else c='0'+k+7;
s.push_back(c);
x=x/n;
}
reverse(s.begin(),s.end());
return s;
}`
+运行截图
0d96e44322f4560daa497c738f1114af

任务六

+代码
`#include

include

int main() {
std::cout << " ";
for (char c = 'a'; c <= 'z'; ++c) {
std::cout << c << " ";
}
std::cout << "\n";

for (int i = 1; i <= 26; ++i) {
std::cout << std::setw(2) << std::left << i;
for (int j = 0; j < 26; ++j) {
char c = 'A' + (i - 1 + j) % 26;
std::cout << c << " ";
}
std::cout << "\n";
}

return 0;
}`
+运行截图
image

任务七

+代码
‘#include

include

include

include

using namespace std;

int main() {
srand(time(0));

int correct_count = 0;
const int total_questions = 10;

cout << "欢迎进行小学生算术测试!\n";
cout << "-----------------------------------\n";

for (int i = 1; i <= total_questions; ++i) {
int op = rand() % 4;
int num1, num2, answer, user_answer;
char symbol;

if (op == 0) {
num1 = rand() % 10 + 1;
num2 = rand() % 10 + 1;
symbol = '+';
answer = num1 + num2;
}
else if (op == 1) {
num1 = rand() % 10 + 1;
num2 = rand() % 10 + 1;
if (num1 < num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
symbol = '-';
answer = num1 - num2;
}
else if (op == 2) {
num1 = rand() % 10 + 1;
num2 = rand() % 10 + 1;
symbol = '*';
answer = num1 * num2;
}
else {
num2 = rand() % 10 + 1;
int multiplier = rand() % 10 + 1;
num1 = num2 * multiplier;
symbol = '/';
answer = num1 / num2;
}

cout << "第 " << i << " 题: " << num1 << " " << symbol << " " << num2 << " = ";
cin >> user_answer;

if (user_answer == answer) {
cout << "回答正确!\n";
correct_count++;
} else {
cout << "回答错误。正确答案是: " << answer << "\n";
}
cout << "-----------------------------------\n";
}

double accuracy = (static_cast(correct_count) / total_questions) * 100.0;

cout << "测试结束!\n";
cout << "你答对了 " << correct_count << " 道题。\n";
cout << "正确率为: " << fixed << setprecision(2) << accuracy << "%\n";

return 0;
}’

posted @ 2026-10-22 17:36  17cc  阅读(5)  评论(0)    收藏  举报