五年级小学生用仅有的一点C++知识编出的一个计算器:
英文版:
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include "windows.h"
using namespace std;
long long factorial(long long x)
{
long long sum = 1;
for (long long i = 1; i <= x; i++)
{
sum *= i;
}
return sum;
}
int main()
{
//此程序为c++计算器的4.0版本,较上一版(c++计算器3.0版本)将求平方和求立方的两个功能合并成一个功能(幂),且将加、减、乘三个功能变成可计算浮点值,且新增了求平方根、求立方根、求阶乘和求pi四个功能。
//此版本最终完成时间为2023年10月21日11:39
int s1;
cout << "Welcome to c++ calculator 4.0!" << endl;
cout << "Please enter order:" << endl;
cout << "1.addition 2.subduction 3.nultiplicative 4.division 5.cloth cover 6.square root 7.cube root 8.factorial 9.pi(circumference ratio)." << endl;
cin >> s1;
if (s1 >= 1 && s1 <= 4)
{
if (s1 == 1)
{
int num;
cout << "Please enter the number of arithmetic numbers:";
cin >> num;
double sum = 0;
for (int i = 1; i <= num; i++)
{
double x;
cin >> x;
sum += x;
}
cout << sum << endl;
cout << "\033[32;1mThe operation is finished.\033[0m" << endl;
cout << "\033[37;1mWelcome to use c++ calculator 4.0 next time!\033[0m" << endl;
return 0;
}
if (s1 == 2)
{
int num;
cout << "Please enter the number of arithmetic numbers:";
cin >> num;
double v, x;
cin >> x;
v = x;
for (int i = 1; i <= num-1; i++)
{
cin >> x;
v -= x;
}
cout << v << endl;
cout << "\033[32;1mThe operation is finished.\033[0m" << endl;
cout << "\033[37;1mWelcome to use c++ calculator 4.0 next time!\033[0m" << endl;
return 0;
}
if (s1 == 3)
{
int num;
cout << "Please enter the number of arithmetic numbers:";
cin >> num;
double mul = 1.0;
for (int i = 1; i <= num; i++)
{
double x;
cin >> x;
mul *= x;
}
cout << mul << endl;
cout << "\033[32;1mThe operation is finished.\033[0m" << endl;
cout << "\033[37;1mWelcome to use c++ calculator 4.0 next time!\033[0m" << endl;
return 0;
}
if (s1 == 4)
{
cout << "Please indicate how the output results are:" << endl;
cout << "1.round down 2.round up 3.half adjust 4.quotient and remainder 5.Quotient exact floating-point value" << endl;
int s2;
cout << "prompt:Current version number 4(quotient and remainder)the function only supports inputting 1 dividend and 1 divisor." << endl;
cin >> s2;
if (s2 == 1)
{
int num;
cout << "Please enter the number of arithmetic numbers:";
cin >> num;
double v, x;
cin >> x;
v = x;
for (int i = 1; i <= num-1; i++)
{
cin >> x;
v /= x;
}
cout << int(v) << endl;
}
if (s2 == 2)
{
int num;
cout << "Please enter the number of arithmetic numbers:";
cin >> num;
double v, x;
cin >> x;
v = x;
for (int i = 1; i <= num-1; i++)
{
cin >> x;
v /= x;
}
cout << ceil(v) << endl;
}
if (s2 == 3)
{
int num;
cout << "Please enter the number of arithmetic numbers:";
cin >> num;
double v, x;
cin >> x;
v = x;
for (int i = 1; i <= num-1; i++)
{
cin >> x;
v /= x;
}
cout << round(v) << endl;
}
if (s2 == 4)
{
int num;
cout << "Please enter the number of arithmetic numbers:";
cin >> num;
int a, b;
cin >> a >> b;
cout << "quotient:" << a / b << " " << "remainder:" << a % b << endl;
}
if (s2 == 5)
{
int num;
cout << "Please enter the number of arithmetic numbers:";
cin >> num;
double v, x;
cin >> x;
v = x;
for (int i = 1; i <= num-1; i++)
{
cin >> x;
v /= x;
}
cout << v << endl;
}
cout << "\033[32;1mThe operation is finished.\033[0m" << endl;
cout << "\033[37;1mWelcome to use c++ calculator 4.0 next time!\033[0m" << endl;
return 0;
}
}
if (s1 >= 5 && s1 <= 9)
{
if (s1 == 5)
{
long long num1, num2;
cout << "Please enter the base number:";
cin >> num1;
cout << "Please enter the index:";
cin >> num2;
cout << pow(num1, num2) << endl;
cout << "\033[32;1mThe operation is finished.\033[0m" << endl;
cout << "\033[37;1mWelcome to use c++ calculator 4.0 next time!\033[0m" << endl;
return 0;
}
if (s1 == 6)
{
long long num;
cout << "Please enter the number of arithmetic numbers:";
cin >> num;
cout << sqrt(num) << endl;
cout << "\033[32;1mThe operation is finished.\033[0m" << endl;
cout << "\033[37;1mWelcome to use c++ calculator 4.0 next time!\033[0m" << endl;
return 0;
}
if (s1 == 7)
{
long long num;
cout << "Please enter the number of arithmetic numbers:";
cin >> num;
cout << cbrt(num) << endl;
cout << "\033[32;1mThe operation is finished.\033[0m" << endl;
cout << "\033[37;1mWelcome to use c++ calculator 4.0 next time!\033[0m" << endl;
return 0;
}
if (s1 == 8)
{
long long num;
cout << "Please enter the number:";
cin >> num;
cout << factorial(num) << endl;
cout << "\033[32;1mThe operation is finished.\033[0m" << endl;
cout << "\033[37;1mWelcome to use c++ calculator 4.0 next time!\033[0m" << endl;
return 0;
}
if (s1 == 9)
{
string pi = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912";
cout << "Please enter the decimal number:";
int dn;
cin >> dn;
if (dn <= 0)
{
cout << "\033[31;1mInput error.\033[0m" << endl;
return 0;
}
if (dn == 1)
{
cout << "3.1..." << endl;
cout << "approximate:3.1" << endl;
cout << "\033[32;1mThe operation is finished.\033[0m" << endl;
cout << "\033[37;1mWelcome to use c++ calculator 4.0 next time!\033[0m" << endl;
return 0;
}
cout << "3.";
for (int i = 2; i <= dn+1; i++)
{
cout << pi[i];
}
cout << "..." << endl;
cout << "approximate:" << endl;
cout << "3.";
for (int i = 2; i <= dn; i++)
{
cout << pi[i];
}
if (pi[dn+1] <= 4)
{
cout << pi[dn+1] << endl;
}
else
{
cout << pi[dn+1]+1 << endl;
}
cout << "\033[32;1mThe operation is finished.\033[0m" << endl;
cout << "\033[37;1mWelcome to use c++ calculator 4.0 next time!\033[0m" << endl;
return 0;
}
}
return 0;
}
中文版:
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include "windows.h"
using namespace std;
long long factorial(long long x)
{
long long sum = 1;
for (long long i = 1; i <= x; i++)
{
sum *= i;
}
return sum;
}
int main()
{
//此程序为c++计算器的4.0版本,较上一版(c++计算器3.0版本)将求平方和求立方的两个功能合并成一个功能(幂),且将加、减、乘三个功能变成可计算浮点值,且新增了求平方根、求立方根、求阶乘和求pi四个功能。
//此版本最终完成时间为2023年10月21日11:39
int s1;
cout << "欢迎来到c++计算器4.0版本!" << endl;
cout << "请输入您要完成的运算:" << endl;
cout << "1.加法 2.减法 3.乘法 4.除法 5.幂运算 6.平方根 7.立方根 8.阶乘 9.pi(圆周率)." << endl;
cin >> s1;
if (s1 >= 1 && s1 <= 4)
{
if (s1 == 1)
{
int num;
cout << "请输入加数数量:";
cin >> num;
double sum = 0;
for (int i = 1; i <= num; i++)
{
double x;
cin >> x;
sum += x;
}
cout << sum << endl;
cout << "\033[32;1m运算完毕。\033[0m" << endl;
cout << "\033[37;1m欢迎下次使用c++计算器4.0版本!\033[0m" << endl;
return 0;
}
if (s1 == 2)
{
int num;
cout << "请输入减法运算数量:";
cin >> num;
double v, x;
cin >> x;
v = x;
for (int i = 1; i <= num-1; i++)
{
cin >> x;
v -= x;
}
cout << v << endl;
cout << "\033[32;1m运算完毕。\033[0m" << endl;
cout << "\033[37;1m欢迎下次使用c++计算器4.0版本!\033[0m" << endl;
return 0;
}
if (s1 == 3)
{
int num;
cout << "请输入因数数量:";
cin >> num;
double mul = 1.0;
for (int i = 1; i <= num; i++)
{
double x;
cin >> x;
mul *= x;
}
cout << mul << endl;
cout << "\033[32;1m运算完毕。\033[0m" << endl;
cout << "\033[37;1m欢迎下次使用c++计算器4.0版本!\033[0m" << endl;
return 0;
}
if (s1 == 4)
{
cout << "请输入输出结果类型:" << endl;
cout << "1.向下取整 2.向上取整 3.四舍五入 4.商和余数 5.商准确浮点值" << endl;
int s2;
cout << "提示:当前版本4号(商和余数)功能仅支持输入1个被除数和一个除数。" << endl;
cin >> s2;
if (s2 == 1)
{
int num;
cout << "请输入除法运算次数:";
cin >> num;
double v, x;
cin >> x;
v = x;
for (int i = 1; i <= num-1; i++)
{
cin >> x;
v /= x;
}
cout << int(v) << endl;
}
if (s2 == 2)
{
int num;
cout << "请输入除法运算次数:";
cin >> num;
double v, x;
cin >> x;
v = x;
for (int i = 1; i <= num-1; i++)
{
cin >> x;
v /= x;
}
cout << ceil(v) << endl;
}
if (s2 == 3)
{
int num;
cout << "请输入除法运算次数:";
cin >> num;
double v, x;
cin >> x;
v = x;
for (int i = 1; i <= num-1; i++)
{
cin >> x;
v /= x;
}
cout << round(v) << endl;
}
if (s2 == 4)
{
int num;
cout << "请输入被除数和除数:";
cin >> num;
int a, b;
cin >> a >> b;
cout << "商:" << a / b << " " << "余数:" << a % b << endl;
}
if (s2 == 5)
{
int num;
cout << "请输入除法运算次数:";
cin >> num;
double v, x;
cin >> x;
v = x;
for (int i = 1; i <= num-1; i++)
{
cin >> x;
v /= x;
}
cout << v << endl;
}
cout << "\033[32;1m运算完毕。\033[0m" << endl;
cout << "\033[37;1m欢迎下次使用c++计算器4.0版本!\033[0m" << endl;
return 0;
}
}
if (s1 >= 5 && s1 <= 9)
{
if (s1 == 5)
{
long long num1, num2;
cout << "请输入底数:";
cin >> num1;
cout << "请输入指数:";
cin >> num2;
cout << pow(num1, num2) << endl;
cout << "\033[32;1m运算完毕。\033[0m" << endl;
cout << "\033[37;1m欢迎下次使用c++计算器4.0版本!\033[0m" << endl;
return 0;
}
if (s1 == 6)
{
long long num;
cout << "请输入原数字:";
cin >> num;
cout << sqrt(num) << endl;
cout << "\033[32;1m运算完毕。\033[0m" << endl;
cout << "\033[37;1m欢迎下次使用c++计算器4.0版本!\033[0m" << endl;
return 0;
}
if (s1 == 7)
{
long long num;
cout << "请输入原数字:";
cin >> num;
cout << cbrt(num) << endl;
cout << "\033[32;1m运算完毕。\033[0m" << endl;
cout << "\033[37;1m欢迎下次使用c++计算器4.0版本!\033[0m" << endl;
return 0;
}
if (s1 == 8)
{
long long num;
cout << "请输入几的阶乘:";
cin >> num;
cout << factorial(num) << endl;
cout << "\033[32;1m运算完毕。\033[0m" << endl;
cout << "\033[37;1m欢迎下次使用c++计算器4.0版本!\033[0m" << endl;
return 0;
}
if (s1 == 9)
{
string pi = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912";
cout << "请输入小数点后几位:";
int dn;
cin >> dn;
if (dn <= 0)
{
cout << "\033[31;1m输入错误。\033[0m" << endl;
return 0;
}
if (dn == 1)
{
cout << "3.1..." << endl;
cout << "约等于:3.1" << endl;
cout << "\033[32;1m运算完毕。\033[0m" << endl;
cout << "\033[37;1m欢迎下次使用c++计算器4.0版本!\033[0m" << endl;
return 0;
}
cout << "3.";
for (int i = 2; i <= dn+1; i++)
{
cout << pi[i];
}
cout << "..." << endl;
cout << "约等于:" << endl;
cout << "3.";
for (int i = 2; i <= dn; i++)
{
cout << pi[i];
}
if (pi[dn+1] <= 4)
{
cout << pi[dn+1] << endl;
}
else
{
cout << pi[dn+1]+1 << endl;
}
cout << "\033[32;1m运算完毕。\033[0m" << endl;
cout << "\033[37;1m欢迎下次使用c++计算器4.0版本!\033[0m" << endl;
return 0;
}
}
return 0;
}
一开始由于输出中文乱码,所以被迫使用英文。后来在网上查到了解决方法,完美解决,于是又全换成了中文,大家就看中文版吧。
程序肯定有不完善的地方,如果大家发现了可以提出来,我也会改进的。
代码共251行,用的编译软件是Embarcadero Dev-C++ Version 6.3版本;