数据结构之简单表达式计算器(读入中缀转后缀,通过后缀计算结果)

# include <stdio.h>
# include <stdlib.h>
# include <ctype.h>
# include <string.h>

typedef struct Node					//中缀转后缀表达式使用的结点
{
	char str;
	struct Node * pNext;
}NODE,* PNODE;

typedef struct iNode				//后缀表达式使用的结点
{
	double val;
	struct iNode * pNext;
}INODE,* PINODE;

typedef struct stack				//中缀转后缀表达式使用的栈
{
	PNODE pBase;
	PNODE pTop;
}STACK,* PSTACK;

typedef struct istack				//后缀表达式使用的栈
{
	PINODE pBase;
	PINODE pTop;
}ISTACK,* PISTACK;

//以下为中缀转后缀表达式使用的函数
void init(PSTACK);
void push(PSTACK,char);
bool pop(PSTACK,char *);
bool empty(PSTACK);
void traverse(PSTACK);

//以下为后缀表达式使用的函数
void initi(PSTACK);
void pushi(PSTACK,double);
bool popi(PSTACK,double *);
bool emptyi(PSTACK);
void traversei(PSTACK);

char * midtolast();		//读入中缀表达式转后缀表达式使用的函数
void result(char*);		//用后缀表达式计算结果

int main()
{		
	char * pstr;
	pstr= midtolast();
	strcat(pstr,"#");
	result(pstr);
	return 0;
}

void init(PSTACK pStack)
{
	pStack->pBase=
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个可以计算加减乘除根号指数运算的中缀表达式计算器,并且支持可重复计算: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define MAX_SIZE 100 void clear_input_buffer() { int c; while ((c = getchar()) != '\n' && c != EOF); } double calculate(double num1, double num2, char operator) { switch (operator) { case '+': return num1 + num2; case '-': return num1 - num2; case '*': return num1 * num2; case '/': if (num2 == 0) { printf("Error: divide by zero\n"); return 0; } else { return num1 / num2; } case '^': return pow(num1, num2); case 'r': return pow(num2, 1.0/num1); default: printf("Invalid operator\n"); return 0; } } int main() { double stack[MAX_SIZE]; char operator_stack[MAX_SIZE]; int top = -1, operator_top = -1; double num, result; char operator, input[MAX_SIZE]; while (1) { printf("Enter an expression (or 'q' to quit): "); fgets(input, MAX_SIZE, stdin); if (input[0] == 'q') { break; } char *token = strtok(input, " "); while (token) { if (sscanf(token, "%lf", &num) == 1) { stack[++top] = num; } else { operator = token[0]; if (operator == '(') { operator_stack[++operator_top] = operator; } else if (operator == ')') { while (operator_top > -1 && operator_stack[operator_top] != '(') { double num2 = stack[top--]; double num1 = stack[top--]; char op = operator_stack[operator_top--]; stack[++top] = calculate(num1, num2, op); } operator_top--; } else if (operator == '+' || operator == '-') { while (operator_top > -1 && operator_stack[operator_top] != '(') { double num2 = stack[top--]; double num1 = stack[top--]; char op = operator_stack[operator_top--]; stack[++top] = calculate(num1, num2, op); } operator_stack[++operator_top] = operator; } else if (operator == '*' || operator == '/') { while (operator_top > -1 && operator_stack[operator_top] != '(' && operator_stack[operator_top] != '+' && operator_stack[operator_top] != '-') { double num2 = stack[top--]; double num1 = stack[top--]; char op = operator_stack[operator_top--]; stack[++top] = calculate(num1, num2, op); } operator_stack[++operator_top] = operator; } else if (operator == '^' || operator == 'r') { while (operator_top > -1 && operator_stack[operator_top] != '(' && operator_stack[operator_top] != '+' && operator_stack[operator_top] != '-' && operator_stack[operator_top] != '*' && operator_stack[operator_top] != '/') { double num2 = stack[top--]; double num1 = stack[top--]; char op = operator_stack[operator_top--]; stack[++top] = calculate(num1, num2, op); } operator_stack[++operator_top] = operator; } else { printf("Invalid input\n"); } } token = strtok(NULL, " "); } while (operator_top > -1) { double num2 = stack[top--]; double num1 = stack[top--]; char op = operator_stack[operator_top--]; stack[++top] = calculate(num1, num2, op); } result = stack[top]; printf("Result: %.2lf\n", result); clear_input_buffer(); } return 0; } ``` 这个计算器使用了两个栈,一个用来存储数字,一个用来存储操作符。在读入一个表达式之后,先将其化为后缀表达式,然后再计算出结果。可以输入多个表达式,直到用户输入 "q" 退出程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值