c语言利用栈实现+ - * / 运算,没有括号的表达式,以#结尾。编写完整代码
时间: 2024-12-13 21:26:13 浏览: 22
根据提供的引用内容,以下是C语言利用栈实现四则运算的完整代码,该代码适用于没有括号的表达式,以#结尾。
```
#include <stdio.h>
#include <stdlib.h>
#include "Evaluation.h"
#include "Evaluation.c"
int main() {
InitOperand(&operands);
InitOperator(&operators);
while(1) {
char ch;
ExpCalculation(&operands,&operators);
EmptyRand(&operands);
EmptyRator(&operators);
ch = getchar();
if(ch == '#') //以#结尾
break;
}
return 0;
}
//Evaluation.h
#ifndef EVALUATION_H_INCLUDED
#define EVALUATION_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20
//操作数
typedef struct Operand {
int Data[MAXSIZE];
int top;
}Rand;
//运算符
typedef struct Operator {
char Data[MAXSIZE];
int top;
}Rator;
//定义栈存放操作数和运算符
Rand operands;
Rator operators;
//初始化栈
void InitOperand(Rand *ra);
void InitOperator(Rator *op);
//判栈空
int EmptyRand(Rand *ra);
int EmptyRator(Rator *op);
//进栈
int PushRand(Rand *ra,int e);
int PushRator(Rator *op,char e);
//出栈
int PopRand(Rand *ra,int *e);
int PopRator(Rator *op,char *e);
//取栈顶元素
int GetTopRand(Rand *ra);
char GetTopRator(Rator *op);
//判断字符是否为运算符
int InOp(char ch);
//判断运算符优先级
int Priority(char s);
//比较运算符优先级
int Precede(char op1,char op2);
//判断符号并运算
int Calculation(int a,int b,char c);
//计算表达式
int ExpCalculation(Rand *ra,Rator *op);
#endif // EVALUATION_H_INCLUDED
//Evaluation.c
#include "Evaluation.h"
void InitOperand(Rand *ra) {
ra->top = -1;
}
void InitOperator(Rator *op) {
op->top = -1;
}
int EmptyRand(Rand *ra) {
if(ra->top == -1)
return 1;
else
return 0;
}
int EmptyRator(Rator *op) {
if(op->top == -1)
return 1;
else
return 0;
}
int PushRand(Rand *ra,int e) {
if(ra->top == MAXSIZE-1)
return 0;
else {
ra->top++;
ra->Data[ra->top] = e;
return 1;
}
}
int PushRator(Rator *op,char e) {
if(op->top == MAXSIZE-1)
return 0;
else {
op->top++;
op->Data[op->top] = e;
return 1;
}
}
int PopRand(Rand *ra,int *e) {
if(ra->top == -1)
return 0;
else {
*e = ra->Data[ra->top];
ra->top--;
return 1;
}
}
int PopRator(Rator *op,char *e) {
if(op->top == -1)
return 0;
else {
*e = op->Data[op->top];
op->top--;
return 1;
}
}
int GetTopRand(Rand *ra) {
if(ra->top == -1)
return 0;
else
return ra->Data[ra->top];
}
char GetTopRator(Rator *op) {
if(op->top == -1)
return 0;
else
return op->Data[op->top];
}
int InOp(char ch) {
if(ch == '+' || ch == '-' || ch == '*' || ch == '/')
return 1;
else
return 0;
}
int Priority(char s) {
switch(s) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
int Precede(char op1,char op2) {
int p1,p2;
p1 = Priority(op1);
p2 = Priority(op2);
if(p1 >= p2)
return 1;
else
return 0;
}
int Calculation(int a,int b,char c) {
switch(c) {
case '+':
return a+b;
case '-':
return a-b;
case '*':
return a*b;
case '/':
return a/b;
default:
return 0;
}
}
int ExpCalculation(Rand *ra,Rator *op) {
char ch;
int num;
int a,b;
int result;
ch = getchar();
while(ch != '#') {
if(ch >= '0' && ch <= '9') {
num = ch - '0';
ch = getchar();
while(ch >= '0' && ch <= '9') {
num = num*10 + ch - '0';
ch = getchar();
}
PushRand(ra,num);
}
else if(InOp(ch)) {
while(!EmptyRator(op) && Precede(GetTopRator(op),ch)) {
PopRand(ra,&b);
PopRand(ra,&a);
PopRator(op,&ch);
result = Calculation(a,b,ch);
PushRand(ra,result);
}
PushRator(op,ch);
ch = getchar();
}
else {
ch = getchar();
}
}
while(!EmptyRator(op)) {
PopRand(ra,&b);
PopRand(ra,&a);
PopRator(op,&ch);
result = Calculation(a,b,ch);
PushRand(ra,result);
}
printf("%d\n",GetTopRand(ra));
return 1;
}
```
阅读全文