| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 644 人关注过本帖
标题:大家帮忙改一下错
只看楼主 加入收藏
corrine81
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-10-9
收藏
 问题点数:0 回复次数:0 
大家帮忙改一下错
/*-------------------------------------------------
                calculator
Author:Wrf
Date:mm//dd//yy
--------------------------------------------------*/

/*-----------PREPROCESING  DIRECTIVES---------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <malloc.h>
#include <math.h>


/*-----------------PROGRAM VARIABLES-----------------*/

#define  STACK_INIT_SIZE  100
#define  STACKINCREMENT  10
typedef struct  {
    float data; /*存放操作数或者计算结果*/
    char ch; /*存放运算符*/
}SNode;
typedef struct {
SNode *base,*top;
 int stacksize;
}Stack;
 
 char c;
/*--------------------------------------------------------
           MAINLINE  CONTROL
---------------------------------------------------------*/
main()
{float iresult;
char ch;
L1:iresult=EvaluatExpression();
 printf("\nThe iresult is:%d\n");
  printf("Do you want to exit,please input y||n\n");     
        
        
      if(ch=='y'||ch=='Y')
           return;
        else
           { goto L1;}
   
    
    return 0;
}

/*--------------------EvaluatExpression--------------------*/

 EvaluatExpression()
{    Stack OPTR,OPND,S;
     SNode e,a,b;
  char c,x,theta;
 
  
  InitStack( OPTR);  Push(OPTR,'#');
  InitStack( OPND);  c=getchar();
  while(c!='#'|| GetTop(OPTR)!='#')
 {
   if(!In(c)){Push((OPND),c);c=getchar();}
   else
    switch(Precede(GetTop(OPTR),c))
   {case '<':
             Push(OPTR,c);c=getchar();
             break;
    case '=':
            Pop(OPTR,x);c=getchar();
            break;
    case '>':
           Pop(OPTR,theta);
           Pop(OPND,b);Pop(OPND,a);
           Push(OPND,Operate(a,theta,b));
         break;
   }/*switch*/

}/*while*/
return  GetTop(OPND);
}/*EvaluatExpression*/

/*--------------------InitStack--------------------------*/
int InitStack(Stack &S)
{
    S.base=(SNode *)malloc(STACK_INIT_SIZE * sizeof(struct SNode));
    if(S.base==NULL)
    {
        printf("动态分配内存失败!");
        return -1;
    }
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    return 0;

}/*InitStack*/


/*------------------------GetTop---------------------------*/

GetTop(Stack &S,SNode &e)
{
   if(S.top==S.base)return -1;
    e=*(S.top-1);
return 1;
}/*GetTop*/

/*-------------------------Push----------------------------*/

 Push(Stack &S,SNode &e)
{
    if(S.top-S.base>=S.stacksize)
    {
        S.base=(SNode *)realloc(S.base,
(S.stacksize+STACKINCREMENT *sizeof(struct SNode)));
        if(!S.base)exit(OVERFLOW);
        S.top=S.base+S.stacksize;
        S.stacksize+=STACKINCREMENT ;
    }
    *S.top=e;
    S.top++;
    return 1;
}/*Push*/

/*---------------------------Pop----------------------------*/
 

Pop(Stack &S,SNode &e)
{
  if(S.top==S.base)return -1;
  e==*(--S.top);
  return  1;
}/*Pop*/

/*---------------------------IN----------------------------*/

In(c)
{  if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'||c=='=')
        return 0;
    else
        return 1;
}


/*-----------------------------Precede---------------------*/

char Precede(char s,char c)
{{
    switch(s)
    {
        case '+':                 
        case '-':
             if(c=='+'||c=='-')
                 return '>';
             else if(c=='*'||c=='/')
                 return '<';
             else if(c=='(')
                 return '<';
             else if(c==')')
                 return '>';
             else if(c=='c'||c=='s'||c=='l'||c=='t'||c=='o'||c=='!')
                 return '<';
             else
                 return '>';
        case '*':
        case '/':
             if(c=='+'||c=='-')
                 return '>';
             else if(c=='*'||c=='/')
                 return '>';
             else if(c=='(')
                 return '<';
             else if(c==')')
                 return '>';
             else if(c=='c'||c=='s'||c=='l'||c=='t'||c=='o'||c=='!')
                 return '<';
             else
                 return '>';
        case '(':
             if(c=='+'||c=='-')
                 return '<';
             else if(c=='*'||c=='/')
                 return '<';
             else if(c=='(')
                 return '<';
             else if(c==')')
                 return '=';
             else if(c=='c'||c=='s'||c=='l'||c=='t'||c=='o'||c=='!')
                 return '<';
             else
                 return 'E';
        case ')':
             if(c=='+'||c=='-')
                 return '>';
             else if(c=='*'||c=='/')
                 return '>';
             else if(c=='(')
                 return 'E';
             else if(c==')')
                 return '>';
             else if(c=='c'||c=='s'||c=='l'||c=='t'||c=='o'||c=='!')
                 return '>';
             else
                 return '>';
        case '#':
             if(c=='+'||c=='-')
                 return '<';
             else if(c=='*'||c=='/')
                 return '<';
             else if(c=='(')
                 return '<';
             else if(c==')')
                 return 'E';
             else if(c=='c'||c=='s'||c=='l'||c=='t'||c=='o'||c=='!')
                 return '<';
             else
                 return '=';
        
        default:
             break;
    }
    return ;   
}


/*------------------------- Operate--------------------------*/

 float Operate(float x, char opr, float y)
{
    float result;
    
    switch (opr)
    {
        case '+':
             result = x + y;
             break;
        case '-':
             result = x - y;
             break;
        case '*':
             result = x * y;
             break;
        case '/':
             if (y == 0)
             {
                printf("Divided by zero!\n");
                return 0;
             }
             else
             {
                 result = x / y;
                 break;
             }
        
       default:
             printf("Bad Input.\n");
             return 0;
    }
    return result;
} }   


程序还未写完
2007-12-05 13:50
快速回复:大家帮忙改一下错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029004 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved