c++计算器

因为用的是MFC,所以就不把代码贴出来了,安装包在文章顶部,在此只把另一个计算器的代码贴出来:

#include<cstdio>
#include<cmath>
#include<iostream>
#include<string>
#include<windows.h>
using namespace std;
int Atoi(string s,int radix) {
	int ans=0;
	for(int i=0; i<s.size(); i++) {
		char t=s[i];
		if(t>='0'&&t<='9') ans=ans*radix+t-'0';
		else ans=ans*radix+t-'A'+10;
	}
	return ans;
}
string intToA(int n,int radix) {
	string ans="";
	do {
		int t=n%radix;
		if(t>=0&&t<=9)	ans+=t+'0';
		else ans+=t-10+'A';
		n/=radix;
	} while(n!=0);
	return ans;
}
void jinzhi() {

	string str1;
	long long d1, d2;
	cout<<"输入现在的数:   ";
	cin>>str1;
	cout<<"输入现在的进制(2~36):   ";
	cin>>d1;
	cout<<"输入目标进制(2~36):   " ;
	cin>>d2;
	if(d1<2||d1>36||d2<2||d2>36||str1[str1.size()-1]>d1)  cout<<"数据错误!"<<endl;
	else {
		if(d2!=10)	cout<<"结果为"<<intToA(Atoi(str1, d1), d2)<<endl;
		else	cout<<"结果为"<<Atoi(str1, d1)<<endl;
	}
	system("pause");
	getchar();
	system("cls");
}
void sushu() {
	long long n;
	cout<<"输入要判断的数:   ";
	cin>>n;
	for(int i=2; i<=sqrt(n); i++) {
		if(n%i==0) {
			cout<<n<<"是合数"<<endl;
			system("pause");
			getchar();
			system("cls");
			return;
		}
	}
	cout<<n<<"是素数"<<endl;
	system("pause");
	getchar();
	system("cls");
}
void jia() {
	long long a, b;
	cout<<"输入2个数进行加法:   ";
	cin>>a>>b;
	cout<<endl<<"结果为"<<a+b<<endl;
	system("pause");
	getchar();
	system("cls");
}
void jian() {
	long long a, b;
	cout<<"输入2个数进行减法:   ";
	cin>>a>>b;
	cout<<endl<<"结果为"<<a-b<<endl;
	system("pause");
	getchar();
	system("cls");
}
void chu() {
	double a, b;
	cout<<"输入2个数进行除法:   ";
	cin>>a>>b;
	printf("结果为%llf\n", a/b);
	system("pause");
	getchar();
	system("cls");
}
void cheng() {
	long long a, b;
	cout<<"输入2个数进行乘法:   ";
	cin>>a>>b;
	cout<<endl<<"结果为"<<a*b<<endl;
	system("pause");
	getchar();
	system("cls");
}
bool f(long long n) {
	for(long long i=2; i<=sqrt(n); i++) {
		if(n%i==0) {
			return 0;
		}
	}
	return 1;
}
void fenjie() {
	int n;
	cout<<"输入要分解的数:   ";
	cin>>n;
	if(f(n)) {
		cout<<n<<"本身就为素数";
	} else {
		cout<<n<<"=";
		for(int i=2; i*i<=n; i++) {
			if(!f(i)) {
				continue;
			} else {
				while(n!=i) {
					if(n%i==0) {
						cout<<i<<"*";
						n=n/i;
					} else {
						break;
					}
				}
			}
		}
		cout<<n;
	}
	cout<<endl;
	system("pause");
	getchar();
	system("cls");
}
void jiecheng() {
	int b;
	cout<<"输入要进行阶乘的数:   ";
	cin>>b;
	long long a=1;
	for(int i=b; i>=1; i--) {
		a*=i;
	}
	cout<<"结果为"<<a<<endl;
	system("pause");
	getchar();
	system("cls");
}
void cifang() {
	int b, c;
	cout<<"输入要求次方的数:   ";
	cin>>b;
	cout<<"输入要求的几次方:   ";
	cin>>c;
	long long a=1;
	for(int i=c; i>=1; i--) {
		a*=b;
	}
	cout<<"结果为"<<a<<endl;
	system("pause");
	getchar();
	system("cls");
}
void kaifang() {
	long long b;
	cout<<"输入要求开方的数:   ";
	cin>>b;
	double a=sqrt(b);
	printf("结果为%llf\n", a);
	system("pause");
	getchar();
	system("cls");
}
void gongyin() {
	long long b, a, max=-123415;
	cout<<"输入要求最大公因数的两个数:   ";
	cin>>b>>a;
	for(long long i=1; i<=min(a, b); i++) {
		if(a%i==0&&b%i==0&&i>max) {
			max=i;
		}
	}
	cout<<"结果为"<<max<<endl;
	system("pause");
	getchar();
	system("cls");
}
void gongbei() {
	long long b, a, min;
	cout<<"输入要求最小公倍数的两个数:   ";
	cin>>b>>a;
	for(long long i=max(a, b); i<=b*a; i++) {
		if(i%i==0&&i%b==0) {
			min=i;
			break;
		}
	}
	cout<<"结果为"<<min<<endl;
	system("pause");
	getchar();
	system("cls");
}
void paixu() {
	int n;
	cout<<"请输入元素个数:";
	cin >> n;
	int a[100000]= {};
	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 << "排序后的元素:\n";
	for (int i = 0; i < n; i++) {
		cout << a[i] << " ";
	}
	cout<<endl;
	system("pause");
	getchar();
	system("cls");
}
void gongneng(int a) {
	switch(a) {
		case 1:
			jia();
			return;
		case 2:
			jian();
			return;
		case 3:
			chu();
			return;
		case 4:
			cheng();
			return;
		case 5:
			sushu();
			return;
		case 6:
			jiecheng();
			return;
		case 7:
			cifang();
			return;
		case 8:
			kaifang();
			return;
		case 9:
			fenjie();
			return;
		case 10:
			gongyin();
			return;
		case 11:
			gongbei();
			return;
		case 12:
			paixu();
			return;
		case 13:
			jinzhi();
			return;
	}
}
int main() {
	int a;
	while(1) {
		cout<<"**************计算器V2.0**************"<<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.求最大公因数      *"<<endl;
		cout<<"*               11.求最小公倍数      *"<<endl;
		cout<<"*               12.排序              *"<<endl;
		cout<<"*               13.进制转换          *"<<endl;
		cout<<"*               14.退出              *"<<endl;
		cout<<"**************************************"<<endl;
		cout<<"请选择第几项功能:  ";
		cin>>a;
		system("cls");
		if(a==14) {
			break;
		} else if(a>14||a<1) {
			cout<<"数据错误!"<<endl;
			system("pause");
			getchar();
			system("cls");
		} else {
			gongneng(a);
		}
	}
	return 0;
}

这两篇代码皆为原创,非转载!!!

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值