看了懒猫老师的表达式求值的视频后,自己写了份简易版的C语言版表达式求值,不是最好的算法,以后多多进步,多多改进!https://www.bilibili.com/medialist/play/26340287?from=space&business=space&sort_field=pubtime&spm_id_from=333.999.0.0
#include <stdio.h>
#include <stdlib.h>
#include "biaodashi.cpp"
//主函数
int main(int argc, const char * argv[]) {
Stack OPTR;
Stack_num OPND;
char ch,x,theta;
double a,b;
Initstack(OPTR);
Initstack_num(OPND);
push(OPTR, '#');
// printf("%c",precede('*', '('));
scanf("%c",&ch);
//ch = getchar();
do{
if (isOptr(ch)){
switch (precede(top(OPTR), ch)) {
case '<':
push(OPTR, ch);
scanf("%c",&ch);
break;
case '=':
if(ch == ')' ) x = pop_sym(OPTR);
scanf("%c",&ch);
break;
case '>':
theta = pop_sym(OPTR);
b = pop_num(OPND);
a = pop_num(OPND);
push_num(OPND, operate(a, theta, b));
printf("OPERATE: %.2f\n",top_num(OPND));
break;
}
}
else{
ch = (ch - '0')*1.0;
push_num(OPND, ch);
printf("push is : %.2f\n",top_num(OPND));
scanf("%c",&ch);
}
}while ((ch != '#') || (top(OPTR) != '#'));
printf("the result: %.2f\n",top_num(OPND));
Destroy(OPTR);
Destroy(OPND);
return 0;
}
.c文件
#include "biaodashi.hpp"
#include <stdlib.h>
#include <stdio.h>
//判断是否是数字
bool isOpnd(char ch){
if (ch >= '0' && ch <= '9') {
return true;
}
else return false;
}
//判断是否是字符
bool isOptr(char ch){
int i;
for(i = 0;i < M;i++) {
if (ch == CalPriority[i][0]) return true;
}
return false;
}
void Initstack(Stack &s){
s.elemtype = (char*)malloc(MaxSize*sizeof(char));
s.len = -1;
}
void Initstack_num(Stack_num &s){
s.elemtype = (double*)malloc(MaxSize*sizeof(double));
s.len = -1;
}
char precede(char a,char b){
int i,j;
//寻找角2
for (i = 0; i < N ; i++) {
if (CalPriority[0][i] == b) break;
}
//寻找角1
for (j = 0; j < M ; j++) {
if (CalPriority[j][0] == a) break;
}
return CalPriority[j][i];
}
bool isfull(Stack s){
if(s.len == MaxSize) return true;
else return false;
}
bool isempty(Stack s){
if (s.len == -1) return true;
else return false;
}
bool isfull_num(Stack_num s){
if (s.len == MaxSize) return true;
else return false;
}
bool isempty_num(Stack_num s){
if(s.len == -1) return true;
else return false;
}
char top(Stack s){
if(isempty(s)) return false;
else return s.elemtype[s.len];
}
double top_num(Stack_num s){
if(isempty_num(s)) return false;
else return s.elemtype[s.len];
}
char pop_sym(Stack &s){
if(isempty(s)) return false;
else{
return s.elemtype[s.len--];
}
}
double pop_num(Stack_num &s){
if (isempty_num(s)) return false;
else return s.elemtype[s.len--];
}
bool push_num(Stack_num &s,double ch){
if(isfull_num(s)) return false;
else{
s.elemtype[++s.len] = ch;
return true;
}
}
bool push(Stack &s,char ch){
if(isfull(s)) return false;
else{
s.elemtype[++s.len] = ch;
return true;
}
}
double operate(double a, char theta ,double b){
double result;
switch (theta) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
}
return result;
}
void Destroy(Stack &s){
free(s.elemtype);
}
void Destroy(STACK_num &s){
free(s.elemtype);
}
.h文件
#ifndef biaodashi_hpp
#define biaodashi_hpp
#include <stdio.h>
#include <stdbool.h>
#define M 8
#define N 8
#define MaxSize 20
typedef struct STACK_ch{
char *elemtype;
int len;
}Stack;
typedef struct STACK_num{
double *elemtype;
int len;
}Stack_num;
char CalPriority[M][N]=
{ 0 ,'+','-','*','/','(',')','#',
'+','>','>','<','<','<','>','>',
'-','>','>','<','<','<','>','>',
'*','>','>','>','>','<','>','>',
'/','>','>','>','>','<','>','>',
'(','<','<','<','<','<','=', 0 ,
')','>','>','>','>', 0 ,'>','>',
'#','<','<','<','<','<', 0 ,'='
};
//辅助函数
char precede(char a,char b);
char top(Stack s);
double top_num(Stack_num s);
bool isOpnd(char ch); //判断是否是数字
bool isOptr(char ch); //判断是否是字符
double operate(double a ,char theta ,double b);
//栈函数
void Initstack(Stack &s);
void Initstack_num(Stack_num &s);
bool isfull(Stack s);
bool isempty(Stack s);
bool isfull_num(Stack_num s);
bool isempty_num(Stack_num s);
char pop_sym(Stack &s);
double pop_num(Stack_num &s);
bool push(Stack &s,char ch);
bool push_num(Stack_num &s,double ch);
void Destroy(Stack &s);
void Destroy(STACK_num &s);
#endif /* biaodashi_hpp */
因为我用的Xcode,.h文件为.hpp, .m文件变成了.cpp文件,所以小伙伴们记得改一下,代码理解不难,感谢懒猫老师!