anxinchen

导航

实验1作业

任务1

1.源代码task

#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() {
	//reverse这是反转字符 
    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(), ' ');
    // 将s0反转后结果拷贝到s2, s0自身不变 
    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);
}

  

2.运行结果

屏幕截图 2026-10-16 113840

 

3问题回答

 1.在test2中,reverse是直接将对象的内容直接反转,reverse_copy是将对象反转过来再复制到容器中,同时原容器保持不变。

  2.rotate(v1.begin(), v1.begin()+k, v1.end()); 这个第一空指开始处,第二空指的是开始处的相对移动,+k是移动的方式,为正表示开头往后走,整体向左k布,-的就是相反的,第三空指结束处。

 

任务2

1.源代码task

#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();//generate的作用 
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';
}
//[0-100]
int generate_random_number() {
    return std::rand() % 101;
}
// 测试1:对容器类对象指定迭代器区间赋值、排序,直接使用sort 
void test1() {
    using namespace std; 
    vector<int> v0(10);  // 创建一个动态数组
    generate(v0.begin(), v0.end(), generate_random_number); // 生成随机数填充v0
    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); //sort(v2.begin()+a,v2.end()-b)a是指从开头的第几布开始, b是最后前几部开始 
    cout << "v2: "; output(v2);
}
// 测试2:对容器类对象指定迭代器区间赋值、计算最大值/最小值/均值
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;
}

  

2.运行结果

屏幕截图 2026-10-18 150455

 

image

 

3.问题回答

 1.generate(v0.begin(), v0.end(), generate_random_number),用指定函数generate_random_number生成的数据来补充v0的内容。

 2.minmax_element只需遍历一次,利用first,second可以直接得到最小最大值,min_element和max_element需要遍历两遍,所以相对来说前者更加高效。

 3.将函数注释,利用std::rand直接返回生成随机数,使用Lambda效果相同,更加简洁方便。

 

任务3

 1.源代码task

#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);//将字符的ASCLL码加一并转成 
    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(), ' ');//用si.size个空格填满s2 
    std::transform(s1.begin(), s1.end(),//调用fun函数 
                   s2.begin(),
                   func);
    std::cout << "s2 = " << s2 << '\n';
}

  

 2.运行结果

屏幕截图 2026-10-18 161240

 

 3.问题回答

 1.func的作用:将传入的字符进行变换,z变成a,Z变成A,其他的字母按ASCLL码+1再变回字符,在test2中transform中被调用。

 2.tolower:标准库函数,输入大写字母会返回小写字母。

    toupper:标准库函数,输入小写字母会返回大写字符。

 3.transform(a,b,c,d)的意义a表示读数据的起始点,b表示读完数据的点,c表示读取数据存起的地方,前三者都是迭代器,d表示如何处理数据的函数。
 
任务4
1.源代码task
#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;

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


// 函数is_palindrome定义
// 待补足
bool is_palindrome(const std::string &s) {
	using namespace std;
	string t=s;
	reverse(t.begin(),t.end());
	return t==s;
}

// 函数is_palindrome_ignore_case定义
// 待补足

bool is_palindrome_ignore_case(const std::string &s) {
	std::string t=s;
	std::transform(t.begin(),t.end(),t.begin(),::tolower);
	std::string re_t=t;
	std::reverse(re_t.begin(),re_t.end());
	return re_t==t;
}

  

2.实验结果
屏幕截图 2026-10-18 171820

 

3.问题回答
 1.cin>>s在读取字符串时,遇到空格,制表符等会认为输入结束,我了解之后知道可以使用getline(cin,s)读取一整行,直到换行符。
 
任务5
 1.源代码task
#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"; 
    }
}

// 函数dec2n定义
// 待补足
std::string dec2n(int x,int n){
	if(x==0)
	return "0";
	std::string chars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	std::string s="";
	while(x>0){
		int k=x%n;
		s=s+chars[k];
		x=x/n;
	}
	std::reverse(s.begin(),s.end());
	return s;
}

   2.运行结果

屏幕截图 2026-10-18 190210

 

任务6

1.源代码task

 

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
int main(){
	cout<<left<<setw(3)<<" ";
	for(char c='a';c<='z';++c){
		cout<<c<<" ";
	}
	cout<<"\n";
	for(int i=0;i<26;i++){
		if(i<9)
		cout<<" "<<setw(2)<<i+1;
		else
		cout<<left<<setw(3)<<(i+1);
		for(int j=0;j<26;j++){
			char c='A'+(j+i)%26;
			cout<<c<<" ";
		}
		cout<<"\n";
	}
	return 0;
}

2.运行结果

屏幕截图 2026-10-18 195143

 

任务七

 1.源代码task

 

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<iomanip>
using namespace std;
int main(){
	srand(time(0));//随机添加种子
	int s=0;
	for(int i=0;i<10;i++){
		int a=rand()%10+1;
		int b=rand()%10+1;
		int c=rand()%4;
		int ans=0;
		char sign;
		if(c==0){
			sign='+';
			ans=a+b;
		}
		else if(c==1){
			sign='-';
			if(a<b)
			swap(a,b);
			ans=a-b; 
		}
		else if(c==2){
			sign='*';
			ans=a*b;
		}
		else if(c==3){
			sign='/';
			while(a%b!=0){
				a=rand()%10+1;
				b=rand()%10+1;
			}
			ans=a/b;
		}
		cout<<a<<sign<<b<<"=";
		int wer;
		cin>>wer;
		if(ans==wer)
		s++;
	}
	double rate=s*100.0/10;
	cout<<"正确率: "<<fixed<<setprecision(2)<<rate<<'%'<<endl;
	return 0; 
}

 2.运行结果

屏幕截图 2026-10-18 203230

屏幕截图 2026-10-18 203305

 

实验总结:

学会了使用reverse对对象反转,使用transform直接利用函数生成数据储存在容器中,使用随机数srand(time(0))更利于随机生成数据,以及tolower,toupper直接输出小写,大写字母,c++语言更加方便,同时还有setw,fixed,setprecision可以用来是输出内容更加对齐。

 

  

 

posted on 2026-10-18 20:57  anxinchen  阅读(4)  评论(0)    收藏  举报