首页 > 编程语言 >c++计算器

c++计算器

时间:2024-07-27 09:25:37浏览次数:9  
标签:cout int str1 system long c++ 计算器 include

因为用的是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;
}

标签:cout,int,str1,system,long,c++,计算器,include
From: https://blog.csdn.net/j5486545648564/article/details/140730016

相关文章

  • c++ 线程函数传递数据 对象和变量
         CMakeLists.txtcmake_minimum_required(VERSION3.10)project(MyProject)#查找并添加线程库find_package(ThreadsREQUIRED)#添加可执行文件add_executable(my_programmain.cpp)#添加线程库链接target_link_libraries(my_programThreads::Threa......
  • [C++]广度优先遍历
       代码与图见上图思路定义一个一维数组(char)和一个二维数组(int),一个bool类型数组来判断该节点是否被访问过。函数中定义队列,对各个结点进行入队出队,并标记为已访问。当该邻结点未被标记且与该节点连接,进行上述操作。注意:for循环的i变量初始赋值随二维数组而变化。如:......
  • C++文件系统操作6 - 跨平台实现文件和文件夹的拷贝
    1.关键词2.fileutil.h3.fileutil.cpp4.filesystem_win.h5.filesystem_win.cpp6.filesystem_unix.cpp7.源码地址1.关键词C++文件系统操作拷贝文件拷贝文件夹跨平台2.fileutil.h#pragmaonce#include<string>#include<cstdio>#include<cstdint>#i......
  • 【C++/STL】map和set介绍
    ......
  • C++11 emplace系列函数:性能提升的秘密武器
    C++11引入的emplace系列函数(如emplace_back、emplace、emplace_hint等)为STL容器提供了一种更高效的元素插入方式。这些函数不仅可以提高代码的性能,还能让我们的代码更加简洁优雅。今天,我们将深入探讨emplace函数的优势,并通过实例来展示它们的强大之处。emplace函数的主要优势......
  • C++初学者指南-6.函数对象--lambdas(基础)
    C++初学者指南-6.函数对象–lambdas(基础)文章目录C++初学者指南-6.函数对象--lambdas(基础)提醒:函数类和对象Lambdas变量捕获保存闭包通用Lambdas(C++14)广义捕获(C++14)相关内容幻灯片提醒:函数类和对象类至少提供一个operator()(…){…}函数能像一个......
  • Visual C++ 官方版下载与安装教程(微软常用运行库合集|DLL报错必装)
    前言MicrosoftVisualC++Redistributable(简称MSVC,VB/VC,系统运行库)是Windows操作系统应用程序的基础类型库组件。此版VisualC++运行库组件合集(微软常用运行库合集)由国内封装爱好者@Dreamcast打包而成,整合VisualC++组件安装包运行库所有版本,提供图形安装界面,可自选更新V......
  • C++中的内存管理
    目录一.C/C++的内存分布二.C++内存管理方式 1.new/delete操作内置类型2.new/delete操作自定义类型 三.operatornew与operatordelete函数 四.new和delete的实现原理1.实现内置类型 2.实现自定义类型五.定位new表达式(placement-new)六.malloc/free......
  • 体积计算器(三种语言)源码、效果图
    声明⚠️:英文、藏语翻译均来源网络,若不准确,请于本人联系,请勿抄袭!源码#A-1V=0#体积r=0#小半径R=0#大半径a=0#棱长&长b=0#宽h=0#高DMJ=0#底面积PI=3.14#π(保留两位小数)four_three=1/4*3#四分之三three_one=1/3*1#三分之一R......
  • C++优先队列 涵盖各种易错,技巧,应用和原理(附手写代码)
    当然也可以不看==> 阅读我的文章前请务必先阅读此文章! 都是废话这个文章里有视频,不知道为什么一点开文章就会播放,可以先翻到最后暂停一下再回来看目录阅读文章须知引言优先队列的概念优先队列的创建优先队列的操作*还有一些不常用的:优先队列的技巧如果类型是结构......