更新:
- 增加了进率转换
但是。。。
大家看
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <cfloat>
#include <Windows.h>
#include <cmath>
using namespace std;
string getmes;
int n;
char ps[115814] = {};
void getmes_console();
int calc_console();
int fcalc_console();
int precalc_console();
int prefcalc_console();
int powcalc_console();
int sqrcalc_console();
int admin_console();
int bit_console();
int main() {
cout << "Starting Console Application......" << endl << endl;
Sleep(1500);
cout << "Installized." << endl;
cout << "版本1.0.2.0.12125009。输入\"help\"启动帮助文件。" << endl;
for (;;) {
getmes_console();
if (getmes == "exit") {
cout << "成功退出." << endl;
system("pause");
exit(0);
} else if (getmes == "help") {
cout << "这是一个控制台应用程序,整合了一些功能。" << endl;
cout << "具体操作如下:" << endl;
cout << "看到了:" << endl;
cout << "Console>" << endl;
cout << "就说明我们可以开始使用了,现有以下命令:" << endl;
cout << "1:exit" << endl;
cout << "退出程序。" << endl;
cout << "2:help" << endl;
cout << "帮助。" << endl;
cout << "3:calc" << endl;
cout << "计算器程序。现有加(+),减(-),乘(*),除(/)和取余运算(%),输入格式为:1 + 2。" << endl;
cout << "4:fcalc" << endl;
cout << "calc的小数点版本。" << endl;
cout << "5:precalc" << endl;
cout << "高级函数计算器。现有取绝对值(abs)和平方根(sqrt),输入格式为:abs -3。" << endl;
cout << "6:sqrcalc" << endl;
cout << "关于三角形的计算器。现有正弦函数(sin),余弦函数(cos),正切函数(tan),反正弦函数(asin)和反正切函数(atan),输入格式为:sin 20.8。"
<< endl;
cout << "7:prefcalc" << endl;
cout << "precalc的小数版本。增加向上取整(ceil)和向下取整(floor)。" << endl;
cout << "8:powclac" << endl;
cout << "计算第一个值的第二个值次方,输入格式为:1 2。" << endl;
cout << "9:filewrite" << endl;
cout << "文件写入器。在程序目录下会生成一个您提供的名字的文件,内容也为您提供的。" << endl;
cout << "10:limits" << endl;
cout << "显示变量类型在运行操作系统中的上下限和位数。" << endl;
} else if (getmes == "calc") {
calc_console();
} else if (getmes == "fcalc") {
fcalc_console();
} else if (getmes == "precalc") {
precalc_console();
} else if (getmes == "prefcalc") {
prefcalc_console();
} else if (getmes == "powcalc") {
powcalc_console();
} else if (getmes == "sqrcalc") {
sqrcalc_console();
} else if (getmes == "limits") {
cout.setf(std::ios_base::fixed);
cout.setf(std::ios_base::dec);
cout << "请注意每个系统的变量上下限不同,当前显示的为运行系统的变量上下限。" << endl;
cout << "上限:" << endl;
cout << "short:" << SHRT_MAX << endl;
cout << "int:" << INT_MAX << endl;
cout << "long:" << LONG_MAX << endl;
cout << "long long(C++11):" << LLONG_MAX << endl;
cout << "unsigned short:" << USHRT_MAX << endl;
cout << "unsigned int:" << UINT_MAX << endl;
cout << "unsigned long:" << ULONG_MAX << endl;
cout << "unsigned long long(C++11):" << ULLONG_MAX << endl;
cout.setf(std::ios_base::scientific);
cout << "float:" << FLT_MAX << endl;
cout << "double:" << DBL_MAX << endl;
cout << "long double:" << LDBL_MAX << endl;
cout << "下限:" << endl;
cout.setf(std::ios_base::fixed);
cout << "short:" << SHRT_MIN << endl;
cout << "int:" << INT_MIN << endl;
cout << "long:" << LONG_MIN << endl;
cout << "long long(C++11):" << LLONG_MIN << endl;
cout.setf(std::ios_base::scientific);
cout << "float:" << FLT_MIN << endl;
cout << "double:" << DBL_MIN << endl;
cout << "long double:" << LDBL_MIN << endl;
cout.setf(std::ios_base::fixed);
cout << "unsigned short:" << 0 << endl;
cout << "unsigned int:" << 0 << endl;
cout << "unsigned long:" << 0 << endl;
cout << "unsigned long long(C++11):" << 0 << endl;
cout << "位数:" << endl;
cout << "short:" << sizeof(short) << endl;
cout << "int:" << sizeof(int) << endl;
cout << "long:" << sizeof(int) << endl;
cout << "long long(C++11):" << sizeof(long long) << endl;
cout << "unsigned short:" << sizeof(unsigned short) << endl;
cout << "unsigned int:" << sizeof(unsigned int) << endl;
cout << "unsigned long:" << sizeof(unsigned long) << endl;
cout << "unsigned long long(C++11):" << sizeof(unsigned long long) << endl;
cout << "float:" << sizeof(float) << endl;
cout << "double:" << sizeof(double) << endl;
cout << "long double:" << sizeof(long double) << endl;
cout << "bool(C++11):" << sizeof(bool) << endl;
cout << "char:" << sizeof(char) << endl;
} else if (getmes == "admin") {
admin_console();
} else if (getmes == "appver") {
cout << "ConsoleApplication 控制台应用程序 版本1.0.2.0.12125009" << endl;
cout << "C++版本,本产品适用于Windows Vista SP1 x64及以上版本,并支持C++11标准的环境" << endl;
} else if (getmes == "bitcalc") {
bit_console();
} else {
cout << "Error000:未输入有效的命令。\a" << endl;
}
getmes = "";
}
return 0;
}
void getmes_console() {
cout << "Console>";
cin >> getmes;
}
int calc_console() {
long long a = 0;
char b = 'N';
long long c = 0;
long long ans = 0;
cout << "Message001:切换视图Calc。" << endl;
cout << "Calc>";
cin >> a >> b >> c;
switch (b) {
case '+':
ans = a + c;
break;
case '-':
ans = a - c;
break;
case '*':
ans = a * c;
break;
case '/':
if (b <= 0) {
cout << "来自视图Calc:Error002:除数不是有效的正整数。\a";
cout << "Error002:程序“clac”异常退出,返回代码1。\a";
exit(1);
}
ans = a / b;
break;
case '%':
ans = a % b;
break;
default:
cout << "来自视图Calc:Error001:未输入有效的运算符,计算无法继续。\a" << endl;
cout << "Error002:程序“clac”异常退出,返回代码1。\a";
exit(1);
}
cout << ans << endl;
return 1;
}
int fcalc_console() {
long double a = 0;
char b = 'N';
long double c = 0;
long double ans = 0;
int k = 0;
long double r = 0.0;
cout << "Message001:切换视图fCalc。" << endl;
cout << "fCalc>";
cin >> a >> b >> c;
switch (b) {
case '+':
ans = a + c;
break;
case '-':
ans = a - c;
break;
case '*':
ans = a * c;
break;
case '/':
if (c <= 0) {
cout << "来自视图fCalc:Error002:除数不是有效的正实数。\a";
cout << "Error002:程序“fcalc”异常退出,返回代码1。\a";
exit(1);
}
ans = a / c;
break;
case '%':
k = (long long)a / c;
r = a - k * c;
ans = r;
break;
default:
cout << "来自视图Calc:Error001:未输入有效的运算符,计算无法继续。\a" << endl;
cout << "Error002:程序“fcalc”异常退出,返回代码1。\a";
exit(1);
}
cout << ans << endl;
return 1;
}
int precalc_console() {
string math;
long long x = 0;
long long ans = 0;
cout << "Message001:切换视图preCalc。" << endl;
cout << "preCalc>";
cin >> math >> x;
if (math == "abs") {
ans = abs(x);
} else if (math == "sqrt") {
ans = (int)sqrt(x);
} else {
cout << "来自视图preCalc:Error001:未输入有效的运算函数,计算无法继续。\a" << endl;
return 1;
}
cout << ans << endl;
return 0;
}
int prefcalc_console() {
string math;
long double x = 0.0;
long double ans = 0.0;
cout << "Message001:切换视图prefCalc。" << endl;
cout << "prefCalc>";
cin >> math >> x;
if (math == "abs") {
ans = fabs(x);
} else if (math == "ceil") {
ans = ceil(x);
} else if (math == "floor") {
ans = floor(x);
} else if (math == "sqrt") {
ans = sqrt(x);
} else {
cout << "来自视图prefCalc:Error001:未输入有效的运算函数,计算无法继续。\a" << endl;
return 1;
}
cout << ans << endl;
return 0;
}
int powcalc_console() {
long double x = 0.0;
long double y = 0.0;
long double ans = 0.0;
cout << "Message001:切换视图powCalc。" << endl;
cout << "powCalc>";
cin >> x >> y;
ans = pow(x, y);
cout << ans << endl;
return 0;
}
int sqrcalc_console() {
string math;
long double x = 0.0;
long double ans = 0.0;
cout << "Message001:切换视图sqrCalc。" << endl;
cout << "sqrCalc>";
cin >> math >> x;
if (math == "sin") {
ans = sin(x);
} else if (math == "cos") {
ans = cos(x);
} else if (math == "tan") {
ans = tan(x);
} else if (math == "asin") {
ans = asin(x);
} else if (math == "acos") {
ans = acos(x);
} else if (math == "atan") {
ans = atan(x);
} else {
cout << "来自视图sqrCalc:Error001:未输入有效的运算函数,计算无法继续。\a" << endl;
return 1;
}
cout << ans << endl;
return 0;
}
int admin_console() {
unsigned long long password = 0;
cin >> password;
if (password == 1212) {
cout << "Console-admin>";
} else {
cout << "验证错误。" << endl;
return 1;
}
cin >> getmes;
if (getmes == "memcheck") {
cout << "全局定义变量" << endl;
cout << "getmes:string:" << hex << &getmes << endl;
cout.setf(std::ios_base::dec);
return 0;
} else if (getmes == "exit") {
return 0;
} else {
cout << "Error000:未输入有效的命令。\a" << endl;
}
return 0;
}
int bit_console() {
#define size CHAR_BIT * sizeof(int)
cout << "请输入一个整数:";
cin >> n;
cout << n << "的二进制为";
for (int i = size - 1; i >= 0; i--, n >>= 1) {
ps[i] = (01 & n) + '0';
}
ps[size] = '\0';
int i = 0;
while (ps[i]) {
putchar(ps[i]);
if (++i % 4 == 0 && ps[i]) {
putchar(' ');
}
}
cout << endl;
cout << n << "的八进制为";
printf("%o\n", n);
cout << n << "的十进制为";
printf("%d\n", n);
cout << n << "的十六进制为";
printf("%x\n", n);
return 0;
}
看我的程序怎么了,调用bit_console()时那个n显示不正常: