怎么给这个计算器加个括号……
这个程序是有用的,怎么加个括号?#include <stdlib.h>
#include<iostream.h>
#include<ctype.h>
const int MAX=1000;
class calculator
{
private:
struct keep1 //私有结构体keep1用来存储数字
{
double n[MAX];
int top;//栈顶元素
}
stack1;
struct keep2 //私有结构体keep2存储运算符
{
char n[MAX];
int top;//栈顶元素
}
stack2;
public:
calculator(){}; //空的构造函数
bool stack1empty(keep1 s);//判断数字栈是否为空
bool stack2empty(keep2 s);//判断运算符栈是否为空
void push1(keep1 &s,double num);//将数据压入数字栈
void push2(keep2 &s,char op);//将运算符压入运算符栈
void pop1(keep1 &s,double &num);
void pop2(keep2 &s,char &op);
int in(char op);
int out(char op);
void count(double a,char op,double b);
void cal();
};
bool calculator::stack1empty(keep1 s) //判断数字栈是否为空
{
if(s.top==-1)
return 1;
else return 0;
}
bool calculator::stack2empty(keep2 s)//判断运算符栈是否为空
{
if(s.top==-1)
return 1;
else return 0;
}
void calculator::push1(keep1 &s,double num)//将数据压入数字栈
{
if(s.top==MAX-1)
{cout<<"栈已满 ";
}
else
{
s.top++;
s.n[s.top]=num;
}
}
void calculator::push2(keep2 &s,char op)//将运算符压入运算符栈
{
if(s.top==MAX-1)
{cout<<"栈已满";
}
else
{
s.top++;
s.n[s.top]=op;
}
}
void calculator::pop1(keep1 &s,double &num)//将栈顶的数据元素取出,存放在num中
{
if(s.top==-1)
cout<<"栈为空"<<endl;
else
{
num=s.n[s.top];
s.top--;
}
}
void calculator::pop2(keep2 &s,char &op)//将栈顶的运算符取出,存放在op中
{
if(s.top==-1)
cout<<"栈为空"<<endl;
else
{
op=s.n[s.top];
s.top--;
}
}
int calculator::in(char op)//在栈内优先级的判断
{
if(op=='-'||op=='+')
return 2;
else if(op=='*'||op=='/')
return 4;
else return -1;
}
int calculator::out(char op)//在栈外优先级的判断
{
if(op=='-'||op=='+')
return 1;
else if(op=='*'||op=='/')
return 3;
else return -1;
}
void calculator::count(double a,char op,double b)//进行计算并将所得的结果压入栈顶
{
double sum;
switch(op)
{
case '+':sum=a+b;break;
case '-':sum=a-b;break;
case '*':sum=a*b;break;
case '/':sum=a/b;break;
default:break;
}
push1(stack1,sum);
}
void calculator::cal(void)
{
int i=0,j;
double a,b,c;
char expression[MAX],operate,temp[20];
cout<<"请输入表达式(输完之后请加=):";
cin>>expression;
stack1.top=-1;//清空数字栈
stack2.top=-1;//清空运算符栈
while(expression[i]!='=')//以=号结尾
{
if(isdigit(expression[i]))/*若读入的字符为数字,则继续判断下一个字符,直到下个字符不是数字或者不是小数点,即可保证该操作数是完整的小数,然后将该数入操作数栈*/
{
j=0;
while(isdigit(expression[i])||expression[i]
=='.')
{
temp[j++]=expression[i];
i++;
}
temp[j]='\0';
c=atof(temp);//char类型进行转换
push1(stack1,c);
}
else
{
if(expression[i]=='-'||expression[i]
=='+'||expression[i]=='*'||expression[i]
=='/')//若读入的字符为运算符的情况
{
if(in(stack2.n[stack2.top])<out(expression
[i])||stack2empty(stack2))//读入的运算符与运算符栈顶元素相比,并进行相应的操作
{
push2(stack2,expression[i]);
i++;
}
else
{
pop1(stack1,a);
pop1(stack1,b);
pop2(stack2,operate);
count(b,operate,a);//先进后出原则
}
}
}
}
while(stack2.top!=-1)//读入结束后,继续进行操作,直到运算符栈为空
{
pop1(stack1,a);
pop1(stack1,b);
pop2(stack2,operate);
count(b,operate,a);
}
cout<<stack1.n[stack1.top]<<endl;
}
int main()
{ int i;
calculator cp;
for(;;)
{
cout<<"\t\t*********************************\n";
cout<<"\t\t*********************************\n";
cout<<"\t\t******这是一个神奇的计算器*******\n";
cout<<"1--------------开始计算:\n";
cout<<"2--------------退出系统:\n";
cout<<"\n\n请选择功能:";
cin>>i;
switch(i)
{
case 1:
{
system("cls");//清屏
cp.cal();
break;
system("pause"); }
case 2:
{
cout<<"谢谢您的使用!\n";
return 0;
}
}
}
return 0;
}