首页 > 编程语言 >实验1 现代C++编程初体验

实验1 现代C++编程初体验

时间:2024-10-14 22:10:23浏览次数:1  
标签:begin 初体验 cout int 编程 C++ v0 include define

实验任务1:

task1.cpp

点击查看代码
// 现代C++标准库、算法库体验
// 本例用到以下内容:
// 1. 字符串string, 动态数组容器类vector、迭代器
// 2. 算法库:反转元素次序、旋转元素
// 3. 函数模板、const引用作为形参

#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 << "\n测试2: \n";
    test2();

    cout << "\n测试3: \n";
    test3();
}

// 函数实现
// 输出容器对象c中的元素
template <typename T>
void output(const T &c) {
    for(auto &i: c)
        cout << i << " ";
    cout << endl;
}

// 测试1
// 组合使用算法库、迭代器、string反转字符串
void test1() {
    string s0{"0123456789"};
    cout << "s0 = " << s0 << endl;

    string s1{s0};
    reverse(s1.begin(), s1.end());  // 反转指定迭代器区间的元素
    cout << "s1 = " << s1 << endl;

    string s2{s0};
    reverse_copy(s0.begin(), s0.end(), s2.begin()); // 将指定迭代区间的元素拷贝到指定迭代器开始的目标区间,并且在复制过程中反转次序
    cout << "s2 = " << s2 << endl;
}

// 测试2
// 组合使用算法库、迭代器、vector反转动态数组对象vector内数据
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);
}

// 测试3
// 组合使用算法库、迭代器、vector实现元素旋转移位
void test3() {
    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());  // 旋转指定迭代器区间[v1.begin(), v1.end())之间的数据项,旋转后从迭代器v1.begin()+1位置的数据项开始
    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:

task2.cpp

点击查看代码
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <iomanip>

using namespace std;

// 函数声明
// 模板函数声明
template<typename T>
void output(const T &c);

// 普通函数声明
int rand_int_100();
void test1();
void test2();

int main() {
    cout << "测试1: \n";
    test1();

    cout << "\n测试2: \n";
    test2();
}

// 函数实现
// 输出容器对象c中的元素
template <typename T>
void output(const T &c) {
    for(auto &i: c)
        cout << i << " ";
    cout << endl;
}

// 返回[0, 100]区间内的一个随机整数
int rand_int_100() {
    return rand() % 101;
}

// 测试1
// 对容器类对象指定迭代器区间进行赋值、排序
void test1() {
    vector<int> v0(10);  // 创建一个动态数组对象v0, 对象大小为10
    generate(v0.begin(), v0.end(), rand_int_100); // 产生[0, 100]之间的随机整数赋值给指定迭代器区间[v0.begin(), v0.end())内的每个数据项
    cout << "v0: ";
    output(v0);

    vector<int> v1{v0};
    sort(v1.begin(), v1.end()); // 对指定迭代器区间[v1.begin(), v1.end())内数据项进行升序排序
    cout << "v1: ";
    output(v1);

    vector<int> v2{v0};
    sort(v2.begin()+1, v2.end()-1); // 对指定迭代器区间[v1.begin()+1, v1.end()-1)内数据项进行升序排序
    cout << "v2: ";
    output(v2);
}

// 测试2
// 对容器类对象指定迭代器区间进行赋值、计算最大值/最小值/均值
void test2() {
    vector<int> v0(10);  
    generate(v0.begin(), v0.end(), rand_int_100); 
    cout << "v0: ";
    output(v0);

    auto iter1 = min_element(v0.begin(), v0.end());
    cout << "最小值: " << *iter1 << endl;

    auto iter2 = max_element(v0.begin(), v0.end());
    cout << "最大值: " << *iter2 << 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)/v0.size();
    cout << "均值: " << fixed << setprecision(2) << avg1 << endl;

    cout << endl;

    vector<int> v1{v0};
    cout << "v0: ";
    output(v0);
    sort(v1.begin(), v1.end());
    double avg2 = accumulate(v1.begin()+1, v1.end()-1, 0)/(v1.size()-2);
    cout << "去掉最大值、最小值之后,均值: " << avg2 << endl;
}

运行测试截图

实验任务3:

task3.cpp

点击查看代码
#include <iostream>
#include <string>
#include <algorithm>

bool is_palindrome(std::string s);

int main() {
    using namespace std;
    string s;

    while(cin >> s)  // 多组输入,直到按下Ctrl+Z后结束测试
        cout << boolalpha << is_palindrome(s) << endl;
}

// 函数is_palindrom定义

