c语言字符串表达式计算,表达式计算(C语言版)

#include"stdlib.h"

typedef double DataType;   /*数据存放类型*/

typedef int OpType;        /*运算符存放类型*/

char *code = "+-*/()#";    /*运算符串*/

char priority[7][7] = {'>','>','','>',

'>','>','','>',

'>','>','>','>','','>',

'>','>','>','>','','>',

'',

'>','>','>','>', 0 ,'>','>',

0 , 0 , 0 , 0 , 0 , 0 , 0 };      /*算符优先表*/

DataType Add(DataType a,DataType b)   /*加*/

{

return a+b;

}

DataType Sub(DataType a,DataType b)   /*减*/

{

return a-b;

}

DataType Mul(DataType a,DataType b)   /*乘*/

{

return a*b;

}

DataType Div(DataType a,DataType b)   /*除*/

{

return a/b;

}

DataType (*func[])(DataType,DataType)={Add,Sub,Mul,Div};

typedef struct

{

int top;                                                   /*数据栈*/

int max;       /*最大长度*/

DataType *stack;

}datastack;

typedef struct

{

int top;                                                   /*运算符栈*/

int max;      /*最大长度*/

OpType *stack;

}operastack;

int DataPush(datastack *s,DataType a)                          /*数据入栈*/

{

if(s->top == s->max)

return 0;

s->stack[s->top]=a;

s->top++;

return 1;

}

int DataPop(datastack *s,DataType *d)         /*数据出栈*/

{

if(s->top == 0)

return 0;

s->top--;

*d = s->stack[s->top];

return 1;

}

int OperaPush(operastack *s,OpType a)                         /*运算符入栈*/

{

if(s->top == s->max)

return 0;

s->stack[s->top]=a;

s->top++;

return 1;

}

int OperaPop(datastack *s,OpType *op)        /*运算符出栈*/

{

if(s->top == 0)

return 0;

s->top--;

*op = s->stack[s->top];

return 1;

}

datastack * CreateDataStack(int n)         /*创建数据栈*/

{

datastack *data = (datastack *)malloc(sizeof(datastack));

if(!data)

return 0;

data->stack = (DataType *)malloc(sizeof(DataType)*n);

if(!data->stack)

{

free(data);

return 0;

}

data->top = 0;

data->max = n;

return data;

}

operastack * CreateOperaStack(int n)     /* 创建运算符栈*/

{

operastack *opera = (operastack *)malloc(sizeof(operastack));

if(!opera)

return 0;

opera->stack = (OpType *)malloc(sizeof(OpType)*n);

if(!opera->stack)

{

free(opera);

return 0;

}

opera->top = 0;

opera->max = n;

return opera;

}

int account(char a[],DataType *result)                                     /*表达式计算*/

