实验 1:现代 C++ 编程初体验
实验 1:现代 C++ 编程初体验
语言标准:C++ 17
任务1:
- 源代码:
include
include
include
include
using namespace std;
template
void output (const T&c);
void test1();
void test2();
void test3();
int main(){
cout<<"test one"<<endl;
test1();
cout<<"test two"<<endl;
test2();
cout<<"test three"<<endl;
test3();
}
template
void output (const T&c){
for(auto &i:c)
cout<<i<<' ';
cout<<"\n";
}
void test1(){
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(){
vector
cout<<"v0:";output(v0);
vector
reverse(v1.begin(),v1.end());
cout<<"v1:";output(v1);
vector
reverse_copy(v0.begin(),v0.end(),v2.begin());
cout<<"v2:";output(v2);
}
void test3(){
vector
cout<<"v0:";output(v0);
vector
rotate(v1.begin(),v1.begin()+1,v1.end());
cout<<"v1:";output(v1);
vector
rotate(v2.begin(),v2.begin()+2,v2.end());
cout<<"v2:";output(v2);
vector
rotate(v3.begin(),v3.end()-1,v3.end());
cout<<"v3:";output(v3);
vector
rotate(v4.begin(),v4.end()-2,v4.end());
cout<<"v4:";output(v4);
}
2.运行截图

3.回答问题:
(1):reverse是把作用的对象对调,reverse_copy是照着原来的反着写在一块新的对象上,需要额外的空间。
(2):first是要移动的部分的起始位置,middle是移动位置的终点,last是移动部分的起始位置放置点(放在last的后面)。
任务2:
1.源代码:
include
include
include
include
include
include
include
using namespace std;
template
void output(const T&c);
int generate_random_number();
void test1();
void test2();
int main(){
srand(std::time(0));
std::cout<<"test one:\n";
test1();
cout<<"test two:\n";
test2();
}
template
void output(const T&c){
for(auto &i:c)
cout<<i<<' ';
cout<<"\n";
};
int generate_random_number(){
return rand()%101;
}
void test1(){
vector
generate(v0.begin(),v0.end(),generate_random_number);
cout<<"v0:";output(v0);
vector
sort(v1.begin(),v1.end());
cout<<"v1:";output(v1);
vector
sort(v2.begin()+1,v2.end()-1);
cout<<"v2:";output(v2);
}
void test2(){
vector
generate(v0.begin(),v0.end(),generate_random_number);
cout<<"v0:";output(v0);
auto min_iter=min_element(v0.begin(),v0.end());
cout<<"min_iter:"<<*min_iter<<endl;
auto max_iter=max_element(v0.begin(),v0.end());
cout<<"max_iter:"<<*max_iter<<endl;
auto ans=minmax_element(v0.begin(),v0.end());
cout<<"min:"<<ans.first<<endl;
cout<<"max:"<<ans.second<<endl;
double avg1=accumulate(v0.begin()+1,v0.end()-1,0.0)/v0.size();
cout<<"avg1:"<<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;
}
2:运行截图
3:(1):把1-100的数字随机塞进v0
(2):比较次数为1.5n,min和max的和为2n,比较次数少。
(3):[]可以从函数外面带变量 ,()负责接收参数,{}是函数体,负责执行代码。当需要直接拿外部的变量时,lamdba的[]就更有优势。
任务3:
1代码:#include
include
include
include
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
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';
}
2:截图
3:(1):把字母往后推一位。
(2):前者大写转为小写,后者反之。
(3):前面两个:移动区间的起点和终点,第三个:移动区间放置的起点。第四个:处理的操作。
任务4:#include
include
include
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";
}
}
bool is_palindrome(const std::string &s){
if (s.size() <= 1) return true;
size_t left = 0;
size_t right = s.size() - 1;
while (left < right) {
if (s[left] != s[right])
return false;
++left;
--right;
}
return true;
}
bool is_palindrome_ignore_case(const std::string &s) {
std::string t = s;
std::transform(t.begin(), t.end(), t.begin(),
[](unsigned char c){ return std::tolower(c); });
return is_palindrome(t);
}
2:cin换成while(getline(cin,s))。
任务5:#include
include
include
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) {
if (x == 0) return "0";
bool negative = x < 0;
if (negative) x = -x;
std::string result;
while (x > 0) {
int digit = x % n;
if (digit < 10)
result += static_cast
else
result += static_cast
x /= n;
}
if (negative) result += '-';
std::reverse(result.begin(), result.end());
return result;
}
任务6:#include
int main() {
std::cout << " ";
for (char c = 'a'; c <= 'z'; ++c)
std::cout << c << ' ';
std::cout << '\n';
for (int k = 1; k <= 26; ++k) {
std::cout << k << ' ';
for (int i = 0; i < 26; ++i)
std::cout << static_cast
std::cout << '\n';
}
}
任务7:#include
include
include
include
include
int main() {
std::srand(static_cast
const char ops[] = {'+', '-', '*', '/'};
const int round = 10;
int correct = 0;
for (int i = 0; i < round; ++i) {
int op = std::rand() % 4;
int a = std::rand() % 10 + 1;
int b = std::rand() % 10 + 1;
if (ops[op] == '-' && a < b)
std::swap(a, b);
if (ops[op] == '/') {
int divisors[10];
int count = 0;
for (int d = 1; d <= a; d++)
if (a % d == 0 && d <= 10)
divisors[count++] = d;
b = divisors[std::rand() % count];
}
std::cout << a << ' ' << ops[op] << ' ' << b << " = ";
int answer;
std::cin >> answer;
int expected = 0;
switch (ops[op]) {
case '+': expected = a + b; break;
case '-': expected = a - b; break;
case '*': expected = a * b; break;
case '/': expected = a / b; break;
}
if (answer == expected)
++correct;
}
std::cout << "正确率: " << std::fixed << std::setprecision(2)
<< correct * 100.0 / round << "%\n";
}

浙公网安备 33010602011771号