C++ prime plus-7-編程練習

1,

cpp 复制代码
#include <iostream>

// 函数声明
double harmonicMean(double x, double y);

int main() {
    double x, y, result;

    while (true) {
        std::cout << "请输入两个数(其中一个为0时结束): ";
        std::cin >> x >> y;

        // 检查是否输入了0
        if (x == 0 || y == 0) {
            break;
        }

        // 计算调和平均数
        result = harmonicMean(x, y);

        // 报告结果
        std::cout << "调和平均数是: " << result << std::endl;
    }

    std::cout << "程序结束。" << std::endl;

    return 0;
}

// 函数定义
double harmonicMean(double x, double y) {
    if (x == 0 || y == 0) {
        return 0; // 如果其中一个数为0,则调和平均数没有意义
    }
    return 2.0 * x * y / (x + y);
}

2,

cpp 复制代码
#include <iostream>
#include <vector>

// 声明数组函数
void inputScores(std::vector<int>& scores);
void displayScores(const std::vector<int>& scores);
double calculateAverage(const std::vector<int>& scores);

int main() {
    std::vector<int> scores;
    inputScores(scores);
    displayScores(scores);
    double average = calculateAverage(scores);
    std::cout << "平均成绩是: " << average << std::endl;

    return 0;
}

// 实现输入函数
void inputScores(std::vector<int>& scores) {
    int score;
    std::cout << "请输入高尔夫成绩(输入非数字结束): ";
    while (std::cin >> score && scores.size() < 10) {
        scores.push_back(score);
        std::cout << "继续输入高尔夫成绩(输入非数字结束): ";
    }
    std::cin.clear(); // 清除错误状态
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // 忽略剩余输入
}

// 实现显示函数
void displayScores(const std::vector<int>& scores) {
    std::cout << "输入的成绩为: ";
    for (int i = 0; i < scores.size(); ++i) {
        std::cout << scores[i] << " ";
    }
    std::cout << std::endl;
}

// 实现计算平均成绩函数
double calculateAverage(const std::vector<int>& scores) {
    if (scores.empty()) {
        return 0.0;
    }
    int sum = 0;
    for (int score : scores) {
        sum += score;
    }
    return static_cast<double>(sum) / scores.size();
}

3,

cpp 复制代码
#include <iostream>
#include <cstring> // 用于处理字符串

struct box {
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};

// 函数声明
void displayBox(const box& b);
void calculateVolume(box& b);

int main() {
    box myBox;

    // 获取用户输入
    std::cout << "请输入制造商名称: ";
    std::cin.getline(myBox.maker, 40);
    std::cout << "请输入高度: ";
    std::cin >> myBox.height;
    std::cout << "请输入宽度: ";
    std::cin >> myBox.width;
    std::cout << "请输入长度: ";
    std::cin >> myBox.length;

    // 计算体积
    calculateVolume(myBox);

    // 显示盒子信息
    displayBox(myBox);

    return 0;
}

// 按值传递box结构,并显示每个成员的值
void displayBox(const box& b) {
    std::cout << "制造商: " << b.maker << std::endl;
    std::cout << "高度: " << b.height << std::endl;
    std::cout << "宽度: " << b.width << std::endl;
    std::cout << "长度: " << b.length << std::endl;
    std::cout << "体积: " << b.volume << std::endl;
}

// 传递box结构的地址,并将volume成员设置为其他三维长度的乘积
void calculateVolume(box& b) {
    b.volume = b.height * b.width * b.length;
}

4,

cpp 复制代码
#include <iostream>
#include <cmath> // 用于log和pow函数

// 计算阶乘的函数
double factorial(int n) {
    double result = 1;
    for (int i = 2; i <= n; ++i) {
        result *= i;
    }
    return result;
}

// 计算组合数的函数
double combination(int n, int k) {
    return factorial(n) / (factorial(k) * factorial(n - k));
}

// 计算几率的对数的函数
double logCombination(int n, int k) {
    return lgamma(n + 1) - (lgamma(k + 1) + lgamma(n - k + 1));
}

int main() {
    int fieldNumbers = 47; // 域号码的数量
    int chosenNumbers = 5; // 选择的号码数量
    int specialNumbers = 27; // 特选号码的数量

    // 计算中头奖的几率的对数
    double logOdds = logCombination(fieldNumbers, chosenNumbers) + logCombination(specialNumbers, 1);

    // 输出几率的对数
    std::cout << "中头奖的几率的对数是: " << logOdds << std::endl;

    // 如果需要,可以计算几率的实际值
    double odds = exp(logOdds);
    std::cout << "中头奖的几率是: " << odds << std::endl;

    return 0;
}

