用c怎么解释带()的四则运算?有思路即可。

superwolf 2001-03-22 07:37:00
...全文
193 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
duststar 2001-03-24
  • 打赏
  • 举报
回复
You can find answer in all of books of 数据结构
I have finish it with PrefixExpression (前缀表达式)with C++
I write two classes stac //just is stack , PrefixExpression
and use main.cpp to work this is the version of DOS
//stack.h
#include <iostream.h>
class stac
{
public:

stac(int n);
~stac();
void push(int x);
void pop();
int top();
bool isempty();
int ptop();
int Position();
int Length();
private:
int *ST;
int length;
int position;
};

stac::stac(int n)
{
ST=new int[n];
length=n;
position=-1;
}


void stac::push(int x)
{
if (length-1==position)
{
cout<<"It is full of stac1!"<<endl;
}
else
{
position++;
ST[position]=x;
}
}

void stac::pop()
{
if (position==-1)
{
cout<<"There is no element in stac1!"<<endl;
}
else
{
position--;
}
}

int stac::top()
{
if (position==-1)
{
cout<<"There is no element in stac2!"<<endl;
return 0;
}
else
{
return ST[position];
}
}

int stac::ptop ()
{
int x;
x=top();
pop();
return x;
}

bool stac::isempty ()
{
if (position==-1)
return true;
else
return false;
}

stac::~stac ()
{
delete []ST;
}

int stac::Length ()
{
return length;
}

int stac::Position ()
{
return position;
}
//PrefixExpression.h
#include "stack.h"
#include <string.h>

class PrefixExpression
{
public:

PrefixExpression(char * expression);
void change(char * expression);
void output();
void calculation();

void show();
private:
char * prefix;
int length;
int position;
int answer;
int charToint();
};



PrefixExpression::PrefixExpression(char * expression)
{
length=strlen(expression);
position=-1;
prefix=new char[length];
answer=0;
}

void PrefixExpression::change( char * expression)
{
stac a(length);
stac b(length);
char * exp=expression;
while(*exp!='@')
{
a.push (*exp);
exp++;
}
position=length-1;
while(!a.isempty())
{
switch(a.top())
{ case '.':
{ prefix[position]=a.ptop();
position--;
while(a.top()<='9' && a.top()>='0')
{ prefix[position]=a.ptop();
position--;
}
break;
}
case '-':
case '+': { char w;
w=a.ptop();
if(!b.isempty ())
{

while (b.top()=='*' || b.top()=='/')
{
prefix[position]=b.ptop ();
position--;
}

};
b.push(w);
break;
}

case '*': {
b.push(a.ptop());
break;
}
case '/': {
b.push(a.ptop());
break;
}
case '(': {
a.ptop();
char w;
w=b.ptop();
while(w!=')')
{
prefix[position]=w;
position--;
w=b.ptop();
}
break;
}
case ')': { b.push(a.ptop());
break;
}
}
}
while(!b.isempty ())
{
prefix[position]=b.ptop();
position--;
}
prefix[position]='@';
position--;
}



int PrefixExpression::charToint()
{
int k=0;
int j=1;
while(prefix[position]<='9' && prefix[position]>='0')
{
k=k+(prefix[position--]-'0')*j;
j*=10;
}
return k;
}

void PrefixExpression::calculation ()
{
stac a(length);
position=length-1;
char w;
while((w=prefix[position--])!='@')
{
switch(w)
{
case '.':
{
a.push(charToint());
break;
}
case '+':
{
a.push(a.ptop()+a.ptop());
break;
}
case '-':
{
int j=a.ptop();
a.push(j-a.ptop());
break;
}
case '*':
{
a.push(a.ptop()*a.ptop());
break;
}
case '/':
{
int j=a.ptop();
a.push(j/a.ptop());
break;
}

}

}
answer=a.ptop();
if(!a.isempty ()) cout<<"there are something wrong!"<<endl;

}

void PrefixExpression::output ()
{
cout<<"The answer is "<<answer<<endl;
}



void PrefixExpression::show ()
{
cout<<length<<" "<<position<<endl;
char * exp=prefix;
while(*exp!='\0')
{
cout<<*exp++;
}

}
//main.cpp
#include "PrefixExpression.h"

void main()
{
char * expression;
cout<<"Please give your maths expression ."<<endl;
cout<<"For example :(10.+2.)-8.*50./2.+(2.+3.-211.*6.)@"<<endl;
cout<<"Don't forget the sign of end '@'"<<endl;
cin>>expression;
PrefixExpression fix(expression);

fix.change(expression);
fix.show();

fix.calculation ();
fix.output ();
}
//end all of program
this program only can deal with int expecially divide.
if some high programer can improve this program please write to me
dust2000@sohu.com

Flymouse 2001-03-23
  • 打赏
  • 举报
回复
扫描表达式,把数和符号放入不同的栈,事设定好一个操作符优先级表来表示不同操作符相遇时先执行哪一个.详细的看一看数据结构的书.
superwolf 2001-03-23
  • 打赏
  • 举报
回复
首先声明,我是一个新手。刚学c,更不用提编译原理了。

不过我自己设计算法的时候觉得应该在编译原理里面讲,而且我的设计是用
两个数组分别存操作符和操作数,把操作符数组定义在一个结构里面,结构
的另一个元素就是操作符的优先级。但我设计在扩展的时候遇到了难题,每
增加一个优先级都要重新设计,所以才向大家请教。
ieko 2001-03-23
  • 打赏
  • 举报
回复
对多项式进行扫描,用一个堆栈来存储。目的就是把一个中缀多项式换成后缀多项式。
例如:a*(b+c)-d 就转换成 abc+*d-

如果想了解详细的算法,建议还是去看一下编译原理的书(数据结构的教材应该也有相关的介绍)
zoucaiming 2001-03-22
  • 打赏
  • 举报
回复
请看编译原理的书
brucegong 2001-03-22
  • 打赏
  • 举报
回复



说错了,叫“算符优先文法”。要找到好书看,否则,一个月也看不完那20多页书。



brucegong 2001-03-22
  • 打赏
  • 举报
回复



说错了,叫“算符优先文法”。要找到好书看,否则,一个月也看不完那20多页书。



brucegong 2001-03-22
  • 打赏
  • 举报
回复



叫什么“符号优先级算法”---------编译原理里面很经典的一章。









cnss 2001-03-22
  • 打赏
  • 举报
回复

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