回 帖 发 新 帖 刷新版面

主题:[原创]本人辛苦写了3个小时的四则运算计算器.

由于要的只是它堆栈的精髓,浮华的界面就免了.做的好看不中用也白搭.希望各路高手多多指教.
   对编程有兴趣的朋友可以加我qq314624708,或者发email给我loy20002000@gmail.com
#include<stdio.h>
#include"STACK.C"



Data_Type Getresult(char s[])
{    Data_Type result;/**/
    stack number,sign;/*首先要声明2个堆栈结构体*/
    int count=0,noend=1;
    Data_Type temp_number=0,sum=0,a,b,Ade;

    Init_stack(&number);/*然后对堆栈进行初始化*/
    Init_stack(&sign);

    Push(&sign,Change('='));/*首先将 = 压入符号堆栈,不然的话操作起来回很麻烦,相当与监视哨*/
    #if 0
    {    Data_Type s;
        s=Get_top(&sign);
        printf("操作入栈是%lf\n",s);
    }
    #else
    #endif
    
    while((s[count]!='=')||(Get_top(&sign)!='='))/*当字符数组的最后一个元素和堆栈栈底是=的时候退出循环*/
    {    if(s[count]>='0'&&s[count]<='9')/*首先要将char的字符转换成数字,最麻烦的了*/
        {    
            
            temp_number=Change(s[count++]-'0');
            sum=sum*10+temp_number;
            
            
            #if 0 
            {    printf("sum is %lf\n",sum);
                getch();    
                
            }
            #else
            #endif
            
                
        }
        else
        {    if(noend)/*为了避免在s[count]=='='以后的循环中仍然将num=0压入堆栈,这个很必要,一开始就是没这个东东,让我调试了n次*/
            {    Push(&number,sum);/* ff*/
                sum=0;
            }
            if(s[count]=='=')
                noend=0;
            
                            #if 0
                            {    printf("数字堆栈是 %lf\n",Get_top(&number));
                                getch();    
                            }
                            #else
                            #endif
            
                            #if 0
                            {    printf("Procedo 是 %c\n",Procedo(Get_top(&sign),Change(s[count++])));
                            getch();    
                            }
                            #else
                            #endif


            switch(Procedo(Get_top(&sign),Change(s[count])))/*用来判断运算符号的优先级别*/
            {    
                
                
            
                case '<':
                {    Push(&sign,s[count++]);break;/*如果后面的符号优先级别高就入栈*/
                    
                    
                }
                case '=':/*以下如果优先级别等于或者大于的话就进行计算而且将结果入数字栈*/
                {    
                    b=Pull(&number);
                    a=Pull(&number);
                    Ade=Pull(&sign);
                    result=Operate(a,Ade,b);
                    Push(&number,result);
                    if(s[count]!='=')
                        Push(&sign,s[count]);
                    if(s[count]!='=')
                        count++;
        
                    break;    
                }
                case '>':
                {    
                    b=Pull(&number);
                    a=Pull(&number);
                    Ade=Pull(&sign);
                    result=Operate(a,Ade,b);
                    Push(&number,result);
                    if(s[count]!='=')
                    {
                        Push(&sign,s[count]);
                        
                    }
                    if(s[count]!='=')
                        count++;
        
                    break;
                }
                defalt:printf("procedo 没有被处理\n");
                
                
            }
                
            
                
            
        }
            
    }
            

            
        





    return Get_top(&number); /*返回运算的结果*/
}


main()
{    char strings[1000];
    Data_Type result;
    char loop='y';

        
    #if 0        
    {    char c='#';
        Data_Type temp;
        temp=Change(c);
        printf("转换结果是%lf\n",temp);
        getch();
    }
    #else
    #endif    

    while(loop=='y'||loop=='Y')
    {    printf("请输入一个四则混合运算表达式...\n");
        printf("例如下面的格式:\n4+2*89=(注意:不要使用小括号)\n");
        gets(strings);
        result=Getresult(strings);
        printf("最终结果是%lf\n",result);
        printf("是否还要继续? [y]es OR [n]o\n");
        scanf("%c",&loop);
        fflush(stdin);
    }
}
    

标准函数部分
#include<stdio.h>
#define Data_Type double
#define Init_Space 100
#define More_Space 10
#define OK 1
#define D lf
#define DEBUG 0

typedef struct 
{    Data_Type *base;
    Data_Type *top;
    long length;
}stack; 

int Init_stack(stack *start)
{    

    start->base=(Data_Type*)malloc(sizeof(Data_Type)*Init_Space);
    if(!(start->base))
    {    printf("分配新的空间时内存不足!");
        getch();
        exit(0);
    }
    start->top=start->base;
    start->length=Init_Space;
    return OK;    
    
}

int Push(stack *p,Data_Type e)
{    Data_Type temp;
    if((p->top-p->base)>=p->length)
    {    p->base=(Data_Type*)realloc(p->base,(p->length+More_Space)*sizeof(Data_Type));
        if(!(p->base))
        {    printf("分配新的空间时内存不足!");
            getch();
            exit(0);
        }
        else
        {    
            p->top=(p->top)+(p->length);
            p->length=p->length+More_Space;
        }
    }
    
        
        *(p->top)=e;
        temp=*(p->top++);
        #if DEBUG     
        {    printf("进栈数据是%lf\n",temp);/*跟踪入栈的情况*/
        }
        #else
            
        #endif
        return OK;
    
}

Data_Type Pull(stack *p)
{    Data_Type e;
    if(p->top==p->base)
    {    printf("出栈错误!");
        getch();
        exit(0);
    }
    else
    {    
        #if 0
            printf("栈顶元素是%lf\n",*(--p->top));/*跟踪出栈的情况*/
        #endif
        return *(--p->top);
    }
    
    
}

Data_Type Get_top(stack *p)
{    Data_Type *temp;
    temp=p->top;
    return *(--temp);
}


int Stack_print(stack *p)
{    Data_Type *temp;
    temp=p->top;
    while(temp!=p->base)
    {    
        #if DEBUG     
        {    printf("堆栈的数据是%lf\n",*(--temp));/*查看堆栈的情况*/
            getch();
        }
        #else
            
        #endif
        
    }
    printf("显示完毕!\n");
}


Data_Type Change(long from)
{    Data_Type temp;
    temp=(Data_Type)from;
    return temp;


Data_Type Howbig(Data_Type number)
{    if((long)number=='=')
        return 0;
    else if((long)number=='+'||(long)number=='-')
        return 1;
    else 
        return 2;
}

char Procedo(Data_Type a,Data_Type b)
{    a=Howbig(a);
    b=Howbig(b);
    if(a>b)
        return '>';
    else if(a==b)
        return '=';
    else 
        return '<';
}

Data_Type Operate(Data_Type one,Data_Type sign,Data_Type two)
{    Data_Type number;
        if((long)sign=='+')
            number=one+two;
        else if((long)sign=='-')
            number=one-two;
        else if((long)sign=='*')
            number=one*two;
        else
            number=one/two;
    return number;
}






    
       

回复列表 (共2个回复)

沙发

呵呵
真的辛苦lz了呢
今天早上我写了个带括号的计算思路
用普通的双链表存储
反复迭代寻找运算符(先括号后四则),计算后用计算结果替代已经计算完成的部分
逐步缩短算式
lz要不要构思一下我说的算法模型?

板凳

还有一点
lz的stack.c的代码是?

我来回复

您尚未登录,请登录后再回复。点此登录或注册