5,

cpp 复制代码
#include <iostream>

// 递归函数声明
unsigned long long factorial(int n);

int main() {
    int number;
    std::cout << "请输入一个整数(输入-1结束): ";
    while (std::cin >> number && number != -1) {
        unsigned long long result = factorial(number);
        std::cout << number << "! = " << result << std::endl;
        std::cout << "请输入一个整数(输入-1结束): ";
    }
    std::cout << "程序结束。" << std::endl;
    return 0;
}

// 递归函数定义
unsigned long long factorial(int n) {
    if (n <= 1) { // 基本情况
        return 1;
    } else { // 递归情况
        return n * factorial(n - 1);
    }
}

6,

cpp 复制代码
#include <iostream>
#include <limits> // 用于std::numeric_limits

// 函数声明
int fill_array(double arr[], int size);
void show_array(const double arr[], int size);
void reverse_array(double arr[], int size);

int main() {
    const int size = 10; // 定义数组大小
    double array[size];

    // 使用fill_array函数填充数组
    int count = fill_array(array, size);
    std::cout << "填充了 " << count << " 个数字。" << std::endl;

    // 显示数组内容
    std::cout << "原始数组:" << std::endl;
    show_array(array, count);

    // 反转数组
    reverse_array(array, count);

    // 显示反转后的数组内容
    std::cout << "反转后的数组:" << std::endl;
    show_array(array, count);

    // 再次反转数组中除第一个和最后一个元素之外的所有元素
    reverse_array(array + 1, count - 2);
    
    // 显示最终数组内容
    std::cout << "最终数组:" << std::endl;
    show_array(array, count);

    return 0;
}

// fill_array函数定义
int fill_array(double arr[], int size) {
    int count = 0;
    double value;
    std::cout << "请输入 " << size << " 个double值 (输入非数字结束):" << std::endl;
    while (count < size && std::cin >> value) {
        arr[count++] = value;
    }
    std::cin.clear(); // 清除错误状态
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // 忽略剩余输入
    return count;
}

// show_array函数定义
void show_array(const double arr[], int size) {
    for (int i = 0; i < size; ++i) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;
}

// reverse_array函数定义
void reverse_array(double arr[], int size) {
    for (int i = 0; i < size / 2; ++i) {
        double temp = arr[i];
        arr[i] = arr[size - i - 1];
        arr[size - i - 1] = temp;
    }
}

7,

cpp 复制代码
#include <iostream>
#include <limits> // 用于std::numeric_limits

// 函数声明
double* fill_array(double* start, double* end);
void show_array(const double* start, const double* end);
void reverse_array(double* start, double* end);

int main() {
    const int size = 10; // 定义数组大小
    double array[size];

    // 使用fill_array函数填充数组
    double* fill_end = fill_array(array, array + size);
    std::cout << "数组填充结束位置:" << fill_end << std::endl;

    // 显示数组内容
    std::cout << "原始数组:" << std::endl;
    show_array(array, fill_end);

    // 反转数组
    reverse_array(array, fill_end);

    // 显示反转后的数组内容
    std::cout << "反转后的数组:" << std::endl;
    show_array(array, fill_end);

    // 再次反转数组中除第一个和最后一个元素之外的所有元素
    reverse_array(array + 1, fill_end - 1);
    
    // 显示最终数组内容
    std::cout << "最终数组:" << std::endl;
    show_array(array, fill_end);

    return 0;
}

// fill_array函数定义
double* fill_array(double* start, double* end) {
    double value;
    std::cout << "请输入double值 (输入非数字结束):" << std::endl;
    while (start < end && std::cin >> value) {
        *start++ = value;
    }
    std::cin.clear(); // 清除错误状态
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // 忽略剩余输入
    return start; // 返回最后被填充的位置
}

// show_array函数定义
void show_array(const double* start, const double* end) {
    while (start < end) {
        std::cout << *start++ << " ";
    }
    std::cout << std::endl;
}

// reverse_array函数定义
void reverse_array(double* start, double* end) {
    while (start < end) {
        double temp = *start;
        *start = *end;
        *end = temp;
        start++;
        end--;
    }
}

8,

cpp 复制代码
#include <iostream>
#include <string>

const int NUM_QUARTERS = 4; // 定义季度数量

