用C++编写一下计算器程序

用C++编写一下能加减乘除的计算器程序,谢谢!!!!!!!!!!!1
最新回答
吹梦到西洲

2024-09-18 00:36:41

刚好这2天写过个:

#include <iostream>
#include <string>
#include <sstream>
#include <cstddef>
#include <cmath>
#include <cctype>
#include <cstdlib>
#include <iomanip>
using namespace std;

template<class T>
class calculator
{
public:
calculator();
calculator(const calculator& cal);
calculator(const string& str);
calculator(const char* c_str);

template<class U>
calculator(const U& u);

string GetFormula();
T Result();

calculator operator = (const calculator& cal);

template<class X, class U>
X lexical_cast(U u);
private:
T dummyCal(const string& str);
T analyse();
T ops(T lhs, T rhs, char op);
string formula;
T value;
};

template<class T>
calculator<T>::calculator() : formula(""), value(T())
{
}

template<class T>
calculator<T>::calculator(const calculator& cal) : formula(cal.formula), value(cal.value)
{
}

template<class T>
calculator<T>::calculator(const string& str) : formula(str)
{
value = analyse();
}

template<class T>
calculator<T>::calculator(const char* c_str) : formula(string(c_str))
{
value = analyse();
}

template<class T>
template<class U>
calculator<T>::calculator(const U& u)
{
formula = u.formula;
value = analyse();
}

template<class T>
inline string calculator<T>::GetFormula()
{
return formula;
}

template<class T>
T inline calculator<T>::Result()
{
return value;
}

template<class T>
calculator<T> calculator<T>::operator = (const calculator& cal)
{
formula = cal.formula;
value = cal.value;
return *this;
}

template<class T>
T calculator<T>::ops(T lhs, T rhs, char op)
{
switch(op)
{
case '+':
return lhs + rhs;
case '-':
return lhs - rhs;
case '*':
return lhs * rhs;
case '/':
return lhs / rhs;
case '%':
return static_cast<T>(static_cast<int>(lhs) % static_cast<int>(rhs));
case '^':
return static_cast<T>(pow(static_cast<double>(lhs), static_cast<double>(rhs)));
default:
return 0;
}
}

template<class T>
T calculator<T>::dummyCal(const string& str)
{
string temp(str);
temp.erase(0, temp.find_first_not_of(' '));
size_t opPos;
while((opPos = temp.find_first_of("*/%^")) != string::npos
|| (opPos = temp.find_first_of("+-",1)) != string::npos)
{
size_t lhsValPos = temp.find_last_not_of("0123456789.", opPos - 1) == string::npos ?
0 : temp.find_last_not_of("0123456789.", opPos - 1) + 1;
if(temp[0] == '-' && temp.find_last_of("+-*/%^") != 0)
--lhsValPos;
size_t rhsValPos = temp.find_first_not_of("0123456789.", opPos + 2) == string::npos ?
temp.size() - 1 : temp.find_first_not_of("0123456789.", opPos + 2) - 1;
string LopR(temp, lhsValPos, rhsValPos - lhsValPos + 1);

istringstream isstrm(LopR);
T lhsVal, rhsVal;
char op;
isstrm >> lhsVal >> op >> rhsVal;
T result = ops(lhsVal, rhsVal, op);

temp.erase(lhsValPos, rhsValPos - lhsValPos + 1);
temp.insert(lhsValPos, lexical_cast<string>(result));

if(temp[0] == '-' && temp.find_last_of("+-*/%^") == 0)
break;
if(temp.find('e') != string::npos)
{
size_t ePos = temp.find('e');
if(temp.find_first_of("+-*/%^", ePos) == string::npos
&& (temp.find_last_of('-', ePos) == 0
|| temp.find_last_of("+-*/%^", ePos) == string::npos))
break;
}
}

return lexical_cast<T>(temp);
}

template<class T>
T calculator<T>::analyse()
{
string temp1 = formula;
while(temp1.find_first_of("()") != string::npos)
{
size_t rhsBracket = temp1.find(')');
size_t lhsBracket = temp1.rfind('(', rhsBracket);
string sResult(temp1, lhsBracket + 1, rhsBracket - lhsBracket - 1);

T partResult = dummyCal(sResult);

temp1.erase(lhsBracket, rhsBracket - lhsBracket + 1);
temp1.insert(lhsBracket, lexical_cast<string>(partResult));
}

istringstream sstrm(temp1);
T test;
while(sstrm >> test);
if(temp1 != lexical_cast<string>(test))
return dummyCal(temp1);

return lexical_cast<T>(temp1);
}

template<class T>
template<class X, class U>
X calculator<T>::lexical_cast(U u)
{
stringstream sstrm;
sstrm << u;
X x;
sstrm >> x;
return x;
}

void title()
{
cout.fill('=');
cout << setw(24) << '=' << "计算器" << setw(24) << '=' << '\n' << endl;
cout.fill(' ');
cout << setw(18) << ' ' << "C清屏, X退出, N继续" << setw(12) << ' '<< endl;
cout << "\n操作: 加 +\t减 -\t乘 *\t除 /\t模 %\t乘方 ^\n";
cout << "\n请输入任意计算式:\n";
}

