实验1

//任务1
1
#include <iostream> 2 #include <string> 3 #include <vector> 4 #include <algorithm> 5 6 using namespace std; 7 8 //声明 9 //模板函数声明 10 11 template<typename T> 12 void output(const T &c); 13 14 //普通函数声明 15 void test1(); 16 void test2(); 17 void test3(); 18 19 int main() { 20 cout << "测试1:\n"; 21 test1(); 22 23 cout << "测试2:\n"; 24 test2(); 25 26 cout << "测试3:\n"; 27 test3(); 28 29 return 0; 30 } 31 32 //函数实现 33 //输出容器对象c中的元素 34 template <typename T> 35 void output(const T &c) { 36 for (auto &i : c) 37 cout << i << " "; 38 cout << endl; 39 } 40 41 //测试1 42 //组合使用算法库、迭代器、string反转字符串 43 void test1() { 44 string s0 {"0123456789"}; 45 cout << "s0:" << s0 <<endl; 46 47 string s1{s0}; 48 reverse(s1.begin(),s1.end()); 49 cout << "s1:" <<s1<<endl; 50 51 string s2{s0}; 52 reverse_copy(s1.begin(),s1.end(),s2.begin()); 53 cout << "s2:" << s2 <<endl; 54 } 55 56 //测试2 57 //组合使用算法器、迭代器、vector反转动态数组对象vector内数据 58 void test2(){ 59 vector <int> v0 {0,1,2,3,4,5,6}; 60 cout << "v0:" ; 61 output(v0); 62 63 vector <int> v1{v0}; 64 reverse(v1.begin(),v1.end()); 65 cout << "v1:"; 66 output(v1); 67 68 vector <int> v2(v0); 69 reverse_copy(v1.begin(),v1.end(),v2.begin()); 70 cout << "v2:"; 71 output(v2); 72 } 73 74 //测试3 75 //组合使用算法库、迭代器、vector实现元素旋转位移 76 void test3(){ 77 vector <int> v0{0,1,2,3,4,5}; 78 cout<<"v0:"; 79 output(v0); 80 81 vector <int> v1{v0}; 82 rotate(v1.begin(),v1.begin()+1,v1.end()); 83 //旋转指定迭代器区间[v1.begin(),v1.end())之间的数据项,旋转后从迭代器v1。begin()+1位置的数据项开始 84 cout << "v1"; 85 output(v1); 86 87 vector <int> v2{v0}; 88 rotate(v2.begin(),v2.begin()+2,v2.end()); 89 cout << "v2:"; 90 output(v2); 91 92 vector <int> v3{v0}; 93 rotate(v3.begin(),v3.end()-1,v3.end()); 94 cout << "v3:"; 95 output(v3); 96 97 vector <int> v4{v0}; 98 rotate(v4.begin(),v4.end()-2,v4.end()); 99 cout << "v4:"; 100 output(v4); 101 }

运行结果:

 

任务2

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #include <algorithm>
 5 #include <numeric>
 6 #include <iomanip>
 7 
 8 using namespace std;
 9 
10 // 函数声明
11 // 模板函数声明
12 template<typename T>
13 void output(const T &c);
14 
15 // 普通函数声明
16 int rand_int_100();
17 void test1();
18 void test2();
19 
20 int main() {
21     cout << "测试1: \n";
22     test1();
23 
24     cout << "\n测试2: \n";
25     test2();
26 }
27 
28 // 函数实现
29 // 输出容器对象c中的元素
30 template <typename T>
31 void output(const T &c) {
32     for(auto &i: c)
33         cout << i << " ";
34     cout << endl;
35 }
36 
37 // 返回[0, 100]区间内的一个随机整数
38 int rand_int_100() {
39     return rand() % 101;
40 }
41 
42 // 测试1
43 // 对容器类对象指定迭代器区间进行赋值、排序
44 void test1() {
45     vector<int> v0(10);  // 创建一个动态数组对象v0, 对象大小为10
46     generate(v0.begin(), v0.end(), rand_int_100); // 产生[0, 100]之间的随机整数赋值给指定迭代器区间[v0.begin(), v0.end())内的每个数据项
47     cout << "v0: ";
48     output(v0);
49 
50     vector<int> v1{v0};
51     sort(v1.begin(), v1.end()); // 对指定迭代器区间[v1.begin(), v1.end())内数据项进行升序排序
52     cout << "v1: ";
53     output(v1);
54 
55     vector<int> v2{v0};
56     sort(v2.begin()+1, v2.end()-1); // 对指定迭代器区间[v1.begin()+1, v1.end()-1)内数据项进行升序排序
57     cout << "v2: ";
58     output(v2);
59 }
60 
61 // 测试2
62 // 对容器类对象指定迭代器区间进行赋值、计算最大值/最小值/均值
63 void test2() {
64     vector<int> v0(10);  
65     generate(v0.begin(), v0.end(), rand_int_100); 
66     cout << "v0: ";
67     output(v0);
68 
69     auto iter1 = min_element(v0.begin(), v0.end());
70     cout << "最小值: " << *iter1 << endl;
71 
72     auto iter2 = max_element(v0.begin(), v0.end());
73     cout << "最大值: " << *iter2 << endl;
74 
75     auto ans = minmax_element(v0.begin(), v0.end());
76     cout << "最小值: " << *(ans.first) << endl;
77     cout << "最大值: " << *(ans.second) << endl;
78     double avg1 = accumulate(v0.begin(), v0.end(), 0)/v0.size();
79     cout << "均值: " << fixed << setprecision(2) << avg1 << endl;
80 
81     cout << endl;
82 
83     vector<int> v1{v0};
84     cout << "v0: ";
85     output(v0);
86     sort(v1.begin(), v1.end());
87     double avg2 = accumulate(v1.begin()+1, v1.end()-1, 0)/(v1.size()-2);
88     cout << "去掉最大值、最小值之后,均值: " << avg2 << endl;
89 }