int main() {
    const char* quarters[NUM_QUARTERS] = {"Q1", "Q2", "Q3", "Q4"};
    double expenses[NUM_QUARTERS] = {0};

    // 输入每个季度的开支
    for (int i = 0; i < NUM_QUARTERS; ++i) {
        std::cout << "请输入 " << quarters[i] << " 的开支: ";
        std::cin >> expenses[i];
    }

    // 显示每个季度的开支
    std::cout << "\n季度开支:" << std::endl;
    for (int i = 0; i < NUM_QUARTERS; ++i) {
        std::cout << quarters[i] << ": " << expenses[i] << std::endl;
    }

    return 0;
}

9,

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

const int SLEN = 30;

struct student {
    char fullname[SLEN];
    char hobby[SLEN];
    int age;
};

// 获取学生信息的函数
int getinfo(student *arr, int n) {
    int count = 0;
    string line;

    while (count < n) {
        cout << "请输入学生姓名: ";
        getline(cin, line);
        if (line.empty()) {
            break; // 如果输入的是空行,则终止输入
        }
        strncpy(arr[count].fullname, line.c_str(), SLEN - 1);
        arr[count].fullname[SLEN - 1] = '\0'; // 确保字符串以空字符结尾

        cout << "请输入学生爱好: ";
        getline(cin, line);
        strncpy(arr[count].hobby, line.c_str(), SLEN - 1);
        arr[count].hobby[SLEN - 1] = '\0';

        cout << "请输入学生年龄: ";
        cin >> arr[count].age;
        cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略换行符

        count++;
    }
    return count; // 返回填充的数组元素数量
}

// 显示学生信息的函数
void display(const student *s) {
    cout << "姓名: " << s->fullname << endl;
    cout << "爱好: " << s->hobby << endl;
    cout << "年龄: " << s->age << endl;
}

int main() {
    const int MAX_STUDENTS = 5;
    student students[MAX_STUDENTS];
    int numStudents = getinfo(students, MAX_STUDENTS);

    cout << "\n学生信息:" << endl;
    for (int i = 0; i < numStudents; i++) {
        display(&students[i]);
        cout << endl;
    }

    return 0;
}

10,

cpp 复制代码
#include <iostream>

// 函数声明
double add(double x, double y);
double multiply(double x, double y);
double calculate(double x, double y, double (*pf)(double, double));

int main() {
    double x, y, result;
    double (*pf[3])(double, double) = {add, multiply}; // 函数指针数组

    while (true) {
        std::cout << "请输入两个数字(输入非数字结束): ";
        std::cin >> x >> y;
        if (!std::cin) {
            std::cin.clear(); // 清除错误状态
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // 忽略剩余输入
            break;
        }

        // 使用 calculate 调用不同的函数
        std::cout << "加法结果: " << calculate(x, y, add) << std::endl;
        std::cout << "乘法结果: " << calculate(x, y, multiply) << std::endl;

        std::cout << "\n继续输入两个数字,或输入非数字结束程序。\n";
    }

    std::cout << "程序结束。" << std::endl;
    return 0;
}

// add 函数定义
double add(double x, double y) {
    return x + y;
}

// multiply 函数定义
double multiply(double x, double y) {
    return x * y;
}

// calculate 函数定义
double calculate(double x, double y, double (*pf)(double, double)) {
    return pf(x, y);
}
相关推荐
Hiweir ·10 分钟前
机器翻译之创建Seq2Seq的编码器、解码器
人工智能·pytorch·python·rnn·深度学习·算法·lstm
star数模22 分钟前
2024“华为杯”中国研究生数学建模竞赛(E题)深度剖析_数学建模完整过程+详细思路+代码全解析
python·算法·数学建模
Tak1Na25 分钟前
2024.9.19
算法
sjsjs1144 分钟前
【数据结构-扫描线】力扣57. 插入区间
数据结构·算法·leetcode
王哈哈嘻嘻噜噜1 小时前
数据结构中线性表的定义和特点
数据结构·算法
一杯茶一道题1 小时前
LeetCode 260. 只出现一次的数字 III
算法·leetcode
MogulNemenis1 小时前
力扣415周赛
java·数据结构·算法·leetcode
Rense11 小时前
常用的基于无线射频( UWB)室内定位技术的原理与算法
算法
zzhnwpu1 小时前
代码随想录算法训练营第三七天| 动态规划:完全背包理论基础 518.零钱兑换II 377. 组合总和 Ⅳ 322. 零钱兑换
算法·leetcode·动态规划
一道秘制的小菜2 小时前
C++第十一节课 new和delete
开发语言·数据结构·c++·学习·算法