FORWARD!  
  1 计算器0.1
  2 
  3 
  4 #include <stdio.h>
  5 #include <ctype.h>
  6 #include <stdlib.h>
  7 
  8 #define STACK_INIT_SIZE 20
  9 #define STACKINCREMENT 10
 10 #define MAXBUFFER 10
 11 
 12 typedef double SElemType;
 13 typedef struct sqlStack {
 14     SElemType *base;
 15     SElemType *top;
 16     int stackSize;
 17 } sqlStack;
 18 
 19 void initStack(sqlStack *S) {
 20     S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
 21     if(!S->base) {
 22         printf("Memory allocate failed!\n");
 23         exit(EXIT_FAILURE);
 24     }
 25     S->top = S->base;
 26     S->stackSize = STACK_INIT_SIZE;
 27 
 28     printf("Stack has been initialized successfully!\n");
 29 }
 30 
 31 /*取到栈顶的元素*/
 32 void getTop(sqlStack S, SElemType *data) {
 33     if(S.top == S.base) {
 34         printf("Stack is NULL!\n");
 35     }
 36     *data = *(S.top - 1);
 37 }
 38 
 39 /*将数据元素压栈*/
 40 void Push(sqlStack *S, SElemType data) {
 41     if(S->top - S->base > S->stackSize) {
 42         S->base = (SElemType *)realloc(S->base, (S->stackSize + STACKINCREMENT) * sizeof(SElemType));
 43         if(!S->base) {
 44             printf("Memory allocate failed!\n");
 45             exit(EXIT_FAILURE);
 46         }
 47         S->top = S->base + S->stackSize;
 48         S->stackSize += STACKINCREMENT;
 49     }
 50     /*数据元素入栈*/
 51     *S->top++ = data;
 52 }
 53 
 54 /*元素出栈*/
 55 void Pop(sqlStack *S, SElemType *data) {
 56     if(S->base == S->top) {
 57         printf("Error: Stack is NULL!\n");
 58     }
 59     *data = *--S->top;
 60 }
 61 
 62 /*判断栈是否为空*/
 63 int stackEmpty(sqlStack S) {
 64     if(S.base == S.top)
 65         return 1;
 66     else
 67         return -1;
 68 }
 69 
 70 /*获取栈的长度*/
 71 int stackLength(sqlStack S) {
 72     return S.top - S.base;
 73 }
 74 
 75 int main() {
 76     char opdata;
 77     int i = 0;
 78     double operand1, operand2;
 79     char ops[MAXBUFFER];
 80 
 81     sqlStack S;
 82     initStack(&S);
 83 // sqlStack OPERAND;
 84 // sqlStack OPERATOR;
 85 // initStack(&OPERAND);
 86 // initStack(&OPERATOR);
 87 
 88     /*如果输入的不是换行符就表示继续接收输入*/
 89     printf("Please input the string you want to evaluate: ");
 90     while((opdata = getchar()) != '\n') {
 91         /*当输入的数据为操作数时,就直接打印在屏幕上*/
 92         /*过滤掉所有的数字*/
 93         while(isdigit(opdata) || '.' == opdata) {
 94             ops[i++] = opdata;
 95 
 96             if(i >= MAXBUFFER) {
 97                 printf("Error : The operand is too big!\n");
 98                 return -1;
 99             }
100             opdata = getchar();
101             if(' ' == opdata) {
102                 ops[i] = '\0';
103                 operand1 = atof(ops);
104                 /*如果是数字就入栈,是操作符就出栈*/
105                 Push(&S, operand1);
106                 i = 0;
107                 break;/*跳出循环,输入的数字已经结束*/
108             }
109         }
110 
111         /*当遇到操作符的时候的处理*/
112         switch(opdata) {
113         case '+':
114             Pop(&S, &operand1);
115             Pop(&S, &operand2);
116             Push(&S, operand2 + operand1);
117             break;
118         case '-':
119             Pop(&S, &operand1);
120             Pop(&S, &operand2);
121             Push(&S, operand2 - operand1);
122             break;
123         case '*':
124             Pop(&S, &operand1);
125             Pop(&S, &operand2);
126             Push(&S, operand2 * operand1);
127             break;
128         case '/':
129             Pop(&S, &operand1);
130             Pop(&S, &operand2);
131             if(0 == operand1) {
132                 printf("Error : operand1 cannot be zero!\n");
133                 exit(EXIT_FAILURE);
134             } else {
135                 Push(&S, operand2 / operand1);
136                 break;
137             }
138         default:
139             continue;
140         }
141     }
142     Pop(&S, &operand1);
143     printf("The result is : %lf\n", operand1);
144 
145     return 0;
146 }

 

posted on 2018-11-18 19:09  psland  阅读(151)  评论(0编辑  收藏  举报