第1个回答 2013-09-25
表达式末尾加要加#
例如5+6要打5+6#
#include<cstdio>
#include<malloc.h>
#define NULL 0
typedef struct node{
char date;
struct node *next;
}SNode;
SNode *InitStack(){
SNode *top;
top=(SNode *)malloc(sizeof(SNode));
top->next=NULL;
return top;
}
void PushOptr(SNode *top,char x){
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
p->date=x;
p->next=top->next;
top->next=p;
}
char PopOptr(SNode *top){
SNode *p;
char x;
if(top==NULL)
return NULL;
p=top->next;
x=p->date;
top->next=p->next;
free(p);
return x;
}
void PushOpnd(SNode *top,char x){
SNode *p;
p=(SNode *)malloc(sizeof(SNode));
p->date=x;
p->next=top->next;
top->next=p;
}
char PopOpnd(SNode *top){
SNode *p;
char x;
if(top==NULL)
return NULL;
p=top->next;
x=p->date;
top->next=p->next;
free(p);
return x;
}
char GetTop(SNode *top){
return (top->next)->date;
}
int In(char c){
int n;
switch(c){
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '#':n=1;break;
default:n=0;break;
}
return n;
}
char Precede(char x,char y){
int i,j;
int form[7][7]={{1,1,-1,-1,-1,1,1},{1,1,-1,-1,-1,1,1},{1,1,1,1,-1,1,1},{1,1,1,1,-1,1,1},{-1,-1,-1,-1,-1,0,2},{1,1,1,1,2,1,1},{-1,-1,-1,-1,-1,2,0}};
switch(x){
case '+':i=0;break;
case '-':i=1;break;
case '*':i=2;break;
case '/':i=3;break;
case '(':i=4;break;
case ')':i=5;break;
case '#':i=6;break;
}
switch(y){
case '+':j=0;break;
case '-':j=1;break;
case '*':j=2;break;
case '/':j=3;break;
case '(':j=4;break;
case ')':j=5;break;
case '#':j=6;break;
}
if(form[i][j]==1)
return '>';
else
if(form[i][j]==-1)
return '<';
else
return '=';
}
int Operate(char x,char z,char y){
int a=x-'0',b=y-'0';
switch(z){
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
}
}
char Eval_Exp(){
char a,b,c,r,f,z;
int result;
SNode *top[2];
top[0]=InitStack();
PushOptr(top[0],'#');
top[1]=InitStack();
c=getchar();
while(c!='#'||(GetTop(top[0]))!='#'){
if(!In(c)){
PushOpnd(top[1],c);
c=getchar();
}
else{
r=Precede(GetTop(top[0]),c);
switch(r){
case '<':PushOptr(top[0],c);
c=getchar();
break;
case '=':PopOptr(top[0]);
c=getchar();
break;
case '>':b=PopOptr(top[0]);
a=PopOpnd(top[1]);
z=PopOpnd(top[1]);
result=Operate(z,b,a);
f=result+'0';
PushOpnd(top[1],f);
break;
}
}
}
return f;
}
void main(){
char result;
result=Eval_Exp();
printf("%d\n",result-'0');
}
第2个回答 2013-09-25
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <setjmp.h>
jmp_buf jmpenv;
double opdstack[100];
char optstack[100];
double* sp1 = opdstack;
char* sp2 = optstack;
#define OPD sp1
#define OPT sp2
#define PUSH( s, n ) (*s++ = n)
#define POP( s ) (*--s)
#define TOP( s ) (*(s-1))
#define V( s ) ((void*)(s))
#define EMPTY( s ) ( V(s) == V(sp1) ? V(s) == V(opdstack) : V(s) == V(optstack) )
#define SIZE( s ) ( (double*)s == sp1 ? (double*)s - opdstack : (char*)s - optstack )
int getpred( char c )
{
char* pred[] = { "0123456789","+-", "*/", "()" };
int i = 0;
for ( i = 0; i < 3; ++i )
if ( strchr( pred[i], c ) != NULL )
break;
return i;
}
void initerror()
{
int r;
switch( r = setjmp( jmpenv ) )
{
case 1: puts( "invalid operator combination." ); break;
case 2: puts( "invalid character." ); break;
case 3: puts( "expression is not valid." ); break;
}
if ( r ) exit( 0 );
}
void docal()
{
double a, b;
char c;
b = POP( OPD );a = POP( OPD );c = POP( OPT );
switch( c )
{
case '+': a += b; break;
case '-': a -= b; break;
case '*': a *= b; break;
case '/': a /= b; break;
default: longjmp( jmpenv, 1 );
}
PUSH( OPD, a );
}
double eval( char* expr )
{
char* fwd = expr;
int n;
double num;
while( *fwd ) {
switch( getpred( *fwd ) )
{
case 0:
sscanf( fwd, "%lf%n", &num, &n );
fwd += n;
PUSH( OPD, num );
if ( *fwd == '(' )
longjmp( jmpenv, 1 );
break;
case 1: case 2:
while( TOP( OPT ) != '(' && SIZE( OPD ) > 1 && getpred( *fwd ) <= getpred( TOP( OPT ) ) )
docal();
PUSH( OPT, *fwd );
++fwd;
break;
case 3:
if ( *fwd == '(' ) {
PUSH( OPT, *fwd );
} else {
while( TOP(OPT) != '(' )
docal();
POP( OPT );
}
++fwd;
break;
default:
longjmp( jmpenv, 2 );
exit( 0 );
break;
}
}
while ( !EMPTY( OPT ) )
docal();
num = POP( OPD );
if( !( EMPTY( OPT ) && EMPTY( OPD ) ) )
longjmp( jmpenv, 3 );
return num;
}
int main()
{
initerror();
char a[100] = "2*12+((1+2)/3)*(3-4/4)";
printf( "example:\n%s=%g\n\n", a, eval( a ) );
printf( "please input a valid expr:\n" );
gets( a );
printf( "%s=%g\n", a, eval( a ) );
}本回答被网友采纳