69,373
社区成员
发帖
与我相关
我的任务
分享
//表达式求值
#include <stdio.h>
#include <malloc.h>
#include <string.h>
/*
*功能:根据运算符计算
*参数:a, b参与运算的数, ch运算符
*返回值:计算结果,操作符错误则返回0
*/
int cal(int a, char ch, int b)
{
switch(ch)
{
case '+':
return a+b;
break;
case '-':
return a-b;
break;
case '*':
return a*b;
break;
case '/':
return a/b;
break;
}
return 0;
}
/*
*功能:计算表达式的值(用数组模拟栈)
*参数:表达式字符串
*返回值:计算结果
*/
int evaluateExpression(char *str)
{
int i = 0, result, numSub = 0, operSub = 0;
int tmp, len = strlen(str);
int *operand = (int*)malloc(sizeof(int)*len);
char *operat = (char*)malloc(sizeof(char)*len);
while(str[i] != '\0')
{
switch(str[i])
{
case '+':
while(operSub > 0 && operat[operSub-1] != '(')
{
printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);
operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);
printf("%d\n", operand[numSub-2]);
--numSub;
--operSub;
}
operat[operSub++] = '+';
break;
case '-':
while(operSub > 0 && operat[operSub-1] != '(')
{
printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);
operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);
printf("%d\n", operand[numSub-2]);
--numSub;
--operSub;
}
operat[operSub++] = '-';
break;
case '*':
if(str[i+1] >= '0' && str[i+1] <= '9')
{
tmp = 0;
while(str[i+1] >= '0' && str[i+1] <= '9')
{
tmp = tmp * 10 + str[i+1] - '0';
++i;
}
--i;
printf("%d * %d = ", operand[numSub-1], tmp);
operand[numSub-1] = cal(operand[numSub-1], '*', tmp);
printf("%d\n", operand[numSub-1]);
++i;
}
else
operat[operSub++] = '*';
break;
case '/':
if(str[i+1] >= '0' && str[i+1] <= '9')
{
tmp = 0;
while(str[i+1] >= '0' && str[i+1] <= '9')
{
tmp = tmp * 10 + str[i+1] - '0';
++i;
}
--i;
printf("%d / %d = ", operand[numSub-1], tmp);
operand[numSub-1] = cal(operand[numSub-1], '/', tmp);
printf("%d\n", operand[numSub-1]);
++i;
}
else
operat[operSub++] = '/';
break;
case '(':
operat[operSub++] = '(';
break;
case ')':
while(operat[operSub-1] != '(')
{
printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);
operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);
printf("%d\n", operand[numSub-2]);
--numSub;
--operSub;
}
--operSub;
break;
default:
tmp = 0;
while(str[i] >= '0' && str[i] <= '9')
{
tmp = tmp * 10 + str[i] - '0';
++i;
}
--i;
operand[numSub++] = tmp;
break;
}
++i;
}
while(numSub > 1 && operSub >= 1)
{
printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);
operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);
printf("%d\n", operand[numSub-2]);
--numSub;
--operSub;
}
result = operand[numSub-1];
free(operand);
free(operat);
return result;
}
int main()
{
char *str = "225/15-20+(4-3)*2";
int result;
printf("计算过程:\n");
result = evaluateExpression(str);
printf("计算结果:result = %d\n", result);
return 0;
}
计算过程:
225 / 15 = 15
15 - 20 = -5
4 - 3 = 1
1 * 2 = 2
-5 + 2 = -3
计算结果:result = -3
#include <iostream>
#include "Stack.h"
using namespace std;
int func (char c) //返回栈顶运算符的优先级
{
int top = 0;
switch (c)
{
case '+':
case '-':
top = 1;
break;
case '*':
case '/':
top = 2;
break;
case ')':
top = 3;
break;
case '(':
top = 0;
break;
default:
break;
}
return top;
}
int main ()
{
cout << "请输入中缀表达式:" << endl;
char *string;
Stack<char> str1(1000), str2(1000);
string = new char [1000];
cin >> string;
int i = 0;
int *order;
order = new int [1000];
while (string [i] != '\0') //判定输入表达式内运算符的优先级
{
if (string [i] == '+' || string [i] == '-')
order [i] = 1;
else if (string [i] == '*' || string [i] == '/')
order [i] = 2;
else if (string [i] == ')')
order [i] = 3;
else if (string [i] == '(')
order [i] = 0;
else
{ }
i ++;
}
i = 0;
int top;
char z;
while (string [i] != '\0')
{
if (string [i] == '+' || string [i] == '-'
|| string [i] == '*' || string [i] == '/'
|| string [i] == '(' || string [i] == ')')
{
if (!str2.isEmpty ())
{
if (order [i] <= top)
{
while (order[i] <= top && !str2.isEmpty () && string[i] != '(')
{
str2.pop (z);
str1.push (z);
// str2.push (string [i]);
top = func (str2.getStack());
}
str2.push (string [i]);
}
else
{
str2.push (string [i]);
top = func (str2.getStack ());
}
}
else
{
str2.push (string [i]);
top = func (str2.getStack());
}
}
else
{
str1.push (string [i]);
}
i ++;
}
cout << "\n输出后缀表达式:\n";
while (!str1.isEmpty ())
{
str1.pop (z);
str2.push (z);
}
while (!str2.isEmpty ())
{
str2.pop (z);
if (z != '(' && z != ')')
cout << z;
}
delete [] string;
delete [] order;
cout << endl;
system ("pause");
return 0;
}
#ifndef STACK_H
#define STACK_H
template <class T>
class Stack
{
public:
Stack (int = 20);
~Stack () {delete [] stackPtr;}
void push (const T&);
void pop (T&);
int isEmpty () const {return top == -1;}
int isFull () const {return top == size - 1;}
void addTop (int);
void detructTop (int);
int getTop () {return top;}
T getStack () {return stackPtr[top];}
private:
int size;
int top;
T *stackPtr;
};
template <class T>
Stack<T>::Stack (int s)
{
size = s;
top = -1;
stackPtr = new T [size];
}
template <class T>
void Stack<T>::push (const T &item)
{
if (!isFull())
{
stackPtr[++top] = item;
}
else
cout << "ERRO!" << endl;
}
template <class T>
void Stack<T>::pop (T &item)
{
if (!isEmpty ())
{
item = stackPtr [top--];
}
}
template <class T>
void Stack<T>::addTop (int s)
{
top = top + s;
}
template <class T>
void Stack<T>::detructTop (int s)
{
top = top - s;
}
#endif