bool is_palindrome(std::string s)
{
   int len=s.size();
   for(int i=0;i<len/2;++i)
   {
      if(s[i]!=s[len-i-1])return false;
   }
   return true;
}

运行测试截图

实验任务4:

task4.cpp

点击查看代码
#include <iostream>
#include <string>
#include <algorithm>

std::string dec2n(int x, int n = 2);

int main() {
    using namespace std;

    int x;
    while(cin >> x) {
        cout << "十进制: " << x << endl;
        cout << "二进制: " << dec2n(x) << endl;
        cout << "八进制: " << dec2n(x, 8) << endl;
        cout << "十六进制: " << dec2n(x, 16) << endl << endl;
    }
}

// 函数dec2n定义
std::string dec2n(int x, int n)
{
   std::string s;
   while(x)
   {
      if(x%n<10)s+=((x%n)+'0');
      else 
      {
         switch(x%n)
         {
         case 10:s+="A";break; 
         case 11:s+="B";break;
         case 12:s+="C";break;
         case 13:s+="D";break;
         case 14:s+="E";break;
         case 15:s+="F";break;
         }
      }
      x/=n;
   }
   std::string ss="";
   for(int i=(int)s.size()-1;i>=0;--i)
   {
      ss+=s[i]; 
   }
   if(ss=="")ss="0";
   return ss;
}

运行测试截图

实验任务5:

task5.cpp

点击查看代码
#include <algorithm>

#include <array>

#include <cassert>

#include <cmath>

#include <cstdio>

#include <cstring>

#include <deque>

#include <iostream>

#include <list>

#include <map>

#include <numeric>

#include <queue>

#include <set>

#include <string.h>

#include <string>

#include <type_traits>

#include <unordered_map>

#include <utility>

#include <valarray>

#include <vector>

#include<iomanip>

#define INF 0x3f3f3f3f

#define LLINF 0x3f3f3f3f3f3f3f3f

#define lowbit(x) (x & (-x))

#define PII pair<int, int>

#define TII tuple<int, int, int>

#define mk make_pair

#define fi first

#define se second



using namespace std;

using ll = long long;

using ld = long double;

using ull = unsigned long long;

const int mod = 998244353;

const int MOD = 1e9 + 7;

const int N = 2e5 + 3;

#define int ll

void solve()
{
   string s;
   for(int i=0;i<26;++i)
   {
      char c=i+'A';
      s+=c;
   }
   s+=s;
   for(int i=0;i<=26;++i)
   {
      if(i==0)
      {
         cout<<" ";
         for(int i=0;i<26;++i)
         {
            char c=i+'a';
            cout<<right<<setw(2)<<c;
         }
      }
      else
      {
         cout<<i;
      for(int j=i;j<i+26;++j)
      {
         cout<<right<<setw(2)<<s[j];
      }
      }
      cout<<"\n";
   }
}

signed main() {

  ios::sync_with_stdio(false);

  cin.tie(0);

  cout.tie(0);



  int _ = 1;

 // cin >> _;

  while (_--) {

    solve();

  }

  return 0;

}


运行测试截图

实验任务6:

test6.cpp

点击查看代码
#include <algorithm>

#include <array>

#include <cassert>

#include <cmath>

#include <cstdio>

#include <cstring>

#include <deque>

#include <iostream>

#include <list>

#include <map>

#include <numeric>

#include <queue>

#include <random>
#include <set>

#include <string.h>

#include <string>

#include <type_traits>

#include <unordered_map>

#include <utility>

#include <valarray>

#include <vector>

#include<iomanip>

#define INF 0x3f3f3f3f

#define LLINF 0x3f3f3f3f3f3f3f3f

#define lowbit(x) (x & (-x))

#define PII pair<int, int>

#define TII tuple<int, int, int>

#define mk make_pair

#define fi first

#define se second



using namespace std;

using ll = long long;

using ld = long double;

using ull = unsigned long long;

const int mod = 998244353;

const int MOD = 1e9 + 7;

const int N = 2e5 + 3;

#define int ll

