详细代码如下:
cal.h
#define _CRT_SECURE_NO_WARNINGS 1#ifndef __CAL_H__#define __CAL_H__#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAXSIZE 50typedef int SElemType;typedef struct{ SElemType data[MAXSIZE]; int top;}SqStack;int To_PostFix(char *infix, char *postfix);int Calculator(char *postfix, int *result);#endif // !__CAL_H__
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
cal.c
#define _CRT_SECURE_NO_WARNINGS 1#include"cal.h"/*******************栈的基本操作********************************/int init_stack(SqStack *s){ s->top = -1; return 1;}int clear_stack(SqStack *s){ s->top = -1; return 1;}int stack_empty(SqStack s){ if (s.top == -1) return 1; else return 0;}int stack_length(SqStack *s){ return s->top + 1;}int push(SqStack *s, SElemType e){ if (s->top == MAXSIZE - 1) return 0; s->top++; s->data[s->top] = e; return 1;}int pop(SqStack *s, SElemType *e){ if (s->top == -1) return 0; *e = s->data[s->top]; s->top--; return 1;}/*******************中序表达式转换为后续表达式********************************/int To_PostFix(char *infix, char *postfix){ SqStack s; int e = 0; int i = 0, j = 0; int flag = 0; if (init_stack(&s) != 1) return 0; while (infix[i] != '\0') { while (infix[i] >= '0' && infix[i] <= '9') { if (flag) { flag = 0; postfix[j++] = '-'; } postfix[j++] = infix[i]; i++; if (infix[i]<'0' || infix[i]>'9') postfix[j++] = ' '; } if (infix[i] == ')' ) { pop(&s, &e); while (e != '(' ) { postfix[j++] = e; postfix[j++] = ' '; pop(&s, &e); } } else if (infix[i] == '+' || infix[i] == '-') { if (infix[i] == '-' && (i == 0 || (i != 0 && (infix[i - 1]<'0' || infix[i - 1]>'9')))) flag = 1; else if (stack_empty(s)) push(&s, infix[i]); else { do { pop(&s, &e); if (e == '(' ) push(&s, e); else { postfix[j++] = e; postfix[j++] = ' '; } } while (!stack_empty(s) && e != '(' ); push(&s, infix[i]); } } else if (infix[i] == '*' || infix[i] == '/' || infix[i] == '(' ) push(&s, infix[i]); else if (infix[i] == '\0') break; else return 0; i++; } while (!stack_empty(s)) { pop(&s, &e); postfix[j++] = e; postfix[j++] = ' '; } clear_stack(&s); return 1;}/*******************根据后续表达式计算结果********************************/int Calculator(char *postfix, int *result){ SqStack s; char *op; char *buf = postfix; char *saveptr = NULL; int d, e, f; if (init_stack(&s) != 1) return 0; while ((op = strtok(buf, " ")) != NULL) { buf = NULL; switch (op[0]) { case '+': pop(&s, &d); pop(&s, &e); f = d + e; push(&s, f); break; case '-': if (op[1] >= '0' && op[1] <= '9') { d = atoi(op); push(&s, d); break; } pop(&s, &d); pop(&s, &e); f = e - d; push(&s, f); break; case '*': pop(&s, &d); pop(&s, &e); f = e*d; push(&s, f); break; case '/': pop(&s, &d); pop(&s, &e); f = e / d; push(&s, f); break; default: d = atoi(op); push(&s, d); break; } } pop(&s, result); clear_stack(&s); return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
test.c
void Test(){ char infix[MAXSIZE] = { 0 }; char postfix[MAXSIZE] = { 0 }; int result = 0; printf("请输入一个中缀表达式:\n"); scanf("%s", infix); fflush(stdin); To_PostFix(infix, postfix); printf("转换得到的后缀表达式是:\n"); printf("%s\n", postfix); Calculator(postfix, &result); printf("最后得到的结果:\n"); printf("%d\n", result);}int main(){ Test(); system("pause"); return 0;}