int main()
{
title();
string formula;
int count = 1;
while(true)
{
cout << "\n式子" << count << " : ";
string input;
cin >> input;

if(input.find_first_not_of("CNXcnx0123456789.+-*/%^()") != string::npos)
{
cout << "输入中有错误, 请重新输入!\n";
cin.clear();
continue;
}

if(input.find_first_of("Cc") != string::npos)
{
system("cls");
formula.clear();
count = 1;
title();

}else if(input.find_first_of("Xx") != string::npos)
{
break;

}else
{
formula += input;
calculator<double> cal(formula);
++count;
cout << "结果" << count << " : " << formula << " = " << cal.Result() << endl;
formula = cal.lexical_cast<string>(cal.Result());
}
}
cout << "程序结束..." << endl;

//calculator<int> cal1("1+2+3+4");
//calculator<long> cal2("4*3*2*1");
//calculator<float> cal3("((1*3/2)+(4/(1+1)))*3");
//calculator<double> cal4("((1+3)*(20+4/6+(3-9)))^-2");
//cout << cal1.GetFormula() << " = " << cal1.Result() << '\n';
//cout << cal2.GetFormula() << " = " << cal2.Result() << '\n';
//cout << cal3.GetFormula() << " = " << cal3.Result() << '\n';
//cout << cal4.GetFormula() << " = " << cal4.Result() << endl;
}

打了注释那部分是我以前对算式的测试,你可以运行下看看。

以下是我测试的结果:

计算器
C清屏, X退出, N继续
(3/2+2)^3+(9*(2-5)-3)*2
(3/2+2)^3+(9*(2-5)-3)*2 = -17.125
请选择(C/X/N): n
+1
-17.125+1 = -16.125
请选择(C/X/N): n
/2
-16.125/2 = -8.0625
请选择(C/X/N): x
程序结束...

简单一点的(用的是C):

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <ctype.h>

double ops(double lhs, double rhs, char op)
{
switch(op)
{
case '+':
return lhs + rhs;
case '-':
return lhs - rhs;
case '*':
return lhs * rhs;
case '/':
return lhs / rhs;
case '^':
return pow(lhs, rhs);
default:
return (int)lhs % (int)rhs;
}
}

double cal(double lhs, int flag)
{
unsigned i;
static char OPS[] = {"+-*/%^"};
double rhs;
char op;
if(flag == 1)
{
scanf("%lf", &lhs);
}
lable:
scanf("%c", &op);
for(i = 0; i < sizeof OPS-1; ++i)
{
if(op == OPS[i])
break;
}
if(i == sizeof OPS-1)
{
printf("错误的运算符! 请重新输入: ");
fflush(stdin);
goto lable;
}
scanf("%lf", &rhs);
fflush(stdin);
return ops(lhs, rhs, op);
}

void title()
{
printf("%24c", ' ');
printf("计算器");
printf("%24c\n", ' ');
printf("%18c", ' ');
printf("C清屏, X退出, N继续\n");
printf("%12c", ' ');
printf("\n操作: 加 +\t减 -\t乘 *\t除 /\t模 %c \t乘方 ^\n", '%');
printf("\n请输入任意计算式:\n");
}

void select()
{
title();
char select = '0';
int count = 1;
double result = 0;
while(select != 'X')
{
printf("式子 %d: ", count);
if(count != 1)
printf("%.2lf", result);
if(count == 1)
result = cal(0, 1);
else
result = cal(result, 0);
++count;
printf("结果: %.2lf\n", result);
printf("\n请选择(C/X/N): ");
select = getchar();
select = toupper(select);
fflush(stdin);
while(select != 'C' && select != 'X' && select != 'N')
{
printf("\n错误的输入!\n");
printf("\n请选择(C/X/N): ");
select = getchar();
select = toupper(select);
fflush(stdin);
}
if(select == 'C')
{
system("cls");/* 如果你用TC不能编译通过这句换成clrscr() */
title();
count = 1;
}
}
}

int main()/* 如果不能通过换成void main */
{
select();
}
寂寞念行乐

2024-09-18 00:52:34

#include "iostream.h"
main()
{int i,j;
char k;
int z;
cin>>i>>k>>j>>endl;
if(k=='+') z=i+j;
if(k=='-') z=i-j;
if(k=='*') z=i*j;
if(k=='/') z=i/j;
cout<<i<<k<<j<<'='<<z<<endl;
}
盖世傻白甜

2024-09-18 04:15:34

简单的计算器:
#include <iostream>
using namespace std;
int main()
{
double x,y;
char z;
cout<<"请输入第一个数字:"<<endl;
cin>>x;
cout<<"请输入第二个数字:"<<endl;
cin>>y;
cout<<"请输入输入运算符号(+、-、*、/):"<<endl;
cin>>z;
cout.precision(7);
switch(z)
{
case '+':cout<<"结果:"<< x+y<<endl;
break;
case '-':cout<<"结果:"<< x-y<<endl;
break;
case '*':cout<<"结果:"<< x*y<<endl;
break;
case '/':cout<<"结果:"<< x/y<<endl;
break;
default:cout<<"请输入正确的运算符号。"<<endl;
}
system("pause");
return 0;
}