当前位置:编程学习 > C/C++ >>

用C语言编写一个简单的计算器1

写一个能进行四则运算的计算器。说下思路和代码。

追问:来自手机问问能编个完整点吗
答案:// 个人感觉上面的回答都不够完善,仅能实现两个操作数间的运算。

// 文件1:ssadt.h

/* ----- 栈的抽象数据类型存放在ssadt中 ----- */
#include<stdio.h>
#include<malloc.h>
#include<conio.h>

#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OK 1
#define EQUAL 1
#define OVERFLOW -1

/* ----- 栈的操作对象及关系 ----- */
typedef int Status;


/* ----- 栈的顺序存储表示 ----- */
#define  STACK_INIT_SIZE  100
#define  STACKINCREMENT   10
typedef struct {
  SElemType *base;
  SElemType *top;
  int stacksize;
} SqStack;

/* ----- 栈的基本操作C语言源码 ----- */
Status InitStack(SqStack *S) {
 
  S->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
  if(!S->base) exit(OVERFLOW);
  S->top=S->base;
  S->stacksize=STACK_INIT_SIZE;
  return OK;
}

Status DestroyStack(SqStack *S) {
  free(S->base);
  free(S);
}

Status ClearStack(SqStack *S) {
  S->top=S->base;
}

Status StackEmpty(SqStack S) {
  if(S.top==S.base) return TRUE;
  else return FALSE;
}

int StackLength(SqStack S) {
 return S.top-S.base;
}

Status GetTop(SqStack S, SElemType *e) {
  if(S.top==S.base) return ERROR;
  *e=*(S.top-1);
  return OK;
}

Status Push(SqStack *S, SElemType e) {
  if(S->top-S->base>=S->stacksize) {
    S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));
    if(!S->base) exit(OVERFLOW);
    S->top=S->base+S->stacksize;
    S->stacksize+=STACKINCREMENT;
  }
  *S->top++=e;
  return OK;
}

Status Pop(SqStack *S, SElemType *e) {
  if(S->top==S->base) return ERROR;
  *e=*--S->top;
  return OK;
}

Status StackTraverse(SqStack S,Status (*visit)(SElemType)) {
  while(S.top!=S.base) visit(*--S.top);
}

//-------------------------------------------------------------------------------------------

// 文件2:ssadt2.h

typedef struct {
  ElemType *base;
  ElemType *top;
  int stacksize;
} SqStack2;

/* ----- 栈的基本操作C语言源码 ----- */
Status InitStack2(SqStack2 *S) {
 
  S->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
  if(!S->base) exit(OVERFLOW);
  S->top=S->base;
  S->stacksize=STACK_INIT_SIZE;
  return OK;
}

Status DestroyStack2(SqStack2 *S) {
  free(S->base);
  free(S);
}

Status ClearStack2(SqStack2 *S) {
  S->top=S->base;
}

Status Stack2Empty(SqStack2 S) {
  if(S.top==S.base) return TRUE;
  else return FALSE;
}

int Stack2Length(SqStack2 S) {
 return S.top-S.base;
}

Status GetTop2(SqStack2 S, ElemType *e) {
  if(S.top==S.base) return ERROR;
  *e=*(S.top-1);
  return OK;
}

Status Push2(SqStack2 *S, ElemType e) {
  if(S->top-S->base>=S->stacksize) {
    S->base=(ElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType));
    if(!S->base) exit(OVERFLOW);
    S->top=S->base+S->stacksize;
    S->stacksize+=STACKINCREMENT;
  }
  *S->top++=e;
  return OK;
}

Status Pop2(SqStack2 *S, ElemType *e) {
  if(S->top==S->base) return ERROR;
  *e=*--S->top;
  return OK;
}

Status Stack2Traverse(SqStack2 S,Status (*visit)(ElemType)) {
  while(S.top!=S.base) visit(*--S.top);
}

//-------------------------------------------------------------------------------------------

// 文件3:ExvalExpr.c

/*----课本P54 算法3.4 算术表达式求值ExvalExpression存放在ExvalExpr.c中-----*/
#include<time.h>
#include<string.h>
typedef char SElemType;
typedef int  ElemType;
#include"ssadt.h"
#include"ssadt2.h"

SqStack OPTR;
SqStack2 OPND;
char OP[7]={'+','-','*','/','(',')','#'};

Status In(char ch) {
  int i;
  for(i=0;i<7;i++) if(ch==OP[i]) return TRUE;
  return ERROR;
}

char Precede(char c1,char c2) {
  int i=0,j=0,k;
  static char array[49]={'>', '>', '<', '<', '<', '>', '>',
                         '>', '>', '<', '<', '<', '>', '>',
                         '>', '>', '>', '>', '<', '>', '>',
                         '>', '>', '>', '>', '<', '>', '>',
                         '<', '<', '<', '<', '<', '=', '!',
                         '>', '>', '>', '>', '!', '>', '>',
                         '<', '<', '<', '<', '<', '!', '='};
  for(k=0;k<7;k++) {
    if(c1==OP[k])  i=k;
    if(c2==OP[k])  j=k;
  }
  return (array[7*i+j]);
}

int Operate(int a,char op,int b) {
  switch(op) {
    case '+' : return(a+b); break;
    case '-' : return(a-b); break;
    case '*' : return(a*b); break;
    case '/' : return(a/b); break;
  }
  return ERROR;
}
 
int num(int n) {
  char p[10];
  itoa(n,p,10);
  return(strlen(p));
}

int EvalExpr(char *expr) {
  char *ptr=expr;
  int a,b,m,n;
  char theta,c,top,x;
  InitStack(&OPTR);    Push(&OPTR,'#');
  InitStack2(&OPND);   c = *ptr++;
  GetTop(OPTR,&top);
  while(c!='#'||top!='#') {
    if(!In(c)) {
      --ptr;
      m=atoi(ptr);  /* 取字符串前面的数字段 */
      n=num(m);
      Push2(&OPND,m);
      ptr+=n;
      c=*ptr++;
    } /*if*/
    else {
      GetTop(OPTR,&top);
      switch(Precede(top,c)) {
        case'<':Push(&OPTR,c);  c=*ptr++;  break;
        case'=':Pop(&OPTR,&x);  c=*ptr++;  break;
        case'>':Pop(&OPTR,&theta);
                Pop2(&OPND,&b);  Pop2(&OPND,&a);
                Push2(&OPND,Operate(a,theta,b));
                break;
&nbs

上一个:C语言中的位运算有什么优点?
下一个:『NOIP』推荐本C语言的noip书籍

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,