运行结果:

任务3:

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 
 5 bool is_palindrome(std::string s);
 6 
 7 int main(){
 8     using namespace std;
 9     string s;
10     
11     
12     while(cin>>s)//输入多组。直到按下Ctrl+Z后结束测试
13         cout << boolalpha << is_palindrome(s) <<endl; 
14 }
15 
16 //函数is_palindrome定义
17 bool is_palindrome(std::string s) {
18      int i = 0;
19      int j = s.size()-1;
20      while(i<j) {
21          if (s[i]==s[j])
22              i++,j--;
23          else 
24              return false;
25      }
26     if(i<=j)
27         return true;
28 }

运行结果:

任务4:

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <cstdio>
 5 #include <stack> 
 6 
 7 void dec2n(int x,int n = 2);
 8 char Int2Char(int target);
 9 using namespace std;
10 int main(){
11     using namespace std;
12     int x;
13     while (cin>>x){
14         cout <<"十进制:" <<x<<endl;
15         cout <<"二进制:" ;
16         dec2n(x);
17         cout <<"八进制:" ;
18         dec2n(x,8);
19         cout <<"十六进制:";
20         dec2n(x,16);
21     }
22 } 
23 
24 char Int2Char(int target) {
25     if (target<10)
26         return target +'0' ;
27     else
28         return target - 10 +'A';
29 }
30 //函数dec2n定义
31 void dec2n(int x,int n) {
32     stack<char> s;
33     if (x == 0)
34         s.push(0);
35     else{
36         while(x){
37             s.push(Int2Char(x%n));
38             x /= n;
39         }
40     }    
41     
42     while(!s.empty()) {
43         printf("%c",s.top());
44         s.pop();
45     }
46     cout<<""<<endl;
47     
48 }

运行结果:

任务5:

 1 #include<iostream>
 2 #include<string>
 3 #include<iomanip>
 4 using namespace std;
 5 int main(){
 6 
 7 
 8 
 9 
10     char a;
11     cout<<setw(2)<<' ';
12     for (int i =0;i< 26;i++)
13     {
14         a='a'+i;
15         cout <<setw(2) <<a;
16 
17     }
18     cout << endl;
19 
20     for (int i=1;i<27;i++)
21     {
22         cout <<setw(2)<<i;
23         for (int j=0;j<26;j++)
24         {
25             int t=(i+j)%26;
26             a='A'+t;
27             cout<<setw(2)<<a;
28          }
29             cout<<endl;
30     }
31 
32 
33 
34 
35 
36 
37     return  0 ;
38 }

 

 

运行结果:

 

 任务6:

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <ctime>
 4 #include <iomanip>
 5 
 6 // 生成随机算术运算题目
 7 int num1, num2;
 8 char op;
 9 std::string generateQuestion() {
10     std::srand(static_cast<unsigned int>(std::time(nullptr)));
11     op = static_cast<char>("+-*/"[std::rand() % 4]);
12     num1 = std::rand() % 10 + 1;
13     num2 = std::rand() % 10 + 1;
14 
15     if (op == '-') {
16         while (num1 <= num2) {
17             num1 = std::rand() % 10 + 1;
18             num2 = std::rand() % 10 + 1;
19         }
20     }
21 
22     if (op == '/') {
23         while (num1 % num2!= 0) {
24             num1 = std::rand() % 10 + 1;
25             num2 = std::rand() % 10 + 1;
26         }
27     }
28 
29     std::string question = std::to_string(num1) + " " + op + " " + std::to_string(num2) + " = ";
30     return question;
31 }
32 
33 int main() {
34     int correctCount = 0;
35     int totalCount = 10;
36 
37     for (int i = 0; i < totalCount; ++i) {
38         std::string question = generateQuestion();
39         std::cout << question;
40         int userAnswer;
41         std::cin >> userAnswer;
42 
43         int correctAnswer;
44         if (op == '+') {
45             correctAnswer = num1 + num2;
46         } else if (op == '-') {
47             correctAnswer = num1 - num2;
48         } else if (op == '*') {
49             correctAnswer = num1 * num2;
50         } else if (op == '/') {
51             correctAnswer = num1 / num2;
52         }
53 
54         if (userAnswer == correctAnswer) {
55             correctCount++;
56         }
57     }
58 
59     double accuracy = static_cast<double>(correctCount) / totalCount * 100;
60     std::cout << "正确率为:" << std::fixed << std::setprecision(2) << accuracy << "%" << std::endl;
61 
62     return 0;
63 }

运行结果:

 

posted @ 2024-10-13 19:03  sssshan  阅读(6)  评论(0编辑  收藏  举报