代码仅供作业小组人员使用
(非完成版)C++实现科学计算器主函数代码,(不含调用函数)_克蕾尔的博客-CSDN博客
(初稿)C++实现科学计算器主函数代码(含调用函数)_Honeyseaaa的博客-CSDN博客
#include<iostream>
#include<cmath>
#include <math.h>
using namespace std;
int key1() { //三角函数预处理
int k1;
cout << "请问是否需要对该数字进行预处理,如不需要请输入0:" << endl;
cout << "1.sin 2.cos 3.tan " << endl;
cin >> k1;
while (k1 != 1 && k1 != 2 && k1 != 3 && k1 != 0) {
cout << "请重新输入" << endl;
cin >> k1;
}
switch (k1) {
case 1:
return 1;
break;
case 2:
return 2;
break;
case 3:
return 3;
break;
default:
return 0;
break;
}
}
void key2(double* cal, int a) {
double c = *cal;
switch (a) {//所有涉及cal的都用c来表示
case 1:
c = sin(c);//sinc
break;
case 2:
c = cos(c);//cosc
break;
case 3:
c = tan(c);//tanc
break;
case 0:
c = c;
break;
}
*cal = c;
}
int keys(double* cal) {
int b1;
double b2, c;
c = *cal;
cout << "请输入您所需要的运算符或运算方式前的编号,其中第8个是结束运算:" << endl;
cout << "************************************" << endl;
cout << "* 1. + 2. — 3. × 4. ÷ *" << endl;
cout << "* 5.n√ 6. ^n 7.1/X 8. FIN *" << endl;
cout << "************************************" << endl;
cin >> b1;
while (b1 != 1 && b1 != 2 && b1 != 3 && b1 != 4 && b1 != 5 && b1 != 6 && b1 != 7 && b1 != 8) {
cout << "请重新输入" << endl;
cin >> b1;
}
int a = key1();
cout << "请输入下一个数,如果您已经结束运算请随意输入数字" << endl;
cin >> b2;
key2(&b2, a);
while (b1 != 8) {
switch (b1) {
case 1:
c += b2;
break;
case 2:
c -= b2;
break;
case 3:
c *= b2;
break;
case 4:
c /= b2;
break;
case 5:
c = pow(c, 1 / b2);
break;
case 6:
c = pow(c, b2);
break;
case 7:
c = b2 / c;
break;
default:
break;
}
*cal = c;
return 0;
}
if (b1 == 8) {
*cal = c;
return 1;
}
}
int Narcissus(int n) { //判断水仙花数
int a, b, c, m;
a = n / 100;
b = (n % 100) / 10;
c = n % 10;
m = a * a * a + b * b * b + c * c * c;
if (n == m)
cout << n << "是水仙花数" << endl;
else
cout << n << "不是水仙花数" << endl;
return 0;
}
int Divisor(int a, int b) { //计算最大公因数
int x=0;
cout<<"请输入您想要判断的第一个数:"<<endl;
cin>>a;
cout<<"请输入您想要判断的第二个数:"<<endl;
cin>>b;
for (x = a; x >= 1; x--) {
if (a % x == 0 && b % x == 0)
{
cout <<a<<"和"<<b<<"的最大公因数是"<<x<<endl;
break;
}
}
return 0;
}
int Multiple(int a, int b) { //计算最小公倍数
int y;
cout<<"请输入您想要判断的第一个数:"<<endl;
cin>>a;
cout<<"请输入您想要判断的第二个数:"<<endl;
cin>>b;
for (y = a; y <= a * b; y++) {
if (y % a == 0 && y % b == 0)
{
cout <<a<<"和"<<b<<"的最小公倍数是" <<y<<endl;
break;
}
}
return 0;
}
bool isPrime(int n) { //判断素数
int i;
if (n == 1) {
return false;
}
else {
for (i = 2; i <= sqrt(n); i++) {
if ((n % i) == 0)
return false;
}
}
return true;
}
void tools() { //工具
int a;
cout << "请选择您需要的工具" << endl;
cout << "1. 水仙花数" << endl;
cout << "2. 最大公因数" << endl;
cout << "3. 最小公倍数" << endl;
cout << "4. 素数" << endl;
cout << "5. 阶乘(20以内)" << endl;
cin >> a;
int n=0, m=0;
switch (a) {
case 1:
cout<<"请输入您想要判断的数字(3位数):";
cin >> n;
Narcissus(n);
break;
case 2:
Divisor(n, m);
break;
case 3:
Multiple(n, m);
break;
case 4:
bool isFlag;
cout<<"请输入您想要判断的数字:";
cin>>n;
isFlag = isPrime(n);
if (isFlag) {
cout << n << "是素数" << endl;
}
else {
cout << n << "不是素数" << endl;
}
break;
case 5:
long long n;
long long result = 1;
cin >> n;
for (int i = 1; i <= n; i++) {
result *= i;
}
cout << n << "的阶乘为" << n << "!=" << result << endl;
break;
}
}
void calculation() { //计算
double cal = 0;
int a = key1();
cout << "请输入数字:" << endl;
cin >> cal;
key2(&cal, a);
int n = 0;
while (n == 0) {
n = keys(&cal);
}
cout << cal << endl;
}
int main() { //工具or计算
while(1){
int a1 = 0;
cout << "欢迎使用科学计算器!请选择您需要的功能" << endl;
cout << "1.工具" << endl;
cout << "2.计算" << endl;
cin >> a1;
while (a1 != 1 && a1 != 2) {
cout << "请重新输入" << endl;
cin >> a1;
}
if (a1 == 1) {
tools();
}
else if (a1 == 2) {
calculation();
}
}
}