首页 > 编程语言 >C++程序设计基础实验1

C++程序设计基础实验1

时间:2024-10-21 18:43:39浏览次数:1  
标签:10 cout num2 int C++ v0 实验 程序设计 include

任务一:

源代码task1.cpp

#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()); 
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); 
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);
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;
}

运行结果截图:

 任务三:

代码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){
  std::string s1=s;
  reverse(s.begin(),s.end());
  std::string s2=s;
  return std::equal(s1.begin(),s1.end(),s2.begin());
  
}
// 待补足
// ×××

运行结果截图:

 任务4:

代码task4.cpp:

#include <iostream>
#include <string>
#include <vector>

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;
    }

    cout << "输入结束。\n";
    return 0;
}

std::string dec2n(int x, int n) {
    std::string result;

    if (x == 0) return "0";

    while (x > 0) {
        int remainder = x % n;
        char digit;

        if (remainder < 10) {
            digit = remainder + '0';
        } else {
            digit = remainder - 10 + 'A';
        }

        result = digit + result;
        x /= n;
    }

    return result;
}

运行结果截图:

 任务5:

代码task5.cpp:

#include <iostream>
#include <iomanip> // 用于 std::setw
using namespace std;

int main() {
    // 字母表
    string alphabet = "abcdefghijklmnopqrstuvwxyz";

    // 打印原始字母表,前面加一个空格
    cout << "  ";  // 在第一行开头打印一个空格
    for (char c : alphabet) {
        cout << c << " "; // 每个字符后面加一个空格
    }
    cout << endl; // 换行

    // 生成密文对照表
    for (int i = 1; i <= 26; ++i) {
        // 输出行号,并且行号后面有空格
        cout << i;

        // 输出每个字符,字符之间空格
        for (int j = 0; j < 26; ++j) {
            cout << " " << alphabet[(j + i) % 26]; // 加空格
        }

        // 换行
        cout << endl;
    }

    return 0;
}

运行结果截图:

 任务6:

代码task6.cpp:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip> // 用于 std::setprecision

using namespace std;

int main() {
    // 设置随机数种子
    srand(static_cast<unsigned int>(time(0)));

    const int totalQuestions = 10; // 总题数
    int correctAnswers = 0;         // 正确答案计数

    for (int i = 0; i < totalQuestions; ++i) {
        int num1, num2, answer;
        char operation;
        
        // 随机选择操作符
        int op = rand() % 4; // 0: 加, 1: 减, 2: 乘, 3: 除
        if (op == 0) { // 加法
            num1 = rand() % 10 + 1; // 1-10
            num2 = rand() % 10 + 1; // 1-10
            operation = '+';
            answer = num1 + num2;
        } else if (op == 1) { // 减法
            num2 = rand() % 9 + 1; // 1-9
            num1 = num2 + (rand() % 10 + 1); // 保证 num1 > num2
            operation = '-';
            answer = num1 - num2;
        } else if (op == 2) { // 乘法
            num1 = rand() % 10 + 1; // 1-10
            num2 = rand() % 10 + 1; // 1-10
            operation = '*';
            answer = num1 * num2;
        } else { // 除法
            num2 = rand() % 9 + 1; // 1-9
            num1 = num2 * (rand() % 10 + 1); // 保证 num1 能被 num2 整除
            operation = '/';
            answer = num1 / num2;
        }

        // 输出题目
        cout << num1 << " " << operation << " " << num2 << " =  ";
        
        // 用户输入答案
        int userAnswer;
        cin >> userAnswer;

        // 检查答案
        if (userAnswer == answer) {
            correctAnswers++;
        } 
    }

    // 计算正确率
    double accuracy = (static_cast<double>(correctAnswers) / totalQuestions) * 100;
    
    // 输出正确率,保留两位小数
    cout << "正确率: " << fixed << setprecision(2) << accuracy << "%" << endl;

    return 0;
}

运行结果截图:

 

标签:10,cout,num2,int,C++,v0,实验,程序设计,include
From: https://www.cnblogs.com/1218xzw/p/18490026

