用c++做一个小小计算器,它能够实现以下功能 1 加法 2 减法 3 乘法 4 除法 5 取模运算 6 判断数字的位数 7 计算圆面积 8 判断闰年 9 成绩转换 10 计算从1到输入数之间的奇数和 11 计算从1到输入数之间的偶数和 12 输出从1到输入数之间所有3的倍数 13 求即被3整除又被5整除的数 14 求水仙花数 15 求最大值 16 求最大最小值 17 数组—冒泡排序 18 数组-矩阵转置 19 数组-求矩阵对角线和 20 统计单词输入个数 21 静态创建链表 22 动态创建链表 23 学生成绩排序 24 学生成绩普涨10分 25 退出
时间: 2024-02-11 19:11:50 浏览: 156
好的,以下是一个简单的实现:
```c++
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int choice = 0;
while (choice != 25) {
cout << "请选择要进行的操作:" << endl;
cout << "1. 加法" << endl;
cout << "2. 减法" << endl;
cout << "3. 乘法" << endl;
cout << "4. 除法" << endl;
cout << "5. 取模运算" << endl;
cout << "6. 判断数字的位数" << endl;
cout << "7. 计算圆面积" << endl;
cout << "8. 判断闰年" << endl;
cout << "9. 成绩转换" << endl;
cout << "10. 计算从1到输入数之间的奇数和" << endl;
cout << "11. 计算从1到输入数之间的偶数和" << endl;
cout << "12. 输出从1到输入数之间所有3的倍数" << endl;
cout << "13. 求即被3整除又被5整除的数" << endl;
cout << "14. 求水仙花数" << endl;
cout << "15. 求最大值" << endl;
cout << "16. 求最大最小值" << endl;
cout << "17. 数组—冒泡排序" << endl;
cout << "18. 数组-矩阵转置" << endl;
cout << "19. 数组-求矩阵对角线和" << endl;
cout << "20. 统计单词输入个数" << endl;
cout << "21. 静态创建链表" << endl;
cout << "22. 动态创建链表" << endl;
cout << "23. 学生成绩排序" << endl;
cout << "24. 学生成绩普涨10分" << endl;
cout << "25. 退出" << endl;
cin >> choice;
switch (choice) {
case 1: {
double a, b;
cout << "请输入两个数:";
cin >> a >> b;
cout << "它们的和为:" << a + b << endl;
break;
}
case 2: {
double a, b;
cout << "请输入两个数:";
cin >> a >> b;
cout << "它们的差为:" << a - b << endl;
break;
}
case 3: {
double a, b;
cout << "请输入两个数:";
cin >> a >> b;
cout << "它们的积为:" << a * b << endl;
break;
}
case 4: {
double a, b;
cout << "请输入两个数:";
cin >> a >> b;
if (b == 0) {
cout << "除数不能为0!" << endl;
} else {
cout << "它们的商为:" << a / b << endl;
}
break;
}
case 5: {
int a, b;
cout << "请输入两个整数:";
cin >> a >> b;
cout << "它们的模为:" << a % b << endl;
break;
}
case 6: {
int n;
cout << "请输入一个整数:";
cin >> n;
int digits = 1;
while (n >= 10) {
n /= 10;
digits++;
}
cout << "它的位数为:" << digits << endl;
break;
}
case 7: {
double r;
cout << "请输入圆的半径:";
cin >> r;
double area = M_PI * r * r;
cout << "它的面积为:" << area << endl;
break;
}
case 8: {
int year;
cout << "请输入一个年份:";
cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
cout << year << "年是闰年" << endl;
} else {
cout << year << "年不是闰年" << endl;
}
break;
}
case 9: {
double score;
cout << "请输入成绩:";
cin >> score;
if (score >= 90) {
cout << "等级为:A" << endl;
} else if (score >= 80) {
cout << "等级为:B" << endl;
} else if (score >= 70) {
cout << "等级为:C" << endl;
} else if (score >= 60) {
cout << "等级为:D" << endl;
} else {
cout << "等级为:E" << endl;
}
break;
}
case 10: {
int n, sum = 0;
cout << "请输入一个整数:";
cin >> n;
for (int i = 1; i <= n; i += 2) {
sum += i;
}
cout << "从1到" << n << "之间的奇数和为:" << sum << endl;
break;
}
case 11: {
int n, sum = 0;
cout << "请输入一个整数:";
cin >> n;
for (int i = 2; i <= n; i += 2) {
sum += i;
}
cout << "从1到" << n << "之间的偶数和为:" << sum << endl;
break;
}
case 12: {
int n;
cout << "请输入一个整数:";
cin >> n;
cout << "从1到" << n << "之间所有3的倍数为:";
for (int i = 3; i <= n; i += 3) {
cout << i << " ";
}
cout << endl;
break;
}
case 13: {
int n;
cout << "请输入一个整数:";
cin >> n;
cout << "即被3整除又被5整除的数有:";
for (int i = 1; i <= n; i++) {
if (i % 3 == 0 && i % 5 == 0) {
cout << i << " ";
}
}
cout << endl;
break;
}
case 14: {
int n;
cout << "请输入一个整数:";
cin >> n;
cout << "水仙花数有:";
for (int i = 100; i <= n; i++) {
int a = i / 100;
int b = i / 10 % 10;
int c = i % 10;
if (i == a * a * a + b * b * b + c * c * c) {
cout << i << " ";
}
}
cout << endl;
break;
}
case 15: {
double a, b;
cout << "请输入两个数:";
cin >> a >> b;
double maxNum = a > b ? a : b;
cout << "它们的最大值为:" << maxNum << endl;
break;
}
case 16: {
double a, b;
cout << "请输入两个数:";
cin >> a >> b;
double maxNum = a > b ? a : b;
double minNum = a < b ? a : b;
cout << "它们的最大值为:" << maxNum << endl;
cout << "它们的最小值为:" << minNum << endl;
break;
}
case 17: {
int n;
cout << "请输入数组元素个数:";
cin >> n;
int a[n];
cout << "请输入数组元素:";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
cout << "排序后的数组为:";
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
break;
}
case 18: {
int m, n;
cout << "请输入矩阵的行数和列数:";
cin >> m >> n;
int a[m][n];
cout << "请输入矩阵元素:";
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
cout << "转置后的矩阵为:" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << a[j][i] << " ";
}
cout << endl;
}
break;
}
case 19: {
int n;
cout << "请输入矩阵的行数和列数:";
cin >> n;
int a[n][n];
cout << "请输入矩阵元素:";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i][i];
}
cout << "矩阵对角线元素之和为:" << sum << endl;
break;
}
case 20: {
string s;
cout << "请输入一个字符串:";
cin >> s;
int count = 1;
for (int i = 0; i < s.length(); i++) {
if (s[i] == ' ') {
count++;
}
}
cout << "单词个数为:" << count << endl;
break;
}
case 21: {
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode *head = new ListNode(0);
ListNode *p = head;
int n;
cout << "请输入链表元素个数:";
cin >> n;
cout << "请输入链表元素:";
for (int i = 0; i < n; i++) {
int x;
cin >> x;
ListNode *node = new ListNode(x);
p->next = node;
p = p->next;
}
p = head->next;
cout << "链表元素为:";
while (p != NULL) {
cout << p->val << " ";
p = p->next;
}
cout << endl;
break;
}
case 22: {
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode *head = new ListNode(0);
ListNode *p = head;
int n;
cout << "请输入链表元素个数:";
cin >> n;
cout << "请输入链表元素:";
for (int i = 0; i < n; i++) {
int x;
cin >> x;
ListNode *node = new ListNode(x);
p->next = node;
p = p->next;
}
p = head->next;
cout << "链表元素为:";
while (p != NULL) {
cout << p->val << " ";
p = p->next;
}
cout << endl;
break;
}
case 23: {
struct Student {
string name;
int score;
};
int n;
cout << "请输入学生人数:";
cin >> n;
Student students[n];
cout << "请输入学生姓名和成绩:";
for (int i = 0; i < n; i++) {
cin >> students[i].name >> students[i].score;
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (students[j].score < students[j + 1].score) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
cout << "学生排名为:" << endl;
for (int i = 0; i < n; i++) {
cout << i + 1 << ". " << students[i].name << " " << students[i].score << endl;
}
break;
}
case 24: {
struct Student {
string name;
int score;
};
int n;
cout << "请输入学生人数:";
cin >> n;
Student students[n];
cout << "请输入学生姓名和成绩:";
for (int i = 0; i < n; i++) {
cin >> students[i].name >> students[i].score;
students[i].score += 10;
}
cout << "学生成绩普涨10分后为:" << endl;
for (int i = 0; i < n; i++) {
cout << students[i].name << " " << students[i].score << endl;
}
break;
}
case 25: {
cout << "已退出" << endl;
break;
}
default: {
cout << "无效操作,请重新选择" << endl;
break;
}
}
}
return 0;
}
```
这个计算器实现了所有的功能。你可以根据需要删除或添加功能。
阅读全文