| 
 | 
 
 
 楼主 |
发表于 2004-12-27 00:09:14
|
显示全部楼层
 
 
 
< >这是我编写的程序。请帮我看看有没有什么错误,谢谢</P>< >#define N 50 
#define OK 1 
#define ERROR 0 
#include <ctype.h> 
#include <string.h> 
#include <iostream.h> 
typedef struct{ 
    int top; 
    double array[N]; 
}NumStack; 
typedef struct{ 
    int top; 
    char array[N];</P>< >}OpStack;</P><P>int Cint(char mychar){</P><P>    return (mychar-48);</P><P>}</P><P> Push(NumStack &numstack,double num){</P><P>    if(numstack.top<N)</P><P>{numstack.top++;</P><P>     numstack.array[numstack.top-1]=num; 
     return OK;</P><P>}</P><P>else return ERROR;</P><P>} 
 Pop(NumStack &numstack,double &num){</P><P>    if(numstack.top>0){</P><P>num=numstack.array[numstack.top-1];</P><P>    numstack.top--;</P><P>    return OK;</P><P>}</P><P>else return ERROR;</P><P>} 
 Push(OpStack &opstack,char &op){</P><P>    if(opstack.top<N){</P><P>opstack.top++;</P><P>    opstack.array[opstack.top-1]=op; 
    return OK;</P><P>}</P><P>else return ERROR;</P><P>} 
 Poph(OpStack &opstack,char &op){</P><P>if(opstack.top>0){ </P><P>op=opstack.array[opstack.top-1];</P><P>    opstack.top--;</P><P>return OK;</P><P>}</P><P>else return ERROR;</P><P>} 
double Calc(double a,double b,char c){</P><P>    double result;</P><P>    switch(c){</P><P>        case '+':result=a+b;break;</P><P>        case '-':result=a-b;break;</P><P>        case '*':result=a*b;break;</P><P>        case '/':result=a/b;break;</P><P>    }</P><P>    return result;</P><P>} 
char Priority(char y,char x){</P><P>        char priority='<';</P><P>        switch(x){</P><P>            case '+':</P><P>            case '-':if(y=='(' || y=='#')priority='>';break;</P><P>            case '*':</P><P>            case '/':if(y=='(' || y=='#'|| y=='+' || y=='-')priority='>';break;</P><P>            case '(':priority='>';break;</P><P>            case ')':if(y=='(')priority='=';break;</P><P>            case '#':if(y=='#')priority='=';break;</P><P>            default:priority='E';</P><P>        }</P><P>        return priority;</P><P>} 
void Process(NumStack &numstack,OpStack &opstack,char x){</P><P>    double a,b;char c;</P><P>    static double tempnum=0.00000000;static int len=10;static int dot=0,flags=0;</P><P>    if(isdigit(x) || x=='.'){</P><P>        if(x=='.')dot=1;</P><P>        else{</P><P>            if(dot==0)</P><P>                tempnum=tempnum*10+Cint(x);</P><P>            else{</P><P>                tempnum=tempnum+(double)Cint(x)/len;</P><P>                len*=10;</P><P>            }</P><P>        }</P><P>    }</P><P>    else{</P><P>        if(flags==0 && x!='('){Push(numstack,tempnum);tempnum=0.00000000;len=10;dot=0;}</P><P>        switch(Priority(opstack.array[opstack.top-1],x)){</P><P>            case '>' ush(opstack,x);flags=0;break;</P><P>            case '<':</P><P>                    Poph(opstack,&c);</P><P>                    Pop(numstack,&b);</P><P>                    Pop(numstack,&a);</P><P>                    Push(numstack,Calc(a,b,c));flags=1;</P><P>                    Process(numstack,opstack,x);break;</P><P>            case '=' oph(opstack,&c);flags=1;break;</P><P>            default:printf("Wrong Express!");exit(0);</P><P>        }</P><P>    }</P><P>} 
main(){</P><P>    NumStack numstack;OpStack opstack;char *s;int i=0;</P><P>    numstack.top=0;opstack.top=0;</P><P>    Push(&opstack,'#');</P><P>    printf("\nEnter your expression adn end it with #:");scanf("%s",s);</P><P>    for(i=0;i<strlen(s);i++)</P><P>    Process(&numstack,&opstack,s);</P><P>    printf("The result is %f",numstack.array[numstack.top-1]);</P><P>}</P> |   
 
 
 
 |