当前位置: 首页 > news >正文

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

1,

#include <iostream>// 函数声明
double harmonicMean(double x, double y);int main() {double x, y, result;while (true) {std::cout << "请输入两个数(其中一个为0时结束): ";std::cin >> x >> y;// 检查是否输入了0if (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,

#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,

#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,

#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,

#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,

#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,

#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,

#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,

#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, 

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

http://www.lryc.cn/news/446888.html

相关文章:

  • 计算1 / 1 - 1 / 2 + 1 / 3 - 1 / 4 + 1 / 5 …… + 1 / 99 - 1 / 100 的值,打印出结果
  • Linux本地服务器搭建开源监控服务Uptime Kuma与远程监控实战教程
  • JS 历史简介
  • 爬虫逆向学习(七):补环境动态生成某数四代后缀MmEwMD
  • 光伏电站并网验收需要注意什么细节
  • 页面禁用鼠标右键属于反爬虫措施吗 ?
  • 视频理解大模型最新进展
  • cocos creator 使用 protobuf 的步骤与注意事项
  • mac访达查找文件目录
  • 【数据结构】点分治 点分树
  • K8s Calico替换为Cilium,以及安装Cilium过程(鲁莽版)
  • 背景图鼠标放上去切换图片过渡效果
  • 【Linux】当前进展
  • 阿里云云效多个ssh密钥对配置
  • 前后端跨域问题及其在ThinkPHP中的解决方案
  • 基于CentOS7上安装MicroK8s(最小生产的 Kubernetes)
  • 从《GTA5》的反外挂斗争看网络安全的重要性
  • python如何将字符转换为数字
  • TikTok流量不佳:是网络环境选择不当还是其他原因?
  • QT菜单栏设计(二级菜单栏)
  • 网站建设中,常用的后台技术有哪些,他们分别擅长做什么网站平台
  • 【线程】POSIX信号量---基于环形队列的生产消费者模型
  • Excel 设置自动换行
  • UNI-SOP使用说明
  • 记录-java web 生成并下载zip文件
  • 大数据集群部署文档
  • HTML中的表单(超详细)
  • 初识 C 语言(一)
  • LiveNVR监控流媒体Onvif/RTSP功能-支持电子放大拉框放大直播视频拉框放大录像视频流拉框放大电子放大
  • element ui中当el-dialog需要做全屏时,.fullscreen样式修改问题