跳到正文

剪贴板

剪贴板 7gdlq6j8

原文
剪贴板信息

剪贴板信息

收录
更新

计算器

C++
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <random>
#include <ctime>
#include<windows.h>
using namespace std;

int charToInt(char c) {
	if ('0' <= c && c <= '9')
		return c - '0';
	return c - 'A' + 10;
}

char intToChar(int n) {
	if (n < 10)
		return n + '0';
	return n - 10 + 'A';
}

long long calculateCombination(int n, int m) {
	if (m > n) {
		return 0;
	} else if (m == 0 || m == n) {
		return 1;
	} else {
		return calculateCombination(n - 1, m) + calculateCombination(n - 1, m - 1);
	}
}

long long calculatePermutation(int n, int m) {
	if (m > n) {
		return 0;
	} else if (m == 0 || m == n) {
		return 1;
	} else {
		return calculatePermutation(n - 1, m) * n;
	}
}

int main() {
	system("title 计算器 4.3");
	cout << "                                                      " << "计算器 4.3";
	while (1) {

		random_device rd;
		mt19937 gen(rd());
		uniform_int_distribution<> distrib(1, 100000);
		clock_t start, end;
		long long x, a[105], t, n, ans1 = 1, m, min_n = 0, max_n = 100000, b[10010];
		double c, d, ans = 1, f, g;
		int z1, z2;
		char e;
		bool abc = true;
		string S;
		queue<int>q;

		cout << endl << endl << "请输入你要使用的功能:普通计算输入1,进制转换输入2,根号使用输入3,次方计算输入4,整除计算输入5,整除判断输入6,阶乘计算输入7,阶加计算输入8,质数判断输入9,约瑟夫问题输入10,排列组合输入11,因数计算输入12,因数分解输入13,猜数游戏输入14  (功能正在陆续更新中):";
		cin >> z1;

		if (z1 == 1) {//普通计算
			cout << "请输入算式:";
			cin >> c >> e >> d;
			if (e == '+')
				cout << c << "+" << d << "=" << c + d;
			else if (e == '*')
				cout << c << "×" << d << "=" << c * d;
			else if (e == '/') {
				if (c > d)
					cout << c << "÷" << d << "=" << c / d;
				else if (d > c)
					cout << "错误";
				else if (c == d)
					cout << d << "÷" << c << "=" << "1";
				else
					cout << "错误";
			} else if (e == '-') {
				if (c > d)
					cout << c << "-" << d << "=" << c - d;
				else if (d > c)
					cout << "错误";
				else if (c == d)
					cout << c << "-" << d << "=" << "0";
				else
					cout << "错误";
			}
		}

		else if (z1 == 2) {//进制转换
			cout << "请问你要使用X进制转十进制还是十进制转X进制,X进制转十进制输入1,十进制转X进制输入2:";
			cin >> z2;
			if (z2 == 1) {
				cout << "请输入一个数(如果输入的数是十进制以上的数,字母得是大写):";
				cin >> S;
				cout << "请输入你写入数的进制(不超过31):";
				cin >> x;
				if (x > 31) {
					cout << "错误";
					continue;
				}
				cout << "转成十进制后的数是:";
				int len = S.length();
				for (int i = len - 1; i >= 0; i--)
					a[len - 1 - i] = charToInt(S[i]);
				int ans = 0, w = 1;
				for (int i = 0; i < 100; i++) {
					ans += w * a[i];
					w *= x;
				}
				cout << ans;
			} else if (z2 == 2) {
				cout << "请输入一个十进制数:";
				cin >> n;
				cout << "请输入你要将这个数要转成的进制数(不要超过31):";
				cin >> x;
				if (x > 31) {
					cout << "错误";
					continue;
				}
				cout << "转成" << x << "进制后的数是:";
				while (n) {
					a[t] = n % x;
					n /= x;
					t++;
				}
				for (int i = t - 1; i >= 0; i--)
					cout << intToChar(a[i]);
			} else {
				cout << "错误";
			}
		}

		else if (z1 == 3) {//根号使用
			cout << "你要开根号的数是:";
			cin >> c;
			cout << "结果是:" << sqrt(c);
		}

		else if (z1 == 4) {//次方计算
			ans = 1;
			cout << "你要开次方的数是:";
			cin >> c;
			cout << "你要开的次方是:";
			cin >> n;
			for (int i = 1; i <= n; i++) {
				ans *= c;
			}
			cout << c << "的" << n << "次方是:" << ans;
		}

		else if (z1 == 5) {//整除计算
			cout << "请输入被除数:";
			cin >> n;
			cout << "请输入除数:";
			cin >> x;
			if (x > n) {
				cout << "错误";
				continue;
			}
			g = n % x;
			f = n / x;
			cout << n << "÷" << x << "=" << f << "......" << g;
		}

		else if (z1 == 6) { //整除判断
			cout << "请输入被除数:";
			cin >> n;
			cout << "请输入除数:";
			cin >> x;
			if (x > n) {
				cout << "错误";
				continue;
			}
			g = n % x;
			if (g == 0) {
				cout << "可以整除";
				continue;
			} else {
				cout << "不可以整除";
				continue;
			}
		}

		else if (z1 == 7) {//阶乘计算
			cout << "请问你要计算几的阶乘(不超过35):";
			cin >> n;
			if (n <= 0 || n > 35) {
				cout << "错误";
				continue;
			}
			for (int i = n; i > 0; i--) {
				ans1 *= i;
			}
			cout << ans1;
		}

		else if (z1 == 8) {//阶加计算
			cout << "请问你要计算几的阶加:";
			cin >> n;
			if (n <= 0) {
				cout << "错误";
				return 0;
			}
			for (int i = n; i > 0; i--) {
				ans1 += i;
			}
			cout << ans1;
		}

		else if (z1 == 9) {//质数判断
			int abcd = 0;
			cout << "请输入你要判断的数:" ;
			cin >> n;
			if (n <= 0) {
				cout << "错误";
				continue;
			}
			if (n == 1) {
				cout << "不是质数也不是合数";
				continue;
			}
			for (int i = 2; i <= n; i++) {
				for (int j = 2; j <= n; j++) {
					x = i * j;
					if (x == n) {
						cout << "是合数";
						abcd = 1;
						break;
					}
				}
				if (abcd == 1) {
					break;
				}
			}
			if (abcd == 1) {
				continue;
			}
			cout << "是质数";
		}

		else if (z1 == 10) {//约瑟夫问题
			cout << "请输入个数:";
			cin >> n ;
			cout << "请输入隔几个去掉:";
			cin >> m ;
			m = m + 1;
			cout << "去掉顺序为:" ;
			for (int i = 1; i <= n; i++)
				q.push(i);
			for (int i = 1; i <= n; i++) {
				for (int j = 1; j < m; j++) {
					q.push(q.front());
					q.pop();
				}
				cout << q.front() << " ";
				q.pop();
			}
		}

		else if (z1 == 11) {//排列组合
			cout << "请问你要计算排列(C)还是组合(A),排列输入1,组合输入2:";
			cin >> z2;
			if (z2 == 2) {
				cout << "请输入元素总数;";
				cin >> n;
				cout << "请输入选择的元素个数:";
				cin >> m;
				cout << "从" << n << "个元素中选择" << m << "个元素的组合数为:" << calculateCombination(n, m) << endl;
			} else if (z2 == 1) {
				cout << "请输入元素总数 :";
				cin >> n;
				cout << "请输入选择的元素个数 :";
				cin >> m;
				cout << "从" << n << "个元素中选择" << m << "个元素的排列数为:" << calculatePermutation(n, m) << endl;
			} else {
				cout << "错误";
				continue;
			}
		}

		else if (z1 == 12) {//因数计算
			cout << "请输入要找因数的数:";
			cin >> n;
			int cnt = 0;
			for (int k = 1; k * k <= n; k++)
				if (n % k == 0) {
					cnt++;
					b[cnt] = k;
					if (k != n / k ) {
						cnt++;
						b[cnt] = n / k;
					}
				}
			sort(b, b + cnt + 1);
			for (int i = 1; i <= cnt; i++)
				cout << b[i] << ' ';
		}

		else if (z1 == 13) { //因数分解
			cout << "请输入要分解的数:";
			cin >> n;
			for (long long i = 2; i * i <= n; i++) {
				if (n % i != 0) {
					continue;
				}
				int cnt = 0;
				while (n % i == 0) {
					cnt++;
					n /= i;
				}
				if (abc) {
					abc = false;
				} else
					cout << " * ";
				cout << i;
				if (cnt > 1) {
					cout << "^" << cnt;
				}
			}
			if (n > 1) {
				if (abc) {
					abc = false;
				} else
					cout << " * ";
				cout << n;
			}
		}

		else if (z1 == 14) { //猜数游戏
			ans1 = distrib(gen);
			cout << "输入难度(输入的数即你猜的机会数):" ;
			cin >> d;
			start = clock();
			for (int i = 1; i <= d; i++) {
				cout << "从" << min_n << "-" << max_n << "中猜一个数字:";
				cin >> n;
				if (n > max_n || n < min_n) {
					cout << "错误" << endl << endl;
					continue;
				}
				if (n == ans1) {
					end = clock();
					cout << endl << "恭喜,你成功了!" << endl;
					cout << "你花了 " << (double)(end - start) / CLOCKS_PER_SEC << " 秒";
					continue;
				} else if (n > ans1) {
					if ((n - ans1) > 30000) {
						cout << "太大(与答案差30000以上)" << endl << endl;
						max_n = n - 30000;
					} else {
						cout << "大了(与答案差30000及以内)" << endl << endl;
						max_n = n;
						if (min_n < n - 30000) {
							min_n = n - 30000;
						}
					}
				} else if (n < ans1) {
					if ((ans1 - n) >= 30000) {
						cout << "太小(与答案差30000以上)!" << endl << endl;
						min_n = n + 30000;
					} else {
						cout << "小了(与答案差30000及以内)" << endl << endl;
						min_n = n;
						if (max_n > n + 30000) {
							max_n = n + 30000;
						}
					}
				}
			}
			cout << "你失败了!" << endl << "答案为:" << ans1;
			continue;
		}

		else
			cout << "错误";

	}
	return 0;
}