70,018
社区成员




//******************************
//* 作者:刘澜涛 *
//* 学号:00848200 *
//* 题目:中缀表达式的计算 *
//* 日期:2009/10/18 *
//******************************
#include<iostream>
#include<assert.h>
#include<cmath>
using namespace std;
template<class T>
class CStack//自己的定义的栈
{
private:
T *stack;
int top;
int len;
public:
CStack(int lenth)//栈的构造函数
{
stack=new T[lenth];
top=0;
len=lenth;
}
void push(T node)//栈的增加结点
{
assert(top<len);
stack[top]=node;
top++;
}
T pop()//栈顶结点弹出
{
assert(top>0);
top--;
return stack[top];
}
T gettop()//返回栈顶元素
{
assert(top>0);
return stack[top-1];
}
void makeempty()//清空栈
{
top=0;
}
bool isempty()//返回栈是否为空
{
if(top==0)
return true;
else return false;
}
};
double calculate(char c,double num2,double num1)//基本计算函数
{
switch(c)
{
case '+':return num2+num1;break;
case '-':return num2-num1;break;
case '*':return num2*num1;break;
case '/':return num2/num1;break;
}
}
bool canbedone(char c,double num)//判断是否出现除数为零情况的函数
{
if(c=='/'&&fabs(num)<1.0e-10)
{
cout<<"表达式除数不能为零,请重新输入"<<endl;
return 0;
}
else return 1;
}
int main()
{
char str[1000]={0};//用来存放表达式
CStack<double> num(1000);//用来存放待计算的数字的栈
CStack<char> sign(1000);//用来存计算符号
int len,i,time;
double temp;//过渡变量
while(1)
{
begin:
num.makeempty();
sign.makeempty();
memset(str,0,sizeof(str));
cout<<"请输入您要计算的表达式,输入'end'退出程序"<<endl;//输入提示
cin>>str;
len=strlen(str);
for(i=0;i<=len-1;i++)
{
if(strcmp(str,"end")==0)
return 0;
else
{
if(!((str[i]>='0'&&str[i]<='9')||str[i]=='+'||str[i]=='-'||
str[i]=='*'||str[i]=='/'||str[i]=='('||str[i]==')'||str[i]=='.'))//判断式子中是否存在非法字符
{
cout<<"您输入的表达式有误,请重新输入"<<endl;
goto begin;
}
}
}
temp=0;
time=0;
for(i=0;i<=len-1;i++)
{
if(str[i]>='0'&&str[i]<='9')
{
if(time==0)
temp=str[i]-'0'+temp*10;
else
{
temp=(str[i]-'0')/pow(10,time)+temp;
time++;
}
}//把字符串转化为对应数字
else if(str[i]==')')//遇到闭括号开始弹出元素并计算,到开括号后停止
{
time=0;//遇到闭括号,小数点位数清零
while(sign.gettop()!='(')
{
if(!canbedone(sign.gettop(),temp))
goto begin;//判断是否有除数为0
temp=calculate(sign.pop(),num.pop(),temp);
if(sign.isempty())//没有开括号,说明表达式有误,重新输入若
{
cout<<"表达式有误,请重新输入"<<endl;
goto begin;
}
}
sign.pop();
}
else if(str[i]=='(')
sign.push(str[i]);//遇到开括号直接压栈
else if(str[i]=='.')//若遇到小数点则temp(记录小数点位数)开始记录
time=1;
else
{
time=0;//若遇到运算符则小数点位数清零
if(sign.isempty())
sign.push(str[i]);
else if(sign.gettop()=='/'||sign.gettop()=='*'||((sign.gettop()=='+'||sign.gettop()=='-')&&
(str[i]=='+'||str[i]=='-')))//若运算符str[i],与sign栈顶的运算符同级,则弹出栈顶进行计算,再进行压栈
{
if(!canbedone(sign.gettop(),temp))
goto begin;//判断是否有除数为0
temp=calculate(sign.pop(),num.pop(),temp);
sign.push(str[i]);
}
else
sign.push(str[i]);//不同级则直接压栈
num.push(temp);//把运算符前面的数字压栈
temp=0;
}
}
while(!sign.isempty())
{
if(!canbedone(sign.gettop(),temp))
goto begin;//判断是否有除数为0
if(sign.gettop()=='(')
{
cout<<"表达式有误,请重新输入"<<endl;
goto begin;
}//遇到多余开括号说明表达式有误
temp=calculate(sign.pop(),num.pop(),temp);
}
cout<<temp<<endl<<endl;//输出结果并额外输出一个空行
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void min()
{
int a;
int b;
int c;
char op;
printf("1:");
scanf("%d",&a);
fflush(stdin);
printf("+-*/:");
scanf("%c",&op);
fflush(stdin);
printf("2:");
scanf("%d",&b);
fflush(stdin);
switch(op)
{
case '+':
c=a+b;
break;
case '-':
c=a-b;
break;
case '*':
c=a*b;
break;
case '/':
c=a/b;
break;
default:
printf("+-/*!!!!!error");
return;
}
printf ("%d\n",c);
/**/
return ;
}
int main()
{
while(1)
{
min();
}
return 0;
}