{

int i,j = 0,f = 0,k = 0,n = 100;

char b[20];     /*用来记录数字串*/

DataType temp = 0,temp1 = 0,temp2 = 0;  /*临时数据变量*/

operastack *opera;

datastack *data;

data = CreateDataStack(n);

if(!data)

return 0;

opera = CreateOperaStack(n);

if(!opera)

return 0;

for(i=0;i

{

if(a[i] == ' ')    /*遇空格则不作处理,直接跳过*/

continue;

if(a[i]>='0'&&a[i]<='9'||a[i] == '.')

{

b[j++] = a[i];

f=1;          /*表示有待入栈数据*/

}

else

{

if(f)

{

b[j] = 0;

j = 0;

if(!DataPush(data,atof(b)))    /*数据入栈*/

{

printf("Expression error!\n");

return 0;

}

f=0;

}

for(k=0;a[i]!=code[k];k++);   /*找出运算符在运算符字符串中的位置*/

if(opera->top == 0)   /*运算符栈为空*/

{

if(k == strlen(code)-1)  /*遇#,表达式结束*/  /*所有计算均已完成,返回结果*/

*result = data->stack[--(data->top)];

if(!OperaPush(opera,k))   /*运算符入栈*/

{

printf("Expression error!\n");

return 0;

}

}

else

{    /*对待入栈运算符与栈顶运算符进行优先级比较*/

if(priority[opera->stack[opera->top-1]][k] == '>')   /*栈顶运算符优先级高,进行计算*/

{

while(1)

{

if(opera->stack[opera->top-1] == 5)    /*出现右括号*/

{

opera->top--;

continue;

}

else if(opera->stack[opera->top-1] == 4)   /*出现左括号*/

{

opera->top--;

i--;           /*继续下一字符判断*/

break;

}  /*计算,数据栈出栈栈顶两个数,运算符栈出栈一个运算符*/

if(!DataPop(data,&temp1))

{

printf("Expression error!\n");

return 0;

}

if(!DataPop(data,&temp2))

{

printf("Expression error!\n");

return 0;

}

temp = func[opera->stack[--(opera->top)]](temp2,temp1);

DataPush(data,temp); /*将计算结果入栈*/

i--;  /*继续下一字符判断*/

break;

}

}

else     /*栈顶运算符优先级低,新运算符入栈*/

OperaPush(opera,k);

}

}

}

return 1;

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用栈实现字符串表达式计算的代码,包括将中缀表达式换为后缀表达式计算后缀表达式两部分: ```c #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define MAX_SIZE 100 typedef struct { int top; char data[MAX_SIZE]; } Stack; void push(Stack *s, char c) { if (s->top == MAX_SIZE - 1) { printf("Stack overflow!\n"); exit(1); } s->data[++s->top] = c; } char pop(Stack *s) { if (s->top == -1) { printf("Stack underflow!\n"); exit(1); } return s->data[s->top--]; } char peek(Stack *s) { if (s->top == -1) { printf("Stack underflow!\n"); exit(1); } return s->data[s->top]; } int is_empty(Stack *s) { return s->top == -1; } int is_operator(char c) { return c == '+' || c == '-' || c == '*' || c == '/' || c == '^'; } int precedence(char c) { switch (c) { case '+': case '-': return 1; case '*': case '/': return 2; case '^': return 3; default: return 0; } } void infix_to_postfix(char *infix, char *postfix) { Stack s; s.top = -1; int i, j; char c; for (i = 0, j = 0; infix[i] != '\0'; i++) { if (isdigit(infix[i])) { postfix[j++] = infix[i]; } else if (infix[i] == '(') { push(&s, infix[i]); } else if (infix[i] == ')') { while (!is_empty(&s) && (c = pop(&s)) != '(') { postfix[j++] = c; } if (c != '(') { printf("Mismatched parentheses!\n"); exit(1); } } else if (is_operator(infix[i])) { while (!is_empty(&s) && precedence(infix[i]) <= precedence(peek(&s))) { postfix[j++] = pop(&s); } push(&s, infix[i]); } else { printf("Invalid character in expression!\n"); exit(1); } } while (!is_empty(&s)) { c = pop(&s); if (c == '(') { printf("Mismatched parentheses!\n"); exit(1); } postfix[j++] = c; } postfix[j] = '\0'; } double evaluate_postfix(char *postfix) { Stack s; s.top = -1; int i; char c; double a, b; for (i = 0; postfix[i] != '\0'; i++) { if (isdigit(postfix[i])) { push(&s, postfix[i] - '0'); } else if (is_operator(postfix[i])) { b = pop(&s); a = pop(&s); switch (postfix[i]) { case '+': push(&s, a + b); break; case '-': push(&s, a - b); break; case '*': push(&s, a * b); break; case '/': if (b == 0) { printf("Division by zero!\n"); exit(1); } push(&s, a / b); break; case '^': push(&s, pow(a, b)); break; } } else { printf("Invalid character in expression!\n"); exit(1); } } return pop(&s); } int main() { char infix[MAX_SIZE], postfix[MAX_SIZE]; printf("Enter an infix expression: "); scanf("%s", infix); infix_to_postfix(infix, postfix); printf("Postfix expression: %s\n", postfix); printf("Result: %g\n", evaluate_postfix(postfix)); return 0; } ``` 注意,在计算后缀表达式时,数字字符需要换为数字,可以使用字符减去字符'0'的方式实现。此外,需要注意除数为零和括号匹配的情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值