void solve()
{
   // mt19937 rnd(time(0));
   //rnd();
   // char arr[]={'+','-','*','/'};//4
   int brr[]={1,2,3,4,5,6,7,8,9,10};//10

   int cnt=0,ans;
   int n=10;
   while(n--)
   {
       mt19937 rnd1(time(0));
      int a=brr[rnd1()%10];
       mt19937 rnd2(time(0));
      int b=brr[rnd2()%10];
       mt19937 rnd(time(0));
      int c=rnd()%4;

      switch(c)
      {
      case 0:cout<<a<<"+"<<b<<"=";scanf("%d",&ans);if(a+b==ans)cnt++;break;
      case 1:if(a<b)swap(a,b);cout<<a<<"-"<<b<<"=";scanf("%d",&ans);if(a-b==ans)cnt++;break;
      case 2:cout<<a<<"*"<<b<<"=";scanf("%d",&ans);if(a*b==ans)cnt++;break;
      case 3:if(a<b)swap(a,b);if(a%b!=0){n++;break;}else{cout<<a<<"/"<<b<<"=";scanf("%d",&ans);if(a/b==ans)cnt++;break;}
      }
   }
   double bit=(double)cnt/10*100;
    cout<<"正确率为:";
   cout<<fixed<<setprecision(2)<<bit<<"%"<<"\n";


}

signed main() {

  // ios::sync_with_stdio(false);

  // cin.tie(0);

  // cout.tie(0);



  int _ = 1;

 // cin >> _;

  while (_--) {

    solve();

  }

  return 0;

}


运行测试截图

标签:begin,初体验,cout,int,编程,C++,v0,include,define
From: https://www.cnblogs.com/May007/p/18453581

相关文章

  • C++中int main(int argc, char* argv[])形参解释
    在 intmain(intargc,char*argv[]) 中,argc 和 argv 是主函数 main 的参数,用于处理命令行输入参数。它们允许程序从命令行接收额外的输入值,常用于控制程序的执行行为或传递信息。参数解释argc(argumentcount):表示从命令行传递给程序的参数个数,包括程序本身的名字。它......
  • 《C++内存对齐策略:提升性能的关键之路》
    在C++编程的广阔世界中,高效的内存对齐策略是一个至关重要却常常被忽视的主题。它不仅影响着程序的性能,还关系到内存的使用效率和稳定性。今天,我们就来深入探讨一下如何在C++中实现高效的内存对齐策略。一、为什么内存对齐如此重要?内存对齐在C++中具有重大意义。首先,它......
  • 《C++与区块链节点:职场新势力的崛起》
    在当今数字化的职场环境中,区块链技术正以其独特的魅力和巨大的潜力吸引着众多专业人士的目光。而作为一种强大的编程语言,C++在区块链节点的实现中发挥着至关重要的作用。今天,我们就来探讨一下在职场中,如何使用C++进行区块链节点的实现。一、区块链技术的魅力与挑战区块链......
  • [C++] 红黑树的实现:原理与底层解析
    文章目录@[toc]红黑树的概念红黑树的规则红黑树如何确保最长路径不超过最短路径的2倍红黑树规则最短路径与最长路径的分析最短路径:全黑路径最长路径:红黑交替路径结论:红黑树的平衡性如何保障操作效率红黑树的实现红黑树的节点结构红黑树的插入操作插入基本步骤插入......
  • Python编程基础之变量与数据类型
    一、变量    在Python中,变量是用来存储数据值的标识符。变量的命名需要遵循一定的规则:1. 变量名只能包含字母、数字和下划线。2. 变量名不能以数字开头。3. 变量名不能是Python中的关键字(如if、else、for等)。    Python可以这样定义变量:my_variable=......
  • 实验1 现代C++编程初体验
    任务1:源代码task1.cpp1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78template<typenameT>9voidoutput(constT&c);1011voidtest1();12voidtes......
  • [C++ 核心编程]笔记 4.1.4 类和对象 - 案例1
    类和对象:案例1:设计立方体类(Cube)求出立方体的面积和体积分别用全局函数和成员函数判断两个立方体是否相等。设计方法:创建立方体类设计属性设计行为求立方体面积和体积分别用全局和成员函数判断立方体是否相等#include<iostream>usingnamespacestd;clas......
  • 实验1 c++
    任务1:task1.cpp://现代C++标准库、算法库体验//本例用到以下内容://1.字符串string,动态数组容器类vector、迭代器//2.算法库:反转元素次序、旋转元素//3.函数模板、const引用作为形参#include<iostream>#include<string>#include<vector>#include<algorit......
  • 实验1 现代C++编程初体验
    任务1:源代码task1.cpp1#include<bits/stdc++.h>2usingnamespacestd;34//模板函数声明5template<typenameT>6voidoutput(constT&c);78//普通函数声明9voidtest1();10voidtest2();11voidtest3();1213intmain(){14cout&l......
  • C++:初识
       1.namespace    1.1namespace的价值和定义        在c++中存在着大量的类,这些变量函数,类都存在全局变量当中为了,避免命名冲突c++当中引入了namespace。    在命名冲突中c语言也是普遍存在的观察如下c代码:#include<stdio.h>#include<st......