实验任务1
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
template<typename T>
void output(const T &c);
void test1();
void test2();
void test3();
int main(){
cout<<"测试1:\n";
test1();
cout<<"测试2:\n";
test2();
cout<<"测试3:\n";
test3();
}
template<typename T>
void output(const T &c){
for(auto &i:c) cout<<i<<" ";
cout<<"\n";
}
void test1(){
string s0{"0123456789"};
cout<<"s1="<<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<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(){
vector<int> v0{0,1,2,3,4,5,6,7,8,9};
cout<<"v0:";output(v0);
vector<int> v1{v0},v2{v0},v3{v0},v4{v0};
rotate(v1.begin(),v1.begin()+1,v1.end());
rotate(v2.begin(),v2.begin()+2,v2.end());
rotate(v3.begin(),v3.end()-1,v3.end());
rotate(v4.begin(),v4.end()-2,v4.end());
cout<<"v1:";output(v1);
cout<<"v2:";output(v2);
cout<<"v3:";output(v3);
cout<<"v4:";output(v4);
}
![image]()
- 问题问答
1.reverse函数是直接将字符串本身进行倒置,而reverse_copy函数是将一个字符串的字符复制下来,然后再倒置添加到另一个字符串中。
2.rotate函数的作用是将一个vector数组分为2个部分,并将其进行倒置重新排列,第一个和第三个分别为位置迭代器,表示要旋转的区间。而中间的则表示旋转的点,整个区间依靠这个点翻转一,同时原本顺序不变。
实验任务2
#include<bits/stdc++.h>
using namespace std;
template<typename T>
void output(const T &c);
int generate_random_number();
void test1();
void test2();
int main(){
srand(time(0));
cout<<"测试1:\n";
test1();
cout<<"测试2:\n";
test2();
}
template<typename T>
void output(const T &c){
for(auto &i:c) cout<<i<<" ";
cout<<endl;
}
int generate_random_number(){
return rand()%101;
}
void test1(){
vector<int> v0(10);
generate(v0.begin(),v0.end(),generate_random_number);
cout<<"v0:";output(v0);
vector<int> v1{v0},v2{v0};
sort(v1.begin(),v1.end());
sort(v2.begin()+1,v2.end()-1);
cout<<"v1:";output(v1);
cout<<"v2:";output(v2);
}
void test2(){
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<<"最大值为:"<<*max_iter<<endl;
cout<<"最小值为:"<<*min_iter<<endl;
auto ans=minmax_element(v0.begin(),v0.end());
cout<<"最小值:"<<*(ans.first)<<endl;
cout<<"最小值为:"<<*(ans.second)<<endl;
double avgl=accumulate(v0.begin(),v0.end(),0.0)/v0.size();
cout<<"均值为:"<<fixed<<setprecision(2)<<avgl<<endl;
sort(v0.begin(),v0.end());
double avg2=accumulate(v0.begin()+1,v0.end()-1,0.0)/(v0.size()-2);
cout<<"去掉最大值、最小值之后,均值: "<<avg2<<endl;
}
![image]()
- 问题问答
1.generate函数是将空的vector容器依次填充数字,数字有由第三个填入的数字决定,由于填入的是随机函数值,故数值不同
2.maxmin_element函数相比max_element和min_element能够同时检查最大值和最小值,能够减少一次长度为n的检索,时间更快。
3.效果是相同的,相当于把函数直接写在了另一个函数里。
实验任务3
#include<bits/stdc++.h>
using namespace std;
unsigned char func(unsigned char c);
void test1();
void test2();
int main(){
cout<<"测试1: 字符串大小写转换\n";
test1();
cout<<"\n测试2: 字符变换\n";
test2();
}
unsigned char func(unsigned char c){
if(c=='z') return 'a';
if(c=='Z') return 'A';
if(isalpha(c))
return static_cast<unsigned char>(c+1);
return c;
}
void test2(){
string s1{"I love cosmos!"};
cout<<"s1="<<s1<<"\n";
string s2(s1.size(),' ');
transform(s1.begin(),s1.end(),s2.begin(),func);
cout<<"s2="<<s2<<"\n";
}
void test1(){
string s1{"Hello world 2049!"};
cout<<"s1="<<s1<<"\n";
string s2;
for(auto c:s1) s2+=tolower(c);
cout<<"s2="<<s2<<"\n";
string s3;
for(auto c:s1) s3+=toupper(c);
cout<<"s3="<<s3<<"\n";
}
![image]()
- 问题问答
1.func函数的功能是是将字母按照ASCLL码值加一得到对应的字符,其中a,A变成z,Z.
2.tolower函数的作用是将所有的大写字母转化为小写字,而toupper函数则是将小写字母都变为大写字母。
3.transform函数的第一个和第二个决定了要转化的区间,第三个则表示将转化后的字符从哪个位置起开始写入,而第四个则决定了转化的规则,若将改成s1.begin(),则会将转化后的字符直接覆盖s1。
实验任务4
#include<bits/stdc++.h>
using namespace std;
bool is_palindrome(const string &s);
bool is_palindrome_ignore_case(const string &s);
int main(){
string s;
while(cin>>s){
cout<<boolalpha<<"区分大小写:"<<is_palindrome(s)<<"\n"<<"不区分大小写:"<<is_palindrome_ignore_case(s)<<"\n\n";
}
}
//函数is_palindrome定义
bool is_palindrome(const string &s){
int len=s.size();
if(len%2==0){
for(int i=0;i<len/2;i++){
if(s[i]!=s[len-i-1]) return false;
}
}
else{
for(int i=0;i<(len+1)/2;i++){
if(s[i]!=s[len-i-1]) return false;
}
}
return true;
}
//函数is_palindrome_ignore_case定义
bool is_palindrome_ignore_case(const string &s){
string s1;
for(auto c:s) s1+=tolower(c);
int len=s1.size();
if(len%2==0){
for(int i=0;i<len/2;i++){
if(s1[i]!=s1[len-i-1]) return false;
}
}
else{
for(int i=0;i<(len+1)/2;i++){
if(s1[i]!=s1[len-i-1]) return false;
}
}
return true;
}
![image]()
- 问题问答
若其中包含字符,可以将cin>>s改为getline(cin,s)。
实验任务5
#include<bits/stdc++.h>
using namespace std;
string dec2n(int x,int n=2);
int main(){
int x;
while(cin>>x){
cout<<"十进制: "<<x<<'\n'
<<"二进制: "<<dec2n(x)<< '\n'
<<"八进制: "<<dec2n(x, 8)<<'\n'
<<"十二进制: "<<dec2n(x, 12)<<'\n'
<<"十六进制: "<<dec2n(x, 16)<<'\n'
<<"三十二进制: "<<dec2n(x, 32)<<"\n\n";
}
}
//函数dec2n定义
string dec2n(int x,int n){
string s;
stack<int> st;
int sz=x;
if(sz==0) return "0";
while(sz){
int num=sz%n;
st.push(num);
sz/=n;
}
while(!st.empty()){
int num=st.top();
if(num<10) s+=static_cast<unsigned char>(num+48);
else s+=static_cast<unsigned char>(num+55);
st.pop();
}
return s;
}
![image]()
实验任务6
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<char> v(26);
for(int i=0;i<26;i++) v[i]=static_cast<unsigned char>(i+65);
cout<<" ";
for(int i=0;i<26;i++) cout<<static_cast<unsigned char>(i+97)<<" ";
cout<<"\n";
for(int i=1;i<=26;i++){
printf("%2d ",i);
rotate(v.begin(),v.begin()+1,v.end());
for(auto i:v) cout<<i<<" ";
cout<<"\n";
}
return 0;
}
![image]()
实验任务7
#include<bits/stdc++.h>
using namespace std;
int random_number();
int main(){
srand(time(0));
char ops[]={'+','-','*','/'};
int num=10;
int cnt=0;
while(num){
char op=ops[rand()%4];
int x=random_number(),y=random_number();
int z;
if(op=='+') z=x+y;
else if(op=='-'){
if(x-y<0) continue;
else z=x-y;
}
else if(op=='*') z=x*y;
else{
if(x%y!=0) continue;
else z=x/y;
}
int answer;
cout<<x<<" "<<op<<" "<<y<<" = ";
cin>>answer;
if(answer==z) cnt++;
num--;
}
double zq=(double)cnt/10.0;
zq*=100;
printf("正确率为:%.2lf%%",zq);
}
int random_number(){
return rand()%10+1;
}
![image]()