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

用C++编写一下能加减乘除的计算器程序,谢谢!!!!!!!!!!!1

刚好这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();
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2007-03-25
#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;
}
第2个回答  2007-03-24
简单的计算器:
#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;
}

用c语言设计一个简单的加减乘除计算器
1、打开visual C++ 6.0-文件-新建-文件-C++ Source File。2、输入预处理命令和主函数:#include \/*函数头:输入输出头文件*\/,void main()\/*空类型:主函数*\/。3、定义变量:int a,b,d; \/*定义变量的数据类型为整型*\/,char c;\/*定义变量的数据类型为字符型*\/。4、输入四则运算式:pri...

用c语言 (c++) 编写计算器程序
01 首先我们需要在Dev C++软件中创建一个C语言项目,项目类型选择控制台程序,如下图所示 02 接下来我们在项目下面

用C++编写一个计算器程序。用户输入两个运算数和四则运算符,输出计算结 ...
用C++编写的”输入两个运算数和四则运算符,输出计算结果”计算器程序代码具体如下:include<stdio.h> void main(){int a,b,d;char c;printf("请输入一种运算符:\\n");scanf("%c",&c);printf("请输入两个数:\\n");scanf("%d",&a);scanf("%d",&b);switch(c){ case '+':d=a+...

c++使用宏的计算器?
b) a*b#define DIVIDE(a,b) a\/bint main(){float a,b;scanf("%f %f",&a,&b);printf("%f\\n",PLUS(a,b));printf("%f\\n",MINUS(a,b));printf("%f\\n",MULTI(a,b));printf("%f\\n",DIVIDE(a,

c++计算器源代码
include<string.h> include<conio.h> include<math.h> int resultprocess(char mexp[],double * result);int tokenprocess(char mexp[],double shuzhi[],char signs[]);double resultadd(double shuzhi[],char signs[],int sp);void main(){ char mexp[100];double result=0;printf("Please...

如何用c++,定义计算器类calculator?
{ public:calculator(double a=0,double b=0):a(a),b(b){ } void set(double a,double b){ this->a=a;this->b=b;} double add(){ return a+b;} double subtract(){ return a-b;} double multiply(){ return a*b;} double divide(){ if(b==0)throw "错误,除数不能为0...

计算器程序。用户输入运算数和四则运算符,输出计算结果;当运算符为字...
用C++编写的”输入两个运算数和四则运算符,输出计算结果”计算器程序代码具体如下: #include void main() {int a,b,d; char c; printf("请输入一种运算符:\\n"); scanf("%c",&c); printf("请输入两个数:\\n"); scanf("%d",&a); scanf("%d",&b); ......

利用C语言完成迷你计算器程序编写
float cal(float a,char c,float b){float m;switch(c){case'*':m=a*b;break;case'\/':m=a\/b;break;}return m;}void main(){ float x[10000]={0};float fsum=0;char y[10000]={0};int i=0,j,k,l,r; do{i++;scanf("%f%c",&x[i],&y[i]);}while(y[i]!=...

用c++做一个四则运算计算器(支持加减乘除混合运算,支持括号,倒数,正负...
op1); } if (inPriority(op1)>inPriority(op.top())) { \/\/判断优先级并计算 num1 = dealNum(num1, num2, op1); num2 = num.top(); num.pop(); op1 = op.top(); op.pop(); } else { num2 = dealNum(num2, num.top(), op.top()); num...

如何用c++做一个计算器,能连续加减乘除的,不需要界面,最好能是用while...
str = s[i++] + '\\0'; v.push_back(make_pair<int, string> (1, str)); } else if (s[i] == ')') { str = s[i++] + '\\0'; v.push_back(make_pair < int, string>(2, str)

相似回答