用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-10 08:13:05 浏览: 68
简易计算器_c++jianyijisuanqi_if_简易计算器_labview_
好的,以下是C++代码实现小型计算器,可以实现你所列出的所有功能:
```c++
#include<iostream>
#include<cmath>
using namespace std;
// 计算器函数定义
void calculator() {
int choice;
int num1, num2;
while (true) {
cout << "\n请选择要进行的操作:" << 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:
cout << "请输入两个数:" << endl;
cin >> num1 >> num2;
cout << "两数之和为:" << num1 + num2 << endl;
break;
case 2:
cout << "请输入两个数:" << endl;
cin >> num1 >> num2;
cout << "两数之差为:" << num1 - num2 << endl;
break;
case 3:
cout << "请输入两个数:" << endl;
cin >> num1 >> num2;
cout << "两数之积为:" << num1 * num2 << endl;
break;
case 4:
cout << "请输入两个数:" << endl;
cin >> num1 >> num2;
if (num2 == 0) {
cout << "除数不能为0,请重新输入!" << endl;
break;
}
cout << "两数之商为:" << num1 / num2 << endl;
break;
case 5:
cout << "请输入两个数:" << endl;
cin >> num1 >> num2;
if (num2 == 0) {
cout << "除数不能为0,请重新输入!" << endl;
break;
}
cout << "两数取模运算的结果为:" << num1 % num2 << endl;
break;
case 6:
cout << "请输入一个整数:" << endl;
cin >> num1;
cout << "该整数的位数为:" << to_string(num1).length() << endl;
break;
case 7:
cout << "请输入半径:" << endl;
cin >> num1;
cout << "圆的面积为:" << 3.14 * pow(num1, 2) << endl;
break;
case 8:
cout << "请输入一个年份:" << endl;
cin >> num1;
if ((num1 % 4 == 0 && num1 % 100 != 0) || (num1 % 400 == 0)) {
cout << num1 << "年是闰年!" << endl;
}
else {
cout << num1 << "年不是闰年!" << endl;
}
break;
case 9:
cout << "请输入成绩:" << endl;
cin >> num1;
if (num1 >= 90 && num1 <= 100) {
cout << "A" << endl;
}
else if (num1 >= 80 && num1 < 90) {
cout << "B" << endl;
}
else if (num1 >= 70 && num1 < 80) {
cout << "C" << endl;
}
else if (num1 >= 60 && num1 < 70) {
cout << "D" << endl;
}
else if (num1 >= 0 && num1 < 60) {
cout << "E" << endl;
}
else {
cout << "输入成绩有误!" << endl;
}
break;
case 10:
cout << "请输入一个整数:" << endl;
cin >> num1;
cout << "从1到" << num1 << "之间的奇数和为:" << (num1 + 1) / 2 * (num1 % 2) + num1 / 2 * ((num1 + 1) % 2) << endl;
break;
case 11:
cout << "请输入一个整数:" << endl;
cin >> num1;
cout << "从1到" << num1 << "之间的偶数和为:" << num1 / 2 * 2 * (num1 % 2) + (num1 + 1) / 2 * ((num1 + 1) % 2) << endl;
break;
case 12:
cout << "请输入一个整数:" << endl;
cin >> num1;
cout << "从1到" << num1 << "之间所有3的倍数为:";
for (int i = 3; i <= num1; i += 3) {
cout << i << " ";
}
cout << endl;
break;
case 13:
cout << "请输入一个整数:" << endl;
cin >> num1;
cout << "即被3整除又被5整除的数为:";
for (int i = 15; i <= num1; i += 15) {
cout << i << " ";
}
cout << endl;
break;
case 14:
cout << "所有的水仙花数为:";
for (int i = 100; i <= 999; i++) {
int a = i / 100;
int b = i / 10 % 10;
int c = i % 10;
if (pow(a, 3) + pow(b, 3) + pow(c, 3) == i) {
cout << i << " ";
}
}
cout << endl;
break;
case 15:
cout << "请输入两个数:" << endl;
cin >> num1 >> num2;
cout << "最大值为:" << max(num1, num2) << endl;
break;
case 16:
cout << "请输入三个数:" << endl;
cin >> num1 >> num2 >> num2;
cout << "最大值为:" << max(max(num1, num2), num3) << endl;
cout << "最小值为:" << min(min(num1, num2), num3) << endl;
break;
case 17:
int arr[10];
cout << "请输入10个数:" << endl;
for (int i = 0; i < 10; i++) {
cin >> arr[i];
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "排序后的数组为:";
for (int i = 0; i < 10; i++) {
cout << arr[i] << " ";
}
cout << endl;
break;
case 18:
int matrix[2][3];
cout << "请输入一个2*3的矩阵:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cin >> matrix[i][j];
}
}
cout << "矩阵转置后为:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
cout << matrix[j][i] << " ";
}
cout << endl;
}
break;
case 19:
int matrix1[3][3];
cout << "请输入一个3*3的矩阵:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> matrix1[i][j];
}
}
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += matrix1[i][i];
}
cout << "矩阵对角线和为:" << sum << endl;
break;
case 20:
string s;
cout << "请输入一段英文文本:" << endl;
getline(cin, s);
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == ' ') {
count++;
}
}
cout << "单词个数为:" << count + 1 << endl;
break;
case 21:
struct Node {
int data;
Node* next;
};
Node* head = new Node;
head->next = NULL;
cout << "请输入链表长度:" << endl;
cin >> num1;
cout << "请输入链表元素:" << endl;
for (int i = 0; i < num1; i++) {
Node* p = new Node;
cin >> p->data;
p->next = head->next;
head->next = p;
}
cout << "链表为:";
Node* p = head->next;
while (p) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
break;
case 22:
struct Node1 {
int data;
Node1* next;
};
Node1* head1 = new Node1;
head1->next = NULL;
Node1* p1 = head1;
cout << "请输入链表元素,输入0结束输入:" << endl;
while (true) {
Node1* q = new Node1;
cin >> q->data;
if (q->data == 0) {
break;
}
q->next = NULL;
p1->next = q;
p1 = q;
}
cout << "链表为:";
Node1* q1 = head1->next;
while (q1) {
cout << q1->data << " ";
q1 = q1->next;
}
cout << endl;
break;
case 23:
struct Student {
string name;
int score;
};
Student stu[3];
cout << "请输入3个学生的姓名和成绩:" << endl;
for (int i = 0; i < 3; i++) {
cin >> stu[i].name >> stu[i].score;
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2 - i; j++) {
if (stu[j].score < stu[j + 1].score) {
Student temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
cout << "按照成绩从高到低排序后的学生信息为:" << endl;
for (int i = 0; i < 3; i++) {
cout << "姓名:" << stu[i].name << ",成绩:" << stu[i].score << endl;
}
break;
case 24:
int score[3];
cout << "请输入3个学生的成绩:" << endl;
for (int i = 0; i < 3; i++) {
cin >> score[i];
score[i] += 10;
}
cout << "加分后的学生成绩为:" << endl;
for (int i = 0; i < 3; i++) {
cout << score[i] << " ";
}
cout << endl;
break;
case 25:
cout << "退出程序!" << endl;
exit(0);
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
}
}
int main() {
calculator();
return 0;
}
```
运行程序后,根据菜单提示选择相应的功能即可。
阅读全文