65,208
社区成员
发帖
与我相关
我的任务
分享#include<iostream>
#include<string>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
struct node //数据结点
{
double num;
char op;
node * next;
};
template <class T>
class stack
{
protected:
T elements[100];
int height;
int top;
int maxsize;
public:
stack(int i=10);
bool pop()
{
if(height==0)
return false;
else
{ top--;
height--;
return true;
}
}
void push(T x)
{
elements[++top]=x;
height++;
}
T gettop()
{ return elements[top];}
bool isempty()
{
if(height==0)
return true;
else
return false;
}
};
template<class T>
stack<T>::stack(int i)
{
height=0;
top=-1;
}
class list
{
protected:
node *head;
public:
list()
{
head=new node;
head->next=NULL;
}
void insert(double n)
{
node * p=head,*q;
while(p->next!=NULL)
p=p->next;
q=new node;
q->num=n;
q->op='~';
q->next=p->next;
p->next=q;
}
bool eeempty()
{
if(head->next==NULL)
return true;
else
return false;
}
node* listpop()
{
node *p=head->next,*q;
q=p->next;
head->next=q;
return p;
}
void insert(char ch)
{
node * p=head,*q;
while(p->next!=NULL)
p=p->next;
q=new node;
q->num=0;
q->op=ch;
q->next=p->next;
p->next=q;
}
};
int judge(char a,char b) //判断优先级,1 a优先,-1 b优先,0 一样
{
int table[256]; //优先级表格,扩展性高
table['(']=0;
table['+']=1;
table['-']=1;
table['*']=2;
table['/']=2;
table['%']=2;
table['^']=3;
if( table[a]-table[b] >0 )
return 1;
else if(table[a]-table[b] ==0)
return 0;
else
return -1;
}
void charge(string s) //表达式检查,报错
{
int i, flag=0, j=0, k=0;
for (i=0;s[i]!='\0';i++)
{
if(s[i]=='(')
j++;
if(s[i]==')')
k++;
if(k>j)
{ flag=1;
break;
}
if(s[i]=='(' &&( s[i+1]>'9' || s[i+1]<'0') && s[i+1]!='+' && s[i+1]!='-' && s[i+1]!='(')
{ flag=1;
break;
}
if(s[i+1]==')' && (s[i]>'9' || s[i]<'0' )&& s[i]!=')')
{ flag=1;
break;
}
if(s[i]==')' && s[i+1]=='(')
{
flag=1;
break;
}
if(s[i]=='/'&&s[i+1]=='0')
{
flag=1;
break;
}
if((s[i]<'0' || s[i]>'9' )&& s[i]!='(' && s[i]!=')'&&(s[i+1]<'0' || s[i+1]>'9') && s[i+1]!='(' && s[i+1]!=')')
{ flag=1;
break;
}
}
if (j!=k)
flag=1;
if(flag==1)
cout << " error!" << endl;
else
cout << "表达式有意义"<<endl;
}
int main()
{
string xx;
double result;
cin>>xx;
charge(xx);
cout<<"中缀表达式为:"<<xx<<endl;
list list1; //字符串改中缀表达式,记录在list1中
char c;
int i;
double lin=0;
double m=0;
double n=1;
char sin='S';
char cos='C';
char tan='T';
for(i=0;xx[i]!='\0';)
{
if(xx[i]<='9'&&xx[i]>='0')
{
for(;xx[i]<='9'&&xx[i]>='0';i++)
m=m*10+xx[i]-'0';
if(xx[i]=='.')
{
i++;
for(;xx[i]<='9'&&xx[i]>='0';i++)
{
n=n/10;
m=m+(xx[i]-'0')*n;
}
}
list1.insert(m);
m=0;n=1;
}
else //运算符的处理方式
{
if(xx[i]=='-')
{
if(xx[i-1]=='.'||(xx[i-1]<='9'&&xx[i-1]>='0'))
list1.insert(xx[i++]);
else
{
list1.insert(lin);
list1.insert(xx[i++]);
}
}
//对于单目运算符号,可添加一个数字0使其成为双目运算符
else if(xx[i]=='s') //sin函数
{
list1.insert(lin);
list1.insert(xx[i]);
i=i+3;
}
else
list1.insert(xx[i++]);
}
}
// cout<<"字符串变链表"<<endl;
list list2; //将中缀改后缀,记录在list2中
node *p,*q;
char v;
double aaa;
stack<char> st;
for(;!list1.eeempty();)
{
p=list1.listpop();
if(p->op=='(')
st.push(p->op);
else if(p->op==')')
{
while(st.gettop()!='(')
{
list2.insert(st.gettop());
st.pop();
}
st.pop();
}
else
{
if(p->op=='~')
{
aaa=p->num;
list2.insert(aaa);
}
else
{
while(st.isempty()==false && (judge(st.gettop(),p->op)>=0))
{
list2.insert(st.gettop());
st.pop();
}
st.push(p->op);
}
}
}
while (!st.isempty())
{
list2.insert(st.gettop());
st.pop();
}
// cout<<"中缀改后缀";
// cout<<"后缀表达式为:";
double num1,num2,num3; //中缀表达式求值
int k;
stack<double> sk;
while(!list2.eeempty())
{
p=list2.listpop();
if(p->op=='~')
sk.push(p->num);
else
{
num1=sk.gettop();sk.pop();
num2=sk.gettop();sk.pop();
switch(p->op)
{
case '+':sk.push(num1+num2);break;
case '-':sk.push(num2-num1);break;
case '*':sk.push(num1*num2);break;
case '/':sk.push(num2/num1);break;
case '%':sk.push((int)num2%(int)num1);break;
case '^':sk.push(pow(num2,num1));break;
case 's':sk.push(sin(num2));break;
}
}
}
cout<<"结果:"<<sk.gettop()<<endl;
return 0;
}
#include "StdAfx.h"
#include<iostream>
#include<string>
#include<stdio.h>
#include<cmath>
#include<stdlib.h>
using namespace std;
struct node //数据结点
{
double num;
char op;
node * next;
};
template <class T>
class stack
{
protected:
T elements[100];
int height;
int top;
int maxsize;
public:
stack(int i=10);
bool pop()
{
if(height==0)
return false;
else
{ top--;
height--;
return true;
}
}
void push(T x)
{
elements[++top]=x;
height++;
}
T gettop()
{ return elements[top];}
bool isempty()
{
if(height==0)
return true;
else
return false;
}
};
template<class T>
stack<T>::stack(int i)
{
height=0;
top=-1;
}
class list
{
protected:
node *head;
public:
list()
{
head=new node;
head->next=NULL;
}
void insert(double n)
{
node * p=head,*q;
while(p->next!=NULL)
p=p->next;
q=new node;
q->num=n;
q->op='~';
q->next=p->next;
p->next=q;
}
bool eeempty()
{
if(head->next==NULL)
return true;
else
return false;
}
node* listpop()
{
node *p=head->next,*q;
q=p->next;
head->next=q;
return p;
}
void insert(char ch)
{
node * p=head,*q;
while(p->next!=NULL)
p=p->next;
q=new node;
q->num=0;
q->op=ch;
q->next=p->next;
p->next=q;
}
};
int judge(char a,char b) //判断优先级,1 a优先,-1 b优先,0 一样
{
int table[256]; //优先级表格,扩展性高
table['(']=0;
table['+']=1;
table['-']=1;
table['*']=2;
table['/']=2;
table['%']=2;
table['^']=3;
if( table[a]-table[b] >0 )
return 1;
else if(table[a]-table[b] ==0)
return 0;
else
return -1;
}
void charge(string s) //表达式检查,报错
{
int i, flag=0, j=0, k=0;
for (i=0;s[i]!='\0';i++)
{
if(s[i]=='(')
j++;
if(s[i]==')')
k++;
if(k>j)
{ flag=1;
break;
}
if(s[i]=='(' &&( s[i+1]>'9' || s[i+1]<'0') && s[i+1]!='+' && s[i+1]!='-' && s[i+1]!='(')
{ flag=1;
break;
}
if(s[i+1]==')' && (s[i]>'9' || s[i]<'0' )&& s[i]!=')')
{ flag=1;
break;
}
if(s[i]==')' && s[i+1]=='(')
{
flag=1;
break;
}
if(s[i]=='/'&&s[i+1]=='0')
{
flag=1;
break;
}
if((s[i]<'0' || s[i]>'9' )&& s[i]!='(' && s[i]!=')'&&(s[i+1]<'0' || s[i+1]>'9') && s[i+1]!='(' && s[i+1]!=')')
{ flag=1;
break;
}
}
if (j!=k)
flag=1;
if(flag==1)
cout << " error!" << endl;
else
cout << "表达式有意义"<<endl;
}
int main()
{
string xx;
double result;
cin>>xx;
charge(xx);
cout<<"中缀表达式为:"<<xx<<endl;
list list1; //字符串改中缀表达式,记录在list1中
char c;
int i;
double lin=0;
double m=0;
double n=1;
char sin1='S';//这里把sin定义成一个字符???
char cos='C';// cos没用到
char tan='T';// tan没用到
for(i=0;xx[i]!='\0';)
{
if(xx[i]<='9'&&xx[i]>='0')
{
for(;xx[i]<='9'&&xx[i]>='0';i++)
m=m*10+xx[i]-'0';
if(xx[i]=='.')
{
i++;
for(;xx[i]<='9'&&xx[i]>='0';i++)
{
n=n/10;
m=m+(xx[i]-'0')*n;
}
}
list1.insert(m);
m=0;n=1;
}
else //运算符的处理方式
{
if(xx[i]=='-')
{
if(xx[i-1]=='.'||(xx[i-1]<='9'&&xx[i-1]>='0'))
list1.insert(xx[i++]);
else
{
list1.insert(lin);
list1.insert(xx[i++]);
}
}
//对于单目运算符号,可添加一个数字0使其成为双目运算符
else if(xx[i]=='s') //sin函数
{
list1.insert(lin);
list1.insert(xx[i]);
i=i+3;
}
else
list1.insert(xx[i++]);
}
}
// cout<<"字符串变链表"<<endl;
list list2; //将中缀改后缀,记录在list2中
node *p,*q;
char v;
double aaa;
stack<char> st;
for(;!list1.eeempty();)
{
p=list1.listpop();
if(p->op=='(')
st.push(p->op);
else if(p->op==')')
{
while(st.gettop()!='(')
{
list2.insert(st.gettop());
st.pop();
}
st.pop();
}
else
{
if(p->op=='~')
{
aaa=p->num;
list2.insert(aaa);
}
else
{
while(st.isempty()==false && (judge(st.gettop(),p->op)>=0))
{
list2.insert(st.gettop());
st.pop();
}
st.push(p->op);
}
}
}
while (!st.isempty())
{
list2.insert(st.gettop());
st.pop();
}
// cout<<"中缀改后缀";
// cout<<"后缀表达式为:";
double num1,num2,num3; //中缀表达式求值
int k;
stack<double> sk;
while(!list2.eeempty())
{
p=list2.listpop();
if(p->op=='~')
sk.push(p->num);
else
{
num1=sk.gettop();sk.pop();
num2=sk.gettop();sk.pop();
switch(p->op)
{
case '+':sk.push(num1+num2);break;
case '-':sk.push(num2-num1);break;
case '*':sk.push(num1*num2);break;
case '/':sk.push(num2/num1);break;
case '%':sk.push((int)num2%(int)num1);break;
case '^':sk.push(pow(num2,num1));break;
case 's':sk.push(sin(num2));break;
}
}
}
cout<<"结果:"<<sk.gettop()<<endl;
return 0;
}