相关文章

  • C++研发笔记4——C语言程序设计初阶学习笔记2
            从今天开始我们开始第二模块初识C语言的学习,在本模块中我们将会涉及到一下14个内容:什么是C语言、第一个C语言程序、数据类型、变量、常量、字符串+转义字符+注释、选择语句、循环语句、函数、数组、操作符、常见关键字、define定义常量和宏、指针......
  • 20222320 2024-2025-1 《网络与系统攻防技术》实验三实验报告
    目录目录目录0.认识工具Veil1.实验目标2.实验内容3.实验过程(1)使用msf编码器,使用msfvenom生成如jar之类的其他文件(2)使用veil加壳工具加壳(3)使用C+shellcode编程(4)通过组合应用各种技术实现恶意代码免杀(5)用另一电脑实测,在杀软开启的情况下,可运行并回连成功4.问题及解决方案5.学习感......
  • 《网络与系统攻防技术》实验三实验报告
    1.实践内容(1)正确使用msf编码器,veil-evasion,自己利用shellcode编程等免杀工具或技巧正确使用msf编码器,使用msfvenom生成如jar之类的其他文件veil,加壳工具使用C+shellcode编程(2)通过组合应用各种技术实现恶意代码免杀如果成功实现了免杀的,简单语言描述原理,不要截图。与杀......
  • 20222425 2024-2025-1《网络与系统攻防技术》 实验二实验报告
    目录1.实验内容2.实验问题3.实验过程3.1windows下使用netcat获取kali的Shell,用cron命令启动某项任务3.1.1使用netcat获取kali的shell3.1.2用cron命令启动某项任务3.2kali下使用socat获取windows的Shell,用schtasks命令创建并启动某项任务计划3.2.1实验准备3.2.2......
  • C++静态成员变量和静态成员函数的使用
    1.静态成员变量:定义在类中的静态成员,以static关键字初始化和成员变量区别:a.可以通过类名::变量名进行调用,可访问性还是由(public,private,protected)进行限制 例如下面的mystaitcClass::_id,protected属性内容无法直接进行访问,若要直接访问需要修改为public2.静态成员函数:类似可以通过......
  • 【智能大数据分析 | 实验四】Spark实验:Spark Streaming
    【作者主页】FrancekChen【专栏介绍】⌈⌈⌈智能大数据分析⌋......
  • 高效并行计算:使用C++中的std::thread实现多线程编程
    解锁Python编程的无限可能:《奇妙的Python》带你漫游代码世界在现代计算中,随着多核处理器的普及,如何充分利用硬件资源以提升程序性能成为关键问题之一。C++标准库提供了丰富的多线程支持,其中std::thread是用于实现并发计算的核心工具之一。通过合理的多线程设计,程序可以实现......
  • 用C++构建自己的编译器:从词法分析到代码生成
    解锁Python编程的无限可能:《奇妙的Python》带你漫游代码世界本文将带领读者从零开始构建一个简单的C++编译器。我们将逐步讲解如何进行词法分析、语法分析,以及如何将这些结果转换为目标代码。这篇文章的目标是帮助读者理解编译器的基本构成和工作原理,并提供可扩展的编译器......
  • 高精度乘法C++
    1.高精度乘高精度的简单算法思想:倒置相乘,统一处理进位,还原。复杂度:$o(n^2)$//BySnowDream#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintN=1e5+10;strings1,s2;intn1[N],n2[N],n3[N];//n1储存被乘数,n2储存乘数,n3储存积voidmul(){......
  • 20222323 2024-2025-1 《网络与系统攻防技术》实验三实验报告
    1.实验内容1.1实践目标(1)正确使用msf编码器,veil-evasion,自己利用shellcode编程等免杀工具或技巧正确使用msf编码器,使用msfvenom生成如jar之类的其他文件veil,加壳工具使用C+shellcode编程(2)通过组合应用各种技术实现恶意代码免杀(3)用另一电脑实测,在杀软开启的情况下,可......