实验1 现代C++编程初体验
实验任务1
程序源代码:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
template<typename T>
void output(const T& c);
void test1();
void test2();
void test3();
int main() {
std::cout << "测试1: \n";
test1();
std::cout << "\n测试2: \n";
test2();
std::cout << "\n测试3: \n";
test3();
}
template <typename T>
void output(const T& c) {
for (auto& i : c)
std::cout << i << ' ';
std::cout << '\n';
}
void test1() {
using namespace std;
string s0{ "0123456789" };
cout << "s0 = " << s0 << endl;
string s1(s0);
reverse(s1.begin(), s1.end());
cout << "s1 = " << s1 << endl;
string s2(s0.size(), ' ');
reverse_copy(s0.begin(), s0.end(), s2.begin());
cout << "s2 = " << s2 << endl;
}
void test2() {
using namespace std;
vector<int> v0{ 2, 0, 4, 9 };
cout << "v0: "; output(v0);
vector<int> v1{ v0 };
reverse(v1.begin(), v1.end());
cout << "v1: "; output(v1);
vector<int> v2{ v0 };
reverse_copy(v0.begin(), v0.end(), v2.begin());
cout << "v2: "; output(v2);
}
void test3() {
using namespace std;
vector<int> v0{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
cout << "v0: "; output(v0);
vector<int> v1{ v0 };
rotate(v1.begin(), v1.begin() + 1, v1.end());
cout << "v1: "; output(v1);
vector<int> v2{ v0 };
rotate(v2.begin(), v2.begin() + 2, v2.end());
cout << "v2: "; output(v2);
vector<int> v3{ v0 };
rotate(v3.begin(), v3.end() - 1, v3.end());
cout << "v3: "; output(v3);
vector<int> v4{ v0 };
rotate(v4.begin(), v4.end() - 2, v4.end());
cout << "v4: "; output(v4);
}
运行测试截图:
回答问题
问题1:reverse直接反转原容器元素顺序,reverse_copy是把原容器反转后的元素拷贝到新容器。
问题2:rotate通过将[first,middle)区间元素移到[middle,last)之后改变元素顺序,三个参数代表待旋转区间起始迭代器,新起始位置迭代器,区间末尾迭代器。
实验任务2
程序源代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iomanip>
#include <cstdlib>
#include <ctime>
template<typename T>
void output(const T& c);
int generate_random_number();
void test1();
void test2();
int main() {
std::srand(std::time(0));
std::cout << "测试1: \n";
test1();
std::cout << "\n测试2: \n";
test2();
}
template <typename T>
void output(const T& c) {
for (auto& i : c)
std::cout << i << ' ';
std::cout << '\n';
}
int generate_random_number() {
return std::rand() % 101;
}
void test1() {
using namespace std;
vector<int> v0(10);
generate(v0.begin(), v0.end(), generate_random_number);
cout << "v0: "; output(v0);
vector<int> v1{ v0 };
sort(v1.begin(), v1.end());
cout << "v1: "; output(v1);
vector<int> v2{ v0 };
sort(v2.begin() + 1, v2.end() - 1);
cout << "v2: "; output(v2);
}
void test2() {
using namespace std;
vector<int> v0(10);
generate(v0.begin(), v0.end(), generate_random_number);
cout << "v0: "; output(v0);
auto min_iter = min_element(v0.begin(), v0.end());
auto max_iter = max_element(v0.begin(), v0.end());
cout << "最小值: " << *min_iter << endl;
cout << "最大值: " << *max_iter << endl;
auto ans = minmax_element(v0.begin(), v0.end());
cout << "最小值: " << *(ans.first) << endl;
cout << "最大值: " << *(ans.second) << endl;
double avg1 = accumulate(v0.begin(), v0.end(), 0.0) / v0.size();
cout << "均值: " << fixed << setprecision(2) << avg1 << endl;
sort(v0.begin(), v0.end());
double avg2 = accumulate(v0.begin() + 1, v0.end() - 1, 0.0) / (v0.size() - 2);
cout << "去掉最大值、最小值之后,均值: " << avg2 << endl;
}
运行测试截图:
回答问题
问题1:generate按规则为容器元素生成新值以填充容器
问题2:minmax_element一次遍历同时找最大最小元素,效率更高,代码更简洁。
问题3:lambda适临时简单逻辑,需捕获局部变量,替代轻量级函数对象的场景。
实验任务3
程序源代码:
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
unsigned char func(unsigned char c);
void test1();
void test2();
int main() {
std::cout << "测试1: 字符串大小写转换\n";
test1();
std::cout << "\n测试2: 字符变换\n";
test2();
}
unsigned char func(unsigned char c) {
if (c == 'z')
return 'a';
if (c == 'Z')
return 'A';
if (std::isalpha(c))
return static_cast<unsigned char>(c + 1);
return c;
}
void test1() {
std::string s1{ "Hello World 2049!" };
std::cout << "s1 = " << s1 << '\n';
std::string s2;
for (auto c : s1)
s2 += std::tolower(c);
std::cout << "s2 = " << s2 << '\n';
std::string s3;
for (auto c : s1)
s3 += std::toupper(c);
std::cout << "s3 = " << s3 << '\n';
}
void test2() {
std::string s1{ "I love cosmos!" };
std::cout << "s1 = " << s1 << '\n';
std::string s2(s1.size(), ' ');
std::transform(s1.begin(), s1.end(),
s2.begin(),
func);
std::cout << "s2 = " << s2 << '\n';
}
运行测试截图:
回答问题
问题1:将字母z转换成a,字母Z转换成A,其他字母转换成下一个字母,非字母字符保持不变。
问题2:tolower把大写字母转换成小写,非大写保持不变。toupper把小写字母转大写,非小写不变。
问题3:四个参数依次是输入起始,输入结束,输出起始,处理函数;改后会覆盖输入序列S1原有内容。
实验任务4
程序源代码:
#include <iostream>
#include <string>
#include <algorithm>
bool is_palindrome(const std::string& s);
bool is_palindrome_ignore_case(const std::string& s);
int main() {
using namespace std;
string s;
while (cin >> s) {
cout << boolalpha
<< "区分大小写: " << is_palindrome(s) << "\n"
<< "不区分大小写: " << is_palindrome_ignore_case(s) << "\n\n";
}
}
bool is_palindrome(const std::string& s)
{
using namespace std;
string m=s;
reverse(m.begin(),m.end());
return m == s;
}
bool is_palindrome_ignore_case(const std::string& s)
{
using namespace std;
string m,n;
for (auto c : s)
{
m += tolower(static_cast<unsigned char>(c));
}
n = m;
reverse(m.begin(), m.end());
return n == m;
}
运行测试截图:
回答问题
问题1:使用getline(cin,s)进行多组输入
实验任务5
程序源代码:
#include <iostream>
#include <string>
#include <algorithm>
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";
}
}
std::string dec2n(int x, int n)
{
std::string sum;
std::string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
while (x)
{
sum.insert(0,1,chars[(x % n)]);
x /= n;
}
return sum;
}
运行测试截图:
实验任务6
程序源代码:
#include <iostream>
#include <string>
#include <algorithm>
#define N 26
char func( char c,int n);
int main() {
std::string x = "abcdefghijklmnopqrstuvwxyz";
int y[N];
for (int i = 0; i < N; i++)
y[i] = i + 1;
std::cout << " ";
for (auto i : x)
{
std::cout << " " << i;
}
std::cout << std::endl;
for (auto j : y)
{
if (j < 10)
std::cout << " " << j;
else
std::cout << j;
for (auto i : x)
{
std::cout << " "<<static_cast<char>(toupper(func(i, j)));
}
std::cout << std::endl;
}
return 0;
}
char func(char c,int n)
{
if (c >= 'a' && c <= 'z')
{
return 'a' + (c - 'a' + n) % 26;
}
if (c >= 'A' && c <= 'Z')
{
return 'A' + (c - 'A' + n) % 26;
}
return c;
}
运行测试截图:
实验任务7
程序源代码:
#include <iostream>
#include <string>
#include <algorithm>
#define N 10
int main() {
using namespace std;
srand(static_cast<unsigned int>(time(0)));
string x = "+-*/";
int result[N+1];
int answer[N+1];
int num1 = 0,num2=0;
double rate = 0.0;
for (int i = 0; i < 10; i++)
{
string f;
f.insert(0, 1, x[rand() % 4]);
if (f == "-")
{
int b = rand() % 10;
int a = rand() % 10;
while (a < b)
{
a = rand() % 10;
}
cout << a << "-" << b << "=" ;
result[num1++] = a - b;
cin >> answer[num2++];
}
if (f == "/")
{
int a = rand() % 10;
int b = rand() % 10;
while (a == 0)
{
a = rand() % 10;
}
while (b == 0)
{
b = rand() % 10;
}
while (a % b != 0)
{
a = rand() % 10;
b = rand() % 10;
}
cout << a << "/" << b << "=" ;
result[num1++] = a / b;
cin >> answer[num2++];
}
if (f == "*")
{
int a = rand() % 10;
int b = rand() % 10;
cout << a << "*" << b << "=" ;
result[num1++] = a * b;
cin >> answer[num2++];
}
if (f == "+")
{
int a = rand() % 10;
int b = rand() % 10;
cout << a << "+" << b << "=" ;
result[num1++] = a + b;
cin >> answer[num2++];
}
}
for (int i = 0; i < 10; i++)
{
if (answer[i] == result[i]) rate+=1;
}
rate = rate * 10;
printf("正确率:%.2f%%\n", rate);
return 0;
}
运行测试截图: